From e1192b1185ce29505332045ae6c3641abe268279 Mon Sep 17 00:00:00 2001 From: Ryan Campbell Date: Wed, 28 Apr 2010 12:14:05 -0600 Subject: [PATCH] started work on multiple accounts support --- package/src/mevemon.py | 93 ++++++++++++++++++++------------------- package/src/ui/fremantle/gui.py | 66 ++++++++++++++++++++++----- package/src/ui/models.py | 29 ++++++++++-- 3 files changed, 127 insertions(+), 61 deletions(-) diff --git a/package/src/mevemon.py b/package/src/mevemon.py index 07a044f..d001353 100755 --- a/package/src/mevemon.py +++ b/package/src/mevemon.py @@ -23,6 +23,7 @@ import gtk from eveapi import eveapi import fetchimg import apicache +import os.path # we will store our preferences in gconf import gnome.gconf @@ -40,7 +41,8 @@ class mEveMon(): self.program = hildon.Program() self.program.__init__() self.gconf = gnome.gconf.client_get_default() - self.set_auth() + self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \ + apicache.cache_handler(debug=False)) self.gui = gui.mEveMonUI(self) def run(self): @@ -49,45 +51,41 @@ class mEveMon(): def quit(self, *args): gtk.main_quit() - def get_api_key(self): - return self.gconf.get_string("/apps/maemo/mevemon/eve_api_key") or '' + def get_accounts(self): + accounts = {} + entries = self.gconf.all_entries("/apps/maemo/mevemon/accounts") - def get_uid(self): - return self.gconf.get_string("/apps/maemo/mevemon/eve_uid") or '' + for entry in entries: + key = os.path.basename(entry.get_key()) + value = entry.get_value().to_string() + accounts[key] = value - def set_api_key(self, key): - self.gconf.set_string("/apps/maemo/mevemon/eve_api_key", key) + return accounts + + def get_api_key(self, uid): + return self.gconf.get_string("/apps/maemo/mevemon/accounts/%s" % uid) or '' - def set_uid(self, uid): - self.gconf.set_string("/apps/maemo/mevemon/eve_uid", uid) + def remove_account(self, uid): + self.gconf.unset("/apps/maemo/mevemon/accounts/%s" % uid) + def add_account(self, uid, api_key): + self.gconf.set_string("/apps/maemo/mevemon/accounts/%s" % uid, api_key) + + + def get_auth(self, uid): + + api_key = self.get_api_key(uid) - def set_auth(self): - """ - set self.auth to None if there was a problem. somehow later on we'll - have to pass the error to the UI, but for now I just want the program - to not be broken. --danny - """ - uid = self.get_uid() - api_key = self.get_api_key() - self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \ - apicache.cache_handler( debug = False ) ) try: - self.auth = self.cached_api.auth( userID = uid, apiKey = api_key ) + auth = self.cached_api.auth(userID=uid, apiKey=api_key) except eveapi.Error, e: - # we need to deal with this, so raise --danny - raise - except ValueError, e: - self.auth = None - #raise + return None - def get_auth(self): - return self.auth + return auth - def get_char_sheet(self, charID): - if not self.auth: return None + def get_char_sheet(self, uid, charID): try: - sheet = self.auth.character(charID).CharacterSheet() + sheet = self.get_auth(uid).character(charID).CharacterSheet() except eveapi.Error, e: # we should really have a logger that logs this error somewhere return None @@ -117,7 +115,7 @@ class mEveMon(): return char_id - def get_characters( self ): + def get_characters(self): """ returns a list containing a single character with an error message for a name, if there's a problem. FIXME --danny @@ -125,18 +123,23 @@ class mEveMon(): ui_char_list = [] err_img = "/usr/share/mevemon/imgs/error.jpg" - placeholder_chars = [("Please check your API settings.", err_img)] - if not self.auth: return placeholder_chars - try: - api_char_list = self.auth.account.Characters() - # append each char we get to the list we'll return to the - # UI --danny - for character in api_char_list.characters: - ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ) ) ) - except eveapi.Error, e: - # again, we need to handle this... --danny - return placeholder_chars - #raise + placeholder_chars = ("Please check your API settings.", err_img, "0") + + acct_dict = self.get_accounts() + if not acct_dict: + return [placeholder_chars] + + for uid, apiKey in acct_dict.items(): + auth = self.cached_api.auth(userID=uid, apiKey=apiKey) + try: + api_char_list = auth.account.Characters() + # append each char we get to the list we'll return to the + # UI --danny + for character in api_char_list.characters: + ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ), uid) ) + except eveapi.Error, e: + # again, we need to handle this... --danny + ui_char_list.append(placeholder_chars) return ui_char_list @@ -156,9 +159,9 @@ class mEveMon(): return tree - def get_skill_in_training(self, charID): + def get_skill_in_training(self, uid, charID): try: - skill = self.auth.character(charID).SkillInTraining() + skill = self.get_auth(uid).character(charID).SkillInTraining() except: print e return None diff --git a/package/src/ui/fremantle/gui.py b/package/src/ui/fremantle/gui.py index e634c94..74c72d1 100644 --- a/package/src/ui/fremantle/gui.py +++ b/package/src/ui/fremantle/gui.py @@ -58,18 +58,66 @@ class BaseUI(): return menu - def set_pix(self, filename): - pixbuf = gtk.gdk.pixbuf_new_from_file(filename) - return pixbuf - def settings_clicked(self, button, window): + + RESPONSE_NEW, RESPONSE_EDIT, RESPONSE_DELETE = range(3) + + dialog = gtk.Dialog() + dialog.set_transient_for(window) + dialog.set_title("Settings") + + vbox = dialog.vbox + + acctsLabel = gtk.Label("Accounts:") + acctsLabel.set_justify(gtk.JUSTIFY_LEFT) + + vbox.pack_start(acctsLabel, False, False, 1) + + accounts_model = models.AccountsModel(self.controller) + + accounts_treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_NORMAL) + accounts_treeview.set_model(accounts_model) + self.add_columns_to_accounts(accounts_treeview) + vbox.pack_start(accounts_treeview, False, False, 1) + + # all stock responses are negative, so we can use any positive value + new_button = dialog.add_button("New", RESPONSE_NEW) + edit_button = dialog.add_button("Edit", RESPONSE_EDIT) + delete_button = dialog.add_button("Delete", RESPONSE_DELETE) + ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK) + + dialog.show_all() + result = dialog.run() + if result == RESPONSE_NEW: + self.new_account_clicked(window) + accounts_model.get_accounts() + elif result = RESPONSE_EDIT: + # get the selected treeview item and pop up the account_box + pass + elif result = RESPONSE_DELETE: + # get the selected treeview item, and delete the gconf keys + pass + elif result == gtk.RESPONSE_OK: + self.char_model.get_characters() + dialog.destroy() + + + def add_columns_to_accounts(self, treeview): + #Column 0 for the treeview + renderer = gtk.CellRendererText() + column = gtk.TreeViewColumn('Account ID', renderer, text=0) + column.set_property("expand", True) + treeview.append_column(column) + + + def new_account_clicked(self, window): dialog = gtk.Dialog() #get the vbox to pack all the settings into vbox = dialog.vbox dialog.set_transient_for(window) - dialog.set_title("Settings") + dialog.set_title("New Account") uidLabel = gtk.Label("User ID:") uidLabel.set_justify(gtk.JUSTIFY_LEFT) @@ -77,7 +125,6 @@ class BaseUI(): uidEntry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT) uidEntry.set_placeholder("User ID") - uidEntry.set_text(self.controller.get_uid()) uidEntry.set_property('is_focus', False) vbox.add(uidEntry) @@ -88,7 +135,6 @@ class BaseUI(): apiEntry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT) apiEntry.set_placeholder("API Key") - apiEntry.set_text(self.controller.get_api_key()) apiEntry.set_property('is_focus', False) vbox.add(apiEntry) @@ -98,11 +144,7 @@ class BaseUI(): dialog.show_all() result = dialog.run() if result == gtk.RESPONSE_OK: - self.controller.set_api_key(apiEntry.get_text()) - self.controller.set_uid(uidEntry.get_text()) - self.controller.set_auth() - self.char_model.get_characters() - + self.controller.add_account(uidEntry.get_text(), apiEntry.get_text()) dialog.destroy() diff --git a/package/src/ui/models.py b/package/src/ui/models.py index b4de052..8c4a620 100644 --- a/package/src/ui/models.py +++ b/package/src/ui/models.py @@ -1,11 +1,32 @@ import gtk +class AccountsModel(gtk.ListStore): + C_UID, C_APIKEY = range(2) + + def __init__(self, controller): + gtk.ListStore.__init__(self, str, str) + self.controller = controller + self.get_accounts() + + def get_accounts(self): + self.clear() + + accts_dict = self.controller.get_accounts() + + if not accts_dict: + return None + + for uid, key in accts_dict.items(): + liter = self.append() + self.set(liter, self.C_UID, uid, self.C_APIKEY, key) + + class CharacterListModel(gtk.ListStore): - C_PORTRAIT, C_NAME = range(2) + C_PORTRAIT, C_NAME, C_UID = range(3) def __init__(self, controller): - gtk.ListStore.__init__(self, gtk.gdk.Pixbuf, str) + gtk.ListStore.__init__(self, gtk.gdk.Pixbuf, str, str) self.controller = controller # should we do this on initialization? self.get_characters() @@ -15,9 +36,9 @@ class CharacterListModel(gtk.ListStore): char_list = self.controller.get_characters() - for name, icon in char_list: + for name, icon, uid in char_list: liter = self.append() - self.set(liter, self.C_PORTRAIT, self._set_pix(icon), self.C_NAME, name) + self.set(liter, self.C_PORTRAIT, self._set_pix(icon), self.C_NAME, name, self.C_UID, uid) def _set_pix(self, filename): pixbuf = gtk.gdk.pixbuf_new_from_file(filename) -- 1.7.9.5