BUGFIX : add missing configuration argument
[wifihood] / wifiscanner / wifimap / config.py
index 45afae4..7dd6995 100644 (file)
@@ -11,32 +11,33 @@ class Configuration :
 
     def __init__ ( self , type ) :
         self._type = type
-        self._client = gconf.client_get_default()
+        self.read()
 
-        self.homedir = self._client.get_string( "/apps/wifihood/basedir" ) or "/home/user/MyDocs"
-        self.dbname = self._client.get_string( "/apps/wifihood/dbname" ) or "wifiscanner.db"
-        self.mapsdir = self._client.get_string( "/apps/wifihood/maps" ) or "/home/user/MyDocs/.maps"
-        self.mapclass = self._client.get_string( "/apps/wifihood/maptype" ) or "OpenStreetMap I"
+    def read ( self ) :
+        client = gconf.client_get_default()
 
-        self.scan_period = self._client.get_int( "/apps/wifihood/scan-period" ) or 5000
+        self.homedir = client.get_string( "/apps/wifihood/basedir" ) or "/home/user/MyDocs"
+        self.dbname = client.get_string( "/apps/wifihood/dbname" ) or "wifiscanner.db"
+        self.mapsdir = client.get_string( "/apps/wifihood/maps" ) or "/home/user/MyDocs/.maps"
+        self.mapclass = client.get_string( "/apps/wifihood/maptype" ) or "OpenStreetMap I"
 
-        self.store_log = self._client.get_bool( "/apps/wifihood/store-logfile" )
+        self.scan_period = client.get_float( "/apps/wifihood/scan-period" ) or 5
 
-        self.use_mapper = self._client.get_bool( "/apps/wifihood/use-mapper" )
-        self.store_gps = self._client.get_bool( "/apps/wifihood/store-gps" )
+        self.store_log = client.get_bool( "/apps/wifihood/store-logfile" )
+
+        self.use_mapper = client.get_bool( "/apps/wifihood/use-mapper" )
+        self.store_gps = client.get_bool( "/apps/wifihood/store-gps" )
         if self.use_mapper :
-            # FIXME : This will reset the stored default
-            self.store_gps = False
-            self.lat = self._client.get_float( "/apps/maemo/maemo-mapper/center_latitude" )
-            self.lon = self._client.get_float( "/apps/maemo/maemo-mapper/center_longitude" )
-            self.zoom = self._client.get_int( "/apps/maemo/maemo-mapper/zoom" )
+            self.lat = client.get_float( "/apps/maemo/maemo-mapper/center_latitude" )
+            self.lon = client.get_float( "/apps/maemo/maemo-mapper/center_longitude" )
+            self.zoom = client.get_int( "/apps/maemo/maemo-mapper/zoom" )
         else :
-            self.lat = self._client.get_float( "/apps/wifihood/latitude" )
-            self.lon = self._client.get_float( "/apps/wifihood/longitude" )
-            self.zoom = self._client.get_int( "/apps/wifihood/map-zoom" )
+            self.lat = client.get_float( "/apps/wifihood/latitude" )
+            self.lon = client.get_float( "/apps/wifihood/longitude" )
+            self.zoom = client.get_int( "/apps/wifihood/map-zoom" )
 
         if self._type == 'scanner' :
-            self.zoom = self._client.get_int( "/apps/wifihood/%s-zoom" % self._type )
+            self.zoom = client.get_int( "/apps/wifihood/%s-zoom" % self._type )
 
         if self.lat == 0.0 and self.lon == 0.0 :
             self.lat , self.lon = 40.416 , -3.683
@@ -44,20 +45,31 @@ class Configuration :
             self.zoom = 15
 
     def set_latlon ( self , ( lat , lon ) ) :
-        self._client.set_float( "/apps/wifihood/latitude" , lat )
-        self._client.set_float( "/apps/wifihood/longitude" , lon )
+        if self.store_gps and not self.use_mapper :
+            client = gconf.client_get_default()
+            client.set_float( "/apps/wifihood/latitude" , lat )
+            client.set_float( "/apps/wifihood/longitude" , lon )
         self.lat , self.lon = lat , lon
 
     def set_zoom ( self , zoom ) :
-        self._client.set_int( "/apps/wifihood/%s-zoom" % self._type , zoom )
+        if self._type == 'scanner' or ( self.store_gps and not self.use_mapper ) :
+            client = gconf.client_get_default()
+            client.set_int( "/apps/wifihood/%s-zoom" % self._type , zoom )
         self.zoom = zoom
 
-    def save ( self ) :
-        self._client.set_string( "/apps/wifihood/basedir" , self.homedir )
-        self._client.set_string( "/apps/wifihood/dbname" , self.dbname )
-        self._client.set_string( "/apps/wifihood/maps" , self.mapsdir )
-        self._client.set_string( "/apps/wifihood/maptype" , self.mapclass )
+    def save ( self , widget=None ) :
+        """Saves all the editable configuration elements"""
+
+        client = gconf.client_get_default()
+        client.set_string( "/apps/wifihood/basedir" , self.homedir )
+        client.set_string( "/apps/wifihood/dbname" , self.dbname )
 
+        client.set_float( "/apps/wifihood/scan-period" , self.scan_period )
+        client.set_bool( "/apps/wifihood/store-logfile" , self.store_log )
+        client.set_bool( "/apps/wifihood/use-mapper" , self.use_mapper )
+        client.set_bool( "/apps/wifihood/store-gps" , self.store_gps )
+
+        client.set_int( "/apps/wifihood/%s-zoom" % self._type , self.zoom )
 
 class AbstractSettingsWindow :
 
@@ -72,6 +84,15 @@ class AbstractSettingsWindow :
         vbox.show()
         scrollwin.add_with_viewport( vbox )
 
+        self.add_dataframe( vbox , config )
+        self.add_mapsframe( vbox , config , handler )
+        self.add_coordsframe( vbox , config )
+        self.add_dbframe( vbox , config )
+
+        self.show()
+
+
+    def add_dbframe ( self , vbox , config ) :
 
         dbframe = gtk.Frame( label="Database" )
         dbframe.set_label_align(0 , 0.1)
@@ -103,6 +124,8 @@ class AbstractSettingsWindow :
         database.attach(dbvalue, 1, 2, 1, 2, gtk.EXPAND|gtk.FILL, 0, 0, 5)
 
 
+    def add_mapsframe ( self , vbox , config , handler ) :
+
         mapsframe = gtk.Frame( label="Maps" )
         mapsframe.set_label_align(0 , 0.1)
         mapsframe.show()
@@ -123,6 +146,8 @@ class AbstractSettingsWindow :
         maps.attach(zoomlevel, 1, 2, 0, 1, gtk.EXPAND|gtk.FILL, 0, 0, 5)
 
 
+    def add_coordsframe ( self , vbox , config ) :
+
         gpsframe = gtk.Frame( label="Coordinates" )
         gpsframe.set_label_align(0 , 0.1)
         gpsframe.show()
@@ -134,19 +159,21 @@ class AbstractSettingsWindow :
 
         button = self.CheckButton()
         button.set_label( "Take initial coordinates from maemo-mapper" )
-        button.connect( "toggled" , self.checkbutton_cb , config , "use-mapper" )
+        button.connect( "toggled" , self.checkbutton_cb , config , "use_mapper" )
         button.set_active( config.use_mapper )
         button.show()
         gps.attach(button, 0, 2, 0, 1, gtk.EXPAND|gtk.FILL) #, 0, 0, 5)
 
         button = self.CheckButton()
         button.set_label( "Store changes in coordinates" )
-        button.connect( "toggled" , self.checkbutton_cb , config , "store-gps" )
+        button.connect( "toggled" , self.checkbutton_cb , config , "store_gps" )
         button.set_active( config.store_gps )
         button.show()
         gps.attach(button, 0, 2, 1, 2, gtk.EXPAND|gtk.FILL) #, 0, 0, 5)
 
 
+    def add_dataframe ( self , vbox , config ) :
+
         dataframe = gtk.Frame( label="Data gathering" )
         dataframe.set_label_align(0 , 0.1)
         dataframe.show()
@@ -161,28 +188,29 @@ class AbstractSettingsWindow :
         datatable.attach(scanlabel, 0, 1, 0, 1, gtk.EXPAND|gtk.FILL)
 
         scanvalue = self.Entry()
-        scanvalue.connect( "changed" , self.int_cb , config , "scan-period" , 1000 )
-        scanvalue.set_text( "%s" % ( float(config.scan_period) / 1000 ) )
+        scanvalue.connect( "changed" , self.float_cb , config , "scan-period" )
+        scanvalue.set_text( "%s" % config.scan_period )
         scanvalue.show()
         datatable.attach(scanvalue, 1, 2, 0, 1, gtk.EXPAND|gtk.FILL)
 
         button = self.CheckButton()
         button.set_label( "Write full logfile" )
-        button.connect( "toggled" , self.checkbutton_cb , config , "store-logfile" )
+        button.connect( "toggled" , self.checkbutton_cb , config , "store_log" )
         button.set_active( config.store_log )
         button.show()
         datatable.attach(button, 0, 2, 1, 2, gtk.EXPAND|gtk.FILL)
 
-        self.show()
-
     def entry_cb ( self , entry , config , keyword ) :
-        config._client.set_string( "%s/%s" % ( "/apps/wifihood" , keyword ) , entry.get_text() )
+        config.__dict__[ keyword ] = entry.get_text()
+
+    def float_cb ( self , entry , config , keyword ) :
+        config.__dict__[ keyword ] = float( entry.get_text() )
 
     def int_cb ( self , entry , config , keyword , scale=1 ) :
-        config._client.set_int( "%s/%s" % ( "/apps/wifihood" , keyword ) , int( scale * float( entry.get_text() ) ) )
+        config.__dict__[ keyword ] = int( scale * float( entry.get_text() ) )
 
     def checkbutton_cb ( self , button , config , keyword ) :
-        config._client.set_bool( "%s/%s" % ( "/apps/wifihood" , keyword ) , button.get_active() )
+        config.__dict__[ keyword ] = button.get_active()
 
 if hildon :
 
@@ -283,10 +311,11 @@ else :
     def __init__ ( self , config , handler=None ) :
         gtk.Window.__init__( self )
         AbstractSettingsWindow.__init__( self , config , handler )
+        self.connect("unrealize", config.save )
 
     def MainArea ( self ) :
         scrollwin = gtk.ScrolledWindow()
-        scrollwin.set_size_request(-1, 260)
+        scrollwin.set_size_request(-1, 290)
         scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
         return scrollwin