#    PyNetMony Netmonitor for S60 3rd Edition phones
#
#    Copyright (C) 2007  Carsten Knuetter
#
#    email: netmonitor.s80@o2online.de
#
#    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation;
#     either version 3 of the License, or (at your option) any later version.
#    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
#    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#     You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.


import appuifw
import e32
import location
import sysinfo
import time
from graphics import *


index=0
running=1
sized,offset = appuifw.app.layout(appuifw.EMainPane)
img=Image.new((800,352))
img.rectangle((0,0)+sized,0xff0000,fill=(0,0,60))
img.text((50,20), u'PyNetMony 0.1.6',fill=(255,255,255), font=u"normal")
img.text((12,40), u'(c) 2007 by Carsten Knütter',fill=(255,255,255), font=u"normal")
img.text((10,70), u'netmoitor.s80@o2online.de',fill=(255,255,255), font=u"normal")
img.text((5,100), u'This program comes with',fill=(255,255,255), font=u"normal")
img.text((5,120), u'ABSOLUTELY NO WARRANTY',fill=(255,255,255), font=u"normal")
img.text((5,150), u'This is free software, and you',fill=(255,255,255), font=u"normal")
img.text((5,170), u'are welcome to redistribute',fill=(255,255,255), font=u"normal")
img.text((5,190), u'it under certain conditions',fill=(255,255,255), font=u"normal")
#img.text((2,210), u'certain conditions',fill=(255,255,255), font=u"normal")
img.text((35,230), u'Press Options to start... ',fill=(255,255,255), font=u"normal")

def main_menu_setup():
	appuifw.app.menu = [(u"Netmonitor", netmonitor),(u"Settings", settings)]
	
def settings():
	global index
	L = [u'Light on']
	index = appuifw.multi_selection_list(L , style='checkmark', search_field=0)
	handle_redraw(())
	
def netmonitor():
	global index
	imei=sysinfo.imei()
	ver = sysinfo.os_version()
	while running:
		size,offset = appuifw.app.layout(appuifw.EMainPane)
		#clear(color=(0,0,60))
		#img.line((20,20,20,120),0xff00ee)
		#img.rectangle((0,0,240,235),0xff0000,fill=(0,0,60))
		#img.rectangle((0,0)+size,0xff0000,fill=(0,0,60))
		#img.point((50.,150.),0xff0000,width=40)
		#img.ellipse((100,150,150,180),0x0000ff)
		#img.text((100,80), u'hello')
		img.rectangle((0,0)+size,0xff0000,fill=(0,0,60))
		rnc="n/a"
		gsmloc = location.gsm_location()
		if gsmloc is None:
			mcc = mnc = lac = cid = rnc = cid = "n/a"
		else:
			(mcc, mnc, lac, cid) = gsmloc
			if (mnc == 7) and (mcc == 262) and (cid > 65535):
				cidhex=hex(cid)
				rnc=int(cidhex[2:4],16)
				cid=int(cidhex[len(cidhex)-4:len(cidhex)],16)
		bars = sysinfo.signal_bars()
		rxl = sysinfo.signal_dbm()
		batty = sysinfo.battery()
		if index == (0,):
			e32.reset_inactivity()
		ram = sysinfo.free_ram()/1024
		totram = sysinfo.total_ram()/1024
		x = appuifw.app.layout(appuifw.EScreen)
		img.text((20,20), u'CLK: '+unicode(time.strftime("%H:%M:%S")),fill=(255,255,255), font=u"normal")
		img.text((20,40), u'CID: '+unicode(cid),fill=(255,255,255), font=u"normal")
		img.text((20,60), u'LAC: '+unicode(lac),fill=(255,255,255), font=u"normal")
		img.text((20,100), u'NET: '+unicode(mcc)+"   "+unicode(mnc),fill=(255,255,255), font=u"normal")
		img.text((20,80), u'RNC: '+unicode(rnc),fill=(255,255,255), font=u"normal")
		img.text((20,120), u'RXL: -'+unicode(rxl)+u" dBm ("+unicode(bars)+u")",fill=(255,255,255), font=u"normal")
		img.text((20,140), u'BAT: '+unicode(batty)+u" %",fill=(255,255,255), font=u"normal")
		#img.text((20,160), u'RAM: '+unicode(ram)+u" KB",fill=(255,255,255), font=u"normal")
		img.text((20,160), u'RAM: '+unicode(ram)+u" / "+unicode(totram)+u" KB",fill=(255,255,255), font=u"normal")
		img.text((20,180), u'VER: '+unicode(ver),fill=(255,255,255), font=u"normal")
		img.text((20,200), u'IMEI: '+imei,fill=(255,255,255), font=u"normal")
		img.text((20,220), u'RES: '+unicode(x),fill=(255,255,255), font=u"normal")
		#img.text((20,220), unicode(index),fill=(255,255,255), font=u"normal")
		handle_redraw(())
		e32.ao_sleep(0.5)
		#e32.ao_yield()
		
def handle_redraw(rect):
	canvas.blit(img)
	
canvas=appuifw.Canvas(event_callback=None, redraw_callback=handle_redraw)

def exit_key_handler():
	global script_lock
	global running
	script_lock.signal()
	#appuifw.app.set_exit()
	running=0
	
appuifw.app.screen='normal'

script_lock = e32.Ao_lock()

appuifw.app.title = u"PyNetMony"
appuifw.app.body=canvas

#appuifw.app.body = appuifw.Text(u"Press Options button below ...")

main_menu_setup()
appuifw.app.exit_key_handler = exit_key_handler
script_lock.wait()
