started work on multiple accounts support
[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
75     def get_auth(self, uid):
76         
77         api_key = self.get_api_key(uid)
78
79         try:
80             auth = self.cached_api.auth(userID=uid, apiKey=api_key)
81         except eveapi.Error, e:
82             return None
83
84         return auth
85
86     def get_char_sheet(self, uid, charID):
87         try:
88             sheet = self.get_auth(uid).character(charID).CharacterSheet()
89         except eveapi.Error, e:
90             # we should really have a logger that logs this error somewhere
91             return None
92
93         return sheet
94
95     def char_id2name(self, charID):
96         # the api can take a comma-seperated list of ids, but we'll just take
97         # a single id for now
98         try:
99             chars = self.cached_api.eve.CharacterName(ids=charID).characters
100             name = chars[0].characterName
101         except eveapi.Error, e:
102             return None
103
104         return name
105
106     def char_name2id(self, name):
107         # the api can take a comma-seperated list of names, but we'll just take
108         # a single name for now
109         try:
110             chars = self.cached_api.eve.CharacterID(names=name).characters
111             char_id = chars[0].characterID
112         except eveapi.Error, e:
113             return None
114
115         return char_id
116
117     
118     def get_characters(self):
119         """
120         returns a list containing a single character with an error message for a
121         name, if there's a problem. FIXME --danny
122         """
123         ui_char_list = []
124         err_img = "/usr/share/mevemon/imgs/error.jpg"
125
126         placeholder_chars = ("Please check your API settings.", err_img, "0")
127         
128         acct_dict = self.get_accounts()
129         if not acct_dict:
130             return [placeholder_chars]
131
132         for uid, apiKey in acct_dict.items():
133             auth = self.cached_api.auth(userID=uid, apiKey=apiKey)
134             try:
135                 api_char_list = auth.account.Characters()
136                 # append each char we get to the list we'll return to the
137                 # UI --danny
138                 for character in api_char_list.characters:
139                     ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ), uid) )
140             except eveapi.Error, e:
141                 # again, we need to handle this... --danny
142                 ui_char_list.append(placeholder_chars)
143
144         return ui_char_list
145
146     def get_portrait(self, char_name, size):
147         """
148         returns the relative path of the retrieved portrait
149         """
150         charID = self.char_name2id(char_name)
151         return fetchimg.portrait_filename(charID, size)
152
153     def get_skill_tree(self):
154         try:
155             tree = self.cached_api.eve.SkillTree()
156         except eveapi.Error, e:
157             print e
158             return None
159         
160         return tree
161
162     def get_skill_in_training(self, uid, charID):
163         try:
164             skill = self.get_auth(uid).character(charID).SkillInTraining()
165         except:
166             print e
167             return None
168
169         return skill
170
171
172 if __name__ == "__main__":
173     app = mEveMon()
174     app.run()