Allow error to be passed to UI
[mevemon] / package / src / ui / diablo / gui.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 import sys, time
20
21 import gtk
22 import hildon
23 import gobject
24
25 from ui import models
26 import validation
27 import util
28
29 class BaseUI():
30     menu_items = ("Settings", "About", "Refresh")
31
32     def create_menu(self, window):
33         menu = gtk.Menu()
34
35         for command in self.menu_items:
36             # Create menu entries
37             button = gtk.MenuItem(command)
38
39             if command == "About":
40                 button.connect("activate", self.about_clicked)
41             elif command == "Settings":
42                 button.connect("activate", self.settings_clicked, window)
43             elif command == "Refresh":
44                 button.connect("activate", self.refresh_clicked, window)
45             else:
46                 assert False, command
47
48             # Add entry to the view menu
49             menu.append(button)
50
51         menu.show_all()
52
53         return menu
54
55     def settings_clicked(self, button, window):
56
57         RESPONSE_NEW, RESPONSE_EDIT, RESPONSE_DELETE = range(3)
58
59         dialog = gtk.Dialog()
60         dialog.set_transient_for(window)
61         dialog.set_title("Settings")
62
63
64
65         vbox = dialog.vbox
66
67         acctsLabel = gtk.Label("Accounts:")
68         acctsLabel.set_justify(gtk.JUSTIFY_LEFT)
69
70         vbox.pack_start(acctsLabel, False, False, 1)
71
72         self.accounts_model = models.AccountsModel(self.controller)
73
74         accounts_treeview = gtk.TreeView(model = self.accounts_model)
75         self.add_columns_to_accounts(accounts_treeview)
76         vbox.pack_start(accounts_treeview, False, False, 1)
77
78         # all stock responses are negative, so we can use any positive value
79         new_button = dialog.add_button("New", RESPONSE_NEW)
80         #TODO: get edit button working
81         #edit_button = dialog.add_button("Edit", RESPONSE_EDIT)
82         delete_button = dialog.add_button("Delete", RESPONSE_DELETE)
83         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
84         cancel_button = dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
85
86         #TODO: for some reason the scrollbar shows up in the middle of the
87         # dialog. Why?
88         #scrollbar = gtk.VScrollbar()
89         
90         dialog.show_all()
91
92         result = dialog.run()
93
94         while(result != gtk.RESPONSE_CANCEL):
95             if result == RESPONSE_NEW:
96                 self.new_account_clicked(window)
97             #elif result == RESPONSE_EDIT:
98             #    # get the selected treeview item and pop up the account_box
99             #    self.edit_account(accounts_treeview)
100             elif result == RESPONSE_DELETE:
101                 # get the selected treeview item, and delete the gconf keys
102                 self.delete_account(accounts_treeview)
103             elif result == gtk.RESPONSE_OK:
104                 self.char_model.get_characters()
105                 break
106         
107             result = dialog.run()
108
109         dialog.destroy()
110
111
112
113     def get_selected_item(self, treeview, column):
114         selection = treeview.get_selection()
115         model, miter = selection.get_selected()
116
117         value = model.get_value(miter, column)
118
119         return value
120
121     def edit_account(self, treeview):
122         uid = self.get_selected_item(treeview, 0)
123         # pop up the account dialog
124
125         self.accounts_model.get_accounts()
126
127     def delete_account(self, treeview):
128         uid = self.get_selected_item(treeview, 0)
129         self.controller.remove_account(uid)
130         # refresh model
131         self.accounts_model.get_accounts()
132
133
134     def add_columns_to_accounts(self, treeview):
135         #Column 0 for the treeview
136         renderer = gtk.CellRendererText()
137         column = gtk.TreeViewColumn('User ID', renderer, 
138                 text=models.AccountsModel.C_UID)
139         column.set_property("expand", True)
140         treeview.append_column(column)
141
142         #Column 2 (characters) for the treeview
143         column = gtk.TreeViewColumn('Characters', renderer, 
144                 markup=models.AccountsModel.C_CHARS)
145         column.set_property("expand", True)
146         treeview.append_column(column)
147
148
149     def new_account_clicked(self, window):
150         dialog = gtk.Dialog()
151     
152         #get the vbox to pack all the settings into
153         vbox = dialog.vbox
154     
155         dialog.set_transient_for(window)
156         dialog.set_title("New Account")
157
158         uidLabel = gtk.Label("User ID:")
159         uidLabel.set_justify(gtk.JUSTIFY_LEFT)
160         vbox.add(uidLabel)
161         
162         uidEntry = gtk.Entry()
163         uidEntry.set_property('is_focus', False)
164         
165         vbox.add(uidEntry)
166
167         apiLabel = gtk.Label("API key:")
168         apiLabel.set_justify(gtk.JUSTIFY_LEFT)
169         vbox.add(apiLabel)
170         
171         apiEntry = gtk.Entry()
172         apiEntry.set_property('is_focus', False)
173
174         vbox.add(apiEntry)
175        
176         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
177         cancel_button = dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
178
179         dialog.show_all()
180         result = dialog.run()
181         
182         valid_credentials = False
183
184         while not valid_credentials:
185             if result == gtk.RESPONSE_OK:
186                 uid = uidEntry.get_text()
187                 # auth() fails if api_key has lower-case characters
188                 api_key = apiEntry.get_text().upper()
189             
190                 try:
191                     validation.uid(uid)
192                     validation.api_key(api_key)
193                 except validation.ValidationError, e:
194                     self.report_error(e.message)
195                     result = dialog.run()
196                 else:
197                     valid_credentials = True
198                     self.controller.add_account(uid, api_key)
199                     self.accounts_model.get_accounts()
200             else:
201                 break
202
203         dialog.destroy()
204
205
206     def report_error(self, error):
207         hildon.hildon_banner_show_information(self.win, '', error)
208     
209     def about_clicked(self, button):
210         dialog = gtk.AboutDialog()
211         dialog.set_website(self.controller.about_website)
212         dialog.set_website_label(self.controller.about_website)
213         dialog.set_name(self.controller.about_name)
214         dialog.set_authors(self.controller.about_authors)
215         dialog.set_comments(self.controller.about_text)
216         dialog.set_version(self.controller.app_version)
217         dialog.run()
218         dialog.destroy()
219
220     def add_label(self, text, box, markup=True, align="left", padding=1):
221         label = gtk.Label(text)
222         if markup:
223             label.set_use_markup(True)
224         if align == "left":
225             label.set_alignment(0, 0.5)
226
227         box.pack_start(label, False, False, padding)
228
229         return label
230
231 class mEveMonUI(BaseUI):
232
233     def __init__(self, controller):
234         self.controller = controller
235         gtk.set_application_name("mEveMon")
236
237     def run(self):
238         # create the main window
239         self.win = hildon.Window()
240         self.win.connect("destroy", self.controller.quit)
241         self.win.show_all()
242         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading overview...")
243         progress_bar.set_fraction(0.4)
244
245         # Create menu
246         menu = self.create_menu(self.win)
247
248         # Attach menu to the window
249         self.win.set_menu(menu)
250
251         character_win = CharacterSheetUI(self.controller)
252
253         # create the treeview --danny
254         self.char_model = models.CharacterListModel(self.controller)
255         treeview = gtk.TreeView(model = self.char_model)
256         treeview.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_HORIZONTAL)
257         treeview.connect('row-activated', character_win.build_window)
258         treeview.set_model(self.char_model)
259         self.add_columns_to_treeview(treeview)
260
261         # add the treeview with scrollbar --danny
262         self.win.add_with_scrollbar(treeview)
263         self.win.show_all()
264
265         progress_bar.set_fraction(1)
266         progress_bar.destroy()
267
268     def add_columns_to_treeview(self, treeview):
269         #Column 0 for the treeview
270         renderer = gtk.CellRendererPixbuf()
271         column = gtk.TreeViewColumn()
272         column.pack_start(renderer, True)
273         column.add_attribute(renderer, "pixbuf", 
274                 models.CharacterListModel.C_PORTRAIT)
275         treeview.append_column(column)
276
277         #Column 1 for the treeview
278         renderer = gtk.CellRendererText()
279         column = gtk.TreeViewColumn('Character Name', renderer, 
280                 text=models.CharacterListModel.C_NAME)
281         column.set_property("expand", True)
282         treeview.append_column(column)
283  
284     def refresh_clicked(self, button):
285         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading characters...")
286         progress_bar.set_fraction(1)
287         self.char_model.get_characters()
288         progress_bar.destroy()
289
290 class CharacterSheetUI(BaseUI):
291     #time between live sp updates (in milliseconds)
292     UPDATE_INTERVAL = 1000
293
294     def __init__(self, controller):
295         self.controller = controller
296         self.sheet = None
297         self.char_id = None
298         self.skills_model = None
299
300
301     def build_window(self, treeview, path, view_column):
302         # TODO: this is a really long and ugly function, split it up somehow
303
304         self.win = hildon.Window()
305
306         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading character sheet...")
307
308         self.win.show_all() 
309         progress_bar.set_fraction(0.4)
310
311         # Create menu
312         # NOTE: we probably want a window-specific menu for this page, but the
313         # main appmenu works for now
314         menu = self.create_menu(self.win)
315         # Attach menu to the window
316         self.win.set_menu(menu)
317
318         model = treeview.get_model()
319         miter = model.get_iter(path)
320         
321         # column 0 is the portrait, column 1 is name
322         char_name = model.get_value(miter, 1)
323         self.uid = model.get_value(miter, 2)
324         self.char_id = self.controller.char_name2id(char_name)
325
326         self.sheet = self.controller.get_char_sheet(self.uid, self.char_id)
327
328         self.win.set_title(char_name)
329
330
331         hbox = gtk.HBox(False, 0)
332         info_vbox = gtk.VBox(False, 0)
333
334         portrait = gtk.Image()
335         portrait.set_from_file(self.controller.get_portrait(char_name, 256))
336         portrait.show()
337
338         hbox.pack_start(portrait, False, False, 10)
339         hbox.pack_start(info_vbox, False, False, 5)
340
341         vbox = gtk.VBox(False, 0)
342
343         vbox.pack_start(hbox, False, False, 0)
344
345         self.fill_info(info_vbox)
346         self.fill_stats(info_vbox)
347
348         separator = gtk.HSeparator()
349         vbox.pack_start(separator, False, False, 5)
350         separator.show()
351
352         
353         self.add_label("<big>Skill in Training:</big>", vbox, align="normal")
354
355         self.display_skill_in_training(vbox)
356
357         separator = gtk.HSeparator()
358         vbox.pack_start(separator, False, False, 0)
359         separator.show()
360         
361         self.add_label("<big>Skills:</big>", vbox, align="normal")
362
363
364         self.skills_model = models.CharacterSkillsModel(self.controller, self.char_id)
365         skills_treeview = gtk.TreeView(model=self.skills_model)
366         self.add_columns_to_skills_view(skills_treeview)
367
368         vbox.pack_start(skills_treeview, False, False, 0)
369
370         self.win.add_with_scrollbar(vbox)
371         self.win.show_all()
372
373         progress_bar.set_fraction(1)
374         progress_bar.destroy()
375         
376         # diablo doesnt have a glib module, but gobject module seems to have
377         # the same functions...
378         self.timer = gobject.timeout_add(self.UPDATE_INTERVAL, self.update_live_sp)
379         self.win.connect("destroy", self.back)
380
381     def back(self, widget):
382         gobject.source_remove(self.timer)
383         gtk.Window.destroy(self.win)
384
385     def display_skill_in_training(self, vbox):
386         skill = self.controller.get_skill_in_training(self.uid, self.char_id)
387         
388         if skill.skillInTraining:
389
390             skilltree = self.controller.get_skill_tree()
391             
392             # I'm assuming that we will always find a skill with the skill ID
393             for group in skilltree.skillGroups:
394                 found_skill = group.skills.Get(skill.trainingTypeID, False)
395                 if found_skill:
396                     skill_name = found_skill.typeName
397                     break
398                 
399             self.add_label("%s <small>(Level %d)</small>" % (skill_name, skill.trainingToLevel),
400                     vbox, align="normal")
401             self.add_label("<small>start time: %s\t\tend time: %s</small>" 
402                     %(time.ctime(skill.trainingStartTime),
403                 time.ctime(skill.trainingEndTime)), vbox, align="normal")
404
405             progressbar = gtk.ProgressBar()
406             fraction_completed = (time.time() - skill.trainingStartTime) / \
407                     (skill.trainingEndTime - skill.trainingStartTime)
408
409             progressbar.set_fraction(fraction_completed)
410             align = gtk.Alignment(0.5, 0.5, 0.5, 0)
411             vbox.pack_start(align, False, False, 5)
412             align.show()
413             align.add(progressbar)
414             progressbar.show()
415         else:
416             self.add_label("<small>No skills are currently being trained</small>",
417                     vbox, align="normal")
418
419
420
421     def fill_info(self, box):
422         self.add_label("<big><big>%s</big></big>" % self.sheet.name, box)
423         self.add_label("<small>%s %s %s</small>" % (self.sheet.gender, 
424             self.sheet.race, self.sheet.bloodLine), box)
425         self.add_label("", box, markup=False)
426         self.add_label("<small><b>Corp:</b> %s</small>" % self.sheet.corporationName, box)
427         self.add_label("<small><b>Balance:</b> %s ISK</small>" % 
428                 util.comma(self.sheet.balance), box)
429
430         self.live_sp_val = self.controller.get_sp(self.uid, self.char_id)
431         self.live_sp = self.add_label("<small><b>Total SP:</b> %s</small>" %
432                 util.comma(int(self.live_sp_val)), box)
433         
434         self.spps = self.controller.get_spps(self.uid, self.char_id)[0]
435
436
437     def fill_stats(self, box):
438
439         atr = self.sheet.attributes
440
441         self.add_label("<small><b>I: </b>%d  <b>M: </b>%d  <b>C: </b>%d  " \
442                 "<b>P: </b>%d  <b>W: </b>%d</small>" % (atr.intelligence,
443                     atr.memory, atr.charisma, atr.perception, atr.willpower), box)
444
445
446     def add_columns_to_skills_view(self, treeview):
447         #Column 0 for the treeview
448         renderer = gtk.CellRendererText()
449         column = gtk.TreeViewColumn('Skill Name', renderer, 
450                 markup=models.CharacterSkillsModel.C_NAME)
451         column.set_property("expand", True)
452         treeview.append_column(column)
453         
454         #Column 1 for the treeview
455         column = gtk.TreeViewColumn('Rank', renderer, 
456                 markup=models.CharacterSkillsModel.C_RANK)
457         column.set_property("expand", True)
458         treeview.append_column(column)
459
460         #Column 2
461         column = gtk.TreeViewColumn('Points', renderer,
462                 markup=models.CharacterSkillsModel.C_SKILLPOINTS)
463         column.set_property("expand", True)
464         treeview.append_column(column)
465
466         #Column 3
467         column = gtk.TreeViewColumn('Level', renderer, 
468                 markup=models.CharacterSkillsModel.C_LEVEL)
469         column.set_property("expand", True)
470         treeview.append_column(column)
471
472
473     def refresh_clicked(self, button):
474         progress_bar = hildon.hildon_banner_show_progress(self.win, None, "Loading overview...")
475         progress_bar.set_fraction(1)
476         self.skills_model.get_skills()
477         progress_bar.destroy()
478
479
480     def update_live_sp(self):
481         self.live_sp_val = self.live_sp_val + self.spps * (self.UPDATE_INTERVAL / 1000)
482         self.live_sp.set_label("<small><b>Total SP:</b> %s</small>" %
483                                 util.comma(int(self.live_sp_val)))
484
485         return True
486
487
488 if __name__ == "__main__":
489     main()
490