1
# -*- coding: UTF-8 -*-
3
# +-------------------------------------------------------------------------------------+
5
# +-------------------------------------------------------------------------------------+
6
# | Copyright (c) 2005,2006 Adolfo González Blázquez <code@infinicode.org> |
8
# | This program is free software; you can redistribute it and/or |
9
# | modify it under the terms of the GNU General Public License |
10
# | as published by the Free Software Foundation; either version 2 |
11
# | of the License, or (at your option) any later version. |
13
# | This program is distributed in the hope that it will be useful, |
14
# | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
# | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
# | GNU General Public License for more details. |
18
# | You should have received a copy of the GNU General Public License |
19
# | along with this program; if not, write to the Free Software |
20
# | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21
# +-------------------------------------------------------------------------------------+
40
from computertemp import temp_meta as tempf
41
from computertemp import computertemp_globals as glob
42
from computertemp import computertemp_prefs as prefs
43
from computertemp import computertemp_gconf as gconf
45
print "Error importing computertemp libs"
52
gettext.bindtextdomain('computertemp', '/usr/share/locale')
53
gettext.textdomain('computertemp')
54
locale.bindtextdomain('computertemp', '/usr/share/locale')
55
locale.textdomain('computertemp')
56
gettext.install('computertemp', '/usr/share/locale', unicode=1)
60
class computertempApplet(gnomeapplet.Applet):
62
# Set files and dir logs
63
def setlogfilename(self):
65
if self.autologname or self.log_filename == '':
66
self.log_filename = self.log_filename_default
67
self.gconf.save_prefs_log_filename()
72
if self.log_info == '':
73
self.log_info = self.log_info_default
74
self.gconf.save_prefs_log_info()
77
def init_log_tooltips(self):
80
"Possible substitution patterns: \n" + \
81
"{home} /home/user \n" + \
82
"{date} 22feb1980 \n" + \
85
"{monthname} february \n" + \
86
"{monthsimp} feb \n" +\
88
"{dayname} friday \n" + \
89
"{daysimp} fri \n" + \
90
"{applet} applet_2 \n" + \
94
"Possible substitution patterns: \n" + \
96
"{unit} °C or °F \n" + \
97
"{applet} applet_2 \n" + \
98
"{sensor} HHDTEMP \n" + \
101
"{compdate} Fri Feb 22 23:12:02 1980 \n" + \
102
"{date} 22feb1980 \n" + \
103
"{time} 23:12:02 \n" + \
106
"{monthname} february \n" + \
107
"{monthsimp} feb \n" +\
109
"{dayname} friday \n" + \
110
"{daysimp} fri \n" + \
112
"{user} username \n" + \
113
"{sysname} Linux \n" + \
114
"{hostname} localhost \n" + \
115
"{kernel} 2.6.15-27-686 \n" + \
121
# Changes logfilename if special patterns found on preferences dialog
122
def parselogfilename(self, logname):
124
logname = logname.replace('{home}', os.environ["HOME"])
125
logname = logname.replace('{date}', time.strftime("%d%b%Y", time.localtime()))
126
logname = logname.replace('{year}', time.strftime("%Y", time.localtime()))
127
logname = logname.replace('{month}', time.strftime("%m", time.localtime()))
128
logname = logname.replace('{monthname}', time.strftime("%B", time.localtime()))
129
logname = logname.replace('{monthsimp}', time.strftime("%b", time.localtime()))
130
logname = logname.replace('{day}', time.strftime("%d", time.localtime()))
131
logname = logname.replace('{dayname}', time.strftime("%A", time.localtime()))
132
logname = logname.replace('{daysimp}', time.strftime("%a", time.localtime()))
133
logname = logname.replace('{applet}', self.gconf.get_applet_name())
134
logname = logname.replace('{sensor}', self.tempmon.get_sensor_name()).replace(' ','_')
138
def parseloginfo(self, loginfo):
140
if self.visualization_units == 1: unit = "°F"
143
loginfo = loginfo.replace('{temp}', `int(self.data)`)
144
loginfo = loginfo.replace('{unit}', unit)
145
loginfo = loginfo.replace('{applet}', self.gconf.get_applet_name())
146
loginfo = loginfo.replace('{sensor}', self.tempmon.get_sensor_name())
147
loginfo = loginfo.replace('{zone}', self.tempmon.get_zone_display_name(self.thermalzone))
148
loginfo = loginfo.replace('{compdate}', time.asctime(time.localtime()))
149
loginfo = loginfo.replace('{date}', time.strftime("%d%b%Y", time.localtime()))
150
loginfo = loginfo.replace('{time}', time.strftime("%H:%M:%S", time.localtime()))
151
loginfo = loginfo.replace('{year}', time.strftime("%Y", time.localtime()))
152
loginfo = loginfo.replace('{month}', time.strftime("%m", time.localtime()))
153
loginfo = loginfo.replace('{monthname}', time.strftime("%B", time.localtime()))
154
loginfo = loginfo.replace('{monthsimp}', time.strftime("%b", time.localtime()))
155
loginfo = loginfo.replace('{day}', time.strftime("%d", time.localtime()))
156
loginfo = loginfo.replace('{dayname}', time.strftime("%A", time.localtime()))
157
loginfo = loginfo.replace('{daysimp}', time.strftime("%a", time.localtime()))
158
loginfo = loginfo.replace('{user}', os.environ["USER"])
159
loginfo = loginfo.replace('{sysname}', os.uname()[0])
160
loginfo = loginfo.replace('{hostname}', os.uname()[1])
161
loginfo = loginfo.replace('{kernel}', os.uname()[2])
162
loginfo = loginfo.replace('{arch}', os.uname()[4])
163
loginfo = loginfo.replace('#', '\t')
167
# Calculates temperature depending on preferences (celsius or fahrenheit)
168
def temperature(self, temp):
169
if self.visualization_units == 1:
170
return int(round(long(temp) * 9.00/5.00 + 32.00))
174
# Display dialog window error
175
def dialog(self, text):
176
dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, text)
181
def set_icon(self, path):
184
self.icon.set_from_file(self.icon_path)
187
# Update applet icon depending on temperature
188
def update_icon(self):
190
if self.visualization_mode == 1: self.icon.hide()
194
if self.tempmon.get_sensor_name() == "HDDTEMP":
199
if self.tempmon_enabled:
200
diff = (self.max_temp - self.min_temp) / 4
202
temp = self.temperature(int(self.tempmon.get_zone_temp(self.tempmon.get_zone_name(self.thermalzone))))
204
if temp < self.min_temp+(diff*1) and self.tempzone != 1:
206
self.icon_path = glob.pixmaps_dir + "/" + prefix + "_25.png"
207
self.set_icon(self.icon_path)
208
elif self.min_temp+(diff*1) <= temp < self.min_temp+(diff*2) and self.tempzone != 2:
209
self.icon_path = glob.pixmaps_dir + "/" + prefix + "_50.png"
211
self.set_icon(self.icon_path)
212
elif self.min_temp+(diff*2) <= temp < self.min_temp+(diff*3) and self.tempzone != 3:
213
self.icon_path = glob.pixmaps_dir + "/" + prefix + "_75.png"
215
self.set_icon(self.icon_path)
216
elif self.min_temp+(diff*3) <= temp and self.tempzone != 4:
217
self.icon_path = glob.pixmaps_dir + "/" + prefix + "_100.png"
219
self.set_icon(self.icon_path)
221
self.icon_path = glob.pixmaps_dir + "/temp_na.png"
223
self.set_icon(self.icon_path)
226
# Updates data shown on applet tooltip
227
def update_tooltip(self):
229
if self.tempmon_enabled:
230
text = _(' Temperatures ')
232
if self.visualization_units == 1: unit = "°F"
236
for i in xrange(len(self.thermal_zones)):
237
text += "\n %s: %s%s" % \
238
(self.tempmon.get_zone_display_name(i),
239
int(self.temperature (self.tempmon.get_zone_temp(self.thermal_zones[i]))), unit)
240
if count == self.thermalzone: text += " *"
243
# Debug info on tooltip and console
245
diff = (self.max_temp - self.min_temp) / 4
246
zones = `self.min_temp` + ' - ' + `self.min_temp+(diff*1)` + ' - ' + `self.min_temp+(diff*2)`+ ' - ' + `self.min_temp+(diff*3)`
248
text += "\n Applet name: %s" % self.gconf.get_applet_name()
249
text += "\n Sensor: %s" % self.tempmon.get_sensor_name()
250
text += "\n Enabled: %s" % self.tempmon_enabled
251
text += "\n Logging: %s" % self.logging
252
text += "\n Alarm: %s" % self.alarm_enabled
253
text += "\n Min Temp: %s" % self.min_temp
254
text += "\n Max Temp: %s" % self.max_temp
255
text += "\n Zone: %s (%s)" % (self.tempzone, zones)
256
text += "\n Temp timeout: %s" % (self.timeout / 1000)
257
text += "\n Log timeout: %s" % (self.timeout_log / 1000)
258
text += "\n GConf key: %s" % self.gconf.gconf_applet
259
text += "\n Icon: %s" % self.icon_path
260
text += "\n Log: %s" % self.parselogfilename(self.log_filename)
262
self.tooltip.set_tip(self.applet, text)
264
self.tooltip.set_tip(self.applet, _('No Thermal Monitor Support!'))
267
def update_text(self):
269
if self.visualization_mode == 0:
274
if self.tempmon_enabled:
275
self.data = int(self.tempmon.get_zone_temp(self.tempmon.get_zone_name(self.thermalzone)))
277
if self.visualization_units == 1: unit = "°F"
280
self.temp.set_text(`self.temperature(self.data)`+unit)
282
self.temp.set_text(self.data)
286
def update_log(self):
290
log_filename = self.parselogfilename(self.log_filename)
291
log_info = self.parseloginfo(self.log_info)
293
if not os.path.isdir(os.path.dirname(log_filename)):
295
os.mkdir(os.path.dirname(log_filename))
297
print _('There was an error creating directory'), os.path.dirname(log_filename)
298
self.dialog(_('Can not create directory \'%s\'\nPlease select another path') % os.path.dirname(log_filename))
299
self.log_filename = self.log_filename_default
300
self.setlogfilename()
301
self.prefs.logentry.set_text(self.log_filename)
302
self.prefs.logcheck.set_active(False)
303
self.autologcheck = True
304
self.autologname = True
305
#self.prefs.logentry.set_sensitive(True)
307
self.prefs.logentry.set_sensitive(False)
308
self.prefs.preferences_show(None)
311
file = open(log_filename, "a")
313
if self.alarm_running: file.write("\tALARM!")
317
print _('There was an error writing to'), log_filename
318
self.dialog(_('Can not write to \'%s\'\nPlease select another path') % log_filename)
319
self.log_filename = self.log_filename_default
320
self.setlogfilename()
321
self.prefs.logentry.set_text(self.log_filename)
322
self.prefs.logcheck.set_active(False)
324
self.prefs.logentry.set_sensitive(False)
325
self.prefs.preferences_show(None)
328
def update_alarm(self):
330
# Is the command still being executed?
332
self.alarm_alive = not os.waitpid(self.alarm_pid, os.WNOHANG)[0]
334
self.alarm_alive = False
336
if self.alarm_enabled:
338
if not self.alarm_running: # alarm_running = False
341
if self.data > self.alarm_limit:
342
self.alarm_running = True
343
commands = self.alarm_command.split(' ')
344
self.alarm_pid = os.spawnvpe(os.P_NOWAIT, commands[0], commands, os.environ)
346
if self.data < self.alarm_limit:
347
self.alarm_running = True
348
commands = self.alarm_command.split(' ')
349
self.alarm_pid = os.spawnvpe(os.P_NOWAIT, commands[0], commands, os.environ)
351
else: # alarm_running = True
353
if self.alarm_repeat and not self.alarm_alive:
354
commands = self.alarm_command.split(' ')
355
self.alarm_pid = os.spawnvpe(os.P_NOWAIT, commands[0], commands, os.environ)
358
if self.data < self.alarm_limit: self.alarm_running = False
360
if self.data > self.alarm_limit: self.alarm_running = False
363
# Reset the alarm if the command is not being executed
364
def reset_alarm(self):
365
self.alarm_running = False or self.alarm_alive
368
# Update data displayed on icon
369
def update_temp(self):
373
self.update_tooltip()
377
def update_main(self):
379
# Update log if necessary
380
if self.timeout_log_count % (self.timeout_log / 1000) == 0:
381
self.timeout_log_count = 0
383
self.timeout_log_count += 1
385
# Update temperatures (icon, text and tooltips) if necessary
386
if self.timeout_count % (self.timeout / 1000) == 0:
387
self.timeout_count = 0
389
self.timeout_count += 1
394
# Callback: panel background changed
395
def change_background(self, panelapplet, backgroundtype, color, pixmap):
396
self.applet.modify_bg(gtk.STATE_NORMAL, color)
397
self.box.modify_bg(gtk.STATE_NORMAL, color)
399
if pixmap is not None:
400
s1 = self.applet.get_style()
401
s2 = self.box.get_style()
402
s1.bg_pixmap[gtk.STATE_NORMAL] = pixmap
403
s2.bg_pixmap[gtk.STATE_NORMAL] = pixmap
406
# Callback: panel orientation changed
407
def change_orientation(self,arg1,data):
408
self.orientation = self.applet.get_orient()
410
self.inside_applet.remove(self.icon)
411
self.inside_applet.remove(self.temp)
412
self.box.remove(self.inside_applet)
414
if self.orientation == 2 or self.orientation == 3:
415
self.inside_applet = gtk.VBox()
417
self.inside_applet = gtk.HBox()
419
self.inside_applet.pack_start(self.icon)
420
self.inside_applet.pack_start(self.temp)
421
self.inside_applet.show_all()
422
self.box.add(self.inside_applet)
425
def create_applet(self):
427
app_window = self.applet
430
event_box = gtk.EventBox()
431
event_box.set_events(gtk.gdk.BUTTON_PRESS_MASK |
432
gtk.gdk.POINTER_MOTION_MASK |
433
gtk.gdk.POINTER_MOTION_HINT_MASK |
436
# Creates icon for applet
437
self.icon = gtk.Image()
440
# Create label for temp
441
self.temp = gtk.Label()
444
# Creates hbox with icon and temp
445
self.inside_applet = gtk.HBox()
446
self.inside_applet.pack_start(self.icon)
447
self.inside_applet.pack_start(self.temp)
450
self.tooltip = gtk.Tooltips()
451
self.update_tooltip()
453
# Adds hbox to eventbox
454
event_box.add(self.inside_applet)
455
app_window.add(event_box)
456
app_window.show_all()
461
if self.alarm_enabled:
462
self.applet.setup_menu_from_file (glob.resources_dir, "computertemp_menu2.xml", "computertemp", self.verbs)
464
self.applet.setup_menu_from_file (glob.resources_dir, "computertemp_menu.xml", "computertemp", self.verbs)
467
def menu_alarm_toggled(self, event, data=None):
469
self.alarm_enabled = not self.alarm_enabled
473
self.prefs.alarm_enabled_check.set_active(self.alarm_enabled)
474
self.prefs.alarm_enabled_check_toggled(None)
478
def about_info(self,event,data=None):
480
about = gtk.AboutDialog()
481
about.set_name(glob.name_long)
482
about.set_version(glob.version)
483
about.set_authors(glob.authors)
484
about.set_artists(glob.artists)
485
about.set_translator_credits(_('translator-credits'))
486
about.set_logo(gtk.gdk.pixbuf_new_from_file(glob.pixmaps_dir+"/computertemp.png"))
487
about.set_license(glob.license)
488
about.set_wrap_license(True)
489
about.set_copyright(glob.copyright)
491
def openHomePage(widget,url,url2):
493
webbrowser.open_new(url)
495
gtk.about_dialog_set_url_hook(openHomePage,glob.website)
496
about.set_website(glob.website)
502
def __init__(self,applet,iid):
504
# Applet as a gboject
505
self.__gobject_init__()
510
self.log_filename_default = '{home}/logs/{sensor}_{date}.log'
511
self.log_info_default = '{date} {time}#{sensor}#{zone}#{temp}'
512
self.log_filename = self.log_filename_default
513
self.log_info = self.log_info_default
515
self.autologname = True
517
self.tempmon_enabled = False
518
self.thermal_zones = []
519
self.thermalzone = ''
522
self.timeout_count = 1
523
self.timeout_log = 5000
524
self.timeout_log_count = 1
527
self.visualization_mode = 0 # 0 = Graphic 1 = Text 2 = Graphic and Text
528
self.visualization_units = 0 # 0 = Celsius 1 = Farenheit
529
self.alarm_enabled = False
530
self.alarm_limit = 80
531
self.alarm_when = 1 # 0 = Smaller 1 = Greater
532
self.alarm_command = ''
533
self.alarm_running = False
534
self.alarm_alive = False
536
self.alarm_repeat = False
539
self.tips_logname = ''
540
self.tips_loginfo = ''
542
# GConf stuff init for preferences
543
self.gconf = gconf.ComputertempGConf(self)
544
print "Applet GConf id:", self.gconf.get_applet_name()
545
self.gconf.read_prefs()
546
self.gconf.save_prefs_all()
547
self.setlogfilename()
549
self.init_log_tooltips()
551
# Set Debug info via commandline
554
self.gconf.save_prefs_debug()
556
# Init Temperature Monitor functions
557
self.tempmon = tempf.TempFuncs(self)
559
# Lets create the gui
560
self.box = self.create_applet()
561
self.change_orientation(None, None) # Forcing to get panel position
562
self.prefs = prefs.ComputertempPrefs(self)
564
# Display error if there are no sensors enabled
565
if not self.tempmon_enabled: self.dialog(_('\nThere is not Thermal Monitor support on your machine!!!'))
567
# Init context menu stuff
568
self.verbs = [("Alarm", self.menu_alarm_toggled),
569
("Prefs", self.prefs.preferences_show),
570
("About", self.about_info)]
576
# Callbacks for timeout and change_background and orientation
577
self.timeout_source = gobject.timeout_add (1000, self.update_main)
578
self.applet.connect("change_background", self.change_background)
579
self.applet.connect("change-orient", self.change_orientation)
581
gobject.type_register(computertempApplet)
585
# Bonobo factory of computertempApplet
586
def computertemp_applet_factory(applet, iid):
587
computertempApplet(applet, iid)
590
# Main method. Decide if we should run computertemp as applet or systray icon
597
gnomeapplet.bonobo_factory("OAFIID:GNOME_ComputertempApplet_Factory",
598
computertempApplet.__gtype__,
601
computertemp_applet_factory)
b'\\ No newline at end of file'