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