Testing new repo.
[mevemon] / src / mevemon.py
1 #
2 # mEveMon - A character monitor for EVE Online
3 # Copyright (c) 2010  Ryan and Danny Campbell, and the mEveMon Team
4 #
5 # mEveMon is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # mEveMon is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19
20 import hildon
21 import gtk
22 from eveapi import eveapi
23 import fetchimg
24 import apicache
25
26 # we will store our preferences in gconf
27 import gnome.gconf
28
29 #ugly hack to check maemo version. any better way?
30 if hasattr(hildon, "StackableWindow"):
31     from ui.fremantle import ui
32 else:
33     from ui.diablo import ui
34
35 class mEveMon():
36     def __init__(self):
37         self.program = hildon.Program()
38         self.program.__init__()
39         self.gconf = gnome.gconf.client_get_default()
40         self.set_auth()
41         self.ui = ui.mEveMonUI(self)
42
43     def run(self):
44         gtk.main()
45     
46     def quit(self, *args):
47         gtk.main_quit()
48
49     def get_api_key(self):
50         return self.gconf.get_string("/apps/maemo/mevemon/eve_api_key") or ''
51
52     def get_uid(self):
53         return self.gconf.get_string("/apps/maemo/mevemon/eve_uid") or ''
54
55     def set_api_key(self, key):
56         self.gconf.set_string("/apps/maemo/mevemon/eve_api_key", key)
57
58     def set_uid(self, uid):
59         self.gconf.set_string("/apps/maemo/mevemon/eve_uid", uid)
60
61
62     def set_auth(self):
63         """
64         set self.auth to None if there was a problem. somehow later on we'll
65         have to pass the error to the UI, but for now I just want the program
66         to not be broken. --danny
67         """
68         uid = self.get_uid()
69         api_key = self.get_api_key()
70         self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
71                 apicache.cache_handler( debug = False ) )
72         try:
73             self.auth = self.cached_api.auth( userID = uid, apiKey = api_key )
74         except eveapi.Error, e:
75             # we need to deal with this, so raise --danny
76             raise
77         except ValueError, e:
78             self.auth = None
79             #raise
80
81     def get_auth(self):
82         return self.auth
83
84     def get_char_sheet(self, charID):
85         if not self.auth: return None
86         try:
87             sheet = self.auth.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         placeholder_chars = [("Please check your API settings.", "imgs/error.jpg")]
124         if not self.auth: return placeholder_chars
125         try:
126             api_char_list = self.auth.account.Characters()
127             # append each char we get to the list we'll return to the
128             # UI --danny
129             for character in api_char_list.characters:
130                 ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ) ) )
131         except eveapi.Error, e:
132             # again, we need to handle this... --danny
133             raise
134
135         return ui_char_list
136
137     def get_portrait(self, char_name, size):
138         """
139         returns the relative path of the retrieved portrait
140         """
141         charID = self.char_name2id(char_name)
142         return fetchimg.portrait_filename(charID, size)
143
144 if __name__ == "__main__":
145     app = mEveMon()
146     app.run()