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