Fix bug #5645, and other minor bug
[mevemon] / package / src / ui / fremantle / gui.py
index eff19eb..8544f70 100644 (file)
@@ -22,25 +22,14 @@ import gtk
 import hildon
 import gobject
 
-import glib
-
-import validation
-
 from ui import models
+import validation
+import util
 
 class BaseUI():
-    
-    about_name = 'mEveMon'
-    about_text = ('Mobile character monitor for EVE Online')
-    about_authors = ['Ryan Campbell <campbellr@gmail.com>',
-                     'Danny Campbell <danny.campbell@gmail.com>']
-
-    about_website = 'http://mevemon.garage.maemo.org'
-    app_version = '0.4'
-
     menu_items = ("Settings", "About", "Refresh")
 
-    def create_menu(self, window): 
+    def create_menu(self, window):
         menu = hildon.AppMenu()
 
         for command in self.menu_items:
@@ -53,7 +42,7 @@ class BaseUI():
             elif command == "Settings":
                 button.connect("clicked", self.settings_clicked, window)
             elif command == "Refresh":
-                button.connect("clicked", self.refresh_clicked, window)
+                button.connect("clicked", self.refresh_clicked)
             else:
                 assert False, command
 
@@ -71,18 +60,13 @@ class BaseUI():
         dialog = gtk.Dialog()
         dialog.set_transient_for(window)
         dialog.set_title("Settings")
-        
+
         pannable_area = hildon.PannableArea()
 
         dialog_vbox = dialog.vbox
 
         vbox = gtk.VBox(False, 1)
 
-        #acctsLabel = gtk.Label("Accounts:")
-        #acctsLabel.set_justify(gtk.JUSTIFY_LEFT)
-     
-        #vbox.pack_start(acctsLabel, False, False, 1)
-       
         self.accounts_model = models.AccountsModel(self.controller)
         
         accounts_treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_NORMAL)
@@ -101,10 +85,12 @@ class BaseUI():
         pannable_area.add_with_viewport(vbox)
         
         dialog_vbox.pack_start(pannable_area, True, True, 1)
-       
+      
+      
         dialog.show_all()
-        
+
         result = dialog.run()
+
         while(result != gtk.RESPONSE_DELETE_EVENT):
             if result == RESPONSE_NEW:
                 self.new_account_clicked(window)
@@ -117,17 +103,19 @@ class BaseUI():
             elif result == gtk.RESPONSE_OK:
                 self.char_model.get_characters()
                 break
-            
+        
             result = dialog.run()
 
         dialog.destroy()
 
+
+
     def get_selected_item(self, treeview, column):
         selection = treeview.get_selection()
-        model, miter = selection.get_selected() 
-        
+        model, miter = selection.get_selected()
+
         value = model.get_value(miter, column)
-        
+
         return value
 
     def edit_account(self, treeview):
@@ -135,14 +123,14 @@ class BaseUI():
         # pop up the account dialog
 
         self.accounts_model.get_accounts()
-         
+
     def delete_account(self, treeview):
-        uid = self.get_selected_item(treeview, 0) 
+        uid = self.get_selected_item(treeview, 0)
         self.controller.remove_account(uid)
         # refresh model
         self.accounts_model.get_accounts()
 
-    
+
     def add_columns_to_accounts(self, treeview):
         #Column 0 for the treeview
         renderer = gtk.CellRendererText()
@@ -157,6 +145,7 @@ class BaseUI():
         column.set_property("expand", True)
         treeview.append_column(column)
 
+
     def new_account_clicked(self, window):
         dialog = gtk.Dialog()
     
@@ -189,7 +178,6 @@ class BaseUI():
         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
 
         dialog.show_all()
-        
         result = dialog.run()
         
         valid_credentials = False
@@ -197,7 +185,8 @@ class BaseUI():
         while not valid_credentials:
             if result == gtk.RESPONSE_OK:
                 uid = uidEntry.get_text()
-                api_key = apiEntry.get_text()
+                # auth() fails if api_key has lower-case characters
+                api_key = apiEntry.get_text().upper()
             
                 try:
                     validation.uid(uid)
@@ -214,18 +203,18 @@ class BaseUI():
 
         dialog.destroy()
 
-   
+
     def report_error(self, error):
         hildon.hildon_banner_show_information(self.win, '', error)
-
+    
     def about_clicked(self, button):
         dialog = gtk.AboutDialog()
-        dialog.set_website(self.about_website)
-        dialog.set_website_label(self.about_website)
-        dialog.set_name(self.about_name)
-        dialog.set_authors(self.about_authors)
-        dialog.set_comments(self.about_text)
-        dialog.set_version(self.app_version)
+        dialog.set_website(self.controller.about_website)
+        dialog.set_website_label(self.controller.about_website)
+        dialog.set_name(self.controller.about_name)
+        dialog.set_authors(self.controller.about_authors)
+        dialog.set_comments(self.controller.about_text)
+        dialog.set_version(self.controller.app_version)
         dialog.run()
         dialog.destroy()
 
@@ -240,14 +229,15 @@ class BaseUI():
 
         return label
 
-
 class mEveMonUI(BaseUI):
 
     def __init__(self, controller):
         self.controller = controller
         gtk.set_application_name("mEveMon")
-    
-        #create the main window
+
+
+    def run(self):
+        # create the main window
         self.win = hildon.StackableWindow()
         self.win.connect("destroy", self.controller.quit)
         self.win.show_all()
@@ -260,16 +250,14 @@ class mEveMonUI(BaseUI):
 
         pannable_area = hildon.PannableArea()
 
-        character_win = CharacterSheetUI(self.controller)
 
         # gtk.HILDON_UI_MODE_NORMAL -> not selection in the treeview
         # gtk.HILDON_UI_MODE_EDIT -> selection in the treeview
         treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_NORMAL)
-        treeview.connect('row-activated', character_win.build_window)
+        treeview.connect('row-activated', self.do_charactersheet)
 
         self.char_model = models.CharacterListModel(self.controller)
         treeview.set_model(self.char_model)
-
         self.add_columns_to_treeview(treeview)
 
         pannable_area.add(treeview)
@@ -280,7 +268,6 @@ class mEveMonUI(BaseUI):
 
         hildon.hildon_gtk_window_set_progress_indicator(self.win, 0)
 
-    
     def add_columns_to_treeview(self, treeview):
         #Column 0 for the treeview
         renderer = gtk.CellRendererPixbuf()
@@ -297,57 +284,70 @@ class mEveMonUI(BaseUI):
         column.set_property("expand", True)
         treeview.append_column(column)
  
-      
-    def refresh_clicked(self, button, window):
-        hildon.hildon_gtk_window_set_progress_indicator(window, 1)
+    def refresh_clicked(self, button):
+        hildon.hildon_gtk_window_set_progress_indicator(self.win, 1)
         self.char_model.get_characters()
-        hildon.hildon_gtk_window_set_progress_indicator(window, 0)
+        hildon.hildon_gtk_window_set_progress_indicator(self.win, 0)
     
 
+    def do_charactersheet(self, treeview, path, view_column):
+
+        model = treeview.get_model()
+        miter = model.get_iter(path)
+        
+        # column 0 is the portrait, column 1 is name
+        char_name = model.get_value(miter, 1)
+        uid = model.get_value(miter, 2)
+        
+        if uid:
+            CharacterSheetUI(self.controller, char_name, uid)
+        else:
+            pass
+
+
 class CharacterSheetUI(BaseUI):
     UPDATE_INTERVAL = 1
 
-    def __init__(self, controller):
+    def __init__(self, controller, char_name, uid):
         self.controller = controller
+        self.char_name = char_name
+        self.uid = uid
         self.sheet = None
         self.char_id = None
         self.skills_model = None
+        
+        self.build_window()
 
 
-    def build_window(self, treeview, path, view_column):
+    def build_window(self):
         # TODO: this is a really long and ugly function, split it up somehow
 
         self.win = hildon.StackableWindow()
-        #win.show_all() 
         hildon.hildon_gtk_window_set_progress_indicator(self.win, 1)
+        self.win.show_all() 
 
         # Create menu
         # NOTE: we probably want a window-specific menu for this page, but the
         # main appmenu works for now
         menu = self.create_menu(self.win)
-        # Attach menu to the window        
+        # Attach menu to the window
         self.win.set_app_menu(menu)
 
         pannable_area = hildon.PannableArea()
 
-        model = treeview.get_model()
-        miter = model.get_iter(path)
-        
-        # column 0 is the portrait, column 1 is name
-        char_name = model.get_value(miter, 1)
-        self.uid = model.get_value(miter, 2)
-        self.char_id = self.controller.char_name2id(char_name)
+        self.char_id = self.controller.char_name2id(self.char_name)
 
         self.sheet = self.controller.get_char_sheet(self.uid, self.char_id)
 
-        self.win.set_title(char_name)
+        self.win.set_title(self.char_name)
 
 
         hbox = gtk.HBox(False, 0)
         info_vbox = gtk.VBox(False, 0)
 
         portrait = gtk.Image()
-        portrait.set_from_file(self.controller.get_portrait(char_name, 256))
+        portrait.set_from_file(self.controller.get_portrait(self.char_name, 256))
+        portrait.show()
 
         hbox.pack_start(portrait, False, False, 10)
         hbox.pack_start(info_vbox, False, False, 5)
@@ -358,18 +358,20 @@ class CharacterSheetUI(BaseUI):
         vbox.pack_start(hbox, False, False, 0)
 
         self.fill_info(info_vbox)
-        
         self.fill_stats(info_vbox)
 
         separator = gtk.HSeparator()
         vbox.pack_start(separator, False, False, 5)
+        separator.show()
+
         
         self.add_label("<big>Skill in Training:</big>", vbox, align="normal")
-        
+
         self.display_skill_in_training(vbox)
 
         separator = gtk.HSeparator()
         vbox.pack_start(separator, False, False, 0)
+        separator.show()
         
         self.add_label("<big>Skills:</big>", vbox, align="normal")
 
@@ -383,6 +385,16 @@ class CharacterSheetUI(BaseUI):
         self.win.show_all()
 
         hildon.hildon_gtk_window_set_progress_indicator(self.win, 0)
+        
+        # if we start the timer too early, get_is_topmost() returns False
+        self.timer = gobject.timeout_add_seconds(self.UPDATE_INTERVAL, self.update_live_sp)
+
+        self.win.connect("destroy", self.back)
+
+    def back(self, widget):
+        gobject.source_remove(self.timer)
+        gtk.Window.destroy(self.win)
+
 
     def display_skill_in_training(self, vbox):
         skill = self.controller.get_skill_in_training(self.uid, self.char_id)
@@ -403,13 +415,17 @@ class CharacterSheetUI(BaseUI):
             self.add_label("<small>start time: %s\t\tend time: %s</small>" 
                     %(time.ctime(skill.trainingStartTime),
                 time.ctime(skill.trainingEndTime)), vbox, align="normal")
+
             progressbar = gtk.ProgressBar()
             fraction_completed = (time.time() - skill.trainingStartTime) / \
                     (skill.trainingEndTime - skill.trainingStartTime)
+
             progressbar.set_fraction(fraction_completed)
             align = gtk.Alignment(0.5, 0.5, 0.5, 0)
             vbox.pack_start(align, False, False, 5)
+            align.show()
             align.add(progressbar)
+            progressbar.show()
         else:
             self.add_label("<small>No skills are currently being trained</small>",
                     vbox, align="normal")
@@ -422,17 +438,18 @@ class CharacterSheetUI(BaseUI):
             self.sheet.race, self.sheet.bloodLine), box)
         self.add_label("", box, markup=False)
         self.add_label("<small><b>Corp:</b> %s</small>" % self.sheet.corporationName, box)
-        self.add_label("<small><b>Balance:</b> %s ISK</small>" % self.sheet.balance, box)
+        self.add_label("<small><b>Balance:</b> %s ISK</small>" % 
+                util.comma(self.sheet.balance), box)
+
         self.live_sp_val = self.controller.get_sp(self.uid, self.char_id)
         self.live_sp = self.add_label("<small><b>Total SP:</b> %s</small>" %
-                self.live_sp_val, box)
+                util.comma(int(self.live_sp_val)), box)
         
         self.spps = self.controller.get_spps(self.uid, self.char_id)[0]
 
-        glib.timeout_add_seconds(self.UPDATE_INTERVAL, self.update_live_sp)
 
     def fill_stats(self, box):
-        
+
         atr = self.sheet.attributes
 
         self.add_label("<small><b>I: </b>%d  <b>M: </b>%d  <b>C: </b>%d  " \
@@ -454,12 +471,13 @@ class CharacterSheetUI(BaseUI):
         column.set_property("expand", True)
         treeview.append_column(column)
 
-
+        #Column 2
         column = gtk.TreeViewColumn('Points', renderer,
                 markup=models.CharacterSkillsModel.C_SKILLPOINTS)
         column.set_property("expand", True)
         treeview.append_column(column)
 
+        #Column 3
         column = gtk.TreeViewColumn('Level', renderer, 
                 markup=models.CharacterSkillsModel.C_LEVEL)
         column.set_property("expand", True)
@@ -473,14 +491,9 @@ class CharacterSheetUI(BaseUI):
 
 
     def update_live_sp(self):
-        # we don't want to keep the timer running in the background
-        # when this callback returns False, the timer destorys itself
-        if not self.win.get_is_topmost():
-            return False
-        
         self.live_sp_val = self.live_sp_val + self.spps * self.UPDATE_INTERVAL
-        self.live_sp.set_label("<small><b>Total SP:</b> %d</small>" %
-                                self.live_sp_val)
+        self.live_sp.set_label("<small><b>Total SP:</b> %s</small>" %
+                                util.comma(int(self.live_sp_val)))
 
         return True