Added exception handling to opening file in player
[getaclue.git] / player_mainwindow.py
index 4524106..3ea5420 100644 (file)
@@ -13,10 +13,23 @@ import crosswordpuzzle
 
 class MainWindow:
        def gtk_main_quit (self, *args):
+               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_NO:
+                               dlg.destroy ()
+                               return
+                       dlg.destroy ()
+
                gtk.main_quit ()
 
+       # callback for menu item quit activated event
+       def on_quit_activate (self, menuitem):
+               self.gtk_main_quit ()
+
        # callback for puzzle grid mouse button release event
-       def on_puzzlegrid_button_release_event (self, drawarea, event):
+       def on_puzzlegrid_button_press_event (self, drawarea, event):
                self.window.set_focus (drawarea)
                return True
 
@@ -153,11 +166,42 @@ class MainWindow:
                                                        clue])
 
        def open_file (self, file):
-               self.puzzle = cPickle.load (open (file, "rb"))
-               self.selected_row = 0
-               self.selected_col = 0
-               self.window.set_title ("GetAClue player - " + file)
-               self.load_clues ()
+               # try to open the file
+               try:
+                       # load the puzzle
+                       self.puzzle = cPickle.load (open (file, "rb"))
+                       # assert that it is unfrozen otherwise raise frozen grid exception
+                       self.puzzle.assert_frozen_grid ()
+
+                       # set selected initial row and column to 0
+                       self.selected_row = 0
+                       self.selected_col = 0
+                       self.window.set_title ("GetAClue player - " + file)
+                       # load the clues
+                       self.load_clues ()
+               # handle unpickling, and file errors
+               except (cPickle.UnpicklingError, IOError, OSError):
+                       dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                               gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE,
+                               "Invalid file. Cannot be loaded")
+                       dlg.run ()
+                       dlg.destroy ()
+               # if the puzzle has no words, then it cannot be played obviously
+               except crosswordpuzzle.NoWordsException:
+                       self.puzzle = None
+                       dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                               gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE,
+                               "Word grid has no words. Cannot play")
+                       dlg.run ()
+                       dlg.destroy ()
+               # if the puzzle is not frozen then it cannot be played
+               except crosswordpuzzle.FrozenGridException:
+                       self.puzzle = None
+                       dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                               gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE,
+                               "Word grid is not finalized/frozen. Cannot play")
+                       dlg.run ()
+                       dlg.destroy ()
 
        def __init__ (self, file_to_play = None):
                # load the user interface