93acf5ec50ec00bb9810e513ad13cea136323c8a
[mevemon] / src / opt / mEveMon / 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
27 # we will store our preferences in gconf
28 import gnome.gconf
29
30 #ugly hack to check maemo version. any better way?
31 if hasattr(hildon, "StackableWindow"):
32     from ui.fremantle import ui
33 else:
34     from ui.diablo import ui
35
36 class mEveMon():
37     def __init__(self):
38         self.program = hildon.Program()
39         self.program.__init__()
40         self.gconf = gnome.gconf.client_get_default()
41         self.set_auth()
42         self.ui = ui.mEveMonUI(self)
43
44     def run(self):
45         gtk.main()
46     
47     def quit(self, *args):
48         gtk.main_quit()
49
50     def get_api_key(self):
51         return self.gconf.get_string("/apps/maemo/mevemon/eve_api_key") or ''
52
53     def get_uid(self):
54         return self.gconf.get_string("/apps/maemo/mevemon/eve_uid") or ''
55
56     def set_api_key(self, key):
57         self.gconf.set_string("/apps/maemo/mevemon/eve_api_key", key)
58
59     def set_uid(self, uid):
60         self.gconf.set_string("/apps/maemo/mevemon/eve_uid", uid)
61
62
63     def set_auth(self):
64         """
65         set self.auth to None if there was a problem. somehow later on we'll
66         have to pass the error to the UI, but for now I just want the program
67         to not be broken. --danny
68         """
69         uid = self.get_uid()
70         api_key = self.get_api_key()
71         self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
72                 apicache.cache_handler( debug = False ) )
73         try:
74             self.auth = self.cached_api.auth( userID = uid, apiKey = api_key )
75         except eveapi.Error, e:
76             # we need to deal with this, so raise --danny
77             raise
78         except ValueError, e:
79             self.auth = None
80             #raise
81
82     def get_auth(self):
83         return self.auth
84
85     def get_char_sheet(self, charID):
86         if not self.auth: return None
87         try:
88             sheet = self.auth.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         placeholder_chars = [("Please check your API settings.", "imgs/error.jpg")]
125         if not self.auth: return placeholder_chars
126         try:
127             api_char_list = self.auth.account.Characters()
128             # append each char we get to the list we'll return to the
129             # UI --danny
130             for character in api_char_list.characters:
131                 ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ) ) )
132         except eveapi.Error, e:
133             # again, we need to handle this... --danny
134             raise
135
136         return ui_char_list
137
138     def get_portrait(self, char_name, size):
139         """
140         returns the relative path of the retrieved portrait
141         """
142         charID = self.char_name2id(char_name)
143         return fetchimg.portrait_filename(charID, size)
144
145     def get_skill_tree(self):
146         try:
147             tree = self.cached_api.eve.SkillTree()
148         except eveapi.Error, e:
149             print e
150             return None
151         
152         return tree
153
154     def get_skill_in_training(self, charID):
155         try:
156             skill = self.auth.character(charID).SkillInTraining()
157         except:
158             print e
159             return None
160
161         return skill
162
163
164 if __name__ == "__main__":
165     app = mEveMon()
166     app.run()