From 954dc489c4a75991ccb385efdee41ce3e1777a33 Mon Sep 17 00:00:00 2001
From: James Cameron <quozl@laptop.org>
Date: Wed, 25 Aug 2010 15:49:27 +1000
Subject: [PATCH] fix discard history #1673

User interface changes:

- enable the discard network history button in the network control
  panel, which also now forces a disconnect, and will be insensitive
  if there are no networks to be discarded, (rather than the button
  doing nothing),

Design changes:

- expose trusted access point list count for network control panel,

- changes to the autoconnect connection flag are now cascaded to
  NetworkManager using the Updated signal,

- discarding network history is now cascaded to NetworkManager using
  the Removed signal,

- factoring of connections.cfg path and empty file creation,

Tested extensively on Sugar 0.84.
Not tested on Sugar 0.88.

References:

    http://dev.laptop.org/ticket/9977 (fixes a workaround)
    http://bugs.sugarlabs.org/ticket/1673
---
 extensions/cpsection/network/model.py |    6 +++-
 extensions/cpsection/network/view.py  |    8 ++++-
 src/jarabe/model/network.py           |   58 +++++++++++++++++++++++++++------
 3 files changed, 60 insertions(+), 12 deletions(-)

diff --git a/extensions/cpsection/network/model.py b/extensions/cpsection/network/model.py
index e1c3dab..eed0b03 100644
--- a/extensions/cpsection/network/model.py
+++ b/extensions/cpsection/network/model.py
@@ -17,6 +17,7 @@
 
 import dbus
 from gettext import gettext as _
+from jarabe.model import network
 import gconf
 
 _NM_SERVICE = 'org.freedesktop.NetworkManager'
@@ -116,7 +117,10 @@ def clear_registration():
 def clear_networks():
     """Clear saved passwords and network configurations.
     """
-    pass
+    network.clear_wifi_connections()
+
+def count_networks():
+    return network.count_wifi_connections()
 
 def get_publish_information():
     client = gconf.client_get_default()
diff --git a/extensions/cpsection/network/view.py b/extensions/cpsection/network/view.py
index 588daeb..b0f1336 100644
--- a/extensions/cpsection/network/view.py
+++ b/extensions/cpsection/network/view.py
@@ -104,6 +104,8 @@ class Network(SectionView):
         self._clear_history_button = gtk.Button()
         self._clear_history_button.set_label(_('Discard network history'))
         box_clear_history.pack_start(self._clear_history_button, expand=False)
+        if self._model.count_networks() == 0:
+            self._clear_history_button.set_sensitive(False)
         self._clear_history_button.show()
         box_wireless.pack_start(box_clear_history, expand=False)
         box_clear_history.show()
@@ -217,7 +219,9 @@ class Network(SectionView):
             self._radio_alert.props.msg = detail
             self._radio_valid = False
         else:
-            self._radio_valid = True            
+            self._radio_valid = True
+            if self._model.count_networks() != 0:
+                self._clear_history_button.set_sensitive(True)
 
         self._validate()
         return False
@@ -248,3 +252,5 @@ class Network(SectionView):
 
     def __network_configuration_reset_cb(self, widget):
         self._model.clear_networks()
+        if self._model.count_networks() == 0:
+            self._clear_history_button.set_sensitive(False)
diff --git a/src/jarabe/model/network.py b/src/jarabe/model/network.py
index 66d86d8..2c0afe3 100644
--- a/src/jarabe/model/network.py
+++ b/src/jarabe/model/network.py
@@ -508,6 +508,13 @@ class NMSettings(dbus.service.Object):
         self.secrets_request.send(self, connection=sender,
                                   response=kwargs['response'])
 
+    def clear_wifi_connections(self):
+        for uuid in self.connections.keys():
+            conn = self.connections[uuid]
+            if conn.type == NM_CONNECTION_TYPE_802_11_WIRELESS:
+                conn.Removed()
+                self.connections.pop(uuid)
+
 class SecretsResponse(object):
     ''' Intermediate object to report the secrets from the dialog
     back to the connection object and which will inform NM
@@ -536,6 +543,16 @@ class NMSettingsConnection(dbus.service.Object):
         self._settings = settings
         self._secrets = secrets
 
+    @dbus.service.signal(dbus_interface=NM_CONNECTION_IFACE,
+                         signature='')
+    def Removed(self):
+        pass
+
+    @dbus.service.signal(dbus_interface=NM_CONNECTION_IFACE,
+                         signature='a{sa{sv}}')
+    def Updated(self, settings):
+        pass
+
     def set_connected(self):
         if self._settings.connection.type == NM_CONNECTION_TYPE_GSM:
             self._settings.connection.timestamp = int(time.time())
@@ -544,8 +561,17 @@ class NMSettingsConnection(dbus.service.Object):
                 self._settings.connection.autoconnect = True
                 self._settings.connection.timestamp = int(time.time())
                 if self._settings.connection.type == NM_CONNECTION_TYPE_802_11_WIRELESS:
+                    self.Updated(self._settings.get_dict())
                     self.save()
 
+    def set_disconnected(self):
+        if self._settings.connection.type != NM_CONNECTION_TYPE_GSM and \
+               self._settings.connection.autoconnect:
+            self._settings.connection.autoconnect = False
+            self._settings.connection.timestamp = None
+            self.Updated(self._settings.get_dict())
+            self.save()
+
     def set_secrets(self, secrets):
         self._secrets = secrets
         if self._settings.connection.type == NM_CONNECTION_TYPE_802_11_WIRELESS:
@@ -555,8 +581,7 @@ class NMSettingsConnection(dbus.service.Object):
         return self._settings
 
     def save(self):
-        profile_path = env.get_profile_path()
-        config_path = os.path.join(profile_path, 'nm', 'connections.cfg')
+        config_path = get_wifi_connections_path()
 
         config = ConfigParser.ConfigParser()
         try:
@@ -777,19 +802,23 @@ def add_connection(uuid, settings, secrets=None):
     _nm_settings.add_connection(uuid, conn)
     return conn
 
-def load_wifi_connections():
+def get_wifi_connections_path():
     profile_path = env.get_profile_path()
-    config_path = os.path.join(profile_path, 'nm', 'connections.cfg')
+    return os.path.join(profile_path, 'nm', 'connections.cfg')
 
-    config = ConfigParser.ConfigParser()
+def create_wifi_connections(config_path):
+    if not os.path.exists(os.path.dirname(config_path)):
+        os.makedirs(os.path.dirname(config_path), 0755)
+    f = open(config_path, 'w')
+    f.close()
+
+def load_wifi_connections():
+    config_path = get_wifi_connections_path()
 
     if not os.path.exists(config_path):
-        if not os.path.exists(os.path.dirname(config_path)):
-            os.makedirs(os.path.dirname(config_path), 0755)
-        f = open(config_path, 'w')
-        config.write(f)
-        f.close()
+        create_wifi_connections(config_path)
 
+    config = ConfigParser.ConfigParser()
     try:
         if not config.read(config_path):
             logging.error('Error reading the nm config file')
@@ -895,3 +924,12 @@ def find_gsm_connection():
 
     logging.debug('There is no gsm connection in the NMSettings.')
     return None
+
+def count_wifi_connections():
+    return len(get_settings().connections)
+
+def clear_wifi_connections():
+    _nm_settings.clear_wifi_connections()
+
+    config_path = get_wifi_connections_path()
+    create_wifi_connections(config_path)
-- 
1.7.1

