CODE:
#!/usr/bin/python # -*- coding: utf-8 -*- ”’东南大学 PPPoE 托盘程序 ”’ import gtk import os import gobject import pynotify import datetime # OK def makeMenu(event_button, event_time, icon): menu = gtk.Menu() connetTitle=gtk.STOCK_CONNECT global gIcon if gIcon.pppoe_status!=”Stopped”: connetTitle=gtk.STOCK_DISCONNECT itemConnect = gtk.ImageMenuItem(connetTitle) itemConnect.show() itemConnect.connect(”activate”, connect,menu) menu.append(itemConnect) # connect SEU itemSEU = gtk.ImageMenuItem(gtk.STOCK_CONNECT) label = itemSEU.get_children()[0] label=label.set_label(”连接内网”) itemSEU.show() itemSEU.connect(”activate”, connectSEU, menu) menu.append(itemSEU) # connect NJXY itemNJXY = gtk.ImageMenuItem(gtk.STOCK_CONNECT) label = itemNJXY.get_children()[0] label=label.set_label(”连接外网”) itemNJXY.show() itemNJXY.connect(”activate”, connectNJXY, menu) menu.append(itemNJXY) # about itemAbout = gtk.ImageMenuItem(gtk.STOCK_ABOUT) itemAbout.show() itemAbout.connect(”activate”, about,menu) menu.append(itemAbout) # quit itemQuit = gtk.ImageMenuItem(gtk.STOCK_QUIT) itemQuit.show() itemQuit.connect(”activate”, quit,menu) menu.append(itemQuit) # pop menu.popup(None, None, gtk.status_icon_position_menu, event_button, event_time, icon) # OK def on_right_click(icon, event_button, event_time): makeMenu(event_button, event_time, icon) # OK def quit(self,event): gtk.main_quit() # OK def connect(self,event): if gIcon.pppoe_status != ‘Stopped’: start = os.popen(’gksu poff -a’) else: lastStatus = ‘SEU’ pppoe(’dsl-provider’) # OK def connectSEU(self, event): global lastStatus lastStatus = ‘SEU’ pppoe(’seu’) # OK def connectNJXY(self, event): global preTime,lastStatus lastStatus = ‘NJXY’ pppoe(’njxy’) preTime = datetime.datetime.now() # OK def pppoe(arg): start=os.popen(”gksu poff -a && gksu pon “+arg) start.readline() start.close() # OK def on_press(self): global gIcon showNotify(update(gIcon)) return True # OK def about(self,event): about=”’About: Author: Xu Yuan Modify: PT Email: xuyuan.cn@gmail.com pt0079@gmail.com”’ showNotify(about) # OK def Init(parent=None): icon = gtk.status_icon_new_from_stock(”Running.png”) icon.connect(’popup-menu’, on_right_click) icon.set_tooltip(”control the PPPoE”) icon.connect(”activate”,on_press) icon.pppoe_status=”Running” return icon # OK def update(icon): global lastStatus,curTime,preTime icon.pppoe_status = GetPPPoEStatus() if icon.pppoe_status!=’Stopped’: if lastStatus != ‘SEU’: curTime = datetime.datetime.now() delTime = (curTime - preTime).seconds/60.0 info = ‘连接正常’ + ‘\n’ + ‘已连接外网%f分钟’%delTime info += ‘已烧%f 元RMB’%(delTime*0.01) else: info = ‘连接正常\n校园网==>免费!’ else: info = ‘网络连接断开’ #tooltip icon.set_tooltip(”东南大学 PPPoE\n”+info) #change the icon filename=icon.pppoe_status+”.png” icon.set_from_file(filename) return info # OK def showNotify(info): # pop tip pynotify.init(”东南大学 PPPoE”) n=pynotify.Notification(”东南大学 PPPoE”,info) n.set_timeout(5000) n.show() return # OK def GetPPPoEStatus(): s=os.popen(”/sbin/ifconfig -a | grep ppp0″) r=s.readline() if r==”": result=”Stopped” else: result=”Running” s.close() return result if __name__ == ‘__main__’: global lastStatus,gIcon,gTimer,preTime,curTime preTime = datetime.datetime.now() #在我的机器上默认的PPPoE连接是@seu的 lastStatus = ‘SEU’ gIcon=Init() showNotify(update(gIcon)) gTimer=gobject.timeout_add(2000,update,gIcon) gtk.main() |