Fixed a few bugs
[mevemon] / package / src / mevemon.py
1 #!/usr/bin/env python
2 #
3 # mEveMon - A character monitor for EVE Online
4 # Copyright (c) 2010  Ryan and Danny Campbell, and the mEveMon Team
5 #
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.
10 #
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.
15 #
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/>.
18 #
19
20
21 import hildon
22 import gtk
23 from eveapi import eveapi
24 import fetchimg
25 import apicache
26
27 # we will store our preferences in gconf
28 import gnome.gconf
29
30 #ugly hack to check maemo version. any better way?
31 if hasattr(hildon, "StackableWindow"):
32     from ui.fremantle import gui
33     is_fremantle = True
34 else:
35     from ui.diablo import gui
36     is_fremantle = False
37
38 class mEveMon():
39     def __init__(self):
40         self.program = hildon.Program()
41         self.program.__init__()
42         self.gconf = gnome.gconf.client_get_default()
43         self.set_auth()
44         self.gui = gui.mEveMonUI(self)
45
46     def run(self):
47         gtk.main()
48     
49     def quit(self, *args):
50         gtk.main_quit()
51
52     def get_api_key(self):
53         return self.gconf.get_string("/apps/maemo/mevemon/eve_api_key") or ''
54
55     def get_uid(self):
56         return self.gconf.get_string("/apps/maemo/mevemon/eve_uid") or ''
57
58     def set_api_key(self, key):
59         self.gconf.set_string("/apps/maemo/mevemon/eve_api_key", key)
60
61     def set_uid(self, uid):
62         self.gconf.set_string("/apps/maemo/mevemon/eve_uid", uid)
63
64
65     def set_auth(self):
66         """
67         set self.auth to None if there was a problem. somehow later on we'll
68         have to pass the error to the UI, but for now I just want the program
69         to not be broken. --danny
70         """
71         uid = self.get_uid()
72         api_key = self.get_api_key()
73         self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
74                 apicache.cache_handler( debug = False ) )
75         try:
76             self.auth = self.cached_api.auth( userID = uid, apiKey = api_key )
77         except eveapi.Error, e:
78             # we need to deal with this, so raise --danny
79             raise
80         except ValueError, e:
81             self.auth = None
82             #raise
83
84     def get_auth(self):
85         return self.auth
86
87     def get_char_sheet(self, charID):
88         if not self.auth: return None
89         try:
90             sheet = self.auth.character(charID).CharacterSheet()
91         except eveapi.Error, e:
92             # we should really have a logger that logs this error somewhere
93             return None
94
95         return sheet
96
97     def char_id2name(self, charID):
98         # the api can take a comma-seperated list of ids, but we'll just take
99         # a single id for now
100         try:
101             chars = self.cached_api.eve.CharacterName(ids=charID).characters
102             name = chars[0].characterName
103         except eveapi.Error, e:
104             return None
105
106         return name
107
108     def char_name2id(self, name):
109         # the api can take a comma-seperated list of names, but we'll just take
110         # a single name for now
111         try:
112             chars = self.cached_api.eve.CharacterID(names=name).characters
113             char_id = chars[0].characterID
114         except eveapi.Error, e:
115             return None
116
117         return char_id
118
119     
120     def get_characters( self ):
121         """
122         returns a list containing a single character with an error message for a
123         name, if there's a problem. FIXME --danny
124         """
125         ui_char_list = []
126         err_img = "/usr/share/mevemon/imgs/error.jpg"
127
128         placeholder_chars = [("Please check your API settings.", err_img)]
129         if not self.auth: return placeholder_chars
130         try:
131             api_char_list = self.auth.account.Characters()
132             # append each char we get to the list we'll return to the
133             # UI --danny
134             for character in api_char_list.characters:
135                 ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ) ) )
136         except eveapi.Error, e:
137             # again, we need to handle this... --danny
138             return placeholder_chars
139             #raise
140
141         return ui_char_list
142
143     def get_portrait(self, char_name, size):
144         """
145         returns the relative path of the retrieved portrait
146         """
147         charID = self.char_name2id(char_name)
148         return fetchimg.portrait_filename(charID, size)
149
150     def get_skill_tree(self):
151         try:
152             tree = self.cached_api.eve.SkillTree()
153         except eveapi.Error, e:
154             print e
155             return None
156         
157         return tree
158
159     def get_skill_in_training(self, charID):
160         try:
161             skill = self.auth.character(charID).SkillInTraining()
162         except:
163             print e
164             return None
165
166         return skill
167
168
169 if __name__ == "__main__":
170     app = mEveMon()
171     app.run()