-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.py
231 lines (186 loc) · 6.36 KB
/
check.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
228
229
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gobject
import pygtk
pygtk.require("2.0")
import gtk
import sys
import os
import gtk
from listdisks import listdisks
import subprocess
gtk.gdk.threads_init()
'''Constant for easy modifications, next version I use dictionaries'''
COL_PATH = 0
COL_PIXBUF = 2
VOLUME_STORE=0
VOLUME_VOLUM=1
VOLUME_FS=4
VOLUME_MOUNT=3
DISK_STORE=0
DISK_PRODUCT=1
class CheckDisk:
def about_check(self,widget):
about=self.builder.get_object('aboutWin')
about.set_logo(self.diskIcon)
about.run()
about.hide()
def error_message(self, message):
print message
# create an error message dialog and display modally to the user
dialog = gtk.MessageDialog(None,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, message)
dialog.run()
dialog.destroy()
'''
Event form combobox, get the partitions and disks from self(init method)
and iterate to find the partitions from disk selected at iconview and fill
data at form.
'''
def on_cb_change(self,combo):
selected=combo.get_active_text()
for volume in self.volums:
if selected ==volume[VOLUME_VOLUM]:
self.lbVolume.set_text(volume[VOLUME_VOLUM])
self.lbFS.set_text(volume[VOLUME_FS])
if volume[VOLUME_MOUNT]!='':
self.lbMount.set_text(volume[VOLUME_MOUNT])
self.hboxWarning.show()
self.btCheck.set_sensitive(False)
else:
self.hboxWarning.hide()
self.lbMount.set_text("None")
self.btCheck.set_sensitive(True)
'''
Help method for get icon from actual theme, we pass size and name icon
'''
def get_icon(self, name, size):
theme = gtk.icon_theme_get_default()
return theme.load_icon(name, size, 0)
'''
Fill comobox model declarated at builder file(glade)
'''
def fill_comboBox(self,volums,disk):
self.comboModel.clear()
for volum in volums:
if volum[VOLUME_STORE]==disk:
self.comboModel.append([str(volum[VOLUME_VOLUM]),volum[VOLUME_FS],self.volumIcon])
'''
Fill iconView with disk stores
'''
def fill_iconView(self,disks):
self.iconModel.clear()
for disk in disks:
self.iconModel.append([disk[DISK_STORE]+"\n"+disk[DISK_PRODUCT],disk[DISK_PRODUCT],self.diskIcon])
'''
When iconView double click activate this callback
'''
def on_listaDiscos_selection_changed(self,widget):
self.clear_data()
self.fill_comboBox(self.volums,self.disks[widget.get_selected_items()[0][0]][DISK_STORE])
'''
Easy method to quit app
'''
def quit_app(self,widget):
gtk.main_quit()
'''
Refres list of Disks and fill into IconView
'''
def refresh_disks(self,widget):
self.disks, self.volums = listdisks.discover()
self.fill_iconView(self.disks)
self.clear_data()
'''Reset the state of form to initial'''
def clear_data(self):
self.comboModel.clear()
self.lbVolume.set_text("None")
self.lbFS.set_text("None")
self.lbMount.set_text("None")
self.btCheck.set_sensitive(False)
self.hboxWarning.hide()
'''Launch check to actual selected partition, lauch gobject io_add_watch to fill buffer text'''
def launch_check(self,widget):
self.wCheck.show()
self.textBufferVT.set_text("")
if self.sourceId!=None:
gobject.source_remove(self.sourceId)
cmd="fsck -y " + self.lbVolume.get_text()
command = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
self.sourceId=gobject.io_add_watch(command.stdout, gobject.IO_IN | gobject.IO_HUP,
self.read_output)
self.lbStatus.set_text("Checking partition " + self.lbVolume.get_text())
self.wCheck.show()
'''Callback for io_add_watch to fill textBuffer'''
def read_output(self,source, condition):
if condition == gobject.IO_IN:
line = source.readline()
self.progress.pulse()
self.textBufferVT.insert_at_cursor(line)
if condition == gobject.IO_HUP:
self.progress.set_fraction(1)
self.textBufferVT.insert_at_cursor("Command finished.\n")
return False
return True
'''
Callback to hide check window
'''
def on_btCerrar_clicked(self, widget):
self.wCheck.hide()
def __init__(self):
'''Init variables and data from aplication'''
self.sourceId=None
self.disks, self.volums = listdisks.discover()
self.shareDir ='share'
self.update = True
self.xmlFile = 'check.glade'
self.pathGlade= os.path.join(sys.path[0],self.shareDir,self.xmlFile)
self.iconImage='icon.png'
self.iconPath = os.path.join(sys.path[0],self.shareDir,self.iconImage)
self.diskIcon=self.get_icon("drive-harddisk",48)
self.volumIcon= self.get_icon(gtk.STOCK_HARDDISK,22)
self.builder = gtk.Builder()
self.builder.add_from_file(self.pathGlade)
self.builder.connect_signals(self)
'''Init widgets that we use in app'''
self.iconModel=self.builder.get_object('iconModel')
self.comboModel=self.builder.get_object('comboModel')
self.iconView = self.builder.get_object('listaDiscos')
self.window= self.builder.get_object('mainWindow')
self.combo = self.builder.get_object('cbPartitions')
self.lbVolume = self.builder.get_object('lbVolume')
self.lbFS = self.builder.get_object('lbFsType')
self.lbMount = self.builder.get_object('lbMount')
self.lbStatus = self.builder.get_object('lbStatus')
self.hboxWarning=self.builder.get_object('hboxWarning')
self.btCheck=self.builder.get_object('btCheck')
self.textBufferVT=self.builder.get_object('textBufferVT')
self.wCheck= self.builder.get_object('wCheck')
self.progress= self.builder.get_object('pbCheck')
#self.borderbox= self.builder.get_object('eventbox1')
#
#'''Set the eventbox'''
#self.borderbox.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#333'))
#self.borderbox.set_size_request(-1, 130)
'''Set icon from app'''
self.window.set_icon_from_file(self.iconPath)
'''Fill for fist time iconView'''
self.fill_iconView(self.disks)
self.iconView.set_text_column(COL_PATH)
self.iconView.set_pixbuf_column(COL_PIXBUF)
'''Init combobox render'''
self.cellImage = gtk.CellRendererPixbuf()
self.cellVolum = gtk.CellRendererText()
self.cellFS = gtk.CellRendererText()
self.combo.pack_start(self.cellVolum, True)
self.combo.pack_start(self.cellFS, True)
self.combo.pack_start(self.cellImage,True)
self.combo.add_attribute(self.cellVolum, 'text', 0)
self.combo.add_attribute(self.cellFS, 'text', 1)
self.combo.add_attribute(self.cellImage,'pixbuf',2)
def main(self):
self.window.show()
gtk.main()
if __name__ == "__main__":
check = CheckDisk()
check.main()