#!/usr/bin/env python2.3 import ConfigParser import commands import gobject import gtk import os import string import egg.trayicon as trayicon import socket from time import time config = ConfigParser.ConfigParser() config.read(os.path.expanduser('~/.networkstatusrc')) try: connecting = config.get("icons", "connecting") online = config.get("icons", "online") offline = config.get("icons", "offline") error = config.get("icons", "error") except: connecting = "/usr/share/gajim/data/iconsets/stellar/16x16/connecting.gif" online = "/usr/share/gajim/data/iconsets/stellar/16x16/online.png" offline = "/usr/share/gajim/data/iconsets/stellar/16x16/offline.png" error = "/usr/share/gajim/data/iconsets/stellar/16x16/not_in_roster.png" # -2 - error # -1 - offline # 0 - connecting # 1 - online state = 0 last_pong = time() img_map = error, offline, connecting, online img = gtk.Image() img.set_from_file(img_map[state + 2]) eb = gtk.EventBox() eb.add(img) t = trayicon.TrayIcon('Network Status') t.add(eb) t.show_all() SERVER_ADDR = 'kot' SERVER_PORT = 10003 SELF_PORT = 10002 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(('', SELF_PORT)) s.setblocking(0) def send(): s.sendto('ala', (SERVER_ADDR, SERVER_PORT)) def read(): global state, last_pong result = state try: while 1: result = int(s.recvfrom(20)[0]) last_pong = time() finally: return result def handler(): global state, last_pong if time() - last_pong > 5: state = -2 img.set_from_file(img_map[state + 2]) result = read() if state != result: state = result img.set_from_file(img_map[state + 2]) send() gobject.timeout_add(500, handler) gobject.timeout_add(500, handler) gtk.main()