From 486b45510571be462b5c45046b09f0a88d81b12b Mon Sep 17 00:00:00 2001
From: root <root@localhost.localdomain>
Date: Thu, 14 Jan 2010 15:41:59 -0200
Subject: [PATCH 2/3] 3G Connection Info

---
 extensions/deviceicon/network.py |   62 ++++++++++++++++++++++++++++++++++---
 src/jarabe/model/network.py      |   13 +++++---
 2 files changed, 65 insertions(+), 10 deletions(-)

diff --git a/extensions/deviceicon/network.py b/extensions/deviceicon/network.py
index 18f0c4c..088fbd0 100644
--- a/extensions/deviceicon/network.py
+++ b/extensions/deviceicon/network.py
@@ -23,7 +23,7 @@ import hashlib
 import socket
 import struct
 import re
-
+import datetime, time
 import gtk
 import gobject
 import gconf
@@ -51,6 +51,7 @@ _NM_PATH = '/org/freedesktop/NetworkManager'
 _NM_DEVICE_IFACE = 'org.freedesktop.NetworkManager.Device'
 _NM_WIRED_IFACE = 'org.freedesktop.NetworkManager.Device.Wired'
 _NM_WIRELESS_IFACE = 'org.freedesktop.NetworkManager.Device.Wireless'
+_NM_SERIAL_IFACE = 'org.freedesktop.NetworkManager.Device.Serial'
 _NM_ACCESSPOINT_IFACE = 'org.freedesktop.NetworkManager.AccessPoint'
 _NM_ACTIVE_CONN_IFACE = 'org.freedesktop.NetworkManager.Connection.Active'
 
@@ -221,6 +222,16 @@ class GsmPalette(Palette):
     }
 
     def __init__(self):
+        def _padded(child, xalign=0, yalign=0.5):
+            padder = gtk.Alignment(xalign=xalign, yalign=yalign,
+                                   xscale=1, yscale=0.33)
+            padder.set_padding(style.DEFAULT_SPACING,
+                               style.DEFAULT_SPACING,
+                               style.DEFAULT_SPACING,
+                               style.DEFAULT_SPACING)
+            padder.add(child)
+            return padder
+
         Palette.__init__(self, label=_('GSM Device'))
 
         self._current_state = None
@@ -228,6 +239,18 @@ class GsmPalette(Palette):
         self._toggle_state_item = gtk.MenuItem('')
         self.set_state(_GSM_STATE_NOT_READY)
 
+        self._info = gtk.VBox()
+        self._data_label = gtk.Label()
+        self._data_label.props.xalign = 0.0
+        self._info.pack_start(_padded(self._data_label))
+        
+        self._conn_time_label = gtk.Label()
+        self._conn_time_label.props.xalign = 0.0
+        self._info.pack_start(_padded(self._conn_time_label))
+
+        self._info.show_all()
+        self.set_content(self._info)
+
         self._toggle_state_item.connect('activate', self._toggle_state_cb)
         self.menu.append(self._toggle_state_item)
         self._toggle_state_item.show()
@@ -558,6 +581,9 @@ class GsmDeviceView(TrayIcon):
     FRAME_POSITION_RELATIVE = 303
 
     def __init__(self, device):
+        self.__connection_time_handler = None
+        self.__connection_timestamp = None
+
         client = gconf.client_get_default()
         color = xocolor.XoColor(client.get_string('/desktop/sugar/user/color'))
 
@@ -572,7 +598,10 @@ class GsmDeviceView(TrayIcon):
                                       signal_name='StateChanged',
                                       path=self._device.object_path,
                                       dbus_interface=_NM_DEVICE_IFACE)
-
+        self._bus.add_signal_receiver(self.__ppp_stats_changed_cb,
+                                      signal_name='PppStats',
+                                      path=self._device.object_path,
+                                      dbus_interface=_NM_SERIAL_IFACE)
     def create_palette(self):
         palette = GsmPalette()
 
@@ -637,8 +666,20 @@ class GsmDeviceView(TrayIcon):
 
         if state is network.DEVICE_STATE_ACTIVATED:
             gsm_state = _GSM_STATE_CONNECTED
-
-        elif state is network.DEVICE_STATE_DISCONNECTED:
+            connection = network.find_gsm_connection()
+            if (connection <> None):
+                connection.set_connected()
+                self.__connection_timestamp =  time.time() - connection.get_settings().connection.timestamp
+                self.__connection_time_handler = gobject.timeout_add(1000, self.connection_timecount_cb) 
+                self.__ppp_stats_changed_cb(0, 0)
+                self.connection_timecount_cb()                
+                self._palette._info.show_all() 
+        else: 
+            self.__connection_timestamp = 0
+            if (self.__connection_time_handler <> None):
+                gobject.source_remove(self.__connection_time_handler)
+            self._palette._info.hide_all() 
+        if state is network.DEVICE_STATE_DISCONNECTED:
             gsm_state = _GSM_STATE_DISCONNECTED
 
         elif state in [network.DEVICE_STATE_UNMANAGED, _NM_DEVICE_STATE_UNAVAILABLE, _NM_DEVICE_STATE_UNKNOWN]:
@@ -655,7 +696,18 @@ class GsmDeviceView(TrayIcon):
                                          signal_name='StateChanged',
                                          path=self._device.object_path,
                                          dbus_interface=_NM_DEVICE_IFACE)
-
+        
+    def __ppp_stats_changed_cb(self, in_bytes, out_bytes):
+        in_bytes = in_bytes / 1024
+        out_bytes = out_bytes / 1024
+        self._palette ._data_label.set_text("Data sent {0} kb / received {1} kb".format(out_bytes ,in_bytes))
+
+    def connection_timecount_cb(self):
+        self.__connection_timestamp = self.__connection_timestamp + 1
+        connection_time = datetime.datetime.fromtimestamp(self.__connection_timestamp)
+        self._palette._conn_time_label.set_text("Connection time " + connection_time.strftime('%H : %M : %S'))
+        return True
+    
 class WirelessDeviceObserver(object):
     def __init__(self, device, tray):
         self._device = device
diff --git a/src/jarabe/model/network.py b/src/jarabe/model/network.py
index 7c3849d..18c15d0 100644
--- a/src/jarabe/model/network.py
+++ b/src/jarabe/model/network.py
@@ -324,11 +324,14 @@ class NMSettingsConnection(dbus.service.Object):
         self._secrets = secrets
 
     def set_connected(self):
-        if not self._settings.connection.autoconnect:
-            self._settings.connection.autoconnect = True
-            self._settings.connection.timestamp = int(time.time())
-            if self._settings.connection.type == NM_CONNECTION_TYPE_802_11_WIRELESS:
-                self.save()
+        if self._settings.connection.type == NM_CONNECTION_TYPE_GSM:
+             self._settings.connection.timestamp = int(time.time())
+        else:
+            if not self._settings.connection.autoconnect:
+                self._settings.connection.autoconnect = True
+                self._settings.connection.timestamp = int(time.time())
+                if self._settings.connection.type == NM_CONNECTION_TYPE_802_11_WIRELESS:
+                    self.save()
 
     def set_secrets(self, secrets):
         self._secrets = secrets
-- 
1.6.2.5

