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