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 #conic is used for connection handling
30 #import socket for handling socket exceptions
33 # we will store our preferences in gconf
36 #ugly hack to check maemo version. any better way?
37 if hasattr(hildon, "StackableWindow"):
38 from ui.fremantle import gui
40 from ui.diablo import gui
44 The controller class for mEvemon. The intent is to help
45 abstract the EVE API and settings code from the UI code.
49 GCONF_DIR = "/apps/maemo/mevemon"
52 self.program = hildon.Program()
53 self.program.__init__()
54 self.gconf = gnome.gconf.client_get_default()
55 #NOTE: remove this after a few releases
56 self.update_settings()
58 self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
59 apicache.cache_handler(debug=False))
60 self.gui = gui.mEveMonUI(self)
65 def quit(self, *args):
68 def update_settings(self):
70 Update from the old pre 0.3 settings to the new settings layout.
71 We should remove this eventually, once no one is using pre-0.3 mEveMon
73 uid = self.gconf.get_string("%s/eve_uid" % self.GCONF_DIR)
76 key = self.gconf.get_string("%s/eve_api_key" % self.GCONF_DIR)
77 self.add_account(uid, key)
78 self.gconf.unset("%s/eve_uid" % self.GCONF_DIR)
79 self.gconf.unset("%s/eve_api_key" % self.GCONF_DIR)
82 def get_accounts(self):
84 Returns a dictionary containing uid:api_key pairs gathered from gconf
87 entries = self.gconf.all_entries("%s/accounts" % self.GCONF_DIR)
90 key = os.path.basename(entry.get_key())
91 value = entry.get_value().to_string()
96 def get_api_key(self, uid):
98 Returns the api key associated with the given uid.
100 return self.gconf.get_string("%s/accounts/%s" % (self.GCONF_DIR, uid)) or ''
102 def remove_account(self, uid):
104 Removes the provided uid key from gconf
106 self.gconf.unset("%s/accounts/%s" % (self.GCONF_DIR, uid))
108 def add_account(self, uid, api_key):
110 Adds the provided uid:api_key pair to gconf.
112 self.gconf.set_string("%s/accounts/%s" % (self.GCONF_DIR, uid), api_key)
114 def get_auth(self, uid):
116 Returns an authentication object to be used for eveapi calls
117 that require authentication.
119 api_key = self.get_api_key(uid)
122 auth = self.cached_api.auth(userID=uid, apiKey=api_key)
128 def get_char_sheet(self, uid, char_id):
130 Returns an object containing information about the character specified
131 by the provided character ID.
134 sheet = self.get_auth(uid).character(char_id).CharacterSheet()
136 # TODO: we should really have a logger that logs this error somewhere
141 def charid2uid(self, char_id):
143 Takes a character ID and returns the user ID of the account containing
146 Returns None if the character isn't found in any of the registered accounts.
149 acct_dict = self.get_accounts()
151 for uid, api_key in acct_dict.items():
152 auth = self.cached_api.auth(userID=uid, apiKey=api_key)
153 api_char_list = auth.account.Characters()
155 for character in api_char_list.characters:
156 if character.characterID == char_id:
162 def char_id2name(self, char_id):
164 Takes a character ID and returns the character name associated with
166 The EVE API accepts a comma-separated list of IDs, but for now we
167 will just handle a single ID.
170 chars = self.cached_api.eve.CharacterName(ids=char_id).characters
171 name = chars[0].characterName
177 def char_name2id(self, name):
179 Takes the name of an EVE character and returns the characterID.
181 The EVE api accepts a comma separated list of names, but for now
182 we will just handle single names/
185 chars = self.cached_api.eve.CharacterID(names=name).characters
186 char_id = chars[0].characterID
187 char_name = chars[0].name
193 def get_chars_from_acct(self, uid):
195 Returns a list of characters associated with the provided user ID.
197 auth = self.get_auth(uid)
202 api_char_list = auth.account.Characters()
203 char_list = [char.name for char in api_char_list.characters]
209 def get_characters(self):
211 Returns a list of (character_name, image_path, uid) tuples from all the
212 accounts that are registered to mEveMon.
214 If there is an authentication issue, then instead of adding a valid
215 pair to the list, it appends an 'error message'
219 err_img = "/usr/share/mevemon/imgs/error.jpg"
221 placeholder_chars = ("Please check your API settings.", err_img, "0")
223 acct_dict = self.get_accounts()
225 return [placeholder_chars]
227 for uid in acct_dict.keys():
228 char_names = self.get_chars_from_acct(uid)
231 ui_char_list.append(placeholder_chars)
233 # append each char we get to the list we'll return to the
235 for char_name in char_names:
236 ui_char_list.append((char_name, self.get_portrait(char_name, 64) , uid) )
240 def get_portrait(self, char_name, size):
242 Returns the file path of the retrieved portrait
244 char_id = self.char_name2id(char_name)
246 return fetchimg.portrait_filename(char_id, size)
248 def get_skill_tree(self):
250 Returns an object from eveapi containing skill tree info
253 tree = self.cached_api.eve.SkillTree()
259 def get_skill_in_training(self, uid, char_id):
261 Returns an object from eveapi containing information about the
262 current skill in training
266 skill = self.get_auth(uid).character(char_id).SkillInTraining()
272 def connection_cb(self, connection, event, mgc):
277 connection = conic.Connection()
279 connection.connect("connection-event", self.connection_cb, 0xAA55)
280 assert(connection.request_connection(conic.CONNECT_FLAG_NONE))
283 if __name__ == "__main__":