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