From 373ca24a96a79b39951589206ae1253013d45b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Cort=C3=A9z?= Date: Thu, 5 Mar 2015 11:27:48 -0600 Subject: [PATCH] Session manager is now fully MVC, it can removes an account too --- src/controller/mainController.py | 13 ++++--- src/sessionmanager/sessionManager.py | 51 +++++++++++++++++----------- src/sessionmanager/wxUI.py | 34 ++++++++++++------- 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/src/controller/mainController.py b/src/controller/mainController.py index c1ac215f..56489a41 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -1059,11 +1059,14 @@ class Controller(object): sm.fill_list() sm.show() for i in sm.new_sessions: - call_threaded(self.add_account, i) - - def add_account(self, i): - self.create_buffers(session_.sessions[i]) - self.start_buffers(session_.sessions[i]) + self.create_buffers(session_.sessions[i]) + call_threaded(self.start_buffers, session_.sessions[i]) + for i in sm.removed_sessions: + if session_.sessions[i].logged == True: + self.logout_account(session_.sessions[i].session_id) + self.destroy_buffer(session_.sessions[i].settings["twitter"]["user_name"], session_.sessions[i].settings["twitter"]["user_name"]) + self.accounts.remove(session_.sessions[i].settings["twitter"]["user_name"]) + session_.sessions.pop(i) def __del__(self): config.app.write() \ No newline at end of file diff --git a/src/sessionmanager/sessionManager.py b/src/sessionmanager/sessionManager.py index 784ae58c..6b11b36c 100644 --- a/src/sessionmanager/sessionManager.py +++ b/src/sessionmanager/sessionManager.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- -import wx +import shutil +import widgetUtils import wxUI as view import paths import time @@ -17,8 +18,11 @@ class sessionManagerController(object): super(sessionManagerController, self).__init__() log.debug("Setting up the session manager.") manager.setup() - self.view = view.sessionManagerWindow(self) + self.view = view.sessionManagerWindow() + widgetUtils.connect_event(self.view.new, widgetUtils.BUTTON_PRESSED, self.manage_new_account) + widgetUtils.connect_event(self.view.remove, widgetUtils.BUTTON_PRESSED, self.remove) self.new_sessions = {} + self.removed_sessions = [] def fill_list(self): sessionsList = [] @@ -35,11 +39,11 @@ class sessionManagerController(object): self.sessions.append(i) # else: # log.debug("Ignoring session %s" % (i,)) - if hasattr(self, "view"): self.view.fill_list(sessionsList) + self.view.fill_list(sessionsList) def show(self): - if self.view.ShowModal() == wx.ID_CANCEL: - self.view.Destroy() + if self.view.get_response() == widgetUtils.OK: + self.do_ok() def do_ok(self): log.debug("Starting sessions...") @@ -52,17 +56,26 @@ class sessionManagerController(object): session.sessions[i] = s self.new_sessions[i] = s - def manage_new_account(self): - location = (str(time.time())[-6:]) - log.debug("Creating session in the %s path" % (location,)) - s = session.Session(location) - manager.manager.add_session(location) - s.get_configuration() - try: - s.authorise() - self.sessions.append(location) - self.view.add_new_session_to_list() - except: - log.exception("Error authorising the session") - self.view.show_unauthorised_error() - return \ No newline at end of file + def manage_new_account(self, *args, **kwargs): + if self.view.new_account_dialog() == widgetUtils.YES: + location = (str(time.time())[-6:]) + log.debug("Creating session in the %s path" % (location,)) + s = session.Session(location) + manager.manager.add_session(location) + s.get_configuration() + try: + s.authorise() + self.sessions.append(location) + self.view.add_new_session_to_list() + except: + log.exception("Error authorising the session") + self.view.show_unauthorised_error() + return + + def remove(self, *args, **kwargs): + if self.view.remove_account_dialog() == widgetUtils.YES: + selected_account = self.sessions[self.view.get_selected()] + self.view.remove_session(self.view.get_selected()) + self.removed_sessions.append(selected_account) + self.sessions.remove(selected_account) + shutil.rmtree(path=paths.config_path(selected_account), ignore_errors=True) diff --git a/src/sessionmanager/wxUI.py b/src/sessionmanager/wxUI.py index a61808f9..a8903044 100644 --- a/src/sessionmanager/wxUI.py +++ b/src/sessionmanager/wxUI.py @@ -3,9 +3,8 @@ import wx from multiplatform_widgets import widgets class sessionManagerWindow(wx.Dialog): - def __init__(self, controller): + def __init__(self): super(sessionManagerWindow, self).__init__(parent=None, title="Session manager", size=wx.DefaultSize) - self.controller = controller panel = wx.Panel(self) sizer = wx.BoxSizer(wx.VERTICAL) label = wx.StaticText(panel, -1, u"Select a twitter account to start TW Blue", size=wx.DefaultSize) @@ -14,14 +13,13 @@ class sessionManagerWindow(wx.Dialog): listSizer.Add(label, 0, wx.ALL, 5) listSizer.Add(self.list.list, 0, wx.ALL, 5) sizer.Add(listSizer, 0, wx.ALL, 5) - new = wx.Button(panel, -1, u"New account", size=wx.DefaultSize) - new.Bind(wx.EVT_BUTTON, self.new_account) + self.new = wx.Button(panel, -1, u"New account", size=wx.DefaultSize) + self.remove = wx.Button(panel, -1, _(u"Remove account")) ok = wx.Button(panel, wx.ID_OK, size=wx.DefaultSize) ok.SetDefault() - ok.Bind(wx.EVT_BUTTON, self.ok) cancel = wx.Button(panel, wx.ID_CANCEL, size=wx.DefaultSize) buttons = wx.BoxSizer(wx.HORIZONTAL) - buttons.Add(new, 0, wx.ALL, 5) + buttons.Add(self.new, 0, wx.ALL, 5) buttons.Add(ok, 0, wx.ALL, 5) buttons.Add(cancel, 0, wx.ALL, 5) sizer.Add(buttons, 0, wx.ALL, 5) @@ -43,12 +41,8 @@ class sessionManagerWindow(wx.Dialog): self.controller.do_ok() self.EndModal(wx.ID_OK) - def new_account(self, ev): - dlg = wx.MessageDialog(self, _(u"The request for the required Twitter authorization to continue will be opened on your browser. You only need to do it once. Would you like to autorhise a new account now?"), _(u"Authorisation"), wx.YES_NO) - if dlg.ShowModal() == wx.ID_NO: - return - else: - self.controller.manage_new_account() + def new_account_dialog(self): + return wx.MessageDialog(self, _(u"The request for the required Twitter authorization to continue will be opened on your browser. You only need to do it once. Would you like to autorhise a new account now?"), _(u"Authorisation"), wx.YES_NO).ShowModal() def add_new_session_to_list(self): total = self.list.get_count() @@ -56,5 +50,19 @@ class sessionManagerWindow(wx.Dialog): self.list.insert_item(False, name) if self.list.get_count() == 1: self.list.select_item(0) + def show_unauthorised_error(self): - wx.MessageDialog(None, _(u"Your access token is invalid or the authorisation has failed. Please try again."), _(u"Invalid user token"), wx.ICON_ERROR).ShowModal() \ No newline at end of file + wx.MessageDialog(None, _(u"Your access token is invalid or the authorisation has failed. Please try again."), _(u"Invalid user token"), wx.ICON_ERROR).ShowModal() + + def get_response(self): + return self.ShowModal() + + def remove_account_dialog(self): + return wx.MessageDialog(self, _(u"Do you really want delete this account?"), _(u"Remove account"), wx.YES_NO).ShowModal() + + def get_selected(self): + return self.list.get_selected() + + def remove_session(self, sessionID): + self.list.remove_item(sessionID) +