diff --git a/src/main.py b/src/main.py index 34b393f..82de162 100644 --- a/src/main.py +++ b/src/main.py @@ -52,6 +52,7 @@ def setup(): log.debug("Created Application mainloop object") sm = sessionManager.sessionManagerController() + sm.show() del sm r = mainController.Controller() call_threaded(r.login) diff --git a/src/sessionmanager/session.py b/src/sessionmanager/session.py index 84b162e..5ab34d7 100644 --- a/src/sessionmanager/session.py +++ b/src/sessionmanager/session.py @@ -111,7 +111,7 @@ class vkSession(object): def is_logged(self): return self.logged - def get_configuration(self): + def get_configuration(self, nosound=False): """ Gets settings for a session.""" @@ -119,9 +119,10 @@ class vkSession(object): # try: log.debug("Creating config file %s" % (file_,)) self.settings = Configuration(os.path.join(paths.config_path(), file_), os.path.join(paths.app_path(), "session.defaults")) - self.soundplayer = sound.soundSystem(config.app["sound"]) - pub.subscribe(self.play_sound, "play-sound") - pub.subscribe(self.post, "post") + if nosound == False: + self.soundplayer = sound.soundSystem(config.app["sound"]) + pub.subscribe(self.play_sound, "play-sound") + pub.subscribe(self.post, "post") # except: # log.exception("The session configuration has failed.") diff --git a/src/sessionmanager/sessionManager.py b/src/sessionmanager/sessionManager.py index 68d9a1b..3a54367 100644 --- a/src/sessionmanager/sessionManager.py +++ b/src/sessionmanager/sessionManager.py @@ -5,6 +5,7 @@ import widgetUtils import paths import time import logging +import shutil from authenticator.official import AuthenticationError from . import wxUI as view from . import session @@ -16,12 +17,16 @@ class sessionManagerController(object): def __init__(self): super(sessionManagerController, self).__init__() log.debug("Setting up the session manager.") + 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.fill_list() - if not hasattr(self, "session"): + if len(self.sessions) == 0: log.debug("the session list is empty, creating a new one...") self.manage_new_account() def fill_list(self): + self.sessions = [] log.debug("Filling the session list...") for i in os.listdir(paths.config_path()): if os.path.isdir(os.path.join(paths.config_path(), i)): @@ -29,12 +34,10 @@ class sessionManagerController(object): config_test = Configuration(os.path.join(paths.config_path(), i, "session.conf")) name = config_test["vk"]["user"] if name != "" and config_test["vk"]["password"] != "": - self.session = i - s = session.vkSession(self.session) - s.get_configuration() - session.sessions[self.session] = s + self.sessions.append((i, name)) + self.view.list.insert_item(False, *[name]) - def manage_new_account(self): + def manage_new_account(self, *args, **kwargs): if view.new_account_dialog() == widgetUtils.YES: location = (str(time.time())[-6:]) log.debug("Creating session in the %s path" % (location,)) @@ -42,11 +45,11 @@ class sessionManagerController(object): path = os.path.join(paths.config_path(), location) if not os.path.exists(path): os.mkdir(path) - s.get_configuration() + s.get_configuration(True) self.get_authorisation(s) - session.sessions[location] = s - else: - sys.exit() + name = s.settings["vk"]["user"] + self.sessions.append((location, name)) + self.view.list.insert_item(False, *[name]) def get_authorisation(self, c): log.debug("Starting the authorisation process...") @@ -59,4 +62,29 @@ class sessionManagerController(object): except AuthenticationError: c.settings["vk"]["password"] = "" c.settings["vk"]["user"] - return self.get_authorisation(c) \ No newline at end of file + return self.get_authorisation(c) + + def do_ok(self): + selected_session = self.sessions[self.view.list.get_selected()] + self.session = selected_session[0] + self.session = session.vkSession(self.session) + self.session.get_configuration() + session.sessions[selected_session[1]] = self.session + + def show(self): + if len(self.sessions) > 1: + answer = self.view.get_response() + else: + answer = widgetUtils.OK + if answer == widgetUtils.OK: + self.do_ok() + else: + sys.exit() + self.view.destroy() + + def remove(self, *args, **kwargs): + if self.view.remove_account_dialog() == widgetUtils.YES: + selected_session = self.sessions[self.view.list.get_selected()] + shutil.rmtree(path=os.path.join(paths.config_path(), selected_session[0]), ignore_errors=True) + self.sessions.remove(selected_session) + self.view.list.remove_item(self.view.list.get_selected()) \ No newline at end of file diff --git a/src/sessionmanager/wxUI.py b/src/sessionmanager/wxUI.py index 05ad367..a6e1016 100644 --- a/src/sessionmanager/wxUI.py +++ b/src/sessionmanager/wxUI.py @@ -36,4 +36,32 @@ class newSessionDialog(widgetUtils.BaseDialog): return self.email.GetValue() def get_password(self): - return self.passw.GetValue() \ No newline at end of file + return self.passw.GetValue() + +class sessionManagerWindow(widgetUtils.BaseDialog): + def __init__(self): + super(sessionManagerWindow, self).__init__(parent=None, title=_("Select an account"), size=wx.DefaultSize) + panel = wx.Panel(self) + sizer = wx.BoxSizer(wx.VERTICAL) + label = wx.StaticText(panel, -1, _(u"Accounts list"), size=wx.DefaultSize) + listSizer = wx.BoxSizer(wx.HORIZONTAL) + self.list = widgetUtils.list(panel, _("Account"), style=wx.LC_SINGLE_SEL|wx.LC_REPORT) + listSizer.Add(label, 0, wx.ALL, 5) + listSizer.Add(self.list.list, 0, wx.ALL, 5) + sizer.Add(listSizer, 0, wx.ALL, 5) + self.new = wx.Button(panel, -1, _("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() + cancel = wx.Button(panel, wx.ID_CANCEL, size=wx.DefaultSize) + buttons = wx.BoxSizer(wx.HORIZONTAL) + 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) + panel.SetSizer(sizer) + min = sizer.CalcMin() + self.SetClientSize(min) + + def remove_account_dialog(self): + return wx.MessageDialog(self, _("Do you really want to delete this account?"), _("Remove account"), wx.YES_NO).ShowModal() \ No newline at end of file