forked from thpani/hdapsicon
/
hdapsicon.py
228 lines (187 loc) · 8.17 KB
/
hdapsicon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!@PYTHON@
# Copyright (c) 2008-2010 Thomas Pani <thomas.pani@gmail.com>
# based on an awn applet Copyright (c) 2008 onox <denkpadje@gmail.com>
#
# 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, version 3 of the License.
#
# 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 errno
import os
import platform
import sys
import pygtk
pygtk.require("2.0")
import gobject
import gtk
import gtk.gdk
__version__ = '@VERSION@'
# Interval in milliseconds between two successive status checks.
CHECK_STATUS_INTERVAL = 250
# The sysfs mountpoint.
SYSDIR = '/sys'
# Check whether we're using /sys/block/%s/device/unload_heads (>=2.6.28)
# or /sys/block/%s/queue/protect (<=2.6.27).
# Native kernel interface measures in milliseconds, HDAPS in seconds.
PROTECT_FACTOR = 1
if '2.6.27' <= platform.release():
PROTECT_FACTOR = 1000
# Images used as the applet's icon to reflect the current status of HDAPS.
IMAGE_DIR = '@pixmapdir@'
icon_running = gtk.gdk.pixbuf_new_from_file(
os.path.join(IMAGE_DIR, "hdapsicon-logo.svg"))
icon_paused = gtk.gdk.pixbuf_new_from_file(
os.path.join(IMAGE_DIR, "hdapsicon-paused.svg"))
icon_error = gtk.gdk.pixbuf_new_from_file(
os.path.join(IMAGE_DIR, "hdapsicon-error.svg"))
def get_protect_file(device):
"""Returns the protect file based on the PROTECT_FACTOR."""
if PROTECT_FACTOR != 1:
return os.path.join(SYSDIR, 'block', device, 'device/unload_heads')
else:
return os.path.join(SYSDIR, 'block', device, 'queue/protect')
class ThinkHDAPSAboutDialog(gtk.AboutDialog):
"""ThinkHDAPS's AboutDialog."""
def __init__(self):
gtk.AboutDialog.__init__(self)
self.set_icon(icon_running)
self.set_authors(('Thomas Pani <thomas.pani@gmail.com>',))
self.set_artists(('Jakub Steiner',
'Lapo Calamandrei',
'Rodney Dawes',
'Garrett LeSage',
'onox'))
self.set_copyright(u'Copyright \u00a9 2008-2010 Thomas Pani')
self.set_logo(icon_running)
self.set_name('HDAPSicon')
self.set_version(__version__)
self.set_license('''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/>.''')
self.set_wrap_license(True)
self.set_website('http://thpani.at/projects/thinkhdaps/')
class ThinkHDAPSApplet(gtk.StatusIcon):
"""StatusIcon that shows the status of HDAPS."""
def __init__(self):
gtk.StatusIcon.__init__(self)
# context menu
menu = gtk.Menu()
menuItem = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
menuItem.connect('activate', self.about_cb)
menu.append(menuItem)
menuItem = gtk.ImageMenuItem(gtk.STOCK_QUIT)
menuItem.connect('activate', self.quit_cb)
menu.append(menuItem)
self.connect('popup-menu', self.popup_menu_cb, menu)
# status
self.paused = None
self.was_paused = None
# initialize
self.check_status_cb()
gobject.timeout_add(CHECK_STATUS_INTERVAL, self.check_status_cb)
def about_cb(self, widget, data=None):
dlg = ThinkHDAPSAboutDialog()
dlg.run()
dlg.destroy()
def quit_cb(self, widget, data=None):
gtk.main_quit()
def popup_menu_cb(self, widget, button, time, data=None):
if button == 3:
if data:
data.show_all()
data.popup(None, None, None, 3, time)
def check_status_cb(self):
"""Check the status the hard disk monitored by HDAPS and change
the applet's icon if necessary.
"""
self.was_paused = self.paused
self.paused = {}
# Find HDAPS devices.
# Check if and for how long the devices are unloaded.
for device in os.listdir(os.path.join(SYSDIR, 'block')):
self.paused[device] = None
protect_file = get_protect_file(device)
if os.path.isfile(protect_file):
try:
f = open(protect_file, 'r')
self.paused[device] = \
float(f.readline()) / PROTECT_FACTOR
except IOError, e:
if e.errno == errno.EOPNOTSUPP:
# device does not support the unload feature.
pass
else:
# report other errors.
self.paused[device] = -1
print >> sys.stderr, "Error reading %s: %s" % \
(protect_file, e.strerror)
# Change icon and tooltip if status has changed.
if self.paused != self.was_paused:
tt_text = ""
min_unload_time = min(self.paused.values())
max_unload_time = max(self.paused.values())
if max_unload_time is None:
# No device supports HDAPS.
self.set_from_pixbuf(icon_error)
self.set_tooltip('\n'.join((
"HDAPS disabled",
"Can't find a device that supports HDAPS!",
"Probed devices: %s" % ' '.join(self.paused.keys())
))
)
elif max_unload_time == -1:
# All devices threw errors while reading protect file.
self.set_from_pixbuf(icon_error)
self.set_tooltip('\n'.join((
"HDAPS disabled",
"Errors fetching HDAPS state on all devices!",
"Probed devices: %s" % ' '.join(self.paused.keys())
))
)
elif max_unload_time >= 0:
# At least one device running.
if min_unload_time == -1:
# One or more devices threw errors.
self.set_from_pixbuf(icon_error)
elif max_unload_time > 0:
self.set_from_pixbuf(icon_paused)
else:
self.set_from_pixbuf(icon_running)
tt_text = "HDAPS enabled"
for device in self.paused:
status = self.paused[device]
status_text = ""
if status == -1:
status_text = "error reading protect file"
elif status is None:
continue # ignore devices that don't support HDAPS.
elif status == 0:
status_text = "running"
else:
status_text = "Parked (%.3fs remaining)" % \
self.paused[device]
tt_text += "\n%s: %s" % (device, str(status_text))
self.set_tooltip(tt_text)
else:
assert False
return True
def on_launch_browser_mailer(dialog, link, user_data=None):
if user_data == 'mail':
gtk.show_uri(None, 'mailto:'+link, gtk.gdk.CURRENT_TIME)
else:
gtk.show_uri(None, link, gtk.gdk.CURRENT_TIME)
if __name__ == "__main__":
try:
gtk.about_dialog_set_email_hook(on_launch_browser_mailer, 'mail')
gtk.about_dialog_set_url_hook(on_launch_browser_mailer, 'url')
hdaps_icon = ThinkHDAPSApplet()
gtk.main()
except KeyboardInterrupt:
pass