Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101908745
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: LINUX

2008-04-06 13:36:27

作者: iSPanle 出自:
之前Linux版两牛人写了bras的拨号托盘程序,仰仗ing

然后就手痒改写了xuyuan牛人的程序,改成宿舍宽带使用

change notes:
1.将普通的菜单改成代STOCK ICON的菜单

2.记录连接外网的时间,计算出本次上外网的花费[按6毛钱每小时算]

3.方便的选择内外网切换

学到的东西:
1.如何更该一个带STOCK ICON的ImageMenuItem的标签

2.用datetime module测算出经过的时间
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()

程序打包
阅读(977) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~