4 from __future__ import with_statement
11 from PyQt4 import QtGui
12 from PyQt4 import QtCore
15 from util import qtpie
16 from util import qui_utils
17 from util import misc as misc_utils
22 _moduleLogger = logging.getLogger(__name__)
26 class Dialcentral(object):
28 def __init__(self, app):
31 self._hiddenCategories = set()
32 self._hiddenUnits = {}
33 self._clipboard = QtGui.QApplication.clipboard()
35 self._mainWindow = None
37 self._fullscreenAction = QtGui.QAction(None)
38 self._fullscreenAction.setText("Fullscreen")
39 self._fullscreenAction.setCheckable(True)
40 self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
41 self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen)
43 self._logAction = QtGui.QAction(None)
44 self._logAction.setText("Log")
45 self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
46 self._logAction.triggered.connect(self._on_log)
48 self._quitAction = QtGui.QAction(None)
49 self._quitAction.setText("Quit")
50 self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
51 self._quitAction.triggered.connect(self._on_quit)
53 self._app.lastWindowClosed.connect(self._on_app_quit)
56 self._mainWindow = MainWindow(None, self)
57 self._mainWindow.window.destroyed.connect(self._on_child_close)
59 def load_settings(self):
61 with open(constants._user_settings_, "r") as settingsFile:
62 settings = simplejson.load(settingsFile)
64 _moduleLogger.info("No settings")
67 _moduleLogger.info("Settings were corrupt")
70 self._fullscreenAction.setChecked(settings.get("isFullScreen", False))
72 def save_settings(self):
74 "isFullScreen": self._fullscreenAction.isChecked(),
76 with open(constants._user_settings_, "w") as settingsFile:
77 simplejson.dump(settings, settingsFile)
78 self._mainWindow.destroy()
81 def fsContactsPath(self):
82 return os.path.join(constants._data_path_, "contacts")
85 def fullscreenAction(self):
86 return self._fullscreenAction
90 return self._logAction
94 return self._quitAction
96 def _close_windows(self):
97 if self._mainWindow is not None:
98 self._mainWindow.window.destroyed.disconnect(self._on_child_close)
99 self._mainWindow.close()
100 self._mainWindow = None
103 @QtCore.pyqtSlot(bool)
104 @misc_utils.log_exception(_moduleLogger)
105 def _on_app_quit(self, checked = False):
108 @QtCore.pyqtSlot(QtCore.QObject)
109 @misc_utils.log_exception(_moduleLogger)
110 def _on_child_close(self, obj = None):
111 self._mainWindow = None
114 @QtCore.pyqtSlot(bool)
115 @misc_utils.log_exception(_moduleLogger)
116 def _on_toggle_fullscreen(self, checked = False):
117 for window in self._walk_children():
118 window.set_fullscreen(checked)
121 @QtCore.pyqtSlot(bool)
122 @misc_utils.log_exception(_moduleLogger)
123 def _on_log(self, checked = False):
124 with open(constants._user_logpath_, "r") as f:
125 logLines = f.xreadlines()
126 log = "".join(logLines)
127 self._clipboard.setText(log)
130 @QtCore.pyqtSlot(bool)
131 @misc_utils.log_exception(_moduleLogger)
132 def _on_quit(self, checked = False):
133 self._close_windows()
136 class DelayedWidget(object):
138 def __init__(self, app):
139 self._layout = QtGui.QVBoxLayout()
140 self._widget = QtGui.QWidget()
141 self._widget.setLayout(self._layout)
144 self._isEnabled = True
151 return self._child is not None
153 def set_child(self, child):
154 if self._child is not None:
155 self._layout.removeWidget(self._child.toplevel)
157 if self._child is not None:
158 self._layout.addWidget(self._child.toplevel)
163 self._child.disable()
166 self._isEnabled = True
167 if self._child is not None:
171 self._isEnabled = False
172 if self._child is not None:
173 self._child.disable()
176 if self._child is not None:
179 def refresh(self, force=True):
180 if self._child is not None:
181 self._child.refresh(force)
184 def _tab_factory(tab, app, session, errorLog):
186 return gv_views.__dict__[tab](app, session, errorLog)
189 class MainWindow(object):
203 assert len(_TAB_TITLES) == MAX_TABS
206 functools.partial(_tab_factory, "Dialpad"),
207 functools.partial(_tab_factory, "History"),
208 functools.partial(_tab_factory, "Messages"),
209 functools.partial(_tab_factory, "Contacts"),
211 assert len(_TAB_CLASS) == MAX_TABS
213 def __init__(self, parent, app):
215 self._session = session.Session(constants._data_path_)
216 self._session.error.connect(self._on_session_error)
217 self._session.loggedIn.connect(self._on_login)
218 self._session.loggedOut.connect(self._on_logout)
219 self._session.draft.recipientsChanged.connect(self._on_recipients_changed)
221 self._credentialsDialog = None
222 self._smsEntryDialog = None
223 self._accountDialog = None
225 self._errorLog = qui_utils.QErrorLog()
226 self._errorDisplay = qui_utils.ErrorDisplay(self._errorLog)
228 self._tabsContents = [
229 DelayedWidget(self._app)
230 for i in xrange(self.MAX_TABS)
232 for tab in self._tabsContents:
235 self._tabWidget = QtGui.QTabWidget()
236 if qui_utils.screen_orientation() == QtCore.Qt.Vertical:
237 self._tabWidget.setTabPosition(QtGui.QTabWidget.South)
239 self._tabWidget.setTabPosition(QtGui.QTabWidget.West)
240 for tabIndex, tabTitle in enumerate(self._TAB_TITLES):
241 self._tabWidget.addTab(self._tabsContents[tabIndex].toplevel, tabTitle)
242 self._tabWidget.currentChanged.connect(self._on_tab_changed)
244 self._layout = QtGui.QVBoxLayout()
245 self._layout.addWidget(self._errorDisplay.toplevel)
246 self._layout.addWidget(self._tabWidget)
248 centralWidget = QtGui.QWidget()
249 centralWidget.setLayout(self._layout)
251 self._window = QtGui.QMainWindow(parent)
252 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
253 qui_utils.set_autorient(self._window, True)
254 qui_utils.set_stackable(self._window, True)
255 self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
256 self._window.setCentralWidget(centralWidget)
258 self._loginTabAction = QtGui.QAction(None)
259 self._loginTabAction.setText("Login")
260 self._loginTabAction.triggered.connect(self._on_login_requested)
262 self._importTabAction = QtGui.QAction(None)
263 self._importTabAction.setText("Import")
264 self._importTabAction.triggered.connect(self._on_import)
266 self._accountTabAction = QtGui.QAction(None)
267 self._accountTabAction.setText("Account")
268 self._accountTabAction.triggered.connect(self._on_account)
270 self._refreshTabAction = QtGui.QAction(None)
271 self._refreshTabAction.setText("Refresh")
272 self._refreshTabAction.setShortcut(QtGui.QKeySequence("CTRL+r"))
273 self._refreshTabAction.triggered.connect(self._on_refresh)
275 self._closeWindowAction = QtGui.QAction(None)
276 self._closeWindowAction.setText("Close")
277 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
278 self._closeWindowAction.triggered.connect(self._on_close_window)
281 fileMenu = self._window.menuBar().addMenu("&File")
282 fileMenu.addAction(self._loginTabAction)
283 fileMenu.addAction(self._refreshTabAction)
285 toolsMenu = self._window.menuBar().addMenu("&Tools")
286 toolsMenu.addAction(self._accountTabAction)
287 toolsMenu.addAction(self._importTabAction)
289 self._window.addAction(self._closeWindowAction)
290 self._window.addAction(self._app.quitAction)
291 self._window.addAction(self._app.fullscreenAction)
293 fileMenu = self._window.menuBar().addMenu("&File")
294 fileMenu.addAction(self._loginTabAction)
295 fileMenu.addAction(self._refreshTabAction)
296 fileMenu.addAction(self._closeWindowAction)
297 fileMenu.addAction(self._app.quitAction)
299 viewMenu = self._window.menuBar().addMenu("&View")
300 viewMenu.addAction(self._app.fullscreenAction)
302 toolsMenu = self._window.menuBar().addMenu("&Tools")
303 toolsMenu.addAction(self._accountTabAction)
304 toolsMenu.addAction(self._importTabAction)
306 self._window.addAction(self._app.logAction)
308 self._initialize_tab(self._tabWidget.currentIndex())
309 self.set_fullscreen(self._app.fullscreenAction.isChecked())
316 def walk_children(self):
321 for child in self.walk_children():
325 for child in self.walk_children():
330 for child in self.walk_children():
331 child.window.destroyed.disconnect(self._on_child_close)
336 if self._session.state != self._session.LOGGEDOUT_STATE:
337 self._session.logout()
339 def set_fullscreen(self, isFullscreen):
341 self._window.showFullScreen()
343 self._window.showNormal()
344 for child in self.walk_children():
345 child.set_fullscreen(isFullscreen)
347 def _initialize_tab(self, index):
348 assert index < self.MAX_TABS
349 if not self._tabsContents[index].has_child():
350 tab = self._TAB_CLASS[index](self._app, self._session, self._errorLog)
351 self._tabsContents[index].set_child(tab)
352 self._tabsContents[index].refresh(force=False)
354 @QtCore.pyqtSlot(str)
355 @misc_utils.log_exception(_moduleLogger)
356 def _on_session_error(self, message):
357 self._errorLog.push_message(message)
360 @misc_utils.log_exception(_moduleLogger)
362 for tab in self._tabsContents:
366 @misc_utils.log_exception(_moduleLogger)
367 def _on_logout(self):
368 for tab in self._tabsContents:
372 @misc_utils.log_exception(_moduleLogger)
373 def _on_recipients_changed(self):
374 if self._session.draft.get_num_contacts() == 0:
377 if self._smsEntryDialog is None:
379 self._smsEntryDialog = dialogs.SMSEntryWindow(self.window, self._app, self._session, self._errorLog)
383 @QtCore.pyqtSlot(bool)
384 @misc_utils.log_exception(_moduleLogger)
385 def _on_login_requested(self, checked = True):
386 if self._credentialsDialog is None:
388 self._credentialsDialog = dialogs.CredentialsDialog()
389 username, password = self._credentialsDialog.run("", "", self.window)
390 self._session.login(username, password)
392 @QtCore.pyqtSlot(int)
393 @misc_utils.log_exception(_moduleLogger)
394 def _on_tab_changed(self, index):
395 self._initialize_tab(index)
398 @QtCore.pyqtSlot(bool)
399 @misc_utils.log_exception(_moduleLogger)
400 def _on_refresh(self, checked = True):
401 index = self._tabWidget.currentIndex()
402 self._tabsContents[index].refresh(force=True)
405 @QtCore.pyqtSlot(bool)
406 @misc_utils.log_exception(_moduleLogger)
407 def _on_import(self, checked = True):
408 csvName = QtGui.QFileDialog.getOpenFileName(self._window, caption="Import", filter="CSV Files (*.csv)")
412 shutil.copy2(csvName, self._app.fsContactsPath)
413 self._tabsContents[self.CONTACTS_TAB].update_addressbooks()
416 @QtCore.pyqtSlot(bool)
417 @misc_utils.log_exception(_moduleLogger)
418 def _on_account(self, checked = True):
419 if self._accountDialog is None:
421 self._accountDialog = dialogs.AccountDialog()
422 self._accountDialog.accountNumber = self._session.get_account_number()
423 response = self._accountDialog.run()
424 if response == QtGui.QDialog.Accepted:
425 if self._accountDialog.doClear():
426 self._session.logout_and_clear()
427 elif response == QtGui.QDialog.Rejected:
428 _moduleLogger.info("Cancelled")
430 _moduleLogger.info("Unknown response")
433 @QtCore.pyqtSlot(bool)
434 @misc_utils.log_exception(_moduleLogger)
435 def _on_close_window(self, checked = True):
440 app = QtGui.QApplication([])
441 handle = Dialcentral(app)
446 if __name__ == "__main__":
449 logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
450 logging.basicConfig(level=logging.DEBUG, format=logFormat)
452 os.makedirs(constants._data_path_)