#!/usr/bin/python

from gtk import *
import string

class RandomBar:
    def __init__(self, table, label, y, percent=0, tag="xx%"):
        lw = GtkLabel(label + " ")
        lw.set_alignment(xa=1, ya=0.5)
        table.attach(lw, 0, 1, y, y+1)

        self.progress = GtkProgressBar()
        table.attach(self.progress, 1, 2, y, y+1)

        self.tag = GtkLabel(" " + tag)
        table.attach(self.tag, 2, 3, y, y+1)

    def update(self, percent=0, tag="xx%"):
        self.progress.set_percentage(percent)
        self.tag.set_text(" " + tag)

    def monitor(self):
        pass

class BatteryBar (RandomBar):
    def __init__(self, table, y):
        RandomBar.__init__(self, table, "Battery", y)

    def monitor(self):
        f = open("/proc/apm", "r")
        line = f.readline()
        f.close()
        fields = string.split(line)
        val = fields[6]
        percent = string.atoi(string.replace(val, "%", "")) / 100.0
        if percent < 0.0: percent = 0.0
        if percent > 1.0: percent = 1.0
        self.update(percent, val)

class WirelessQualityBar (RandomBar):
    def __init__(self, table, y):
        RandomBar.__init__(self, table, "Quality", y)

    def monitor(self):
        f = open("/proc/net/wireless", "r")
        line = f.readline()
        line = f.readline()
        line = f.readline()
        f.close()
        fields = string.split(line)
        val = fields[2]
        val = string.replace(val, ".", "")
        # 92.0 is magic here!
        percent = string.atoi(val) / 92.0
        if percent < 0.0: percent = 0.0
        if percent > 1.0: percent = 1.0
        self.update(percent, val+"/92")

class WirelessLevelBar (RandomBar):
    def __init__(self, table, y):
        RandomBar.__init__(self, table, "Level", y)

    def monitor(self):
        f = open("/proc/net/wireless", "r")
        line = f.readline()
        line = f.readline()
        line = f.readline()
        f.close()
        fields = string.split(line)
        val = fields[3]
        strength = string.atoi(string.replace(val, ".", "")) - 256
        # This will be negative.  We're not sure how to read it.
        # (Normalize to -100 to -20 dBm; we don't get better than
	# -20 even being right next to Tara's mushroom.)
        percent = (strength + 100)/80.0
        if percent < 0.0: percent = 0.0
        if percent > 1.0: percent = 1.0
        self.update(percent, str(strength)+"dBm")

class WirelessNoiseBar (RandomBar):
    def __init__(self, table, y):
        RandomBar.__init__(self, table, "Noise", y)

    def monitor(self):
        f = open("/proc/net/wireless", "r")
        line = f.readline()
        line = f.readline()
        line = f.readline()
        f.close()
        fields = string.split(line)
        val = fields[4]
        strength = string.atoi(string.replace(val, ".", "")) - 256
        # This will be negative.  We're not sure how to read it.
        percent = (strength + 100)/80.0
        if percent < 0.0: percent = 0.0
        if percent > 1.0: percent = 1.0
        self.update(percent, str(strength)+"dBm")

def destroy(*args):
    mainquit()

def monitor_all(bars):
    for bar in bars:
        bar.monitor()
    return TRUE

def make_menu():
    "Return the main menu for the application"
    menu = GtkMenuBar()
    file = GtkMenu()
    file.set_title("File")
    item = GtkMenuItem(label="Exit")
    item.connect("activate", destroy)
    file.append(item)
    item = GtkMenuItem(label="File")
    item.set_submenu(file)
    menu.append(item)
    return menu

def make_table():
    table = GtkTable(rows=4, cols=3)
    bars = []
    bars.append(BatteryBar(table, 0))
    bars.append(WirelessQualityBar(table, 1))
    bars.append(WirelessLevelBar(table, 2))
    bars.append(WirelessNoiseBar(table, 3))
    return table, bars

def make_main():
    "Return the application window"
    win = GtkWindow(type=WINDOW_TOPLEVEL)
    win.connect("destroy", destroy)

    box = GtkVBox()
    box.pack_start(make_menu(), expand=FALSE)
    table, bars = make_table()
    box.pack_start(table, fill=FALSE)
    
    win.add(box)
    return win, bars

win, bars = make_main()
win.show_all()
timeout_add(100, monitor_all, bars)
monitor_all(bars)
mainloop()

