From 956b06db5efb5a6d03fdec178fbf8a885bd71a6c Mon Sep 17 00:00:00 2001
From: latu <latu@localhost.localdomain>
Date: Tue, 2 Feb 2010 16:17:43 -0200
Subject: [PATCH 2/2] Add Connection Information to 3G (GSM) Modem Support- V2

---
 extensions/deviceicon/network.py |   86 ++++++++++++++++++++++----------------
 1 files changed, 50 insertions(+), 36 deletions(-)

diff --git a/extensions/deviceicon/network.py b/extensions/deviceicon/network.py
index 8e8eb64..04b38fd 100644
--- a/extensions/deviceicon/network.py
+++ b/extensions/deviceicon/network.py
@@ -23,7 +23,8 @@ import hashlib
 import socket
 import struct
 import re
-import datetime, time
+import datetime
+import time
 import gtk
 import gobject
 import gconf
@@ -221,17 +222,17 @@ class GsmPalette(Palette):
                                  gobject.TYPE_NONE, ([])),
     }
 
-    def __init__(self):
-
-        def _padded(child, xalign=0, yalign=0.5):
-            padder = gtk.Alignment(xalign=xalign, yalign=yalign,
+    def _add_widget_with_padding(self, child, xalign=0, yalign=0.5):
+            alignment = gtk.Alignment(xalign=xalign, yalign=yalign,
                                    xscale=1, yscale=0.33)
-            padder.set_padding(style.DEFAULT_SPACING,
+            alignment.set_padding(style.DEFAULT_SPACING,
                                style.DEFAULT_SPACING,
                                style.DEFAULT_SPACING,
                                style.DEFAULT_SPACING)
-            padder.add(child)
-            return padder
+            alignment.add(child)
+            return alignment
+
+    def __init__(self):
 
         Palette.__init__(self, label=_('Wireless modem'))
 
@@ -244,17 +245,24 @@ class GsmPalette(Palette):
 
         self.set_state(_GSM_STATE_NOT_READY)
 
-        self._info = gtk.VBox()
+        self.info_box = gtk.VBox()
+
         self._data_label = gtk.Label()
         self._data_label.props.xalign = 0.0
-        self._info.pack_start(_padded(self._data_label))
-        
+	label_alignment = self._add_widget_with_padding(self._data_label) 
+        self.info_box.pack_start(label_alignment)
+        self._data_label.show()
+        label_alignment.show()
+
         self._conn_time_label = gtk.Label()
         self._conn_time_label.props.xalign = 0.0
-        self._info.pack_start(_padded(self._conn_time_label))
+	label_alignment = self._add_widget_with_padding(self._conn_time_label) 
+        self.info_box.pack_start(label_alignment)
+	self._conn_time_label.show()
+        label_alignment.show()
 
-        self._info.show_all()
-        self.set_content(self._info)
+        self.info_box.show()
+        self.set_content(self.info_box)
 
     def set_state(self, state):
         self._current_state = state
@@ -707,8 +715,8 @@ class GsmDeviceView(TrayIcon):
     FRAME_POSITION_RELATIVE = 303
 
     def __init__(self, device):
-        self.__connection_time_handler = None
-        self.__connection_timestamp = None
+        self._connection_time_handler = None
+        self._connection_timestamp = 0
 
         client = gconf.client_get_default()
         color = xocolor.XoColor(client.get_string('/desktop/sugar/user/color'))
@@ -801,20 +809,20 @@ class GsmDeviceView(TrayIcon):
         if state is network.DEVICE_STATE_ACTIVATED:
             gsm_state = _GSM_STATE_CONNECTED
             connection = network.find_gsm_connection()
-            if (connection <> None):
+            if connection is not 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() 
+                self._connection_timestamp =  time.time() - connection.get_settings().connection.timestamp
+                self._connection_time_handler = gobject.timeout_add(1000, self.__connection_timecount_cb) 
+                self._update_stats(0, 0)
+                self._update_connectiontime()                
+                self._palette.info_box.show() 
+
         if state is network.DEVICE_STATE_DISCONNECTED:
             gsm_state = _GSM_STATE_DISCONNECTED
+            self._connection_timestamp = 0
+            if self._connection_time_handler is not None:
+                gobject.source_remove(self._connection_time_handler)
+            self._palette.info_box.hide() 
 
         elif state in [network.DEVICE_STATE_UNMANAGED,
                        network.DEVICE_STATE_UNAVAILABLE,
@@ -836,15 +844,21 @@ class GsmDeviceView(TrayIcon):
                                          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
+        self._update_stats(in_bytes, out_bytes)
+
+    def _update_stats(self, in_bytes, out_bytes):
+        in_kbytes = in_bytes / 1024
+        out_kbytes = out_bytes / 1024
+        self._palette._data_label.set_text(_("Data sent %d kb / received %d kb") % (out_kbytes ,in_kbytes))
+
+    def __connection_timecount_cb(self):
+        self._connection_timestamp = self._connection_timestamp + 1
+        self._update_connectiontime()
+	return True
+
+    def _update_connectiontime(self):
+        connection_time = datetime.datetime.fromtimestamp(self._connection_timestamp)
+        self._palette._conn_time_label.set_text(_("Connection time ") + connection_time.strftime('%H : %M : %S'))
     
 class WirelessDeviceObserver(object):
     def __init__(self, device, tray, device_type):
-- 
1.6.2.5

