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/>.
25 #import socket for handling socket exceptions
28 import logging.handlers
32 #conic is used for connection handling
34 # we will store our preferences in gconf
37 from eveapi import eveapi
41 #ugly hack to check maemo version. any better way?
42 if hasattr(hildon, "StackableWindow"):
43 from ui.fremantle import gui
45 from ui.diablo import gui
47 LOGNAME = "mevemon.log"
48 CONFIG_DIR = os.path.expanduser("~/.mevemon/")
49 LOGPATH = os.path.join(CONFIG_DIR, LOGNAME)
53 """ The controller class for mEvemon. The intent is to help
54 abstract the EVE API and settings code from the UI code.
57 about_name = 'mEveMon'
58 about_text = ('Mobile character monitor for EVE Online')
59 about_authors = ['Ryan Campbell <campbellr@gmail.com>',
60 'Danny Campbell <danny.campbell@gmail.com>']
62 about_website = 'http://mevemon.garage.maemo.org'
66 GCONF_DIR = "/apps/maemo/mevemon"
69 self.program = hildon.Program()
70 self.program.__init__()
71 self.gconf = gnome.gconf.client_get_default()
72 #NOTE: remove this after a few releases
73 self.update_settings()
74 self.connect_to_network()
75 self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
76 apicache.cache_handler(debug=False))
77 self.gui = gui.mEveMonUI(self)
83 def quit(self, *args):
86 def update_settings(self):
87 """ Update from the old pre 0.3 settings to the new settings layout.
88 We should remove this eventually, once no one is using pre-0.3 mEveMon
90 uid = self.gconf.get_string("%s/eve_uid" % self.GCONF_DIR)
93 key = self.gconf.get_string("%s/eve_api_key" % self.GCONF_DIR)
94 self.add_account(uid, key)
95 self.gconf.unset("%s/eve_uid" % self.GCONF_DIR)
96 self.gconf.unset("%s/eve_api_key" % self.GCONF_DIR)
99 def get_accounts(self):
100 """ Returns a dictionary containing uid:api_key pairs gathered from gconf
103 entries = self.gconf.all_entries("%s/accounts" % self.GCONF_DIR)
105 for entry in entries:
106 key = os.path.basename(entry.get_key())
107 value = entry.get_value().to_string()
108 accounts[key] = value
112 def get_api_key(self, uid):
113 """ Returns the api key associated with the given uid.
115 return self.gconf.get_string("%s/accounts/%s" % (self.GCONF_DIR, uid)) or ''
117 def remove_account(self, uid):
118 """ Removes the provided uid key from gconf
120 self.gconf.unset("%s/accounts/%s" % (self.GCONF_DIR, uid))
122 def add_account(self, uid, api_key):
124 Adds the provided uid:api_key pair to gconf.
126 self.gconf.set_string("%s/accounts/%s" % (self.GCONF_DIR, uid), api_key)
128 def get_auth(self, uid):
129 """ Returns an authentication object to be used for eveapi calls
130 that require authentication.
132 api_key = self.get_api_key(uid)
135 auth = self.cached_api.auth(userID=uid, apiKey=api_key)
137 self.gui.report_error(str(e))
138 traceback.print_exc()
143 def get_char_sheet(self, uid, char_id):
144 """ Returns an object containing information about the character specified
145 by the provided character ID.
148 sheet = self.get_auth(uid).character(char_id).CharacterSheet()
150 self.gui.report_error(str(e))
151 # TODO: we should really have a logger that logs this error somewhere
152 traceback.print_exc()
157 def charid2uid(self, char_id):
158 """ Takes a character ID and returns the user ID of the account containing
161 Returns None if the character isn't found in any of the registered accounts.
164 acct_dict = self.get_accounts()
166 for uid, api_key in acct_dict.items():
167 auth = self.cached_api.auth(userID=uid, apiKey=api_key)
169 api_char_list = auth.account.Characters()
170 characters = api_char_list.characters
174 for character in characters:
175 if character.characterID == char_id:
181 def char_id2name(self, char_id):
182 """ Takes a character ID and returns the character name associated with
184 The EVE API accepts a comma-separated list of IDs, but for now we
185 will just handle a single ID.
188 chars = self.cached_api.eve.CharacterName(ids=char_id).characters
189 name = chars[0].characterName
191 self.gui.report_error(str(e))
192 traceback.print_exc()
197 def char_name2id(self, name):
198 """ Takes the name of an EVE character and returns the characterID.
200 The EVE api accepts a comma separated list of names, but for now
201 we will just handle single names/
204 chars = self.cached_api.eve.CharacterID(names=name).characters
205 char_id = chars[0].characterID
206 char_name = chars[0].name
208 self.gui.report_error(str(e))
209 traceback.print_exc()
214 def get_chars_from_acct(self, uid):
215 """ Returns a list of characters associated with the provided user ID.
217 auth = self.get_auth(uid)
222 api_char_list = auth.account.Characters()
223 char_list = [char.name for char in api_char_list.characters]
225 self.gui.report_error(str(e))
226 traceback.print_exc()
231 def get_characters(self):
232 """ Returns a list of (character_name, image_path, uid) tuples from all the
233 accounts that are registered to mEveMon.
235 If there is an authentication issue, then instead of adding a valid
236 pair to the list, it appends an 'error message'
240 err_img = "/usr/share/mevemon/imgs/error.jpg"
241 err_txt = "Problem fetching info for account"
243 placeholder_chars = (err_txt, err_img, None)
245 acct_dict = self.get_accounts()
247 return [placeholder_chars]
249 for uid in acct_dict.keys():
250 char_names = self.get_chars_from_acct(uid)
253 ui_char_list.append((err_txt + "\t(UID: %s)" % uid, err_img, None))
255 # append each char we get to the list we'll return to the
257 for char_name in char_names:
258 ui_char_list.append((char_name, self.get_portrait(char_name, 64) , uid) )
262 def get_portrait(self, char_name, size):
263 """ Returns the file path of the retrieved portrait
265 char_id = self.char_name2id(char_name)
267 return fetchimg.portrait_filename(char_id, size)
269 def get_skill_tree(self):
270 """ Returns an object from eveapi containing skill tree info
273 tree = self.cached_api.eve.SkillTree()
275 self.gui.report_error(str(e))
276 traceback.print_exc()
281 def get_skill_in_training(self, uid, char_id):
282 """ Returns an object from eveapi containing information about the
283 current skill in training
286 skill = self.get_auth(uid).character(char_id).SkillInTraining()
288 self.gui.report_error(str(e))
289 traceback.print_exc()
294 def connection_cb(self, connection, event, mgc):
295 """ I'm not sure why we need this, but connection.connect() won't work
296 without it, even empty.
301 def connect_to_network(self):
302 """ This will connect to the default network if avaliable, or pop up the
303 connection dialog to select a connection.
304 Running this when we start the program ensures we are connected to a
307 connection = conic.Connection()
309 connection.connect("connection-event", self.connection_cb, 0xAA55)
310 assert(connection.request_connection(conic.CONNECT_FLAG_NONE))
313 def get_sp(self, uid, char_id):
314 """ Adds up the SP for all known skills, then calculates the SP gained
315 from an in-training skill.
319 sheet = self.get_char_sheet(uid, char_id)
320 for skill in sheet.skills:
321 actual_sp += skill.skillpoints
323 live_sp = actual_sp + self.get_training_sp(uid, char_id)
327 def get_spps(self, uid, char_id):
328 """ Calculate and returns the skill points per hour for the given character.
330 skill = self.get_skill_in_training(uid, char_id)
332 if not skill.skillInTraining:
335 total_sp = skill.trainingDestinationSP - skill.trainingStartSP
336 total_time = skill.trainingEndTime - skill.trainingStartTime
338 spps = float(total_sp) / total_time
340 return (spps, skill.trainingStartTime)
342 def get_training_sp(self, uid, char_id):
343 """ returns the additional SP that the in-training skill has acquired
345 spps_tuple = self.get_spps(uid, char_id)
349 spps, start_time = spps_tuple
350 eve_time = time.time() #evetime is utc, right?
351 time_diff = eve_time - start_time
353 return (spps * time_diff)
356 def excepthook(ex_type, value, tb):
357 """ a replacement for the default exception handler that logs errors"""
358 #tb2 = "".join(traceback.format_exception(ex_type, value, tb))
360 logging.getLogger('meEveMon').error('Uncaught exception:',
361 exc_info=(ex_type, value, tb))
364 """ sets up the logging """
365 MAXBYTES = 1 * 1000 * 1000 # 1MB
368 logger = logging.getLogger("mEveMon")
369 logger.setLevel(logging.DEBUG)
371 fileHandler = logging.handlers.RotatingFileHandler(LOGPATH,
373 backupCount=LOGCOUNT)
374 logger.addHandler(fileHandler)
376 #create console handler
377 console = logging.StreamHandler()
378 console.setLevel(logging.DEBUG)
379 #formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
380 #console.setFormatter(formatter)
381 logger.addHandler(console)
384 if __name__ == "__main__":
386 sys.excepthook = excepthook
390 except KeyboardInterrupt: