0650ca2d0960b0cc214c82b0b86a21f05e36b47b
[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.config = None
21         self.gconf = gnome.gconf.client_get_default()
22         self.ui = ui.mEveMonUI(self)
23
24
25     def run(self):
26         gtk.main()
27     
28     def quit(self, *args):
29         gtk.main_quit()
30
31     def get_api_key(self):
32         return self.gconf.get_string("/apps/maemo/mevemon/eve_api_key") or ''
33
34     def get_uid(self):
35         return self.gconf.get_string("/apps/maemo/mevemon/eve_uid") or ''
36
37     def set_api_key(self, key):
38         self.gconf.set_string("/apps/maemo/mevemon/eve_api_key", key)
39
40     def set_uid(self, uid):
41         self.gconf.set_string("/apps/maemo/mevemon/eve_uid", uid)
42
43     # really quick hack to get character list. doesn't handle errors well, and if it can't get the gconf settings it just returns the placeholders, when in reality it should tell the UI or something. basically half finished, just uploading to show ry... FIXME --danny
44     def get_characters( self ):
45         ui_char_list = []
46         # error message --danny
47         placeholder_chars = [("Please check your API settings.", "imgs/error.jpg")]
48         cached_api = eveapi.EVEAPIConnection( cacheHandler = apicache.cache_handler( debug = False ) )
49         uid = self.get_uid()
50         api_key = self.get_api_key()
51         # if they are entered into gconf, try getting api data --danny
52         if ( uid and api_key ):
53             auth = cached_api.auth( userID = uid, apiKey = api_key )
54             try:
55                 api_char_list = auth.account.Characters()
56             except eveapi.Error, e:
57                 # if we can't, return the error message/pic --danny
58                 return placeholder_chars
59             except Exception, e:
60                 # unknown exception, dunno if this needs to be here if I just ignore it... probably a bad idea, but it was in the apitest.py example... --danny
61                 ##return placeholder_chars
62                 raise
63             # append each char we get to the list we'll return to the UI --danny
64             for character in api_char_list.characters:
65                 ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ) ) )
66             return ui_char_list
67         # if not entered into gconf, error message --danny
68         else:
69             return placeholder_chars
70
71 if __name__ == "__main__":
72     app = mEveMon()
73     app.run()