finished preliminary support for multiple accounts
[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 import os.path
27
28 # we will store our preferences in gconf
29 import gnome.gconf
30
31 #ugly hack to check maemo version. any better way?
32 if hasattr(hildon, "StackableWindow"):
33     from ui.fremantle import gui
34     is_fremantle = True
35 else:
36     from ui.diablo import gui
37     is_fremantle = False
38
39 class mEveMon():
40     def __init__(self):
41         self.program = hildon.Program()
42         self.program.__init__()
43         self.gconf = gnome.gconf.client_get_default()
44         self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
45                 apicache.cache_handler(debug=False))
46         self.gui = gui.mEveMonUI(self)
47
48     def run(self):
49         gtk.main()
50     
51     def quit(self, *args):
52         gtk.main_quit()
53
54     def get_accounts(self):
55         accounts = {}
56         entries = self.gconf.all_entries("/apps/maemo/mevemon/accounts")
57
58         for entry in entries:
59             key = os.path.basename(entry.get_key())
60             value = entry.get_value().to_string()
61             accounts[key] = value
62
63         return accounts
64         
65     def get_api_key(self, uid):
66         return self.gconf.get_string("/apps/maemo/mevemon/accounts/%s" % uid) or ''
67
68     def remove_account(self, uid):
69         self.gconf.unset("/apps/maemo/mevemon/accounts/%s" % uid)
70
71     def add_account(self, uid, api_key):
72         self.gconf.set_string("/apps/maemo/mevemon/accounts/%s" % uid, api_key)
73
74     def get_auth(self, uid):
75         api_key = self.get_api_key(uid)
76
77         try:
78             auth = self.cached_api.auth(userID=uid, apiKey=api_key)
79         except eveapi.Error, e:
80             return None
81
82         return auth
83
84     def get_char_sheet(self, uid, charID):
85         try:
86             sheet = self.get_auth(uid).character(charID).CharacterSheet()
87         except eveapi.Error, e:
88             # we should really have a logger that logs this error somewhere
89             return None
90
91         return sheet
92
93     def charid2uid(self, charID):
94         acct_dict = self.get_accounts()
95         
96         for uid, apiKey in acct_dict.items():
97             auth = self.cached_api.auth(userID=uid, apiKey=apiKey)
98             api_char_list = auth.account.Characters()
99             
100             for character in api_char_list.characters:
101                 if character.characterID== charID:
102                     return uid
103
104         
105         return None
106     
107     def char_id2name(self, charID):
108         # the api can take a comma-seperated list of ids, but we'll just take
109         # a single id for now
110         try:
111             chars = self.cached_api.eve.CharacterName(ids=charID).characters
112             name = chars[0].characterName
113         except eveapi.Error, e:
114             return None
115
116         return name
117
118     def char_name2id(self, name):
119         # the api can take a comma-seperated list of names, but we'll just take
120         # a single name for now
121         try:
122             chars = self.cached_api.eve.CharacterID(names=name).characters
123             char_id = chars[0].characterID
124         except eveapi.Error, e:
125             return None
126
127         return char_id
128
129     
130     def get_characters(self):
131         """
132         returns a list containing a single character with an error message for a
133         name, if there's a problem. FIXME --danny
134         """
135         ui_char_list = []
136         err_img = "/usr/share/mevemon/imgs/error.jpg"
137
138         placeholder_chars = ("Please check your API settings.", err_img, "0")
139         
140         acct_dict = self.get_accounts()
141         if not acct_dict:
142             return [placeholder_chars]
143
144         for uid, apiKey in acct_dict.items():
145             auth = self.cached_api.auth(userID=uid, apiKey=apiKey)
146             try:
147                 api_char_list = auth.account.Characters()
148                 # append each char we get to the list we'll return to the
149                 # UI --danny
150                 for character in api_char_list.characters:
151                     ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ), uid) )
152             except eveapi.Error, e:
153                 # again, we need to handle this... --danny
154                 ui_char_list.append(placeholder_chars)
155
156         return ui_char_list
157
158     def get_portrait(self, char_name, size):
159         """
160         returns the relative path of the retrieved portrait
161         """
162         charID = self.char_name2id(char_name)
163         return fetchimg.portrait_filename(charID, size)
164
165     def get_skill_tree(self):
166         try:
167             tree = self.cached_api.eve.SkillTree()
168         except eveapi.Error, e:
169             print e
170             return None
171         
172         return tree
173
174     def get_skill_in_training(self, uid, charID):
175         try:
176             skill = self.get_auth(uid).character(charID).SkillInTraining()
177         except:
178             print e
179             return None
180
181         return skill
182
183
184 if __name__ == "__main__":
185     app = mEveMon()
186     app.run()