d39e708c6cbf75b17f8c3fd93fb5f9b35d610564
[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     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     def eveapi_connect(self):
43         uid = self.get_uid()
44         api_key = self.get_api_key()
45         cached_api = eveapi.EVEAPIConnection( cacheHandler = apicache.cache_handler( debug = False ) )
46
47         try:
48             auth = cached_api.auth( userID = uid, apiKey = api_key )
49         except eveapi.Error, e:
50             # if we can't, return the error message/pic --danny
51             return None
52         except Exception, e:
53             # unknown exception, dunno if this needs to be here if I just
54             # ignore it... probably a bad idea, but it was in the 
55             # apitest.py example... --danny
56             raise
57
58         return auth
59
60     def get_alliances(self, charID):
61         auth = eveapi_connect()
62
63         if auth:
64             alliance_list = auth.character(charID)
65             
66
67
68         
69
70     # really quick hack to get character list. doesn't handle errors well, and
71     # if it can't get the gconf settings it just returns the placeholders, when
72     # in reality it should tell the UI or something. basically half finished,
73     # just uploading to show ry... FIXME --danny
74     def get_characters( self ):
75         ui_char_list = []
76         # error message --danny
77         placeholder_chars = [("Please check your API settings.", "imgs/error.jpg")]
78         
79         auth = self.eveapi_connect()
80
81         try:
82             api_char_list = auth.account.Characters()
83             # append each char we get to the list we'll return to the UI --danny
84             for character in api_char_list.characters:
85                 ui_char_list.append( ( character.name, 
86                     fetchimg.portrait_filename( character.characterID, 64 ) ) )
87             return ui_char_list
88         # if not entered into gconf, error message --danny
89         except eveapi.Error, e:
90             return placeholder_chars
91
92 if __name__ == "__main__":
93     app = mEveMon()
94     app.run()