added some simple validation to 'add account' dialog
authorRyan Campbell <campbellr@gmail.com>
Fri, 21 May 2010 01:02:38 +0000 (19:02 -0600)
committerRyan Campbell <campbellr@gmail.com>
Fri, 21 May 2010 01:02:38 +0000 (19:02 -0600)
package/src/ui/fremantle/gui.py
package/src/validation.py [new file with mode: 0644]

index f01d8e4..eff19eb 100644 (file)
@@ -24,15 +24,19 @@ import gobject
 
 import glib
 
+import validation
+
 from ui import models
 
 class BaseUI():
     
     about_name = 'mEveMon'
     about_text = ('Mobile character monitor for EVE Online')
-    about_authors = ['Ryan Campbell', 'Danny Campbell']
+    about_authors = ['Ryan Campbell <campbellr@gmail.com>',
+                     'Danny Campbell <danny.campbell@gmail.com>']
+
     about_website = 'http://mevemon.garage.maemo.org'
-    app_version = '0.3'
+    app_version = '0.4'
 
     menu_items = ("Settings", "About", "Refresh")
 
@@ -185,17 +189,35 @@ class BaseUI():
         ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
 
         dialog.show_all()
+        
         result = dialog.run()
-        if result == gtk.RESPONSE_OK:
-            self.controller.add_account(uidEntry.get_text(), apiEntry.get_text())
-            self.accounts_model.get_accounts()
-
         
+        valid_credentials = False
+
+        while not valid_credentials:
+            if result == gtk.RESPONSE_OK:
+                uid = uidEntry.get_text()
+                api_key = apiEntry.get_text()
+            
+                try:
+                    validation.uid(uid)
+                    validation.api_key(api_key)
+                except validation.ValidationError, e:
+                    self.report_error(e.message)
+                    result = dialog.run()
+                else:
+                    valid_credentials = True
+                    self.controller.add_account(uid, api_key)
+                    self.accounts_model.get_accounts()
+            else:
+                break
+
         dialog.destroy()
 
-        return result
+   
+    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)
@@ -226,15 +248,15 @@ class mEveMonUI(BaseUI):
         gtk.set_application_name("mEveMon")
     
         #create the main window
-        win = hildon.StackableWindow()
-        win.connect("destroy", self.controller.quit)
-        win.show_all()
-        hildon.hildon_gtk_window_set_progress_indicator(win, 1)
+        self.win = hildon.StackableWindow()
+        self.win.connect("destroy", self.controller.quit)
+        self.win.show_all()
+        hildon.hildon_gtk_window_set_progress_indicator(self.win, 1)
 
         # Create menu
-        menu = self.create_menu(win)
+        menu = self.create_menu(self.win)
         # Attach menu to the window
-        win.set_app_menu(menu)
+        self.win.set_app_menu(menu)
 
         pannable_area = hildon.PannableArea()
 
@@ -252,11 +274,11 @@ class mEveMonUI(BaseUI):
 
         pannable_area.add(treeview)
 
-        win.add(pannable_area);
+        self.win.add(pannable_area);
         
-        win.show_all()
+        self.win.show_all()
 
-        hildon.hildon_gtk_window_set_progress_indicator(win, 0)
+        hildon.hildon_gtk_window_set_progress_indicator(self.win, 0)
 
     
     def add_columns_to_treeview(self, treeview):
diff --git a/package/src/validation.py b/package/src/validation.py
new file mode 100644 (file)
index 0000000..a544c42
--- /dev/null
@@ -0,0 +1,38 @@
+
+class ValidationError(StandardError):
+    def __init__(self, message):
+        self.message = message
+
+    def __str__(self):
+        return repr(self.message)
+
+
+
+def api_key(api_key):
+    """
+    validates an EVE api key. throws ValidationError exception if the
+    format is invalid.
+    """
+    KEY_SIZE = 64 
+
+    #TODO: anything else we can do to validate the api key?
+    
+    if len(api_key) != KEY_SIZE:
+        raise ValidationError("API Key must be %s characters" % KEY_SIZE)
+    elif not api_key.isalnum():
+        raise ValidationError("API Key must only contain alphanumeric characters")
+
+    return True
+
+
+def uid(uid):
+    """
+    validates an EVE Online uid, throws ValidationError exception if the
+    format is invalid.
+    """
+    #TODO: anything else we can do to validate the uid?
+
+    if not uid.isdigit():
+        raise ValidationError("UID must be a number")
+    if len(uid) < 1:
+        raise ValidationError("Missing UID")