only call auth() once, and make it a member of mevemon class.
[mevemon] / mevemon.py
1 import hildon
2 import gtk
3 from eveapi import eveapi
4 import fetchimg
5 import apicache
6
7 # we will store our preferences in gconf
8 import gnome.gconf
9
10 #ugly hack to check maemo version. any better way?
11 if hasattr(hildon, "StackableWindow"):
12     from ui.fremantle import ui
13 else:
14     from ui.diablo import ui
15
16 class mEveMon():
17     def __init__(self):
18         self.program = hildon.Program()
19         self.program.__init__()
20         self.gconf = gnome.gconf.client_get_default()
21         self.set_auth()
22         self.ui = ui.mEveMonUI(self)
23
24     def run(self):
25         gtk.main()
26     
27     def quit(self, *args):
28         gtk.main_quit()
29
30     def get_api_key(self):
31         return self.gconf.get_string("/apps/maemo/mevemon/eve_api_key") or ''
32
33     def get_uid(self):
34         return self.gconf.get_string("/apps/maemo/mevemon/eve_uid") or ''
35
36     def set_api_key(self, key):
37         self.gconf.set_string("/apps/maemo/mevemon/eve_api_key", key)
38
39     def set_uid(self, uid):
40         self.gconf.set_string("/apps/maemo/mevemon/eve_uid", uid)
41
42
43     def set_auth(self):
44         uid = self.get_uid()
45         api_key = self.get_api_key()
46         cached_api = eveapi.EVEAPIConnection( cacheHandler = apicache.cache_handler( debug = False ) )
47
48         try:
49             self.auth = cached_api.auth( userID = uid, apiKey = api_key )
50         except eveapi.Error, e:
51             # if we can't, return the error message/pic --danny
52             return None
53         except Exception, e:
54             # unknown exception, dunno if this needs to be here if I just
55             # ignore it... probably a bad idea, but it was in the 
56             # apitest.py example... --danny
57             raise
58
59     def get_auth(self):
60         return self.auth
61
62     # really quick hack to get character list. doesn't handle errors well, and
63     # if it can't get the gconf settings it just returns the placeholders, when
64     # in reality it should tell the UI or something. basically half finished,
65     # just uploading to show ry... FIXME --danny
66     def get_characters( self ):
67         ui_char_list = []
68         # error message --danny
69         placeholder_chars = [("Please check your API settings.", "imgs/error.jpg")]
70         
71         try:
72             api_char_list = self.auth.account.Characters()
73             # append each char we get to the list we'll return to the UI --danny
74             for character in api_char_list.characters:
75                 ui_char_list.append( ( character.name, 
76                     fetchimg.portrait_filename( character.characterID, 64 ) ) )
77             return ui_char_list
78         # if not entered into gconf, error message --danny
79         except eveapi.Error, e:
80             return placeholder_chars
81
82 if __name__ == "__main__":
83     app = mEveMon()
84     app.run()