3 # mEveMon - A character monitor for EVE Online
4 # Copyright (c) 2010 Ryan and Danny Campbell, and the mEveMon Team
6 # mEveMon is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # mEveMon is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 from eveapi import eveapi
28 # we will store our preferences in gconf
31 #ugly hack to check maemo version. any better way?
32 if hasattr(hildon, "StackableWindow"):
33 from ui.fremantle import gui
35 from ui.diablo import gui
39 The controller class for mEvemon. The intent is to help
40 abstract the EVE API and settings code from the UI code.
44 GCONF_DIR = "/apps/maemo/mevemon"
47 self.program = hildon.Program()
48 self.program.__init__()
49 self.gconf = gnome.gconf.client_get_default()
50 #NOTE: remove this after a few releases
51 self.update_settings()
52 self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
53 apicache.cache_handler(debug=False))
54 self.gui = gui.mEveMonUI(self)
59 def quit(self, *args):
62 def update_settings(self):
64 Update from the old pre 0.3 settings to the new settings layout.
65 We should remove this eventually, once no one is using pre-0.3 mEveMon
67 uid = self.gconf.get_string("%s/eve_uid" % self.GCONF_DIR)
70 key = self.gconf.get_string("%s/eve_api_key" % self.GCONF_DIR)
71 self.add_account(uid, key)
72 self.gconf.unset("%s/eve_uid" % self.GCONF_DIR)
73 self.gconf.unset("%s/eve_api_key" % self.GCONF_DIR)
76 def get_accounts(self):
78 Returns a dictionary containing uid:api_key pairs gathered from gconf
81 entries = self.gconf.all_entries("%s/accounts" % self.GCONF_DIR)
84 key = os.path.basename(entry.get_key())
85 value = entry.get_value().to_string()
90 def get_api_key(self, uid):
92 Returns the api key associated with the given uid.
94 return self.gconf.get_string("%s/accounts/%s" % (self.GCONF_DIR, uid)) or ''
96 def remove_account(self, uid):
98 Removes the provided uid key from gconf
100 self.gconf.unset("%s/accounts/%s" % (self.GCONF_DIR, uid))
102 def add_account(self, uid, api_key):
104 Adds the provided uid:api_key pair to gconf.
106 self.gconf.set_string("%s/accounts/%s" % (self.GCONF_DIR, uid), api_key)
108 def get_auth(self, uid):
110 Returns an authentication object to be used for eveapi calls
111 that require authentication.
113 api_key = self.get_api_key(uid)
116 auth = self.cached_api.auth(userID=uid, apiKey=api_key)
122 def get_char_sheet(self, uid, char_id):
124 Returns an object containing information about the character specified
125 by the provided character ID.
128 sheet = self.get_auth(uid).character(char_id).CharacterSheet()
130 # TODO: we should really have a logger that logs this error somewhere
135 def charid2uid(self, char_id):
137 Takes a character ID and returns the user ID of the account containing
140 Returns None if the character isn't found in any of the registered accounts.
143 acct_dict = self.get_accounts()
145 for uid, api_key in acct_dict.items():
146 auth = self.cached_api.auth(userID=uid, apiKey=api_key)
147 api_char_list = auth.account.Characters()
149 for character in api_char_list.characters:
150 if character.characterID == char_id:
156 def char_id2name(self, char_id):
158 Takes a character ID and returns the character name associated with
160 The EVE API accepts a comma-separated list of IDs, but for now we
161 will just handle a single ID.
164 chars = self.cached_api.eve.CharacterName(ids=char_id).characters
165 name = chars[0].characterName
171 def char_name2id(self, name):
173 Takes the name of an EVE character and returns the characterID.
175 The EVE api accepts a comma separated list of names, but for now
176 we will just handle single names/
179 chars = self.cached_api.eve.CharacterID(names=name).characters
180 char_id = chars[0].characterID
181 char_name = chars[0].name
187 def get_chars_from_acct(self, uid):
189 Returns a list of characters associated with the provided user ID.
191 auth = self.get_auth(uid)
196 api_char_list = auth.account.Characters()
197 char_list = [char.name for char in api_char_list.characters]
203 def get_characters(self):
205 Returns a list of (character_name, image_path, uid) tuples from all the
206 accounts that are registered to mEveMon.
208 If there is an authentication issue, then instead of adding a valid
209 pair to the list, it appends an 'error message'
213 err_img = "/usr/share/mevemon/imgs/error.jpg"
215 placeholder_chars = ("Please check your API settings.", err_img, "0")
217 acct_dict = self.get_accounts()
219 return [placeholder_chars]
221 for uid in acct_dict.keys():
222 char_names = self.get_chars_from_acct(uid)
225 ui_char_list.append(placeholder_chars)
227 # append each char we get to the list we'll return to the
229 for char_name in char_names:
230 ui_char_list.append((char_name, self.get_portrait(char_name, 64) , uid) )
234 def get_portrait(self, char_name, size):
236 Returns the file path of the retrieved portrait
238 char_id = self.char_name2id(char_name)
240 return fetchimg.portrait_filename(char_id, size)
242 def get_skill_tree(self):
244 Returns an object from eveapi containing skill tree info
247 tree = self.cached_api.eve.SkillTree()
253 def get_skill_in_training(self, uid, char_id):
255 Returns an object from eveapi containing information about the
256 current skill in training
260 skill = self.get_auth(uid).character(char_id).SkillInTraining()
267 if __name__ == "__main__":