Adding notification support back in and fixing a dialog 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         def _walk_children(self):
220                 if self._mainWindow is not None:
221                         return (self._mainWindow, )
222                 else:
223                         return ()
224
225         def _close_windows(self):
226                 if self._mainWindow is not None:
227                         self.save_settings()
228                         self._mainWindow.window.destroyed.disconnect(self._on_child_close)
229                         self._mainWindow.close()
230                         self._mainWindow = None
231
232         @QtCore.pyqtSlot()
233         @QtCore.pyqtSlot(bool)
234         @misc_utils.log_exception(_moduleLogger)
235         def _on_app_quit(self, checked = False):
236                 if self._mainWindow is not None:
237                         self.save_settings()
238                         self._mainWindow.destroy()
239
240         @QtCore.pyqtSlot(QtCore.QObject)
241         @misc_utils.log_exception(_moduleLogger)
242         def _on_child_close(self, obj = None):
243                 if self._mainWindow is not None:
244                         self.save_settings()
245                         self._mainWindow = None
246
247         @QtCore.pyqtSlot()
248         @QtCore.pyqtSlot(bool)
249         @misc_utils.log_exception(_moduleLogger)
250         def _on_toggle_fullscreen(self, checked = False):
251                 for window in self._walk_children():
252                         window.set_fullscreen(checked)
253
254         @QtCore.pyqtSlot()
255         @QtCore.pyqtSlot(bool)
256         @misc_utils.log_exception(_moduleLogger)
257         def _on_log(self, checked = False):
258                 with open(constants._user_logpath_, "r") as f:
259                         logLines = f.xreadlines()
260                         log = "".join(logLines)
261                         self._clipboard.setText(log)
262
263         @QtCore.pyqtSlot()
264         @QtCore.pyqtSlot(bool)
265         @misc_utils.log_exception(_moduleLogger)
266         def _on_quit(self, checked = False):
267                 self._close_windows()
268
269
270 class DelayedWidget(object):
271
272         def __init__(self, app, settingsNames):
273                 self._layout = QtGui.QVBoxLayout()
274                 self._layout.setContentsMargins(0, 0, 0, 0)
275                 self._widget = QtGui.QWidget()
276                 self._widget.setContentsMargins(0, 0, 0, 0)
277                 self._widget.setLayout(self._layout)
278                 self._settings = dict((name, "") for name in settingsNames)
279
280                 self._child = None
281                 self._isEnabled = True
282
283         @property
284         def toplevel(self):
285                 return self._widget
286
287         def has_child(self):
288                 return self._child is not None
289
290         def set_child(self, child):
291                 if self._child is not None:
292                         self._layout.removeWidget(self._child.toplevel)
293                 self._child = child
294                 if self._child is not None:
295                         self._layout.addWidget(self._child.toplevel)
296
297                 self._child.set_settings(self._settings)
298
299                 if self._isEnabled:
300                         self._child.enable()
301                 else:
302                         self._child.disable()
303
304         def enable(self):
305                 self._isEnabled = True
306                 if self._child is not None:
307                         self._child.enable()
308
309         def disable(self):
310                 self._isEnabled = False
311                 if self._child is not None:
312                         self._child.disable()
313
314         def clear(self):
315                 if self._child is not None:
316                         self._child.clear()
317
318         def refresh(self, force=True):
319                 if self._child is not None:
320                         self._child.refresh(force)
321
322         def get_settings(self):
323                 if self._child is not None:
324                         return self._child.get_settings()
325                 else:
326                         return self._settings
327
328         def set_settings(self, settings):
329                 if self._child is not None:
330                         self._child.set_settings(settings)
331                 else:
332                         self._settings = settings
333
334
335 def _tab_factory(tab, app, session, errorLog):
336         import gv_views
337         return gv_views.__dict__[tab](app, session, errorLog)
338
339
340 class MainWindow(object):
341
342         KEYPAD_TAB = 0
343         RECENT_TAB = 1
344         MESSAGES_TAB = 2
345         CONTACTS_TAB = 3
346         MAX_TABS = 4
347
348         _TAB_TITLES = [
349                 "Dialpad",
350                 "History",
351                 "Messages",
352                 "Contacts",
353         ]
354         assert len(_TAB_TITLES) == MAX_TABS
355
356         _TAB_ICONS = [
357                 "dialpad.png",
358                 "history.png",
359                 "messages.png",
360                 "contacts.png",
361         ]
362         assert len(_TAB_ICONS) == MAX_TABS
363
364         _TAB_CLASS = [
365                 functools.partial(_tab_factory, "Dialpad"),
366                 functools.partial(_tab_factory, "History"),
367                 functools.partial(_tab_factory, "Messages"),
368                 functools.partial(_tab_factory, "Contacts"),
369         ]
370         assert len(_TAB_CLASS) == MAX_TABS
371
372         # Hack to allow delay importing/loading of tabs
373         _TAB_SETTINGS_NAMES = [
374                 (),
375                 ("filter", ),
376                 ("status", "type"),
377                 ("selectedAddressbook", ),
378         ]
379         assert len(_TAB_SETTINGS_NAMES) == MAX_TABS
380
381         def __init__(self, parent, app):
382                 self._app = app
383
384                 self._errorLog = qui_utils.QErrorLog()
385                 self._errorDisplay = qui_utils.ErrorDisplay(self._errorLog)
386
387                 self._session = session.Session(self._errorLog, constants._data_path_)
388                 self._session.error.connect(self._on_session_error)
389                 self._session.loggedIn.connect(self._on_login)
390                 self._session.loggedOut.connect(self._on_logout)
391                 self._session.draft.recipientsChanged.connect(self._on_recipients_changed)
392                 self._defaultCredentials = "", ""
393                 self._curentCredentials = "", ""
394                 self._currentTab = 0
395
396                 self._credentialsDialog = None
397                 self._smsEntryDialog = None
398                 self._accountDialog = None
399                 self._aboutDialog = None
400
401                 self._tabsContents = [
402                         DelayedWidget(self._app, self._TAB_SETTINGS_NAMES[i])
403                         for i in xrange(self.MAX_TABS)
404                 ]
405                 for tab in self._tabsContents:
406                         tab.disable()
407
408                 self._tabWidget = QtGui.QTabWidget()
409                 if qui_utils.screen_orientation() == QtCore.Qt.Vertical:
410                         self._tabWidget.setTabPosition(QtGui.QTabWidget.South)
411                 else:
412                         self._tabWidget.setTabPosition(QtGui.QTabWidget.West)
413                 for tabIndex, (tabTitle, tabIcon) in enumerate(
414                         zip(self._TAB_TITLES, self._TAB_ICONS)
415                 ):
416                         if constants.IS_MAEMO:
417                                 icon = self._app.get_icon(tabIcon)
418                                 if icon is None:
419                                         self._tabWidget.addTab(self._tabsContents[tabIndex].toplevel, tabTitle)
420                                 else:
421                                         self._tabWidget.addTab(self._tabsContents[tabIndex].toplevel, icon, "")
422                         else:
423                                 icon = self._app.get_icon(tabIcon)
424                                 self._tabWidget.addTab(self._tabsContents[tabIndex].toplevel, icon, tabTitle)
425                 self._tabWidget.currentChanged.connect(self._on_tab_changed)
426                 self._tabWidget.setContentsMargins(0, 0, 0, 0)
427
428                 self._layout = QtGui.QVBoxLayout()
429                 self._layout.setContentsMargins(0, 0, 0, 0)
430                 self._layout.addWidget(self._errorDisplay.toplevel)
431                 self._layout.addWidget(self._tabWidget)
432
433                 centralWidget = QtGui.QWidget()
434                 centralWidget.setLayout(self._layout)
435                 centralWidget.setContentsMargins(0, 0, 0, 0)
436
437                 self._window = QtGui.QMainWindow(parent)
438                 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
439                 qui_utils.set_autorient(self._window, True)
440                 qui_utils.set_stackable(self._window, True)
441                 self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
442                 self._window.setCentralWidget(centralWidget)
443
444                 self._loginTabAction = QtGui.QAction(None)
445                 self._loginTabAction.setText("Login")
446                 self._loginTabAction.triggered.connect(self._on_login_requested)
447
448                 self._importTabAction = QtGui.QAction(None)
449                 self._importTabAction.setText("Import")
450                 self._importTabAction.triggered.connect(self._on_import)
451
452                 self._accountTabAction = QtGui.QAction(None)
453                 self._accountTabAction.setText("Account")
454                 self._accountTabAction.triggered.connect(self._on_account)
455
456                 self._refreshTabAction = QtGui.QAction(None)
457                 self._refreshTabAction.setText("Refresh")
458                 self._refreshTabAction.setShortcut(QtGui.QKeySequence("CTRL+r"))
459                 self._refreshTabAction.triggered.connect(self._on_refresh)
460
461                 self._aboutAction = QtGui.QAction(None)
462                 self._aboutAction.setText("About")
463                 self._aboutAction.triggered.connect(self._on_about)
464
465                 self._closeWindowAction = QtGui.QAction(None)
466                 self._closeWindowAction.setText("Close")
467                 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
468                 self._closeWindowAction.triggered.connect(self._on_close_window)
469
470                 if constants.IS_MAEMO:
471                         fileMenu = self._window.menuBar().addMenu("&File")
472                         fileMenu.addAction(self._loginTabAction)
473                         fileMenu.addAction(self._refreshTabAction)
474
475                         toolsMenu = self._window.menuBar().addMenu("&Tools")
476                         toolsMenu.addAction(self._accountTabAction)
477                         toolsMenu.addAction(self._importTabAction)
478                         toolsMenu.addAction(self._aboutAction)
479
480                         self._window.addAction(self._closeWindowAction)
481                         self._window.addAction(self._app.quitAction)
482                         self._window.addAction(self._app.fullscreenAction)
483                 else:
484                         fileMenu = self._window.menuBar().addMenu("&File")
485                         fileMenu.addAction(self._loginTabAction)
486                         fileMenu.addAction(self._refreshTabAction)
487                         fileMenu.addAction(self._closeWindowAction)
488                         fileMenu.addAction(self._app.quitAction)
489
490                         viewMenu = self._window.menuBar().addMenu("&View")
491                         viewMenu.addAction(self._app.fullscreenAction)
492
493                         toolsMenu = self._window.menuBar().addMenu("&Tools")
494                         toolsMenu.addAction(self._accountTabAction)
495                         toolsMenu.addAction(self._importTabAction)
496                         toolsMenu.addAction(self._aboutAction)
497
498                 self._window.addAction(self._app.logAction)
499
500                 self._initialize_tab(self._tabWidget.currentIndex())
501                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
502
503         @property
504         def window(self):
505                 return self._window
506
507         def set_default_credentials(self, username, password):
508                 self._defaultCredentials = username, password
509
510         def get_default_credentials(self):
511                 return self._defaultCredentials
512
513         def walk_children(self):
514                 return ()
515
516         def start(self):
517                 assert self._session.state == self._session.LOGGEDOUT_STATE, "Initialization messed up"
518                 if self._defaultCredentials != ("", ""):
519                         username, password = self._defaultCredentials[0], self._defaultCredentials[1]
520                         self._curentCredentials = username, password
521                         self._session.login(username, password)
522                 else:
523                         self._prompt_for_login()
524
525         def close(self):
526                 for child in self.walk_children():
527                         child.window.destroyed.disconnect(self._on_child_close)
528                         child.close()
529                 for diag in (
530                         self._credentialsDialog,
531                         self._smsEntryDialog,
532                         self._accountDialog,
533                         self._aboutDialog,
534                 ):
535                         if diag is not None:
536                                 diag.close()
537                 self._window.close()
538
539         def destroy(self):
540                 if self._session.state != self._session.LOGGEDOUT_STATE:
541                         self._session.logout()
542
543         def get_current_tab(self):
544                 return self._currentTab
545
546         def set_current_tab(self, tabIndex):
547                 self._tabWidget.setCurrentIndex(tabIndex)
548
549         def load_settings(self, config):
550                 backendId = 2 # For backwards compatibility
551                 for tabIndex, tabTitle in enumerate(self._TAB_TITLES):
552                         sectionName = "%s - %s" % (backendId, tabTitle)
553                         settings = self._tabsContents[tabIndex].get_settings()
554                         for settingName in settings.iterkeys():
555                                 try:
556                                         settingValue = config.get(sectionName, settingName)
557                                 except ConfigParser.NoOptionError, e:
558                                         _moduleLogger.info(
559                                                 "Settings file %s is missing section %s" % (
560                                                         constants._user_settings_,
561                                                         e.section,
562                                                 ),
563                                         )
564                                         return
565                                 except ConfigParser.NoSectionError, e:
566                                         _moduleLogger.info(
567                                                 "Settings file %s is missing section %s" % (
568                                                         constants._user_settings_,
569                                                         e.section,
570                                                 ),
571                                         )
572                                         return
573                                 except Exception:
574                                         _moduleLogger.exception("Unknown loading error")
575                                         return
576                                 settings[settingName] = settingValue
577                         self._tabsContents[tabIndex].set_settings(settings)
578
579         def save_settings(self, config):
580                 backendId = 2 # For backwards compatibility
581                 for tabIndex, tabTitle in enumerate(self._TAB_TITLES):
582                         sectionName = "%s - %s" % (backendId, tabTitle)
583                         config.add_section(sectionName)
584                         tabSettings = self._tabsContents[tabIndex].get_settings()
585                         for settingName, settingValue in tabSettings.iteritems():
586                                 config.set(sectionName, settingName, settingValue)
587
588         def show(self):
589                 self._window.show()
590                 for child in self.walk_children():
591                         child.show()
592
593         def hide(self):
594                 for child in self.walk_children():
595                         child.hide()
596                 self._window.hide()
597
598         def set_fullscreen(self, isFullscreen):
599                 if isFullscreen:
600                         self._window.showFullScreen()
601                 else:
602                         self._window.showNormal()
603                 for child in self.walk_children():
604                         child.set_fullscreen(isFullscreen)
605
606         def _initialize_tab(self, index):
607                 assert index < self.MAX_TABS, "Invalid tab"
608                 if not self._tabsContents[index].has_child():
609                         tab = self._TAB_CLASS[index](self._app, self._session, self._errorLog)
610                         self._tabsContents[index].set_child(tab)
611                         self._tabsContents[index].refresh(force=False)
612
613         def _prompt_for_login(self):
614                 if self._credentialsDialog is None:
615                         import dialogs
616                         self._credentialsDialog = dialogs.CredentialsDialog(self._app)
617                 username, password = self._credentialsDialog.run(
618                         self._defaultCredentials[0], self._defaultCredentials[1], self.window
619                 )
620                 self._curentCredentials = username, password
621                 self._session.login(username, password)
622
623         def _show_account_dialog(self):
624                 if self._accountDialog is None:
625                         import dialogs
626                         self._accountDialog = dialogs.AccountDialog(self._app)
627                         if self._alarmHandler is None:
628                                 self._accountDialog.setIfNotificationsSupported(False)
629                 if self._alarmHandler is not None:
630                         self._accountDialog.notifications = self._alarmHandler.isEnabled
631                         self._accountDialog.notificationTime = self._alarmHandler.recurrence
632                         self._accountDialog.notifyOnMissed = self._app.notifyOnMissed
633                         self._accountDialog.notifyOnVoicemail = self._app.notifyOnVoicemail
634                         self._accountDialog.notifyOnSms = self._app.notifyOnSms
635                 self._accountDialog.set_callbacks(
636                         self._session.get_callback_numbers(), self._session.get_callback_number()
637                 )
638                 self._accountDialog.accountNumber = self._session.get_account_number()
639                 response = self._accountDialog.run()
640                 if response == QtGui.QDialog.Accepted:
641                         if self._accountDialog.doClear:
642                                 self._session.logout_and_clear()
643                         else:
644                                 callbackNumber = self._accountDialog.selectedCallback
645                                 self._session.set_callback_number(callbackNumber)
646                         if self._alarmHandler is not None:
647                                 self._alarmHandler.apply_settings(self._accountDialog.notifications, self._accountDialog.notificationTime)
648                                 self._app.notifyOnMissed = self._accountDialog.notifyOnMissed
649                                 self._app.notifyOnVoicemail = self._accountDialog.notifyOnVoicemail
650                                 self._app.notifyOnSms = self._accountDialog.notifyOnSms
651                 elif response == QtGui.QDialog.Rejected:
652                         _moduleLogger.info("Cancelled")
653                 else:
654                         _moduleLogger.info("Unknown response")
655
656         @QtCore.pyqtSlot(str)
657         @misc_utils.log_exception(_moduleLogger)
658         def _on_session_error(self, message):
659                 with qui_utils.notify_error(self._errorLog):
660                         self._errorLog.push_error(message)
661
662         @QtCore.pyqtSlot()
663         @misc_utils.log_exception(_moduleLogger)
664         def _on_login(self):
665                 with qui_utils.notify_error(self._errorLog):
666                         changedAccounts = self._defaultCredentials != self._curentCredentials
667                         noCallback = not self._session.get_callback_number()
668                         if changedAccounts or noCallback:
669                                 self._show_account_dialog()
670
671                         self._defaultCredentials = self._curentCredentials
672
673                         for tab in self._tabsContents:
674                                 tab.enable()
675
676         @QtCore.pyqtSlot()
677         @misc_utils.log_exception(_moduleLogger)
678         def _on_logout(self):
679                 with qui_utils.notify_error(self._errorLog):
680                         for tab in self._tabsContents:
681                                 tab.disable()
682
683         @QtCore.pyqtSlot()
684         @misc_utils.log_exception(_moduleLogger)
685         def _on_recipients_changed(self):
686                 with qui_utils.notify_error(self._errorLog):
687                         if self._session.draft.get_num_contacts() == 0:
688                                 return
689
690                         if self._smsEntryDialog is None:
691                                 import dialogs
692                                 self._smsEntryDialog = dialogs.SMSEntryWindow(self.window, self._app, self._session, self._errorLog)
693
694         @QtCore.pyqtSlot()
695         @QtCore.pyqtSlot(bool)
696         @misc_utils.log_exception(_moduleLogger)
697         def _on_login_requested(self, checked = True):
698                 with qui_utils.notify_error(self._errorLog):
699                         self._prompt_for_login()
700
701         @QtCore.pyqtSlot(int)
702         @misc_utils.log_exception(_moduleLogger)
703         def _on_tab_changed(self, index):
704                 with qui_utils.notify_error(self._errorLog):
705                         self._currentTab = index
706                         self._initialize_tab(index)
707
708         @QtCore.pyqtSlot()
709         @QtCore.pyqtSlot(bool)
710         @misc_utils.log_exception(_moduleLogger)
711         def _on_refresh(self, checked = True):
712                 with qui_utils.notify_error(self._errorLog):
713                         self._tabsContents[self._currentTab].refresh(force=True)
714
715         @QtCore.pyqtSlot()
716         @QtCore.pyqtSlot(bool)
717         @misc_utils.log_exception(_moduleLogger)
718         def _on_import(self, checked = True):
719                 with qui_utils.notify_error(self._errorLog):
720                         csvName = QtGui.QFileDialog.getOpenFileName(self._window, caption="Import", filter="CSV Files (*.csv)")
721                         if not csvName:
722                                 return
723                         import shutil
724                         shutil.copy2(csvName, self._app.fsContactsPath)
725                         self._tabsContents[self.CONTACTS_TAB].update_addressbooks()
726
727         @QtCore.pyqtSlot()
728         @QtCore.pyqtSlot(bool)
729         @misc_utils.log_exception(_moduleLogger)
730         def _on_account(self, checked = True):
731                 with qui_utils.notify_error(self._errorLog):
732                         self._show_account_dialog()
733
734         @QtCore.pyqtSlot()
735         @QtCore.pyqtSlot(bool)
736         @misc_utils.log_exception(_moduleLogger)
737         def _on_about(self, checked = True):
738                 with qui_utils.notify_error(self._errorLog):
739                         if self._aboutDialog is None:
740                                 import dialogs
741                                 self._aboutDialog = dialogs.AboutDialog(self._app)
742                         response = self._aboutDialog.run()
743
744         @QtCore.pyqtSlot()
745         @QtCore.pyqtSlot(bool)
746         @misc_utils.log_exception(_moduleLogger)
747         def _on_close_window(self, checked = True):
748                 self.close()
749
750
751 def run():
752         app = QtGui.QApplication([])
753         handle = Dialcentral(app)
754         qtpie.init_pies()
755         return app.exec_()
756
757
758 if __name__ == "__main__":
759         import sys
760
761         logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
762         logging.basicConfig(level=logging.DEBUG, format=logFormat)
763         try:
764                 os.makedirs(constants._data_path_)
765         except OSError, e:
766                 if e.errno != 17:
767                         raise
768
769         val = run()
770         sys.exit(val)