Fixing an alarm handler bug
[gc-dialer] / src / dialcentral_qt.py
1 #!/usr/bin/env python
2 # -*- coding: UTF8 -*-
3
4 from __future__ import with_statement
5
6 import os
7 import base64
8 import ConfigParser
9 import functools
10 import logging
11
12 from PyQt4 import QtGui
13 from PyQt4 import QtCore
14
15 import constants
16 from util import qtpie
17 from util import qui_utils
18 from util import misc as misc_utils
19
20 import session
21
22
23 _moduleLogger = logging.getLogger(__name__)
24
25
26 class Dialcentral(object):
27
28         _DATA_PATHS = [
29                 os.path.join(os.path.dirname(__file__), "../share"),
30                 os.path.join(os.path.dirname(__file__), "../data"),
31         ]
32
33         def __init__(self, app):
34                 self._app = app
35                 self._recent = []
36                 self._hiddenCategories = set()
37                 self._hiddenUnits = {}
38                 self._clipboard = QtGui.QApplication.clipboard()
39                 self._dataPath = None
40                 self.notifyOnMissed = False
41                 self.notifyOnVoicemail = False
42                 self.notifyOnSms = False
43
44                 self._mainWindow = None
45
46                 self._fullscreenAction = QtGui.QAction(None)
47                 self._fullscreenAction.setText("Fullscreen")
48                 self._fullscreenAction.setCheckable(True)
49                 self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
50                 self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen)
51
52                 self._logAction = QtGui.QAction(None)
53                 self._logAction.setText("Log")
54                 self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
55                 self._logAction.triggered.connect(self._on_log)
56
57                 self._quitAction = QtGui.QAction(None)
58                 self._quitAction.setText("Quit")
59                 self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
60                 self._quitAction.triggered.connect(self._on_quit)
61
62                 self._app.lastWindowClosed.connect(self._on_app_quit)
63                 self._mainWindow = MainWindow(None, self)
64                 self._mainWindow.window.destroyed.connect(self._on_child_close)
65
66                 try:
67                         import alarm_handler
68                         if alarm_handler.AlarmHandler is not alarm_handler._NoneAlarmHandler:
69                                 self._alarmHandler = alarm_handler.AlarmHandler()
70                         else:
71                                 self._alarmHandler = None
72                 except (ImportError, OSError):
73                         self._alarmHandler = None
74                 except Exception:
75                         _moduleLogger.exception("Notification failure")
76                         self._alarmHandler = None
77                 if self._alarmHandler is None:
78                         _moduleLogger.info("No notification support")
79
80                 self.load_settings()
81
82                 self._mainWindow.show()
83                 self._idleDelay = QtCore.QTimer()
84                 self._idleDelay.setSingleShot(True)
85                 self._idleDelay.setInterval(0)
86                 self._idleDelay.timeout.connect(lambda: self._mainWindow.start())
87                 self._idleDelay.start()
88
89         def load_settings(self):
90                 try:
91                         config = ConfigParser.SafeConfigParser()
92                         config.read(constants._user_settings_)
93                 except IOError, e:
94                         _moduleLogger.info("No settings")
95                         return
96                 except ValueError:
97                         _moduleLogger.info("Settings were corrupt")
98                         return
99                 except ConfigParser.MissingSectionHeaderError:
100                         _moduleLogger.info("Settings were corrupt")
101                         return
102                 except Exception:
103                         _moduleLogger.exception("Unknown loading error")
104
105                 blobs = "", ""
106                 isFullscreen = False
107                 tabIndex = 0
108                 try:
109                         blobs = (
110                                 config.get(constants.__pretty_app_name__, "bin_blob_%i" % i)
111                                 for i in xrange(len(self._mainWindow.get_default_credentials()))
112                         )
113                         isFullscreen = config.getboolean(constants.__pretty_app_name__, "fullscreen")
114                         tabIndex = config.getint(constants.__pretty_app_name__, "tab")
115                 except ConfigParser.NoOptionError, e:
116                         _moduleLogger.info(
117                                 "Settings file %s is missing option %s" % (
118                                         constants._user_settings_,
119                                         e.option,
120                                 ),
121                         )
122                 except ConfigParser.NoSectionError, e:
123                         _moduleLogger.info(
124                                 "Settings file %s is missing section %s" % (
125                                         constants._user_settings_,
126                                         e.section,
127                                 ),
128                         )
129                         return
130                 except Exception:
131                         _moduleLogger.exception("Unknown loading error")
132                         return
133
134                 if self._alarmHandler is not None:
135                         try:
136                                 self._alarmHandler.load_settings(config, "alarm")
137                                 self.notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
138                                 self.notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
139                                 self.notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
140                         except ConfigParser.NoOptionError, e:
141                                 _moduleLogger.info(
142                                         "Settings file %s is missing option %s" % (
143                                                 constants._user_settings_,
144                                                 e.option,
145                                         ),
146                                 )
147                         except ConfigParser.NoSectionError, e:
148                                 _moduleLogger.info(
149                                         "Settings file %s is missing section %s" % (
150                                                 constants._user_settings_,
151                                                 e.section,
152                                         ),
153                                 )
154                                 return
155                         except Exception:
156                                 _moduleLogger.exception("Unknown loading error")
157                                 return
158
159                 creds = (
160                         base64.b64decode(blob)
161                         for blob in blobs
162                 )
163                 self._mainWindow.set_default_credentials(*creds)
164                 self._fullscreenAction.setChecked(isFullscreen)
165                 self._mainWindow.set_current_tab(tabIndex)
166                 self._mainWindow.load_settings(config)
167
168         def save_settings(self):
169                 _moduleLogger.info("Saving settings")
170                 config = ConfigParser.SafeConfigParser()
171
172                 config.add_section(constants.__pretty_app_name__)
173                 config.set(constants.__pretty_app_name__, "tab", str(self._mainWindow.get_current_tab()))
174                 config.set(constants.__pretty_app_name__, "fullscreen", str(self._fullscreenAction.isChecked()))
175                 for i, value in enumerate(self._mainWindow.get_default_credentials()):
176                         blob = base64.b64encode(value)
177                         config.set(constants.__pretty_app_name__, "bin_blob_%i" % i, blob)
178
179                 if self._alarmHandler is not None:
180                         self._alarmHandler.save_settings(config, "alarm")
181                 config.add_section("2 - Account Info")
182                 config.set("2 - Account Info", "notifyOnMissed", repr(self.notifyOnMissed))
183                 config.set("2 - Account Info", "notifyOnVoicemail", repr(self.notifyOnVoicemail))
184                 config.set("2 - Account Info", "notifyOnSms", repr(self.notifyOnSms))
185
186                 self._mainWindow.save_settings(config)
187
188                 with open(constants._user_settings_, "wb") as configFile:
189                         config.write(configFile)
190
191         def get_icon(self, name):
192                 if self._dataPath is None:
193                         for path in self._DATA_PATHS:
194                                 if os.path.exists(os.path.join(path, name)):
195                                         self._dataPath = path
196                                         break
197                 if self._dataPath is not None:
198                         icon = QtGui.QIcon(os.path.join(self._dataPath, name))
199                         return icon
200                 else:
201                         return None
202
203         @property
204         def fsContactsPath(self):
205                 return os.path.join(constants._data_path_, "contacts")
206
207         @property
208         def fullscreenAction(self):
209                 return self._fullscreenAction
210
211         @property
212         def logAction(self):
213                 return self._logAction
214
215         @property
216         def quitAction(self):
217                 return self._quitAction
218
219         @property
220         def alarmHandler(self):
221                 return self._alarmHandler
222
223         def _walk_children(self):
224                 if self._mainWindow is not None:
225                         return (self._mainWindow, )
226                 else:
227                         return ()
228
229         def _close_windows(self):
230                 if self._mainWindow is not None:
231                         self.save_settings()
232                         self._mainWindow.window.destroyed.disconnect(self._on_child_close)
233                         self._mainWindow.close()
234                         self._mainWindow = None
235
236         @QtCore.pyqtSlot()
237         @QtCore.pyqtSlot(bool)
238         @misc_utils.log_exception(_moduleLogger)
239         def _on_app_quit(self, checked = False):
240                 if self._mainWindow is not None:
241                         self.save_settings()
242                         self._mainWindow.destroy()
243
244         @QtCore.pyqtSlot(QtCore.QObject)
245         @misc_utils.log_exception(_moduleLogger)
246         def _on_child_close(self, obj = None):
247                 if self._mainWindow is not None:
248                         self.save_settings()
249                         self._mainWindow = None
250
251         @QtCore.pyqtSlot()
252         @QtCore.pyqtSlot(bool)
253         @misc_utils.log_exception(_moduleLogger)
254         def _on_toggle_fullscreen(self, checked = False):
255                 for window in self._walk_children():
256                         window.set_fullscreen(checked)
257
258         @QtCore.pyqtSlot()
259         @QtCore.pyqtSlot(bool)
260         @misc_utils.log_exception(_moduleLogger)
261         def _on_log(self, checked = False):
262                 with open(constants._user_logpath_, "r") as f:
263                         logLines = f.xreadlines()
264                         log = "".join(logLines)
265                         self._clipboard.setText(log)
266
267         @QtCore.pyqtSlot()
268         @QtCore.pyqtSlot(bool)
269         @misc_utils.log_exception(_moduleLogger)
270         def _on_quit(self, checked = False):
271                 self._close_windows()
272
273
274 class DelayedWidget(object):
275
276         def __init__(self, app, settingsNames):
277                 self._layout = QtGui.QVBoxLayout()
278                 self._layout.setContentsMargins(0, 0, 0, 0)
279                 self._widget = QtGui.QWidget()
280                 self._widget.setContentsMargins(0, 0, 0, 0)
281                 self._widget.setLayout(self._layout)
282                 self._settings = dict((name, "") for name in settingsNames)
283
284                 self._child = None
285                 self._isEnabled = True
286
287         @property
288         def toplevel(self):
289                 return self._widget
290
291         def has_child(self):
292                 return self._child is not None
293
294         def set_child(self, child):
295                 if self._child is not None:
296                         self._layout.removeWidget(self._child.toplevel)
297                 self._child = child
298                 if self._child is not None:
299                         self._layout.addWidget(self._child.toplevel)
300
301                 self._child.set_settings(self._settings)
302
303                 if self._isEnabled:
304                         self._child.enable()
305                 else:
306                         self._child.disable()
307
308         def enable(self):
309                 self._isEnabled = True
310                 if self._child is not None:
311                         self._child.enable()
312
313         def disable(self):
314                 self._isEnabled = False
315                 if self._child is not None:
316                         self._child.disable()
317
318         def clear(self):
319                 if self._child is not None:
320                         self._child.clear()
321
322         def refresh(self, force=True):
323                 if self._child is not None:
324                         self._child.refresh(force)
325
326         def get_settings(self):
327                 if self._child is not None:
328                         return self._child.get_settings()
329                 else:
330                         return self._settings
331
332         def set_settings(self, settings):
333                 if self._child is not None:
334                         self._child.set_settings(settings)
335                 else:
336                         self._settings = settings
337
338
339 def _tab_factory(tab, app, session, errorLog):
340         import gv_views
341         return gv_views.__dict__[tab](app, session, errorLog)
342
343
344 class MainWindow(object):
345
346         KEYPAD_TAB = 0
347         RECENT_TAB = 1
348         MESSAGES_TAB = 2
349         CONTACTS_TAB = 3
350         MAX_TABS = 4
351
352         _TAB_TITLES = [
353                 "Dialpad",
354                 "History",
355                 "Messages",
356                 "Contacts",
357         ]
358         assert len(_TAB_TITLES) == MAX_TABS
359
360         _TAB_ICONS = [
361                 "dialpad.png",
362                 "history.png",
363                 "messages.png",
364                 "contacts.png",
365         ]
366         assert len(_TAB_ICONS) == MAX_TABS
367
368         _TAB_CLASS = [
369                 functools.partial(_tab_factory, "Dialpad"),
370                 functools.partial(_tab_factory, "History"),
371                 functools.partial(_tab_factory, "Messages"),
372                 functools.partial(_tab_factory, "Contacts"),
373         ]
374         assert len(_TAB_CLASS) == MAX_TABS
375
376         # Hack to allow delay importing/loading of tabs
377         _TAB_SETTINGS_NAMES = [
378                 (),
379                 ("filter", ),
380                 ("status", "type"),
381                 ("selectedAddressbook", ),
382         ]
383         assert len(_TAB_SETTINGS_NAMES) == MAX_TABS
384
385         def __init__(self, parent, app):
386                 self._app = app
387
388                 self._errorLog = qui_utils.QErrorLog()
389                 self._errorDisplay = qui_utils.ErrorDisplay(self._errorLog)
390
391                 self._session = session.Session(self._errorLog, constants._data_path_)
392                 self._session.error.connect(self._on_session_error)
393                 self._session.loggedIn.connect(self._on_login)
394                 self._session.loggedOut.connect(self._on_logout)
395                 self._session.draft.recipientsChanged.connect(self._on_recipients_changed)
396                 self._defaultCredentials = "", ""
397                 self._curentCredentials = "", ""
398                 self._currentTab = 0
399
400                 self._credentialsDialog = None
401                 self._smsEntryDialog = None
402                 self._accountDialog = None
403                 self._aboutDialog = None
404
405                 self._tabsContents = [
406                         DelayedWidget(self._app, self._TAB_SETTINGS_NAMES[i])
407                         for i in xrange(self.MAX_TABS)
408                 ]
409                 for tab in self._tabsContents:
410                         tab.disable()
411
412                 self._tabWidget = QtGui.QTabWidget()
413                 if qui_utils.screen_orientation() == QtCore.Qt.Vertical:
414                         self._tabWidget.setTabPosition(QtGui.QTabWidget.South)
415                 else:
416                         self._tabWidget.setTabPosition(QtGui.QTabWidget.West)
417                 for tabIndex, (tabTitle, tabIcon) in enumerate(
418                         zip(self._TAB_TITLES, self._TAB_ICONS)
419                 ):
420                         if constants.IS_MAEMO:
421                                 icon = self._app.get_icon(tabIcon)
422                                 if icon is None:
423                                         self._tabWidget.addTab(self._tabsContents[tabIndex].toplevel, tabTitle)
424                                 else:
425                                         self._tabWidget.addTab(self._tabsContents[tabIndex].toplevel, icon, "")
426                         else:
427                                 icon = self._app.get_icon(tabIcon)
428                                 self._tabWidget.addTab(self._tabsContents[tabIndex].toplevel, icon, tabTitle)
429                 self._tabWidget.currentChanged.connect(self._on_tab_changed)
430                 self._tabWidget.setContentsMargins(0, 0, 0, 0)
431
432                 self._layout = QtGui.QVBoxLayout()
433                 self._layout.setContentsMargins(0, 0, 0, 0)
434                 self._layout.addWidget(self._errorDisplay.toplevel)
435                 self._layout.addWidget(self._tabWidget)
436
437                 centralWidget = QtGui.QWidget()
438                 centralWidget.setLayout(self._layout)
439                 centralWidget.setContentsMargins(0, 0, 0, 0)
440
441                 self._window = QtGui.QMainWindow(parent)
442                 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
443                 qui_utils.set_autorient(self._window, True)
444                 qui_utils.set_stackable(self._window, True)
445                 self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
446                 self._window.setCentralWidget(centralWidget)
447
448                 self._loginTabAction = QtGui.QAction(None)
449                 self._loginTabAction.setText("Login")
450                 self._loginTabAction.triggered.connect(self._on_login_requested)
451
452                 self._importTabAction = QtGui.QAction(None)
453                 self._importTabAction.setText("Import")
454                 self._importTabAction.triggered.connect(self._on_import)
455
456                 self._accountTabAction = QtGui.QAction(None)
457                 self._accountTabAction.setText("Account")
458                 self._accountTabAction.triggered.connect(self._on_account)
459
460                 self._refreshTabAction = QtGui.QAction(None)
461                 self._refreshTabAction.setText("Refresh")
462                 self._refreshTabAction.setShortcut(QtGui.QKeySequence("CTRL+r"))
463                 self._refreshTabAction.triggered.connect(self._on_refresh)
464
465                 self._aboutAction = QtGui.QAction(None)
466                 self._aboutAction.setText("About")
467                 self._aboutAction.triggered.connect(self._on_about)
468
469                 self._closeWindowAction = QtGui.QAction(None)
470                 self._closeWindowAction.setText("Close")
471                 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
472                 self._closeWindowAction.triggered.connect(self._on_close_window)
473
474                 if constants.IS_MAEMO:
475                         fileMenu = self._window.menuBar().addMenu("&File")
476                         fileMenu.addAction(self._loginTabAction)
477                         fileMenu.addAction(self._refreshTabAction)
478
479                         toolsMenu = self._window.menuBar().addMenu("&Tools")
480                         toolsMenu.addAction(self._accountTabAction)
481                         toolsMenu.addAction(self._importTabAction)
482                         toolsMenu.addAction(self._aboutAction)
483
484                         self._window.addAction(self._closeWindowAction)
485                         self._window.addAction(self._app.quitAction)
486                         self._window.addAction(self._app.fullscreenAction)
487                 else:
488                         fileMenu = self._window.menuBar().addMenu("&File")
489                         fileMenu.addAction(self._loginTabAction)
490                         fileMenu.addAction(self._refreshTabAction)
491                         fileMenu.addAction(self._closeWindowAction)
492                         fileMenu.addAction(self._app.quitAction)
493
494                         viewMenu = self._window.menuBar().addMenu("&View")
495                         viewMenu.addAction(self._app.fullscreenAction)
496
497                         toolsMenu = self._window.menuBar().addMenu("&Tools")
498                         toolsMenu.addAction(self._accountTabAction)
499                         toolsMenu.addAction(self._importTabAction)
500                         toolsMenu.addAction(self._aboutAction)
501
502                 self._window.addAction(self._app.logAction)
503
504                 self._initialize_tab(self._tabWidget.currentIndex())
505                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
506
507         @property
508         def window(self):
509                 return self._window
510
511         def set_default_credentials(self, username, password):
512                 self._defaultCredentials = username, password
513
514         def get_default_credentials(self):
515                 return self._defaultCredentials
516
517         def walk_children(self):
518                 return ()
519
520         def start(self):
521                 assert self._session.state == self._session.LOGGEDOUT_STATE, "Initialization messed up"
522                 if self._defaultCredentials != ("", ""):
523                         username, password = self._defaultCredentials[0], self._defaultCredentials[1]
524                         self._curentCredentials = username, password
525                         self._session.login(username, password)
526                 else:
527                         self._prompt_for_login()
528
529         def close(self):
530                 for child in self.walk_children():
531                         child.window.destroyed.disconnect(self._on_child_close)
532                         child.close()
533                 for diag in (
534                         self._credentialsDialog,
535                         self._smsEntryDialog,
536                         self._accountDialog,
537                         self._aboutDialog,
538                 ):
539                         if diag is not None:
540                                 diag.close()
541                 self._window.close()
542
543         def destroy(self):
544                 if self._session.state != self._session.LOGGEDOUT_STATE:
545                         self._session.logout()
546
547         def get_current_tab(self):
548                 return self._currentTab
549
550         def set_current_tab(self, tabIndex):
551                 self._tabWidget.setCurrentIndex(tabIndex)
552
553         def load_settings(self, config):
554                 backendId = 2 # For backwards compatibility
555                 for tabIndex, tabTitle in enumerate(self._TAB_TITLES):
556                         sectionName = "%s - %s" % (backendId, tabTitle)
557                         settings = self._tabsContents[tabIndex].get_settings()
558                         for settingName in settings.iterkeys():
559                                 try:
560                                         settingValue = config.get(sectionName, settingName)
561                                 except ConfigParser.NoOptionError, e:
562                                         _moduleLogger.info(
563                                                 "Settings file %s is missing section %s" % (
564                                                         constants._user_settings_,
565                                                         e.section,
566                                                 ),
567                                         )
568                                         return
569                                 except ConfigParser.NoSectionError, e:
570                                         _moduleLogger.info(
571                                                 "Settings file %s is missing section %s" % (
572                                                         constants._user_settings_,
573                                                         e.section,
574                                                 ),
575                                         )
576                                         return
577                                 except Exception:
578                                         _moduleLogger.exception("Unknown loading error")
579                                         return
580                                 settings[settingName] = settingValue
581                         self._tabsContents[tabIndex].set_settings(settings)
582
583         def save_settings(self, config):
584                 backendId = 2 # For backwards compatibility
585                 for tabIndex, tabTitle in enumerate(self._TAB_TITLES):
586                         sectionName = "%s - %s" % (backendId, tabTitle)
587                         config.add_section(sectionName)
588                         tabSettings = self._tabsContents[tabIndex].get_settings()
589                         for settingName, settingValue in tabSettings.iteritems():
590                                 config.set(sectionName, settingName, settingValue)
591
592         def show(self):
593                 self._window.show()
594                 for child in self.walk_children():
595                         child.show()
596
597         def hide(self):
598                 for child in self.walk_children():
599                         child.hide()
600                 self._window.hide()
601
602         def set_fullscreen(self, isFullscreen):
603                 if isFullscreen:
604                         self._window.showFullScreen()
605                 else:
606                         self._window.showNormal()
607                 for child in self.walk_children():
608                         child.set_fullscreen(isFullscreen)
609
610         def _initialize_tab(self, index):
611                 assert index < self.MAX_TABS, "Invalid tab"
612                 if not self._tabsContents[index].has_child():
613                         tab = self._TAB_CLASS[index](self._app, self._session, self._errorLog)
614                         self._tabsContents[index].set_child(tab)
615                         self._tabsContents[index].refresh(force=False)
616
617         def _prompt_for_login(self):
618                 if self._credentialsDialog is None:
619                         import dialogs
620                         self._credentialsDialog = dialogs.CredentialsDialog(self._app)
621                 username, password = self._credentialsDialog.run(
622                         self._defaultCredentials[0], self._defaultCredentials[1], self.window
623                 )
624                 self._curentCredentials = username, password
625                 self._session.login(username, password)
626
627         def _show_account_dialog(self):
628                 if self._accountDialog is None:
629                         import dialogs
630                         self._accountDialog = dialogs.AccountDialog(self._app)
631                         if self._app.alarmHandler is None:
632                                 self._accountDialog.setIfNotificationsSupported(False)
633                 if self._app.alarmHandler is not None:
634                         self._accountDialog.notifications = self._app.alarmHandler.isEnabled
635                         self._accountDialog.notificationTime = self._app.alarmHandler.recurrence
636                         self._accountDialog.notifyOnMissed = self._app.notifyOnMissed
637                         self._accountDialog.notifyOnVoicemail = self._app.notifyOnVoicemail
638                         self._accountDialog.notifyOnSms = self._app.notifyOnSms
639                 self._accountDialog.set_callbacks(
640                         self._session.get_callback_numbers(), self._session.get_callback_number()
641                 )
642                 self._accountDialog.accountNumber = self._session.get_account_number()
643                 response = self._accountDialog.run()
644                 if response == QtGui.QDialog.Accepted:
645                         if self._accountDialog.doClear:
646                                 self._session.logout_and_clear()
647                         else:
648                                 callbackNumber = self._accountDialog.selectedCallback
649                                 self._session.set_callback_number(callbackNumber)
650                         if self._app.alarmHandler is not None:
651                                 self._app.alarmHandler.apply_settings(self._accountDialog.notifications, self._accountDialog.notificationTime)
652                                 self._app.notifyOnMissed = self._accountDialog.notifyOnMissed
653                                 self._app.notifyOnVoicemail = self._accountDialog.notifyOnVoicemail
654                                 self._app.notifyOnSms = self._accountDialog.notifyOnSms
655                 elif response == QtGui.QDialog.Rejected:
656                         _moduleLogger.info("Cancelled")
657                 else:
658                         _moduleLogger.info("Unknown response")
659
660         @QtCore.pyqtSlot(str)
661         @misc_utils.log_exception(_moduleLogger)
662         def _on_session_error(self, message):
663                 with qui_utils.notify_error(self._errorLog):
664                         self._errorLog.push_error(message)
665
666         @QtCore.pyqtSlot()
667         @misc_utils.log_exception(_moduleLogger)
668         def _on_login(self):
669                 with qui_utils.notify_error(self._errorLog):
670                         changedAccounts = self._defaultCredentials != self._curentCredentials
671                         noCallback = not self._session.get_callback_number()
672                         if changedAccounts or noCallback:
673                                 self._show_account_dialog()
674
675                         self._defaultCredentials = self._curentCredentials
676
677                         for tab in self._tabsContents:
678                                 tab.enable()
679
680         @QtCore.pyqtSlot()
681         @misc_utils.log_exception(_moduleLogger)
682         def _on_logout(self):
683                 with qui_utils.notify_error(self._errorLog):
684                         for tab in self._tabsContents:
685                                 tab.disable()
686
687         @QtCore.pyqtSlot()
688         @misc_utils.log_exception(_moduleLogger)
689         def _on_recipients_changed(self):
690                 with qui_utils.notify_error(self._errorLog):
691                         if self._session.draft.get_num_contacts() == 0:
692                                 return
693
694                         if self._smsEntryDialog is None:
695                                 import dialogs
696                                 self._smsEntryDialog = dialogs.SMSEntryWindow(self.window, self._app, self._session, self._errorLog)
697
698         @QtCore.pyqtSlot()
699         @QtCore.pyqtSlot(bool)
700         @misc_utils.log_exception(_moduleLogger)
701         def _on_login_requested(self, checked = True):
702                 with qui_utils.notify_error(self._errorLog):
703                         self._prompt_for_login()
704
705         @QtCore.pyqtSlot(int)
706         @misc_utils.log_exception(_moduleLogger)
707         def _on_tab_changed(self, index):
708                 with qui_utils.notify_error(self._errorLog):
709                         self._currentTab = index
710                         self._initialize_tab(index)
711
712         @QtCore.pyqtSlot()
713         @QtCore.pyqtSlot(bool)
714         @misc_utils.log_exception(_moduleLogger)
715         def _on_refresh(self, checked = True):
716                 with qui_utils.notify_error(self._errorLog):
717                         self._tabsContents[self._currentTab].refresh(force=True)
718
719         @QtCore.pyqtSlot()
720         @QtCore.pyqtSlot(bool)
721         @misc_utils.log_exception(_moduleLogger)
722         def _on_import(self, checked = True):
723                 with qui_utils.notify_error(self._errorLog):
724                         csvName = QtGui.QFileDialog.getOpenFileName(self._window, caption="Import", filter="CSV Files (*.csv)")
725                         if not csvName:
726                                 return
727                         import shutil
728                         shutil.copy2(csvName, self._app.fsContactsPath)
729                         self._tabsContents[self.CONTACTS_TAB].update_addressbooks()
730
731         @QtCore.pyqtSlot()
732         @QtCore.pyqtSlot(bool)
733         @misc_utils.log_exception(_moduleLogger)
734         def _on_account(self, checked = True):
735                 with qui_utils.notify_error(self._errorLog):
736                         self._show_account_dialog()
737
738         @QtCore.pyqtSlot()
739         @QtCore.pyqtSlot(bool)
740         @misc_utils.log_exception(_moduleLogger)
741         def _on_about(self, checked = True):
742                 with qui_utils.notify_error(self._errorLog):
743                         if self._aboutDialog is None:
744                                 import dialogs
745                                 self._aboutDialog = dialogs.AboutDialog(self._app)
746                         response = self._aboutDialog.run()
747
748         @QtCore.pyqtSlot()
749         @QtCore.pyqtSlot(bool)
750         @misc_utils.log_exception(_moduleLogger)
751         def _on_close_window(self, checked = True):
752                 self.close()
753
754
755 def run():
756         app = QtGui.QApplication([])
757         handle = Dialcentral(app)
758         qtpie.init_pies()
759         return app.exec_()
760
761
762 if __name__ == "__main__":
763         import sys
764
765         logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
766         logging.basicConfig(level=logging.DEBUG, format=logFormat)
767         try:
768                 os.makedirs(constants._data_path_)
769         except OSError, e:
770                 if e.errno != 17:
771                         raise
772
773         val = run()
774         sys.exit(val)