diff options
author | Bryan Clark <clarkbw@cvs.gnome.org> | 2004-09-08 21:25:55 +0000 |
---|---|---|
committer | Bryan W. Clark <clarkbw@gnome.org> | 2004-09-08 21:25:55 +0000 |
commit | dade73e234d931e6293a6445a82835e5f18c5b99 (patch) | |
tree | 1a2ed6d3bb7ecc4de489d3cdc2ed207e9e5a3beb /examples/python | |
parent | 045981308445ea838488d370be1d24558ecdf690 (diff) | |
download | NetworkManager-dade73e234d931e6293a6445a82835e5f18c5b99.tar.gz |
2004-09-08 Bryan Clark <clarkbw@cvs.gnome.org>
* examples/python/NetworkManager.py:
added CONNECTED, CONNECTING, and DISCONNECTED states
added methods to return number of devices of a single type
* examples/python/systray/network_tray.py:
did some tweaks to get the menu looking near what it is supposed
to look like. Also did a Airo card hack to make it show the
correct AP quality
git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@134 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
Diffstat (limited to 'examples/python')
-rwxr-xr-x | examples/python/NetworkManager.py | 40 | ||||
-rwxr-xr-x | examples/python/systray/network_tray.py | 143 |
2 files changed, 143 insertions, 40 deletions
diff --git a/examples/python/NetworkManager.py b/examples/python/NetworkManager.py index 9deddfd827..36ed0c3ab1 100755 --- a/examples/python/NetworkManager.py +++ b/examples/python/NetworkManager.py @@ -24,6 +24,10 @@ class NetworkManager: WIRED_DEVICE = 1 WIRELESS_DEVICE = 2 + CONNECTED = "connected" + CONNECTING = "connecting" + DISCONNECTED = "disconnected" + NM_SIGNALS = [ "DeviceNoLongerActive", "DeviceNowActive", "DeviceActivating", @@ -107,24 +111,27 @@ class NetworkManager: pass active_device = self.nm_object.getActiveDevice() - active_device_status = self.nm_object.status() if device == active_device: - d["nm.status"] = active_device_status + d["nm.status"] = self.nm_object.status() else: - d["nm.status"] = "not connected" + d["nm.status"] = self.DISCONNECTED + # we already have this device cached, so just update the status if device in self.__devices: for k,v in d.iteritems(): self.__devices[device][k] = v - return self.__devices[device] + # it's a new device so get the info from HAL else: hal = self._get_hal_info(d["nm.udi"]) for k,v in hal.iteritems(): d[k] = v self.__devices[device] = d - return self.__devices[device] - except: + + return self.__devices[device] + + except Error, e: + print e return None """ @@ -148,17 +155,30 @@ class NetworkManager: def get_all_devices(self): return self.__devices.values() - def _has_type_device (self, type): + def has_type_device (self, type): for device in self.get_devices(): if device["nm.type"] == type: return True - return False + return False + def number_device_types(self, type): + count = 0 + for device in self.get_devices(): + if device["nm.type"] == type: + count = count + 1 + return count + + def number_wired_devices(self): + return self.number_device_types(self.WIRED_DEVICE) + + def number_wireless_devices(self): + return self.number_device_types(self.WIRELESS_DEVICE) + def has_wired_device(self): - return self._has_type_device(WIRED_DEVICE) + return self.has_type_device(self.WIRED_DEVICE) def has_wireless_device(self): - return self._has_type_device(WIRELESS_DEVICE) + return self.has_type_device(self.WIRELESS_DEVICE) def _get_hal_info(self, udi): hal_devices = self._hal_manager.FindDeviceStringMatch("info.udi", diff --git a/examples/python/systray/network_tray.py b/examples/python/systray/network_tray.py index bedbee47d0..db766bda7c 100755 --- a/examples/python/systray/network_tray.py +++ b/examples/python/systray/network_tray.py @@ -30,10 +30,9 @@ class network_tray: def _add_label_item(self, label): menuitem = gtk.MenuItem() - menuitem.set_right_justified(gtk.TRUE) menuitem.set_sensitive(gtk.FALSE) gtklabel = gtk.Label() - gtklabel.set_justify(gtk.JUSTIFY_RIGHT) + gtklabel.set_markup("<span size=\"small\" foreground=\"#aaaaaa\" weight=\"ultralight\">%s</span>" % label) gtklabel.set_selectable(gtk.FALSE) hbox = gtk.HBox(homogeneous=gtk.TRUE, spacing=6) @@ -41,26 +40,48 @@ class network_tray: menuitem.add(hbox) self._menu.append(menuitem) menuitem.show_all() - - def _add_device_menu_item(self, device, active=gtk.FALSE): + + def _add_other_wireless_item(self): menuitem = gtk.MenuItem() + menuitem.set_sensitive(gtk.TRUE) + gtklabel = gtk.Label() + gtklabel.set_alignment(0,0) + gtklabel.set_label("Other Wireless Networks...") + hbox = gtk.HBox(homogeneous=gtk.TRUE, spacing=6) + hbox.pack_end(gtklabel,expand=gtk.TRUE, fill=gtk.TRUE, padding=6) + menuitem.add(hbox) + tt = "Add a wireless network that does not appear on the list" + self._tooltips.set_tip(menuitem,tt) + self._menu.append(menuitem) + menuitem.show_all() + + def _add_device_menu_item(self, device): + if not self._is_wireless(device): + menuitem = gtk.RadioMenuItem(group=self.__radio_group) + if self._is_active(device): + menuitem.set_active(1) + else: + menuitem = gtk.MenuItem() + hbox = gtk.HBox(homogeneous=gtk.FALSE, spacing=6) - hbox.pack_start(self._get_icon(device), expand=gtk.FALSE, fill=gtk.FALSE) + hbox.pack_start(self._get_icon(device), expand=gtk.FALSE, fill=gtk.FALSE, padding=6) label = gtk.Label() label.set_justify(gtk.JUSTIFY_LEFT) - if active == gtk.TRUE: - label.set_markup("<span weight=\"bold\">%s</span>" % device["info.product"]) - else: - label.set_text(device["info.product"]) - hbox.pack_start(label, expand=gtk.FALSE, fill=gtk.FALSE) + label.set_text(self._get_device_name(device)) + hbox.pack_start(label, expand=gtk.FALSE, fill=gtk.FALSE, padding=6) menuitem.add(hbox) hbox.show() self._menu.append(menuitem) + tt = "IP: %d\nProduct Name: %s\nVendor: %s\nDevice Name: %s" % (device["nm.ip4"], device["pci.product"], device["info.vendor"], device["nm.name"] ) + self._tooltips.set_tip(menuitem,tt) menuitem.show_all() - - def _add_network_menu_item(self, network): - menuitem = gtk.MenuItem() + + def _add_network_menu_item(self, device, network, active_network): + menuitem = gtk.RadioMenuItem(group=self.__radio_group) menuitem.set_right_justified(gtk.FALSE) + if active_network == gtk.TRUE: + menuitem.set_active(1) + hbox = gtk.HBox(homogeneous=gtk.FALSE, spacing=6) menuitem.add(hbox) label = gtk.Label(network["name"]) @@ -68,20 +89,70 @@ class network_tray: label.show() hbox.pack_start(label,expand=gtk.TRUE, fill=gtk.TRUE) progress = gtk.ProgressBar() - progress.set_fraction(network["quality"]) + progress.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT) + q = self._get_quality(device, network) + progress.set_fraction(q) +# progress.set_text("%s %%" % int(q*100)) progress.show() hbox.pack_start(progress, expand=gtk.FALSE, fill=gtk.FALSE) icon = self._get_encrypted_icon() if network["encrypted"] == 1: icon.hide() + hbox.pack_start(icon,expand=gtk.FALSE, fill=gtk.FALSE) else: icon.show() - hbox.pack_start(icon,expand=gtk.FALSE, fill=gtk.FALSE) + hbox.show() self._menu.append(menuitem) + tt = "Name: %s\nEncrypted: %d\nRate: %d\nFrequency: %f\nAddress: %s\nQuality: %d" % (network['name'], network['encrypted'], network['rate'],network['frequency'], network['address'], network['quality']) + self._tooltips.set_tip(menuitem,tt) menuitem.show() + + def _get_quality(self, device, network): + if network["quality"] == 0: + proc = "/proc/driver/aironet/%s/BSSList" % device["net.interface"] + import fileinput + for line in fileinput.input(proc): + dev_info = line.split() + if network["name"] in dev_info: + fileinput.close() + try: + qual = float(dev_info[4]) + q = float(qual / 100) + return q + except ValueError: + return 0.0 + fileinput.close() + else: + return float(network["quality"]) + return 0.0 + + def _get_device_name(self, device): + if self._is_wireless(device): + if self._nm.number_wireless_devices() > 1: + return device["info.product"] + else: + return "Wireless Network" + else: + if self._nm.number_wired_devices() > 1: + return device["info.product"] + else: + return "Wired Network" + def _is_wireless(self,dev): + if dev["nm.type"] == self._nm.WIRELESS_DEVICE: + return gtk.TRUE + return gtk.FALSE + def _is_active(self, dev): + return dev["nm.status"] != self._nm.DISCONNECTED + + def _number_wired_devices(self, devices): + return self._number_x_devices(devices, self._nm.WIRED_DEVICE) + + def _number_wireless_devices(self, devices): + return self._number_x_devices(devices, self._nm.WIRELESS_DEVICE) + def _network_event(self, interface, signal_name, service, path, message): @@ -91,25 +162,34 @@ class network_tray: devices = self._nm.get_devices() active_device = self._nm.get_active_device() tt = "" - self._add_label_item("Network Connections") + + wireless = gtk.FALSE for device in devices: - if device == active_device: - active = gtk.TRUE - else: - active = gtk.FALSE - self._add_device_menu_item(device, active) + if self._is_wireless(device) and wireless == gtk.FALSE: + wireless = gtk.TRUE + self._add_separator_item() + self._add_label_item("Wireless Networks") + else: + self._add_device_menu_item(device) tt = "%s%s [%s]\n"%(tt,device["info.product"],device["nm.status"]) - self._tooltips.set_tip(self._top_level_menu,tt) + self._tooltips.set_tip(self._top_level_menu,tt) - if active_device["nm.type"] == self._nm.WIRELESS_DEVICE: - self._add_separator_item() - self._add_label_item("Wireless Networks") - for name, network in active_device["nm.networks"].iteritems(): - self._add_network_menu_item(network) - - + if self._is_wireless(device): + for name, network in active_device["nm.networks"].iteritems(): + try: + if active_device["nm.active_network"] == name: + active_network = gtk.TRUE + else: + active_network = gtk.FALSE + except: + active_network = gtk.FALSE + self._add_network_menu_item(device,network,active_network) + + if wireless == gtk.TRUE: + self._add_other_wireless_item() + self._current_icon = self._get_icon(active_device) self._current_icon.show() @@ -170,7 +250,10 @@ class network_tray: self._top_level_menu.add(self._current_icon) self._menu_bar.show() self._top_level_menu.show() - self._menu.show() + self._menu.show() + + self.__radio_group = gtk.RadioMenuItem() + if __name__ == "__main__": nt = network_tray() |