Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / controller.py
1 import gtk
2 from kofoto.shelf import ShelfLockedError, UnsupportedShelfError
3 from kofoto.gkofoto.mainwindow import MainWindow
4 from kofoto.gkofoto.environment import env
5
6 class Controller:
7     def __init__(self):
8         self.__clipboard = None
9         self.__mainWindow = None
10
11     def start(self, setupOk):
12         if setupOk:
13             try:
14                 if env.shelf.isUpgradable():
15                     dialog = gtk.MessageDialog(
16                         type=gtk.MESSAGE_INFO,
17                         buttons=gtk.BUTTONS_OK_CANCEL,
18                         message_format=
19                             "You have a metadata database from an older"
20                             " Kofoto version. It needs to be upgraded"
21                             " before you can continue.\n\n"
22                             "Press OK to upgrade the database"
23                             " automatically. A backup copy of the old"
24                             " database will be made before upgrading.")
25                     dialog.set_default_response(gtk.RESPONSE_OK)
26                     result = dialog.run()
27                     if result == gtk.RESPONSE_CANCEL:
28                         return
29                     dialog.set_response_sensitive(gtk.RESPONSE_CANCEL, False)
30                     dialog.set_response_sensitive(gtk.RESPONSE_OK, False)
31                     dialog.label.set_text("Upgrading database. Please wait...")
32                     while gtk.events_pending():
33                         gtk.main_iteration()
34                     success = env.shelf.tryUpgrade()
35                     dialog.destroy()
36                     if not success:
37                         dialog = gtk.MessageDialog(
38                             type=gtk.MESSAGE_ERROR,
39                             buttons=gtk.BUTTONS_OK,
40                             message_format="Failed to upgrade metadata database format.\n")
41                         dialog.run()
42                         dialog.destroy()
43                         return
44                 env.shelf.begin()
45             except ShelfLockedError, e:
46                 env.startupNotices += [
47                     "Error: Could not open metadata database \"%s\"." % e +
48                     " Another process is locking it.\n"]
49                 setupOk = False
50             except UnsupportedShelfError, e:
51                 env.startupNotices += [
52                     "Error: Too new format for metadata database \"%s\"." % e]
53                 setupOk = False
54         if env.startupNotices:
55             if setupOk:
56                 dialogtype = gtk.MESSAGE_INFO
57             else:
58                 dialogtype = gtk.MESSAGE_ERROR
59             dialog = gtk.MessageDialog(
60                 type=dialogtype,
61                 buttons=gtk.BUTTONS_OK,
62                 message_format="".join(env.startupNotices))
63             if setupOk:
64                 # Doesn't work with x[0].destroy(). Don't know why.
65                 dialog.connect("response", lambda *x: x[0].hide())
66             dialog.run()
67         if setupOk:
68             self.__mainWindow = MainWindow()
69             env.widgets["mainWindow"].connect("destroy", self.quit_cb, False)
70             env.widgets["mainWindow"].show()
71             gtk.main()
72
73     def quit_cb(self, app, cancelButton=True):
74         if env.shelf.isModified():
75             widgets = gtk.glade.XML(env.gladeFile, "quitDialog")
76             quitDialog = widgets.get_widget("quitDialog")
77             if not cancelButton:
78                 widgets.get_widget("cancel").set_sensitive(False)
79             result = quitDialog.run()
80             if result == 0:
81                 env.shelf.commit()
82                 self._doQuit()
83             elif result == 1:
84                 env.shelf.rollback()
85                 self._doQuit()
86             else:
87                 quitDialog.destroy()
88                 return
89         else:
90             env.shelf.rollback()
91             self._doQuit()
92
93     def save_cb(self, app):
94         env.shelf.commit()
95         env.shelf.begin()
96
97     def revert_cb(self, app):
98         dialog = gtk.MessageDialog(
99             type=gtk.MESSAGE_QUESTION,
100             buttons=gtk.BUTTONS_YES_NO,
101             message_format="Revert to the previously saved state and lose all changes?")
102         if dialog.run() == gtk.RESPONSE_YES:
103             env.shelf.rollback()
104             env.shelf.begin()
105             self.__mainWindow.reload()
106         dialog.destroy()
107
108     def _doQuit(self):
109         self.__mainWindow.saveState()
110         gtk.main_quit()