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