4
#Copyright (C) 2007 Helder Fraga
6
#This program is free software; you can redistribute it and/or
7
#modify it under the terms of the GNU General Public License
8
#as published by the Free Software Foundation; either version 2
9
#of the License, or (at your option) any later version.
11
#This program is distributed in the hope that it will be useful,
12
#but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
#GNU General Public License for more details.
16
#You should have received a copy of the GNU General Public License
17
#along with this program; if not, write to the Free Software
18
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
from screenlets import Screenlet
22
from screenlets.options import FloatOption, BoolOption, StringOption, FontOption, ColorOption, IntOption
34
class NetmonitorScreenlet(screenlets.Screenlet):
35
"""A Screenlet that displays net traffic , and month totals"""
37
# default meta-info for Screenlets
38
__name__ = 'NetmonitorScreenlet'
40
__author__ = 'Helder Fraga aka Whise'
62
frame_color = (1, 1, 1, 1)
63
color_text = (0, 0, 0, 0.6)
65
mypath = argv[0][:argv[0].find('NetmonitorScreenlet.py')].strip()
70
def __init__(self, **keyword_args):
71
screenlets.Screenlet.__init__(self, width=180, height=110, uses_theme=True,ask_on_option_override=False, **keyword_args)
73
self.theme_name = "default"
74
self.add_menuitem("toggle", "Toggle mini view")
75
self.add_default_menuitems()
76
self.update_interval = self.update_interval
77
#self.disable_updates = True
79
self.add_options_group('Options', '')
80
self.add_option(BoolOption('Options', 'show_frame',bool(self.show_frame), 'Show frame','Show frame'))
81
self.add_option(BoolOption('Options', 'show_txt',bool(self.show_txt), 'Show In Out text','Show Text'))
82
self.add_option(ColorOption('Options','frame_color',
83
self.frame_color, 'Frame color',
85
self.add_option(ColorOption('Options','color_text',
86
self.color_text, 'Text color', ''))
87
self.add_option(FontOption('Options','font',
88
self.font, 'Text Font',
90
self.add_option(StringOption('Options', 'dev', self.dev,'Select Device', '',choices = self.dev_choices))
91
self.add_option(StringOption('Options', 'download_total', self.download_total,'Download total this month', '',))
92
self.add_option(StringOption('Options', 'upload_total', self.upload_total,'Upload total this month', '',))
93
self.add_option(BoolOption('Options', 'mini', self.mini,'Use mini mode', '',))
95
self.__timeout = gobject.timeout_add(1000, self.update)
96
self.gconf_client = gconf.client_get_default()
97
self.gconf_path = "/apps/screenlets/" + self.__name__
98
self.gconf_client.notify_add(self.gconf_path, self.config_event)
99
self.gconf_key = "/apps/screenlets/" + self.__name__ + "/month"#
100
print self.gconf_client.get_int(self.gconf_key)
101
#self.gconf_client.notify_add(self.gconf_key, self.update)
103
def config_event(self):
106
def get_devices(self):
107
if self.dev_choices != []: del self.dev_choices
108
self.dev_choices = []
109
data = commands.getoutput("cat /proc/net/dev |grep :")
110
for f in data.split(' '):
111
if f != None and f.find(':') != -1 and not f.startswith('0'):
112
self.dev_choices.append(str(f).split(':')[0])
116
def __setattr__(self, name, value):
117
# call Screenlet.__setattr__ in baseclass (ESSENTIAL!!!!)
118
# if name == 'download_total' or name == 'upload_total' :
120
if name != 'xo' or name != 'download' or name != 'upload' or name != 'unit1' or name != 'unit2' or name != 'u2' or name != 'u1' or name != 'n2' or name != 'n1' or name != 'p_layout' or name != 'download_total' or name != 'upload_total':
122
screenlets.Screenlet.__setattr__(self, name, value)
133
def menuitem_callback(self, widget, id):
134
screenlets.Screenlet.menuitem_callback(self, widget, id)
137
self.mini = not self.mini
139
def on_mouse_down(self,event):
140
if event.button == 3:
145
data = commands.getoutput("cat /proc/net/dev")
146
data = data[data.find(self.dev + ":")+5:]
147
self.u1 = float( data.split()[0] )
148
self.n1 = float( data.split()[8] )
149
self.month = self.gconf_client.get_int(self.gconf_key)
151
print 'no month saving new month'
152
self.month = datetime.datetime.now().month
153
self.gconf_client.set_int(self.gconf_key, int(self.month))
155
self.download = str(int(self.u1 -self.u2)/1024)
156
self.upload = str(int(self.n1 -self.n2)/1024)
158
if self.u2 == 0 : self.download = '0'
159
if self.n2 == 0 : self.upload = '0'
163
if str(self.month) == str(datetime.datetime.now().month):
165
self.download_total = int(self.download_total) + (int(self.download))
166
self.upload_total = int(self.upload_total) + (int(self.upload))
168
print 'diferent month'
170
self.download_total = 0
171
self.upload_total = 0
172
self.month = datetime.datetime.now().month
173
self.gconf_client.set_int(self.gconf_key, int(self.month))
175
if int(self.download_total) >= 1024:
177
self.dlt = float(int(self.download_total)) / 1024
180
elif int(self.download_total) >= (1024*1024):
182
self.dlt = (float(int(self.download_total)) / 1024)/1024
185
elif int(self.download_total) >= (1024*1024*1024):
187
self.dlt = ((float(int(self.download_total)) / 1024)/1024)/1024
192
self.dlt = float(int(self.download_total))
194
if int(self.upload_total) >= 1024:
196
self.ult = float(int(self.upload_total)) / 1024
199
elif int(self.upload_total) >= (1024*1024):
201
self.ult = (float(int(self.upload_total)) / 1024)/1024
204
elif int(self.upload_total) >= (1024*1024*1024):
206
self.ult = ((float(int(self.upload_total)) / 1024)/1024)/1024
211
self.ult = float(int(self.upload_total))
214
self.ult = str(self.ult)
215
self.dlt = str(self.dlt)
217
if self.ult.find('.') != 0 :
218
self.ult = self.ult[:self.ult.find('.')+2 ].strip()
219
if self.dlt.find('.') != 0:
220
self.dlt = self.dlt[:self.dlt.find('.')+2 ].strip()
221
print 'download total - '+ self.dlt
222
print 'upload total - ' +self.ult
227
screenlets.Screenlet.__setattr__(self, 'download_total', str(self.download_total))
229
screenlets.Screenlet.__setattr__(self, 'upload_total', str(self.upload_total))
230
self.session.backend.flush()
238
def on_draw(self, ctx):
241
ctx.scale(self.scale, self.scale)
242
ctx.set_operator(cairo.OPERATOR_OVER)
245
ctx.set_source_rgba(1, 1, 1, 1)
248
if len(self.session.instances) != 0:
252
gradient = cairo.LinearGradient(0, self.height*2,0, 0)
253
gradient.add_color_stop_rgba(1,*self.frame_color)
254
gradient.add_color_stop_rgba(0.7,self.frame_color[0],self.frame_color[1],self.frame_color[2],1-self.frame_color[3]+0.5)
256
ctx.set_source(gradient)
258
self.draw_rectangle_advanced (ctx, 0, 0, self.width-12, self.height-12, rounded_angles=(5,5,5,5), fill=True, border_size=2, border_color=(0,0,0,0.5), shadow_size=6, shadow_color=(0,0,0,0.5))
259
self.draw_icon(ctx, 10, 12,gtk.STOCK_GO_DOWN,16,16)
260
self.draw_icon(ctx, 10, 32,gtk.STOCK_GO_UP,16,16)
261
ctx.set_source_rgba(*self.color_text)
262
self.draw_text(ctx, str(self.download) + ' KB', -10, 12, self.font.split(' ')[0], 14, self.width, allignment=pango.ALIGN_RIGHT,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
263
self.draw_text(ctx, str(self.upload) + ' KB', -10, 30, self.font.split(' ')[0], 14, self.width, allignment=pango.ALIGN_RIGHT,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
264
if self.show_txt:self.draw_text(ctx, 'In -\nOut -', 30, 12, self.font.split(' ')[0], 14, self.width, allignment=pango.ALIGN_LEFT,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
266
if self.mini == False:
267
self.draw_icon(ctx, 10, 64,gtk.STOCK_GO_DOWN,16,16)
268
self.draw_icon(ctx, 10, 84,gtk.STOCK_GO_UP,16,16)
269
self.draw_text(ctx, '<small> <small><small>Totals this Month</small></small></small>', 0,45, self.font.split(' ')[0], 14, self.width, allignment=pango.ALIGN_CENTER,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
270
if self.show_txt:self.draw_text(ctx, 'In -\nOut -' , 30, 64, self.font.split(' ')[0], 14, self.width, allignment=pango.ALIGN_LEFT,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
272
self.draw_text(ctx, str(self.dlt) + ' '+ self.unit_d + '\n' + str(self.ult)+ ' '+ self.unit_u, -10,64, self.font.split(' ')[0], 14, self.width,allignment=pango.ALIGN_RIGHT,weight = 0, ellipsize = pango.ELLIPSIZE_NONE)
280
def on_draw_shape(self,ctx):
283
if __name__ == "__main__":
284
import screenlets.session
285
screenlets.session.create_session(NetmonitorScreenlet)