a41676a477f540bf9a00afc893bb991f24400159
[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._loginAction = QtGui.QAction(None)
376                 self._loginAction.setText("Login")
377                 self._loginAction.triggered.connect(self._on_login_requested)
378
379                 self._importAction = QtGui.QAction(None)
380                 self._importAction.setText("Import")
381                 self._importAction.triggered.connect(self._on_import)
382
383                 self._accountAction = QtGui.QAction(None)
384                 self._accountAction.setText("Account")
385                 self._accountAction.triggered.connect(self._on_account)
386
387                 self._refreshConnectionAction = QtGui.QAction(None)
388                 self._refreshConnectionAction.setText("Refresh Connection")
389                 self._refreshConnectionAction.setShortcut(QtGui.QKeySequence("CTRL+a"))
390                 self._refreshConnectionAction.triggered.connect(self._on_refresh_connection)
391
392                 self._refreshTabAction = QtGui.QAction(None)
393                 self._refreshTabAction.setText("Refresh Tab")
394                 self._refreshTabAction.setShortcut(QtGui.QKeySequence("CTRL+r"))
395                 self._refreshTabAction.triggered.connect(self._on_refresh)
396
397                 fileMenu = self._window.menuBar().addMenu("&File")
398                 fileMenu.addAction(self._loginAction)
399                 fileMenu.addAction(self._refreshTabAction)
400                 fileMenu.addAction(self._refreshConnectionAction)
401
402                 toolsMenu = self._window.menuBar().addMenu("&Tools")
403                 toolsMenu.addAction(self._accountAction)
404                 toolsMenu.addAction(self._importAction)
405                 toolsMenu.addAction(self._app.aboutAction)
406
407                 self._initialize_tab(self._tabWidget.currentIndex())
408                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
409                 self.set_orientation(self._app.orientationAction.isChecked())
410
411         def set_default_credentials(self, username, password):
412                 self._defaultCredentials = username, password
413
414         def get_default_credentials(self):
415                 return self._defaultCredentials
416
417         def walk_children(self):
418                 if self._smsEntryDialog is not None:
419                         return (self._smsEntryDialog, )
420                 else:
421                         return ()
422
423         def start(self):
424                 qwrappers.WindowWrapper.start(self)
425                 assert self._session.state == self._session.LOGGEDOUT_STATE, "Initialization messed up"
426                 if self._defaultCredentials != ("", ""):
427                         username, password = self._defaultCredentials[0], self._defaultCredentials[1]
428                         self._curentCredentials = username, password
429                         self._session.login(username, password)
430                 else:
431                         self._prompt_for_login()
432
433         def close(self):
434                 for diag in (
435                         self._credentialsDialog,
436                         self._accountDialog,
437                 ):
438                         if diag is not None:
439                                 diag.close()
440                 for child in self.walk_children():
441                         child.window.destroyed.disconnect(self._on_child_close)
442                         child.window.closed.disconnect(self._on_child_close)
443                         child.close()
444                 self._window.close()
445
446         def destroy(self):
447                 qwrappers.WindowWrapper.destroy(self)
448                 if self._session.state != self._session.LOGGEDOUT_STATE:
449                         self._session.logout()
450
451         def get_current_tab(self):
452                 return self._currentTab
453
454         def set_current_tab(self, tabIndex):
455                 self._tabWidget.setCurrentIndex(tabIndex)
456
457         def load_settings(self, config):
458                 backendId = 2 # For backwards compatibility
459                 for tabIndex, tabTitle in enumerate(self._TAB_TITLES):
460                         sectionName = "%s - %s" % (backendId, tabTitle)
461                         settings = self._tabsContents[tabIndex].get_settings()
462                         for settingName in settings.iterkeys():
463                                 try:
464                                         settingValue = config.get(sectionName, settingName)
465                                 except ConfigParser.NoOptionError, e:
466                                         _moduleLogger.info(
467                                                 "Settings file %s is missing section %s" % (
468                                                         constants._user_settings_,
469                                                         e.section,
470                                                 ),
471                                         )
472                                         return
473                                 except ConfigParser.NoSectionError, e:
474                                         _moduleLogger.info(
475                                                 "Settings file %s is missing section %s" % (
476                                                         constants._user_settings_,
477                                                         e.section,
478                                                 ),
479                                         )
480                                         return
481                                 except Exception:
482                                         _moduleLogger.exception("Unknown loading error")
483                                         return
484                                 settings[settingName] = settingValue
485                         self._tabsContents[tabIndex].set_settings(settings)
486
487         def save_settings(self, config):
488                 backendId = 2 # For backwards compatibility
489                 for tabIndex, tabTitle in enumerate(self._TAB_TITLES):
490                         sectionName = "%s - %s" % (backendId, tabTitle)
491                         config.add_section(sectionName)
492                         tabSettings = self._tabsContents[tabIndex].get_settings()
493                         for settingName, settingValue in tabSettings.iteritems():
494                                 config.set(sectionName, settingName, settingValue)
495
496         def set_orientation(self, isPortrait):
497                 qwrappers.WindowWrapper.set_orientation(self, isPortrait)
498                 if isPortrait:
499                         self._tabWidget.setTabPosition(QtGui.QTabWidget.South)
500                 else:
501                         self._tabWidget.setTabPosition(QtGui.QTabWidget.West)
502
503         def _initialize_tab(self, index):
504                 assert index < self.MAX_TABS, "Invalid tab"
505                 if not self._tabsContents[index].has_child():
506                         tab = self._TAB_CLASS[index](self._app, self._session, self._errorLog)
507                         self._tabsContents[index].set_child(tab)
508                 self._tabsContents[index].refresh(force=False)
509
510         def _prompt_for_login(self):
511                 if self._credentialsDialog is None:
512                         import dialogs
513                         self._credentialsDialog = dialogs.CredentialsDialog(self._app)
514                 credentials = self._credentialsDialog.run(
515                         self._defaultCredentials[0], self._defaultCredentials[1], self.window
516                 )
517                 if credentials is None:
518                         return
519                 username, password = credentials
520                 self._curentCredentials = username, password
521                 self._session.login(username, password)
522
523         def _show_account_dialog(self):
524                 if self._accountDialog is None:
525                         import dialogs
526                         self._accountDialog = dialogs.AccountDialog(self._app)
527                         self._accountDialog.setIfNotificationsSupported(self._app.alarmHandler.backgroundNotificationsSupported)
528                 self._accountDialog.notifications = self._app.alarmHandler.alarmType
529                 self._accountDialog.notificationTime = self._app.alarmHandler.recurrence
530                 self._accountDialog.notifyOnMissed = self._app.notifyOnMissed
531                 self._accountDialog.notifyOnVoicemail = self._app.notifyOnVoicemail
532                 self._accountDialog.notifyOnSms = self._app.notifyOnSms
533                 self._accountDialog.set_callbacks(
534                         self._session.get_callback_numbers(), self._session.get_callback_number()
535                 )
536                 accountNumberToDisplay = self._session.get_account_number()
537                 if not accountNumberToDisplay:
538                         accountNumberToDisplay = "Not Available (%s)" % self._session.state
539                 self._accountDialog.set_account_number(accountNumberToDisplay)
540                 response = self._accountDialog.run(self.window)
541                 if response == QtGui.QDialog.Accepted:
542                         if self._accountDialog.doClear:
543                                 self._session.logout_and_clear()
544                                 self._defaultCredentials = "", ""
545                                 self._curentCredentials = "", ""
546                                 for tab in self._tabsContents:
547                                         tab.disable()
548                         else:
549                                 callbackNumber = self._accountDialog.selectedCallback
550                                 self._session.set_callback_number(callbackNumber)
551
552                         if (
553                                 self._accountDialog.notifyOnMissed or
554                                 self._accountDialog.notifyOnVoicemail or
555                                 self._accountDialog.notifyOnSms
556                         ):
557                                 notifications = self._accountDialog.notifications
558                         else:
559                                 notifications = self._accountDialog.ALARM_NONE
560                         self._app.alarmHandler.apply_settings(notifications, self._accountDialog.notificationTime)
561
562                         self._app.notifyOnMissed = self._accountDialog.notifyOnMissed
563                         self._app.notifyOnVoicemail = self._accountDialog.notifyOnVoicemail
564                         self._app.notifyOnSms = self._accountDialog.notifyOnSms
565                         self._app.save_settings()
566                 elif response == QtGui.QDialog.Rejected:
567                         _moduleLogger.info("Cancelled")
568                 else:
569                         _moduleLogger.info("Unknown response")
570
571         @QtCore.pyqtSlot()
572         @misc_utils.log_exception(_moduleLogger)
573         def _on_new_message_alert(self):
574                 with qui_utils.notify_error(self._errorLog):
575                         if self._app.alarmHandler.alarmType == self._app.alarmHandler.ALARM_APPLICATION:
576                                 if self._currentTab == self.MESSAGES_TAB or not self._app.ledHandler.isReal:
577                                         self._errorLog.push_message("New messages available")
578                                 else:
579                                         self._app.ledHandler.on()
580
581         @QtCore.pyqtSlot(str)
582         @misc_utils.log_exception(_moduleLogger)
583         def _on_session_error(self, message):
584                 with qui_utils.notify_error(self._errorLog):
585                         self._errorLog.push_error(message)
586
587         @QtCore.pyqtSlot()
588         @misc_utils.log_exception(_moduleLogger)
589         def _on_login(self):
590                 with qui_utils.notify_error(self._errorLog):
591                         changedAccounts = self._defaultCredentials != self._curentCredentials
592                         noCallback = not self._session.get_callback_number()
593                         if changedAccounts or noCallback:
594                                 self._show_account_dialog()
595
596                         self._defaultCredentials = self._curentCredentials
597
598                         for tab in self._tabsContents:
599                                 tab.enable()
600                         self._initialize_tab(self._currentTab)
601
602         @QtCore.pyqtSlot()
603         @misc_utils.log_exception(_moduleLogger)
604         def _on_logout(self):
605                 with qui_utils.notify_error(self._errorLog):
606                         for tab in self._tabsContents:
607                                 tab.disable()
608
609         @QtCore.pyqtSlot()
610         @misc_utils.log_exception(_moduleLogger)
611         def _on_app_alert(self):
612                 with qui_utils.notify_error(self._errorLog):
613                         if self._session.state == self._session.LOGGEDIN_STATE:
614                                 messageType = {
615                                         (True, True): self._session.MESSAGE_ALL,
616                                         (True, False): self._session.MESSAGE_TEXTS,
617                                         (False, True): self._session.MESSAGE_VOICEMAILS,
618                                 }[(self._app.notifyOnSms, self._app.notifyOnVoicemail)]
619                                 self._session.update_messages(messageType, force=True)
620
621         @QtCore.pyqtSlot()
622         @misc_utils.log_exception(_moduleLogger)
623         def _on_recipients_changed(self):
624                 with qui_utils.notify_error(self._errorLog):
625                         if self._session.draft.get_num_contacts() == 0:
626                                 return
627
628                         if self._smsEntryDialog is None:
629                                 import dialogs
630                                 self._smsEntryDialog = dialogs.SMSEntryWindow(self.window, self._app, self._session, self._errorLog)
631                                 self._smsEntryDialog.window.destroyed.connect(self._on_child_close)
632                                 self._smsEntryDialog.window.closed.connect(self._on_child_close)
633                                 self._smsEntryDialog.window.show()
634
635         @misc_utils.log_exception(_moduleLogger)
636         def _on_child_close(self, obj = None):
637                 self._smsEntryDialog = None
638
639         @QtCore.pyqtSlot()
640         @QtCore.pyqtSlot(bool)
641         @misc_utils.log_exception(_moduleLogger)
642         def _on_login_requested(self, checked = True):
643                 with qui_utils.notify_error(self._errorLog):
644                         self._prompt_for_login()
645
646         @QtCore.pyqtSlot(int)
647         @misc_utils.log_exception(_moduleLogger)
648         def _on_tab_changed(self, index):
649                 with qui_utils.notify_error(self._errorLog):
650                         self._currentTab = index
651                         self._initialize_tab(index)
652                         if self._app.alarmHandler.alarmType == self._app.alarmHandler.ALARM_APPLICATION:
653                                 self._app.ledHandler.off()
654
655         @QtCore.pyqtSlot()
656         @QtCore.pyqtSlot(bool)
657         @misc_utils.log_exception(_moduleLogger)
658         def _on_refresh(self, checked = True):
659                 with qui_utils.notify_error(self._errorLog):
660                         self._tabsContents[self._currentTab].refresh(force=True)
661
662         @QtCore.pyqtSlot()
663         @QtCore.pyqtSlot(bool)
664         @misc_utils.log_exception(_moduleLogger)
665         def _on_refresh_connection(self, checked = True):
666                 with qui_utils.notify_error(self._errorLog):
667                         self._session.refresh_connection()
668
669         @QtCore.pyqtSlot()
670         @QtCore.pyqtSlot(bool)
671         @misc_utils.log_exception(_moduleLogger)
672         def _on_import(self, checked = True):
673                 with qui_utils.notify_error(self._errorLog):
674                         csvName = QtGui.QFileDialog.getOpenFileName(self._window, caption="Import", filter="CSV Files (*.csv)")
675                         csvName = unicode(csvName)
676                         if not csvName:
677                                 return
678                         import shutil
679                         shutil.copy2(csvName, self._app.fsContactsPath)
680                         if self._tabsContents[self.CONTACTS_TAB].has_child:
681                                 self._tabsContents[self.CONTACTS_TAB].child.update_addressbooks()
682
683         @QtCore.pyqtSlot()
684         @QtCore.pyqtSlot(bool)
685         @misc_utils.log_exception(_moduleLogger)
686         def _on_account(self, checked = True):
687                 with qui_utils.notify_error(self._errorLog):
688                         assert self._session.state == self._session.LOGGEDIN_STATE, "Must be logged in for settings"
689                         self._show_account_dialog()
690
691
692 def run():
693         try:
694                 os.makedirs(constants._data_path_)
695         except OSError, e:
696                 if e.errno != 17:
697                         raise
698
699         logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
700         logging.basicConfig(level=logging.DEBUG, format=logFormat)
701         rotating = logging.handlers.RotatingFileHandler(constants._user_logpath_, maxBytes=512*1024, backupCount=1)
702         rotating.setFormatter(logging.Formatter(logFormat))
703         root = logging.getLogger()
704         root.addHandler(rotating)
705         _moduleLogger.info("%s %s-%s" % (constants.__app_name__, constants.__version__, constants.__build__))
706         _moduleLogger.info("OS: %s" % (os.uname()[0], ))
707         _moduleLogger.info("Kernel: %s (%s) for %s" % os.uname()[2:])
708         _moduleLogger.info("Hostname: %s" % os.uname()[1])
709
710         app = QtGui.QApplication([])
711         handle = Dialcentral(app)
712         qtpie.init_pies()
713         return app.exec_()
714
715
716 if __name__ == "__main__":
717         import sys
718
719         val = run()
720         sys.exit(val)