Implemented saving open puzzle in player
authorHarishankar <v.harishankar@gmail.com>
Wed, 8 Dec 2010 09:01:01 +0000 (14:31 +0530)
committerHarishankar <v.harishankar@gmail.com>
Wed, 8 Dec 2010 09:01:01 +0000 (14:31 +0530)
Implemented saving the puzzle for future restore.

player_mainwindow.py
playerwindow.glade

index 7aa3af7..8234893 100644 (file)
@@ -17,6 +17,18 @@ class MainWindow:
        ACROSS = 1
        DOWN = 2
 
+       # quit verification
+       def verify_quit (self):
+               if self.puzzle:
+                       dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                                       gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
+                                       "Puzzle is open. Are you sure you wish to quit?")
+                       if dlg.run () <> gtk.RESPONSE_YES:
+                               dlg.destroy ()
+                               return False
+                       dlg.destroy ()
+               return True
+
        # callback for menu item open activated event
        def on_open_activate (self, menuitem):
                dlg = gtk.FileChooserDialog ("Open a GetAClue puzzle", self.window,
@@ -29,18 +41,18 @@ class MainWindow:
 
                dlg.destroy ()
 
-       # quit verification
-       def verify_quit (self):
+       # callback for menu item save as activated event
+       def on_save_as_activate (self, menuitem):
                if self.puzzle:
-                       dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
-                                       gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
-                                       "Puzzle is open. Are you sure you wish to quit?")
-                       if dlg.run () <> gtk.RESPONSE_YES:
-                               dlg.destroy ()
-                               return False
-                       dlg.destroy ()
-               return True
+                       dlg = gtk.FileChooserDialog ("Save GetAClue puzzle as", self.window,
+                               gtk.FILE_CHOOSER_ACTION_SAVE,
+                               (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE,
+                                       gtk.RESPONSE_OK))
+                       if dlg.run () == gtk.RESPONSE_OK:
+                               puzzlefile = dlg.get_filename ()
+                               self.save_file (puzzlefile)
 
+                       dlg.destroy ()
 
        # callback for main window destroy
        def on_mainwindow_destroy (self, args):
@@ -443,6 +455,18 @@ class MainWindow:
                                down.append ([ str(self.puzzle.data[word[1]][word[2]].numbered),
                                                        clue])
 
+       def save_file (self, file):
+               # try to save the file
+               try:
+                       cPickle.dump (self.puzzle, open (file, "wb"), cPickle.HIGHEST_PROTOCOL)
+               except (IOError, OSError, cPickle.PicklingError):
+                       dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                               gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE,
+                               "Error in saving puzzle")
+                       dlg.run ()
+                       dlg.destroy ()
+
+       # open a file
        def open_file (self, file):
                # try to open the file
                try:
index 83639ba..32dc750 100644 (file)
@@ -61,6 +61,7 @@
                         <property name="visible">True</property>
                         <property name="label" translatable="yes">Save _As...</property>
                         <property name="use_underline">True</property>
+                        <signal name="activate" handler="on_save_as_activate"/>
                       </object>
                     </child>
                     <child>