Fix bug #5645, and other minor bug
[mevemon] / package / src / mevemon.py
index c4e2404..eb10a49 100755 (executable)
@@ -24,6 +24,8 @@ from eveapi import eveapi
 import fetchimg
 import apicache
 import os.path
+import traceback
+import time
 
 #conic is used for connection handling
 import conic
@@ -46,6 +48,15 @@ class mEveMon():
 
     """
 
+    about_name = 'mEveMon'
+    about_text = ('Mobile character monitor for EVE Online')
+    about_authors = ['Ryan Campbell <campbellr@gmail.com>',
+                     'Danny Campbell <danny.campbell@gmail.com>']
+
+    about_website = 'http://mevemon.garage.maemo.org'
+    app_version = '0.4'
+
+
     GCONF_DIR = "/apps/maemo/mevemon"
 
     def __init__(self):
@@ -54,10 +65,11 @@ class mEveMon():
         self.gconf = gnome.gconf.client_get_default()
         #NOTE: remove this after a few releases
         self.update_settings()
-        self.connect()
+        self.connect_to_network()
         self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
                 apicache.cache_handler(debug=False))
         self.gui = gui.mEveMonUI(self)
+        self.gui.run()
 
     def run(self):
         gtk.main()
@@ -120,7 +132,9 @@ class mEveMon():
 
         try:
             auth = self.cached_api.auth(userID=uid, apiKey=api_key)
-        except: 
+        except Exception, e:
+            self.gui.report_error(str(e))
+            traceback.print_exc()
             return None
 
         return auth
@@ -132,8 +146,10 @@ class mEveMon():
         """
         try:
             sheet = self.get_auth(uid).character(char_id).CharacterSheet()
-        except:
+        except Exception, e:
+            self.gui.report_error(str(e))
             # TODO: we should really have a logger that logs this error somewhere
+            traceback.print_exc()
             return None
 
         return sheet
@@ -150,9 +166,13 @@ class mEveMon():
         
         for uid, api_key in acct_dict.items():
             auth = self.cached_api.auth(userID=uid, apiKey=api_key)
-            api_char_list = auth.account.Characters()
-            
-            for character in api_char_list.characters:
+            try:
+                api_char_list = auth.account.Characters()
+                characters = api_char_list.characters
+            except:
+                characters = []
+
+            for character in characters:
                 if character.characterID == char_id:
                     return uid
 
@@ -169,7 +189,9 @@ class mEveMon():
         try:
             chars = self.cached_api.eve.CharacterName(ids=char_id).characters
             name = chars[0].characterName
-        except:
+        except Exception, e:
+            self.gui.report_error(str(e))
+            traceback.print_exc()
             return None
 
         return name
@@ -185,7 +207,9 @@ class mEveMon():
             chars = self.cached_api.eve.CharacterID(names=name).characters
             char_id = chars[0].characterID
             char_name = chars[0].name
-        except:
+        except Exception, e:
+            self.gui.report_error(str(e))
+            traceback.print_exc()
             return None
 
         return char_id
@@ -201,7 +225,9 @@ class mEveMon():
             try:
                 api_char_list = auth.account.Characters()
                 char_list = [char.name for char in api_char_list.characters]
-            except:
+            except Exception, e:
+                self.gui.report_error(str(e))
+                traceback.print_exc()
                 return None
 
         return char_list
@@ -215,10 +241,12 @@ class mEveMon():
         pair to the list, it appends an 'error message' 
 
         """
+
         ui_char_list = []
         err_img = "/usr/share/mevemon/imgs/error.jpg"
+        err_txt = "Problem fetching info for account"
 
-        placeholder_chars = ("Please check your API settings.", err_img, "0")
+        placeholder_chars = (err_txt, err_img, None)
         
         acct_dict = self.get_accounts()
         if not acct_dict:
@@ -228,7 +256,7 @@ class mEveMon():
             char_names = self.get_chars_from_acct(uid)
             
             if not char_names:
-                ui_char_list.append(placeholder_chars)
+                ui_char_list.append((err_txt + "\t(UID: %s)" % uid, err_img, None))
             else:
                 # append each char we get to the list we'll return to the
                 # UI --danny
@@ -251,7 +279,9 @@ class mEveMon():
         """
         try:
             tree = self.cached_api.eve.SkillTree()
-        except:
+        except Exception, e:
+            self.gui.report_error(str(e))
+            traceback.print_exc()
             return None
         
         return tree
@@ -260,26 +290,83 @@ class mEveMon():
         """
         Returns an object from eveapi containing information about the
         current skill in training
-
         """
         try:
             skill = self.get_auth(uid).character(char_id).SkillInTraining()
-        except:
+        except Exception, e:
+            self.gui.report_error(str(e))
+            traceback.print_exc()
             return None
 
         return skill
 
     def connection_cb(self, connection, event, mgc):
+        """
+        I'm not sure why we need this, but connection.connect() won't work
+        without it, even empty.
+        """
         pass    
 
 
-    def connect(self):
+    def connect_to_network(self):
+        """
+        This will connect to the default network if avaliable, or pop up the
+        connection dialog to select a connection.
+        Running this when we start the program ensures we are connected to a
+        network.
+        """
         connection = conic.Connection()
         #why 0xAA55?
         connection.connect("connection-event", self.connection_cb, 0xAA55)
         assert(connection.request_connection(conic.CONNECT_FLAG_NONE))
 
 
+    def get_sp(self, uid, char_id):
+        """
+        Adds up the SP for all known skills, then calculates the SP gained
+        from an in-training skill.
+        """
+        actual_sp = 0
+        
+        sheet = self.get_char_sheet(uid, char_id)
+        for skill in sheet.skills:
+            actual_sp += skill.skillpoints
+
+        live_sp = actual_sp + self.get_training_sp(uid, char_id)
+
+        return live_sp
+
+    def get_spps(self, uid, char_id):
+        """
+        Calculate and returns the skill points per hour for the given character.
+        """
+        skill = self.get_skill_in_training(uid, char_id)
+        
+        if not skill.skillInTraining:
+            return (0, 0)
+
+        total_sp = skill.trainingDestinationSP - skill.trainingStartSP
+        total_time = skill.trainingEndTime - skill.trainingStartTime
+        
+        spps = float(total_sp) / total_time
+    
+        return (spps, skill.trainingStartTime)
+
+    def get_training_sp(self, uid, char_id):
+        """
+        returns the additional SP that the in-training skill has acquired
+        """
+        spps_tuple = self.get_spps(uid, char_id)
+        
+        if not spps_tuple:
+            return 0
+        spps, start_time = spps_tuple
+        eve_time = time.time() #evetime is utc, right?
+        time_diff =  eve_time - start_time
+
+        return (spps * time_diff) 
+
+
 if __name__ == "__main__":
     app = mEveMon()
     app.run()