version="0.1.1" 
# This file is part of EventGhost.
# Copyright (C) 2005 Lars-Peter Voss <bitmonster@eventghost.org>
# 
# EventGhost is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# EventGhost is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with EventGhost; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#Last change: 2008-02-28 15:43


class WriteResultToFile(eg.ActionClass):
    name = "Write result to file"
    description = "Writes result to selected file."
    iconFile = "icons/page_white_edit"
    class text:
        label = "Write to file: %s"
        FilePath = "Path to file:"
        browseFileDialogTitle = "Choose the file"
        txtMode = "Mode of write"
        overwrite = "File overwrite"
        append = "Append to file"
        newLine = "Append to file with new line"
        writeToLog = "Write to EventGhost log too"
        error = "Could not open file %s"
        systemPage = "System code page"
        pageChoice = "Manual choice"
        hex = "HEX"
        txtCoding = "Output code page"
        codePage = "Code page:"
    
    def __call__(self, fileName='', mode=0, log=False, coding=0, page=""):
        fileName = eg.ParseString(fileName)
        modeStr='w' if mode==0 else 'a'
        if coding == 2:
            result = eg.result.encode('hex').upper()
        else:
            result=eg.result.encode(page if coding==0 else eg.systemEncoding)
        if log:
            print result
        try:
            file = open(fileName, modeStr)
            if mode == 2:
                file.write("\x0A")
            file.write(result)
            file.close()
        except:
            self.PrintError(self.text.error % fileName)
    
    def GetLabel(self, fileName='', mode=0, log=False, coding=0, page=""):
        return self.text.label % basename(fileName)


    def Configure(self, fileName='', mode=0, log=False, coding=0, page=""):
        panel = eg.ConfigPanel(self)
        text = self.text
        fileText = wx.StaticText(panel, -1, text.FilePath)
        filepathCtrl = eg.FileBrowseButton(
            panel, 
            -1, 
            size=(320,-1),
            initialValue=fileName,
            labelText="",
            fileMask="*.*",
            buttonText=eg.text.General.browse,
            dialogTitle=text.browseFileDialogTitle
        )
        radioBoxMode = wx.RadioBox(
            panel, 
            -1, 
            text.txtMode,
            (0,0),
            (200,80),
            choices=[text.overwrite, text.append, text.newLine],
            style=wx.RA_SPECIFY_ROWS
        )
        radioBoxMode.SetSelection(mode)
        radioBoxCoding = wx.RadioBox(
            panel, 
            -1, 
            text.txtCoding,
            (0,0),
            (200,80),
            choices=[text.pageChoice, text.systemPage, text.hex],
            style=wx.RA_SPECIFY_ROWS
        )
        radioBoxCoding.SetSelection(coding)
        writeToLogCheckBox = wx.CheckBox(panel, -1, text.writeToLog)
        writeToLogCheckBox.SetValue(log)

        mySizer = wx.FlexGridSizer(2,2)
        Add = mySizer.Add
        panel.sizer.Add(fileText)
        panel.sizer.Add(filepathCtrl, 0, wx.EXPAND)
        panel.sizer.Add(mySizer)
        Add(radioBoxMode,0,wx.TOP,5)
        Add(writeToLogCheckBox,0,wx.TOP|wx.LEFT,12)
        Add(radioBoxCoding,0,wx.TOP,5)
        pageSizer = wx.BoxSizer(wx.VERTICAL)
        Add(pageSizer, 0, wx.TOP|wx.LEFT,10)
        labelCtrl = panel.StaticText(text.codePage)
        pageCtrl = wx.TextCtrl(panel,-1,page)
        pageSizer.Add(labelCtrl, 0, wx.TOP,-2)
        pageSizer.Add(pageCtrl, 0, wx.TOP,2)
        
        def onCodingChange(event=None):
            if radioBoxCoding.GetSelection()==0:
                if ((pageCtrl.GetValue()=='System code page') | (pageCtrl.GetValue()=='HEX')):
                   pageCtrl.SetValue('') 
                labelCtrl.Enable(True)
                pageCtrl.Enable(True)
            else:
                pageCtrl.SetValue('System code page' if radioBoxCoding.GetSelection()==1 else 'HEX')
                labelCtrl.Enable(False)
                pageCtrl.Enable(False)
            if event:
                event.Skip()
        radioBoxCoding.Bind(wx.EVT_RADIOBOX, onCodingChange)
        onCodingChange()

        
        while panel.Affirmed():
            panel.SetResult(
                filepathCtrl.GetValue(),
                radioBoxMode.GetSelection(),
                writeToLogCheckBox.IsChecked(),
                radioBoxCoding.GetSelection(),
                pageCtrl.GetValue(),
            )        
