From 458d607b9a095f7444e50a8249f89e614b080c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Cort=C3=A9z?= Date: Thu, 26 Feb 2015 15:21:26 -0600 Subject: [PATCH] Start session automatically, login and logout in realtime from the account buffer in the GUI --- src/controller/buffersController.py | 34 +++++++++- src/controller/mainController.py | 93 +++++++++++++++++++++++++--- src/sessionmanager/session.py | 4 ++ src/sessionmanager/sessionManager.py | 9 +-- src/widgetUtils/wxUtils.py | 1 + src/wxUI/buffers/panels.py | 25 ++++++-- 6 files changed, 149 insertions(+), 17 deletions(-) diff --git a/src/controller/buffersController.py b/src/controller/buffersController.py index 5ddc3925..844c7519 100644 --- a/src/controller/buffersController.py +++ b/src/controller/buffersController.py @@ -13,6 +13,7 @@ from twitter import compose, utils from wxUI import buffers, dialogs, commonMessageDialogs from mysc.thread_utils import call_threaded from twython import TwythonError +from pubsub import pub log = logging.getLogger("controller.buffers") @@ -127,7 +128,7 @@ class bufferController(object): call_threaded(self.session.api_call, call_name="update_status_with_media", status=text, media=tweet.image) class accountPanel(bufferController): - def __init__(self, parent, name, account): + def __init__(self, parent, name, account, account_id): super(accountPanel, self).__init__(parent, None, name) log.debug("Initializing buffer %s, account %s" % (name, account,)) self.buffer = buffers.accountPanel(parent, name) @@ -139,6 +140,37 @@ class accountPanel(bufferController): self.account = account self.buffer.account = account self.name = name + self.account_id = account_id + + def setup_account(self): + widgetUtils.connect_event(self.buffer, widgetUtils.CHECKBOX, self.autostart, menuitem=self.buffer.autostart_account) + if self.account_id not in config.app["sessions"]["ignored_sessions"]: + self.buffer.change_autostart(True) + elif self.account_id in config.app["sessions"]["ignored_sessions"]: + self.buffer.change_autostart(False) + if not hasattr(self, "logged"): + self.buffer.change_login(login=False) + widgetUtils.connect_event(self.buffer.login, widgetUtils.BUTTON_PRESSED, self.logout) + else: + widgetUtils.connect_event(self.buffer.login, widgetUtils.BUTTON_PRESSED, self.login) + + def login(self, *args, **kwargs): + del self.logged + self.setup_account() + pub.sendMessage("login", session_id=self.account_id) + + def logout(self, *args, **kwargs): + self.logged = False + self.setup_account() + pub.sendMessage("logout", session_id=self.account_id) + + def autostart(self, *args, **kwargs): + if self.account_id in config.app["sessions"]["ignored_sessions"]: + self.buffer.change_autostart(True) + config.app["sessions"]["ignored_sessions"].remove(self.account_id) + else: + self.buffer.change_autostart(False) + config.app["sessions"]["ignored_sessions"].append(self.account_id) class emptyPanel(bufferController): def __init__(self, parent, name, account): diff --git a/src/controller/mainController.py b/src/controller/mainController.py index 75e665cd..2b0361ce 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -105,6 +105,8 @@ class Controller(object): def bind_other_events(self): """ Binds the local application events with their functions.""" log.debug("Binding other application events...") + pub.subscribe(self.logout_account, "logout") + pub.subscribe(self.login_account, "login") pub.subscribe(self.invisible_shorcuts_changed, "invisible-shorcuts-changed") pub.subscribe(self.manage_stream_errors, "stream-error") pub.subscribe(self.create_new_buffer, "create-new-buffer") @@ -174,6 +176,9 @@ class Controller(object): log.debug("Creating buffers for all sessions...") for i in session_.sessions: log.debug("Working on session %s" % (i,)) + if session_.sessions[i].is_logged == False: + self.create_ignored_session_buffer(session_.sessions[i]) + continue self.create_buffers(session_.sessions[i]) # Connection checker executed each minute. self.checker_function = RepeatingTimer(60, self.check_connection) @@ -182,19 +187,38 @@ class Controller(object): def start(self): """ Starts all buffer objects. Loads their items.""" for i in session_.sessions: + if session_.sessions[i].is_logged == False: continue self.start_buffers(session_.sessions[i]) session_.sessions[session_.sessions.keys()[0]].sound.play("ready.ogg") output.speak(_(u"Ready")) - def create_buffers(self, session): + def create_ignored_session_buffer(self, session): + self.accounts.append(session.settings["twitter"]["user_name"]) + account = buffersController.accountPanel(self.view.nb, session.settings["twitter"]["user_name"], session.settings["twitter"]["user_name"], session.session_id) + account.logged = False + account.setup_account() + self.buffers.append(account) + self.view.add_buffer(account.buffer , name=session.settings["twitter"]["user_name"]) + self.buffer_positions[session.settings["twitter"]["user_name"]] = 1 + + def login_account(self, session_id): + for i in session_.sessions: + if session_.sessions[i].session_id == session_id: session = session_.sessions[i] + session.login() + self.create_buffers(session, False) + self.start_buffers(session) + + def create_buffers(self, session, createAccounts=True): """ Generates buffer objects for an user account. session SessionObject: a sessionmanager.session.Session Object""" session.get_user_info() - self.accounts.append(session.db["user_name"]) - self.buffer_positions[session.db["user_name"]] = 1 - account = buffersController.accountPanel(self.view.nb, session.db["user_name"], session.db["user_name"]) - self.buffers.append(account) - self.view.add_buffer(account.buffer , name=session.db["user_name"]) + if createAccounts == True: + self.accounts.append(session.db["user_name"]) + self.buffer_positions[session.db["user_name"]] = 1 + account = buffersController.accountPanel(self.view.nb, session.db["user_name"], session.db["user_name"], session.session_id) + account.setup_account() + self.buffers.append(account) + self.view.add_buffer(account.buffer , name=session.db["user_name"]) home = buffersController.baseBufferController(self.view.nb, "get_home_timeline", "home_timeline", session, session.db["user_name"]) self.buffers.append(home) self.view.insert_buffer(home.buffer, name=_(u"Home"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) @@ -256,8 +280,41 @@ class Controller(object): buffer.start_stream() self.buffers.append(buffer) self.view.insert_buffer(buffer.buffer, name=_(u"Trending topics for %s") % (buffer.name_), pos=self.view.search(session.db["user_name"], session.db["user_name"])) - timer = RepeatingTimer(300, buffer.start_stream) - timer.start() + buffer.timer = RepeatingTimer(300, buffer.start_stream) + buffer.timer.start() + + def logout_account(self, session_id): + for i in session_.sessions: + if session_.sessions[i].session_id == session_id: session = session_.sessions[i] + user = session.db["user_name"] + self.destroy_buffer("home_timeline", user) + self.destroy_buffer("mentions", user) + self.destroy_buffer("direct_messages", user) + self.destroy_buffer("sent_direct_messages", user) + self.destroy_buffer("sent_tweets", user) + self.destroy_buffer("favourites", user) + self.destroy_buffer("followers", user) + self.destroy_buffer("friends", user) + self.destroy_buffer("blocked", user) + self.destroy_buffer("muted", user) + self.destroy_buffer("events", user) + self.destroy_buffer("timelines", user) + for i in session.settings["other_buffers"]["timelines"]: + self.destroy_buffer("%s-timeline" % (i,), user) + self.destroy_buffer("searches", user) + for i in session.settings["other_buffers"]["tweet_searches"]: + self.destroy_buffer("%s-searchterm" % (i,), user) + for i in session.settings["other_buffers"]["trending_topic_buffers"]: + self.destroy_buffer("%s_tt" % (i,), user) + + def destroy_buffer(self, buffer_name, account): + buffer = self.search_buffer(buffer_name, account) + if buffer == None: return + buff = self.view.search(buffer.name, buffer.account) + if buff == None: return + self.view.delete_buffer(buff) + self.buffers.remove(buffer) + del buffer def search(self, *args, **kwargs): """ Searches words or users in twitter. This creates a new buffer containing the search results.""" @@ -375,6 +432,7 @@ class Controller(object): log.debug("Saving global configuration...") config.app.write() for item in session_.sessions: + if session_.sessions[item]: continue log.debug("Saving config for %s session" % (session_.sessions[item].session_id,)) session_.sessions[item].settings.write() log.debug("Disconnecting streams for %s session" % (session_.sessions[item].session_id,)) @@ -645,6 +703,9 @@ class Controller(object): def up(self, *args, **kwargs): page = self.get_current_buffer() + if not hasattr(page.buffer, "list"): + output.speak(_(u"This account is not logged in twitter.")) + return position = page.buffer.list.get_selected() index = position-1 try: @@ -660,6 +721,9 @@ class Controller(object): def down(self, *args, **kwargs): page = self.get_current_buffer() + if not hasattr(page.buffer, "list"): + output.speak(_(u"This account is not logged in twitter.")) + return position = page.buffer.list.get_selected() index = position+1 try: @@ -676,6 +740,9 @@ class Controller(object): def left(self, *args, **kwargs): buff = self.view.get_current_buffer_pos() buffer = self.get_current_buffer() + if not hasattr(buffer.buffer, "list"): + output.speak(_(u"This account is not logged in twitter.")) + return if buff == self.get_first_buffer(buffer.account) or buff == 0: self.view.change_buffer(self.get_last_buffer(buffer.account)) else: @@ -691,6 +758,9 @@ class Controller(object): def right(self, *args, **kwargs): buff = self.view.get_current_buffer_pos() buffer = self.get_current_buffer() + if not hasattr(buffer.buffer, "list"): + output.speak(_(u"This account is not logged in twitter.")) + return if buff == self.get_last_buffer(buffer.account) or buff+1 == self.view.get_buffer_count(): self.view.change_buffer(self.get_first_buffer(buffer.account)) else: @@ -712,6 +782,9 @@ class Controller(object): account = self.accounts[index] self.current_account = account buff = self.view.search("home_timeline", account) + if buff == None: + output.speak(_(u"This account is not logged in twitter.")) + return self.view.change_buffer(buff) buffer = self.get_current_buffer() try: @@ -729,6 +802,9 @@ class Controller(object): account = self.accounts[index] self.current_account = account buff = self.view.search("home_timeline", account) + if buff == None: + output.speak(_(u"This account is not logged in twitter.")) + return self.view.change_buffer(buff) buffer = self.get_current_buffer() try: @@ -918,6 +994,7 @@ class Controller(object): def check_connection(self): for i in session_.sessions: + if session_.sessions[i].is_logged == False: continue session_.sessions[i].check_connection() def create_new_buffer(self, buffer, account, create): diff --git a/src/sessionmanager/session.py b/src/sessionmanager/session.py index fc52a9ab..64823f6c 100644 --- a/src/sessionmanager/session.py +++ b/src/sessionmanager/session.py @@ -94,6 +94,10 @@ class Session(object): self.db = {} self.reconnection_function_active = False + @property + def is_logged(self): + return self.logged + def get_configuration(self): """ Gets settings for a session.""" diff --git a/src/sessionmanager/sessionManager.py b/src/sessionmanager/sessionManager.py index dcce5dc4..eea48a7f 100644 --- a/src/sessionmanager/sessionManager.py +++ b/src/sessionmanager/sessionManager.py @@ -23,7 +23,7 @@ class sessionManagerController(object): log.debug("Filling the sessions list.") self.sessions = [] for i in os.listdir(paths.config_path()): - if os.path.isdir(paths.config_path(i)) and i not in config.app["sessions"]["ignored_sessions"]: + if os.path.isdir(paths.config_path(i)): log.debug("Adding session %s" % (i,)) strconfig = "%s/session.conf" % (paths.config_path(i)) config_test = Configuration(strconfig) @@ -31,8 +31,8 @@ class sessionManagerController(object): if name != "" and config_test["twitter"]["user_key"] != "" and config_test["twitter"]["user_secret"] != "": sessionsList.append(name) self.sessions.append(i) - else: - log.debug("Ignoring session %s" % (i,)) +# else: +# log.debug("Ignoring session %s" % (i,)) if hasattr(self, "view"): self.view.fill_list(sessionsList) def show(self): @@ -45,7 +45,8 @@ class sessionManagerController(object): for i in self.sessions: s = session.Session(i) s.get_configuration() - s.login() + if i not in config.app["sessions"]["ignored_sessions"]: + s.login() session.sessions[i] = s def manage_new_account(self): diff --git a/src/widgetUtils/wxUtils.py b/src/widgetUtils/wxUtils.py index 465b5af4..0d1f668f 100644 --- a/src/widgetUtils/wxUtils.py +++ b/src/widgetUtils/wxUtils.py @@ -18,6 +18,7 @@ NO = wx.ID_NO CLOSE_EVENT = wx.EVT_CLOSE # This is activated when a button is pressed. BUTTON_PRESSED = wx.EVT_BUTTON +CHECKBOX = wx.EVT_CHECKBOX # This is activated when an user enter text on an edit box. ENTERED_TEXT = wx.EVT_TEXT MENU = wx.EVT_MENU diff --git a/src/wxUI/buffers/panels.py b/src/wxUI/buffers/panels.py index 892ded9c..f861520b 100644 --- a/src/wxUI/buffers/panels.py +++ b/src/wxUI/buffers/panels.py @@ -8,11 +8,28 @@ class accountPanel(wx.Panel): self.name = name self.type = "account" sizer = wx.BoxSizer(wx.VERTICAL) - self.list = widgets.list(self, _(u"Announce")) - sizer.Add(self.list.list, 0, wx.ALL, 5) + self.login = wx.Button(self, -1, _(u"Login")) + sizer.Add(self.login, 0, wx.ALL, 5) + self.autostart_account = wx.CheckBox(self, -1, _(u"Start account automatically")) + sizer.Add(self.autostart_account, 0, wx.ALL, 5) self.SetSizer(sizer) -class emptyPanel(accountPanel): + def change_login(self, login=True): + if login == True: + self.login.SetLabel(_(u"Login")) + else: + self.login.SetLabel(_(u"Logout")) + + def change_autostart(self, autostart=True): + self.autostart_account.SetValue(autostart) + + def get_autostart(self): + return self.autostart_account.GetValue() + +class emptyPanel(wx.Panel): def __init__(self, parent, name): super(emptyPanel, self).__init__(parent=parent, name=name) - self.type = "empty" \ No newline at end of file + self.name = name + self.type = "account" + sizer = wx.BoxSizer(wx.VERTICAL) + self.SetSizer(sizer)