2011-10-31

Kasio - Open & Save

Continue from the last post, we are going to write the event handler for the Open and Save buttons this time.  We will also make use of the filecrypto module we wrote in an earlier post.

First we will have to add a few more imports:

import os
import sys
import wx
import filecrypto

Click the Open button to select a file and enter the password to decrypt the file into the text area.

def onOpen(self, event):
    try:
        # ask for file
        dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*",
            wx.OPEN|wx.CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetPath()
        else:
            return
        # ask for password
        dlg = wx.TextEntryDialog(self, "Password?", "Attention", "",
            style=wx.OK|wx.CANCEL|wx.TE_PASSWORD)
        if dlg.ShowModal() == wx.ID_OK:
            self.password = dlg.GetValue()
        else:
            return
        # create file cipher
        fc = filecrypto.FileCrypto()
        # read & decrypt file
        plaintext = fc.load(self.filename, self.password)
        # convert 8-bit string in utf-8 encoding to unicode string
        plaintext = plaintext.decode('utf-8')
        # populate text control
        self.textbox.SetValue(plaintext)
    except:
        self.filename = ''
        self.password = ''
        self.textbox.SetValue('')
        errmsg = str(sys.exc_info()[1])
        dlg = wx.MessageDialog(None, errmsg, 'Attention', wx.OK | wx.ICON_EXCLAMATION)
        dlg.ShowModal()
        print(errmsg)


Click the Save button to save the content in the text area to a file.  If you are saving a new file, you will have to enter a password to encrypt the content.  If you are saving an existing file, the content will be encrypted using the password you entered to open the file in the first place.

def onSave(self, event):
    try:
        # if no filename, ask filename and password
        if len(self.filename) == 0:
            # ask for filename
            dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*",
                wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
            if dlg.ShowModal() == wx.ID_OK:
                self.filename = dlg.GetPath()
            else:
                return
            # ask for password
            dlg = wx.TextEntryDialog(self, "Password?", "Attention", "",
                style=wx.OK|wx.CANCEL|wx.TE_PASSWORD)
            if dlg.ShowModal() == wx.ID_OK:
                self.password = dlg.GetValue()
            else:
                return
        # create file cipher
        fc = filecrypto.FileCrypto()
        # get the text
        plaintext = self.textbox.GetValue()
        # convert unicode string to 8-bit string in utf-8 encoding
        plaintext = plaintext.encode('utf-8')
        # encrypt and write to file
        fc.save(self.filename, self.password, plaintext)
    except:
        errmsg = str(sys.exc_info()[1])
        dlg = wx.MessageDialog(None, errmsg, 'Attention', wx.OK | wx.ICON_EXCLAMATION)
        dlg.ShowModal()
        print(errmsg)


Seems that wxPython likes unicode while the filecrypto module likes 8-bit string.  Hence you can see there are encode() and decode() calls in the onSave() and onLoad() event handlers to keep them both happy.

Let's implement the Find button in the next post.





No comments: