#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gtk
import hildon
import threading
import time
import sys
import gn_cellinfo
import gn_battery

class ReadSensors(threading.Thread):
	global cell_label
	global battery_label
	global battery_progress
	global ss_progress
	global subwin
	
	def Cellular(self):

		regstatus = gn_cellinfo.RegStatus()
		signalstrength = gn_cellinfo.SignalStrength()

		if (regstatus[0] == 0): status = 'home'
		elif (regstatus[0] == 1): status = 'roam'
		elif (regstatus[0] == 2): status = 'roam_blink'
		elif (regstatus[0] == 3): status = 'no serv.'
		elif (regstatus[0] == 4): status = 'no serv. search'
		elif (regstatus[0] == 5): status = 'no serv. no search'
		elif (regstatus[0] == 6): status = 'no sim'
		elif (regstatus[0] == 8): status = 'power off'
		elif (regstatus[0] == 9): status = 'nsps'
		elif (regstatus[0] == 10): status = 'nsps no cover.'
		elif (regstatus[0] == 10): status = 'sim rejected'
		else: status = 'unknown'

		if (regstatus[5] == 0): network_type = 'home'
		elif (regstatus[5] == 1): network_type = 'preferred'
		elif (regstatus[5] == 2): network_type = 'forbidden'
		elif (regstatus[5] == 2): network_type = 'other'
		else: network_type = 'no network'

		services = ""
		if (regstatus[6] & 0x02): services += 'CS '
		if (regstatus[6] & 0x01): services += 'GPRS '
		if (regstatus[6] & 0x04): services += 'EGPRS '
		if (regstatus[6] & 0x08): services += 'HSDPA '
		if (regstatus[6] & 0x10): services += 'HSUPA'

		fraction = 1.0 / 100 * signalstrength[0]

		ss_progress.set_fraction(fraction)
		text = " - %d dBm" % signalstrength[1]
		ss_progress.set_text(text)

		cell_label[10].set_text("%i" % regstatus[4])
		cell_label[11].set_text("%i" % regstatus[3])
		cell_label[12].set_text("%i" % regstatus[1])
		cell_label[13].set_text("%i" % regstatus[2])
		cell_label[14].set_text(status)
		cell_label[15].set_text(network_type)
		cell_label[16].set_text("%i" % regstatus[7])
		cell_label[17].set_text(services)

	def Battery(self):

		if (gn_battery.BatteryPresent() == 0): present = "not present"
		else: present = "present"

		if (gn_battery.BatteryRechargeable() == 0): rechargeable = "not rechargeable"
		else: rechargeable = "rechargeable"

		battery_label[2].set_text("%s" % present)
		battery_label[3].set_text("%s" % rechargeable)

		battery_label[12].set_text("%s" % gn_battery.BatteryLevelState())

		charging = gn_battery.BatteryCharging()
		discharging = gn_battery.BatteryDischarging()

		if (charging and discharging): charging_state = "charging+discharging"
		elif (charging): charging_state = "charging"
		elif (discharging): charging_state = "discharging"
		else: charging_state = "unknown"
		
		battery_label[13].set_text("%s" % charging_state)

		current = gn_battery.BatteryReportingCurrent()
		design = gn_battery.BatteryReportingDesign()
		unit = gn_battery.BatteryReportingUnit()

		if (charging):
			battery_label[22].set_text("(%i %s)" % (current, unit))
			battery_progress[1].pulse()
			battery_progress[1].set_pulse_step(0.1)
			battery_progress[1].set_text("charging...")
		else:
			battery_label[22].set_text("%i %s" % (current, unit))
			battery_progress[1].set_fraction(1.0 / design * current)
			text = " %d %%" % (100.0 / design * current)
			battery_progress[1].set_text(text)

		battery_label[23].set_text("%i %s" % (design, unit))
		
		current = gn_battery.BatteryVoltageCurrent()
		design_v = gn_battery.BatteryVoltageDesign()
		unit_v = gn_battery.BatteryVoltageUnit()

		battery_label[32].set_text("%i %s" % (current, unit_v))
		battery_label[33].set_text("%i %s" % (design_v, unit_v))

		battery_progress[2].set_fraction(1.0 / design_v * current)
		text = " %d %%" % (100.0 / design_v * current)
		battery_progress[2].set_text(text)

		amps_full = gn_battery.BatteryReportingFull()
		level_full = gn_battery.BatteryLevelFull()
		level_unit = gn_battery.BatteryLevelUnit()

		battery_label[42].set_text("%i %s" % (amps_full, unit))
		battery_label[43].set_text("%i %s" % (level_full, level_unit))

		battery_progress[3].set_fraction(1.0 / design * amps_full)
		text = " %d %%" % (100.0 / design * amps_full)
		battery_progress[3].set_text(text)

	stopthread = threading.Event()

	def run(self):
		proc = 0
		page = None
		while not self.stopthread.isSet() :
			gtk.gdk.threads_enter()
			if (subwin == False or subwin.get_stack() == None):
				if (page != None):
					proc = 0
					page = None
				if (proc == 0):
					cell_label[0].set_text("%s" % time.asctime())
					self.Cellular()
			else:
				title = subwin.get_title()
				if (page != title):
					proc = 0
					page = title
				if (title == "NetMon Battery Status"):
					if (proc == 0): self.Battery()
			gtk.gdk.threads_leave()
			time.sleep(0.2)
			if (proc > 15): proc = 0
			else: proc = proc + 1

	def stop(self):
		self.stopthread.set()

def battery_window(nope):
	global subwin
	global battery_label
	global battery_progress
	
	subwin = hildon.StackableWindow()
	subwin.set_title("NetMon Battery Status")

	battery_label = []
	battery_progress = []

	for i in range(1, 50):
		battery_label.append(None)
		battery_progress.append(None)

	battery_label[1] = gtk.Label("Battery Presence:")
	battery_label[2] = gtk.Label("?")
	battery_label[3] = gtk.Label("?")

	battery_label[11] = gtk.Label("Charging Status:")
	battery_label[12] = gtk.Label("?")
	battery_label[13] = gtk.Label("?")

	battery_label[21] = gtk.Label("Capacity:")
	battery_label[22] = gtk.Label("?")
	battery_label[23] = gtk.Label("?")
	
	battery_label[31] = gtk.Label("Voltage:")
	battery_label[32] = gtk.Label("?")
	battery_label[33] = gtk.Label("?")

	battery_label[41] = gtk.Label("Last Full:")
	battery_label[42] = gtk.Label("?")
	battery_label[43] = gtk.Label("?")

	for i in range(1, 20, 10):
		battery_label[i].set_alignment(0, 0)
		battery_label[i+1].set_alignment(1, 0)
		battery_label[i+2].set_alignment(1, 0)

	for i in range(21, 50, 10):
		battery_label[i].set_alignment(0, 1)
		battery_label[i+1].set_alignment(1, 1)
		battery_label[i+2].set_alignment(1, 1)

	

	battery_progress[1] = gtk.ProgressBar(adjustment=None)
	battery_progress[1].set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
	battery_progress[1].set_text("acquiring battery info...")

	battery_progress[2] = gtk.ProgressBar(adjustment=None)
	battery_progress[2].set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
	battery_progress[2].set_text("acquiring battery info...")

	battery_progress[3] = gtk.ProgressBar(adjustment=None)
	battery_progress[3].set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
	battery_progress[3].set_text("acquiring battery info...")
	
	table = gtk.Table(12,20,True)

	table.attach(battery_label[1], 1, 7, 1, 2)
	table.attach(battery_label[2], 7, 12, 1, 2)
	table.attach(battery_label[3], 12, 19, 1, 2)

	table.attach(battery_label[11], 1, 7, 2, 3)
	table.attach(battery_label[12], 7, 12, 2, 3)
	table.attach(battery_label[13], 12, 19, 2, 3)

	table.attach(battery_label[21], 1, 7, 3, 4)
	table.attach(battery_label[22], 7, 12, 3, 4)
	table.attach(battery_label[23], 12, 19, 3, 4)

	table.attach(battery_progress[1], 1, 19, 4, 6)
	
	table.attach(battery_label[31], 1, 7, 6, 7)
	table.attach(battery_label[32], 7, 12, 6, 7)
	table.attach(battery_label[33], 12, 19, 6, 7)
	
	table.attach(battery_progress[2], 1, 19, 7, 9)

	table.attach(battery_label[41], 1, 7, 9, 10)
	table.attach(battery_label[42], 7, 12, 9, 10)
	table.attach(battery_label[43], 12, 19, 9, 10)

	table.attach(battery_progress[3], 1, 19, 10, 12)
	#table.set_col_spacing(1, 1)

	subwin.add(table)

	subwin.show_all()

def about_window(nope):
	global subwin

	version = "0.0.2"
	
	subwin = hildon.StackableWindow()
	subwin.set_title("NetMon About")
	
	text = "NetMon Cellular Network Monitor\nVersion %s\n(c) Copyright by spag (maemo@golwen.net) 2010\nUse at your own risk!" % version
	label = gtk.Label(text)
	subwin.add(label)
	subwin.show_all()

def create_menu():
	menu = hildon.AppMenu()

	button1 = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
	button1.set_label("Battery")
	button1.connect("clicked", battery_window)
	menu.append(button1)

	button2 = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
	button2.set_label("About")
	button2.connect("clicked", about_window)
	menu.append(button2)
	
	menu.show_all()

	return menu


def main_quit(obj, sensor_thread):
	sensor_thread.stop()
	gtk.main_quit()

def main():
	global ss_absolute
	global ss_progress
	global cell_label
	global subwin

	subwin = False
	program_version = "0.0.2"
	cell_label = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	
	prog = hildon.Program.get_instance()
	
	win = hildon.StackableWindow()
	win.set_title("NetMon Cell Info")

	cell_label[0] = gtk.Label("CELL INFO")
	cell_label[0].set_padding(0, 10)
	ss_progress = gtk.ProgressBar(adjustment=None)
	ss_progress.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
	
	ss_progress.set_text("acquiring signal strength...")
	cell_label[1] = gtk.Label("Signal")
	cell_label[2] = gtk.Label("Mobile Country Code")
	cell_label[3] = gtk.Label("Mobile Network Code")
	cell_label[4] = gtk.Label("Location Area Code")
	cell_label[5] = gtk.Label("Cell ID")
	cell_label[6] = gtk.Label("Network Status")
	cell_label[7] = gtk.Label("Network Type")
	cell_label[8] = gtk.Label("Network Error")
	cell_label[9] = gtk.Label("Supported Services")
	
	cell_label[10] = gtk.Label("?")
	cell_label[11] = gtk.Label("?")
	cell_label[12] = gtk.Label("?")
	cell_label[13] = gtk.Label("?")
	cell_label[14] = gtk.Label("?")
	cell_label[15] = gtk.Label("?")
	cell_label[16] = gtk.Label("?")
	cell_label[17] = gtk.Label("?")

	for i in range(2, 10):
		cell_label[i].set_alignment(0, 0.5)
	for i in range(10, 18):
		cell_label[i].set_alignment(1, 0.5)

	table = gtk.Table(7,7,True)
	table.attach(cell_label[2], 1, 4, 0, 1)
	table.attach(cell_label[3], 1, 4, 1, 2)
	table.attach(cell_label[4], 1, 4, 2, 3)
	table.attach(cell_label[5], 1, 4, 3, 4)
	table.attach(cell_label[6], 1, 4, 4, 6)
	table.attach(cell_label[7], 1, 4, 5, 7)
	table.attach(cell_label[8], 1, 4, 6, 9)
	table.attach(cell_label[9], 1, 4, 7, 10)
	
	table.attach(cell_label[10], 4, 6, 0, 1)
	table.attach(cell_label[11], 4, 6, 1, 2)
	table.attach(cell_label[12], 4, 6, 2, 3)
	table.attach(cell_label[13], 4, 6, 3, 4)
	table.attach(cell_label[14], 4, 6, 4, 6)
	table.attach(cell_label[15], 4, 6, 5, 7)
	table.attach(cell_label[16], 4, 6, 6, 9)
	table.attach(cell_label[17], 4, 6, 7, 10)
	table.set_col_spacing(1, 20)
	
	hbox = gtk.HBox(False, 10)
	vbox = gtk.VBox(False, 0)
	hbox.pack_start(cell_label[1], False, False, 0)
	hbox.pack_end(ss_progress, True, True, 0)
	vbox.pack_start(cell_label[0], False, False, 0)
	vbox.pack_start(table, False, False, 0)
	vbox.pack_end(hbox, False, False, 0)
	win.add(vbox)

	menu = create_menu()
	win.set_app_menu(menu)

	sensor_thread = ReadSensors()
	sensor_thread.start()
	
	
	win.connect("destroy", main_quit, sensor_thread)
	
	win.show_all()

	gtk.main()

gtk.gdk.threads_init()
if __name__ == "__main__":
	main()