aboutsummaryrefslogtreecommitdiff
path: root/h-client/hclient.py
blob: 8b3a463e7cccec4e5cd1ec12e1804ab86aa0dce4 (plain) (blame)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# -*- coding: utf-8 -*-
# h-client, a client for an h-source server (such as http://www.h-node.com)
# Copyright (C) 2011  Antonio Gallo
#
#
# h-client 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.
#
# h-client 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 h-client.  If not, see <http://www.gnu.org/licenses/>.

import pygtk
pygtk.require('2.0')
import gtk

from hlibrary import *

class hclient:

	_years = [
		'not-specified',
		'2011',
		'2010',
		'2009',
		'2008',
		'2007',
		'2006',
		'2005',
		'2004',
		'2003',
		'2002',
		'2001',
		'2000',
		'1999',
		'1998',
		'1997',
		'1996',
		'1995',
		'1994',
		'1993',
		'1992'
	]

	#the device that has to be displaced in the right window
	currentDevice = ''

	#get the active text from a combo
	#combo: the gtk combo instance
	#default: default value if the active index < 0
	def getActive(self, combo, default = 'not-specified'):
		model = combo.get_model()
		active = combo.get_active()
		if active < 0:
			return default
		else:
			return model[active][0]

	#update the current device object by taking the values from the entries
	def applyChanges(self,widget):
		self.currentDevice.setModel(self.modelNameEntry.get_text())

		self.currentDevice.setYear(self.getActive(self.commYearCombo))

		self.currentDevice.setInterface(self.getActive(self.interfaceCombo))

		self.currentDevice.setDistributions([])
		self.currentDevice.addDistributions(self.distributionEntry.get_text())

		self.currentDevice.setKernel(self.kernelEntry.get_text())

		self.currentDevice.setHowItWorks(self.getActive(self.howItWorksCombo))

		self.currentDevice.setDriver(self.driverEntry.get_text())

		descriptionBuffer = self.descriptionText.get_buffer()
		startiter, enditer = descriptionBuffer.get_bounds()
		self.currentDevice.setDescription(descriptionBuffer.get_text(startiter, enditer))

		
	#set the device that has to be displaced in the right window
	def setEntries(self, selection):
		model, path = selection.get_selected()
		if path:
			#get the code:
			code = model.get_value(path, 1)

			if code in self.client.devices:
				
				#set the current device
				self.currentDevice = self.client.devices[code][0]
				#get the device
			
				device = self.client.devices[code][0]
				#set the model entry
				self.setModelEntry()
				#set the vendorid:productid entry
				self.setVendorIdProductIDCode()
				#set the commercialization year entry
				self.setCommYearEntry()
				#set the interface entry
				self.setInterfaceEntry()
				#set the distribution entry
				self.setDistributionEntry()
				#set the kernel entry
				self.setKernelEntry()
				#set the howItWorks entry
				self.setHowItWorksEntry()
				#set the driver entry
				self.setDriverEntry()
				#set the description entry
				self.setDescriptionEntry()

				#make sensitive the apply button
				self.applyButton.set_sensitive(True)


	#set the model name entry
	def setModelEntry(self):
		self.modelNameEntry.set_text(self.currentDevice.getModel())


	#set the vendorid:productid entry
	def setVendorIdProductIDCode(self):
		self.vendorIdProductIdEntry.set_text(self.currentDevice.getVendorId() + ':' + self.currentDevice.getProductId())


	#set the year of commercialization
	def setCommYearEntry(self):
		if self.currentDevice.getYear() in self._years:
			index = self._years.index(self.currentDevice.getYear())
		else:
			index = 0
			
		self.commYearCombo.set_active(index)


	#set the interface
	def setInterfaceEntry(self):
		self.interfaceCombo.get_model().clear()
		for interface in self.currentDevice.getInterfaces():
			self.interfaceCombo.append_text(interface)
		
		if self.currentDevice.getInterface() in self.currentDevice.getInterfaces():
			index = self.currentDevice.getInterfaces().index(self.currentDevice.getInterface())
		else:
			index = 0

		self.interfaceCombo.set_active(index)


	#set the distribution entry
	def setDistributionEntry(self):
		self.distributionEntry.set_text(self.currentDevice.createDistroEntry())


	#set the kernel libre entry
	def setKernelEntry(self):
		self.kernelEntry.set_text(self.currentDevice.getKernel())


	#set the howItWorks entry
	def setHowItWorksEntry(self):
		self.howItWorksCombo.get_model().clear()
		for option in self.currentDevice.getHowItWorksOptions():
			self.howItWorksCombo.append_text(option)

		if self.currentDevice.getHowItWorks() in self.currentDevice.getHowItWorksOptions():
			index = self.currentDevice.getHowItWorksOptions().index(self.currentDevice.getHowItWorks())
		else:
			index = 0

		self.howItWorksCombo.set_active(index)

	#set the driver entry
	def setDriverEntry(self):
		self.driverEntry.set_text(self.currentDevice.getDriver())


	#set the description entry
	def setDescriptionEntry(self):
		textbuffer = gtk.TextBuffer(table=None)
		textbuffer.set_text(self.currentDevice.getDescription())
		self.descriptionText.set_buffer(textbuffer)

	
	#another callback
	def delete_event(self, widget, event, data=None):
		gtk.main_quit()
		return False

	def __init__(self):

		#start the client object
		self.client = Client('h-source')
		self.client.createDevices()
		
		# Create a new window
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

		# This is a new call, which just sets the title of our
		# new window to "Hello Buttons!"
		self.window.set_title("h-client")

		self.window.set_icon_from_file("img/icon.png")
		#self.window.set_size_request(200, 100)

		# Here we just set a handler for delete_event that immediately
		# exits GTK.
		self.window.connect("delete_event", self.delete_event)

		# Sets the border width of the window.
		self.window.set_border_width(0)

		vbox = gtk.VBox(False, 0)
		
		self.centerWindow = gtk.HBox(False, 0)

		

		## build the toolbar ##
		toolbar = gtk.Toolbar()
		toolbar.set_tooltips(True)
		#toolbar.set_style(gtk.TOOLBAR_BOTH)

		pref = gtk.ToolButton(gtk.STOCK_PREFERENCES)
		pref.set_tooltip_text('Preferences')
		#sync = gtk.ToolButton(None,'Synchronize')
		#sync.set_tooltip_text('Synchronize with the server: this will override your entries')
		info = gtk.ToolButton(gtk.STOCK_INFO)
		info.set_tooltip_text('Information')

		toolbar.insert(pref, 0)
		#toolbar.insert(sync, 1)
		toolbar.insert(info, 1)
		toolbar.show_all()

		#add the bottom box
		self.window.add(vbox)

		vbox.pack_start(toolbar, True, True, 0)
		

		vbox.pack_start(self.centerWindow, True, True, 0)
		vbox.show()

		
		## build the left window ##
		
		#start the left vertical box
		self.leftWindow = gtk.VBox(False, 0)
		#self.leftWindow.set_border_width(5)
		self.lframe = gtk.Frame()
		self.lframe.set_border_width(5)

		
		self.centerWindow.pack_start(self.lframe, True, True, 0)

		self.lframe.add(self.leftWindow)
		#self.centerWindow.pack_start(self.rframe, True, True, 5)

		#self.leftWindow.pack_start(gtk.Label("Your hardware:"), False, True, 5)

		self.tree = gtk.TreeView()

		devices = gtk.TreeViewColumn()
		devices.set_title("Your PCI and USB devices")

		cell = gtk.CellRendererText()
		devices.pack_start(cell, False)
		devices.add_attribute(cell, "text", 0)
		
		self.treestore = gtk.TreeStore(str,str)

		it = self.treestore.append(None, ["PCI Devices",""])
		for key,dev in self.client.devices.iteritems():

			if key[0] == 'p':
				self.treestore.append(it, [dev[0].getType(),key])

		selection = self.tree.get_selection()
		selection.connect('changed', self.setEntries)


		self.tree.append_column(devices)
		self.tree.set_model(self.treestore)
		
		self.leftWindow.pack_start(self.tree, False, True, 0)

		self.leftWindow.show_all()
		
		#self.lframe.add(self.leftWindow)

		## build the right window ##

		self.rightTable = gtk.Table(8, 2, True)

		#create the entries

		### model
		#model name label
		self.modelNameLabel = gtk.Label("Model name:")
		#add the label
		self.rightTable.attach(self.modelNameLabel, 0, 1, 0, 1)

		#model name input
		self.modelNameEntry = gtk.Entry()
		#add the input to the table
		self.rightTable.attach(self.modelNameEntry, 1, 2, 0, 1)


		### vendorid:productid
		#vendorid:productid label
		self.vendorIdProductIdLabel = gtk.Label("VendorID:productID code:")
		#add the label
		self.rightTable.attach(self.vendorIdProductIdLabel, 0, 1, 1, 2)

		#vendorid:productid input
		self.vendorIdProductIdEntry = gtk.Entry()
		#set as not editable
		self.vendorIdProductIdEntry.set_editable(False)
		#add the input to the table
		self.rightTable.attach(self.vendorIdProductIdEntry, 1, 2, 1, 2)


		###year of commercialization
		#year of commercialization label
		self.commYearLabel = gtk.Label("Year of commercialization:")
		#add the label
		self.rightTable.attach(self.commYearLabel, 0, 1, 2, 3)

		self.commYearCombo = gtk.combo_box_new_text()

		for item in self._years:
			self.commYearCombo.append_text(item)

		self.commYearCombo.set_active(0)

		#add the combo to the table
		self.rightTable.attach(self.commYearCombo, 1, 2, 2, 3)


		###interface
		#interface label
		self.interfaceLabel = gtk.Label("Interface:")
		#add the label
		self.rightTable.attach(self.interfaceLabel, 0, 1, 3, 4)

		self.interfaceCombo = gtk.combo_box_new_text()

		self.interfaceCombo.append_text('not-specified')

		self.interfaceCombo.set_active(0)

		#add the combo to the table
		self.rightTable.attach(self.interfaceCombo, 1, 2, 3, 4)


		### distribution
		#distribution label
		self.distributionLabel = gtk.Label("Distribution:")
		#add the label
		self.rightTable.attach(self.distributionLabel, 0, 1, 4, 5)

		#distribution input
		self.distributionEntry = gtk.Entry()
		#add the input
		self.rightTable.attach(self.distributionEntry, 1, 2, 4, 5)


		### kernel
		#kernel label
		self.kernelLabel = gtk.Label("Kernel libre version:")
		#add the label
		self.rightTable.attach(self.kernelLabel, 0, 1, 5, 6)

		#kernel input
		self.kernelEntry = gtk.Entry()
		#add the input
		self.rightTable.attach(self.kernelEntry, 1, 2, 5, 6)


		###how it works
		#how it works label
		self.howItWorksLabel = gtk.Label("Does it work?")
		#add the label
		self.rightTable.attach(self.howItWorksLabel, 0, 1, 6, 7)

		self.howItWorksCombo = gtk.combo_box_new_text()

		#add the combo to the table
		self.rightTable.attach(self.howItWorksCombo, 1, 2, 6, 7)
		

		### driver
		#driver label
		self.driverLabel = gtk.Label("Free driver used:")
		#add the label
		self.rightTable.attach(self.driverLabel, 0, 1, 7, 8)

		#driver input
		self.driverEntry = gtk.Entry()
		#add the input
		self.rightTable.attach(self.driverEntry, 1, 2, 7, 8)

		
		### description
		#description label
		self.descriptionLabel = gtk.Label("Description:")

		#description input
		sw = gtk.ScrolledWindow()
		sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		
		self.descriptionText = gtk.TextView()
		self.descriptionText.set_wrap_mode(gtk.WRAP_CHAR)

		sw.add(self.descriptionText)
		sw.show()
		sw.show_all()
		
		##add the input
		#self.rightTable.attach(sw, 1, 2, 7, 8)

		self.rightTable.show_all()

		#apply and submit buttons
		hboxBelowEntries = gtk.VBox(False, 0)
		self.applyButton = gtk.Button(stock=gtk.STOCK_APPLY)
		self.applyButton.set_sensitive(False)
		self.applyButton.connect("clicked", self.applyChanges)

		hboxBelowEntries.pack_start(self.applyButton, False, True, 0)
		hboxBelowEntries.show_all()
		
		#start the left vertical box
		self.rightWindow = gtk.VBox(False, 0)

		self.rightWindow.pack_start(self.rightTable, False, True, 10)
		self.rightWindow.pack_start(self.descriptionLabel, False, True, 3)
		self.rightWindow.pack_start(sw, False, True, 0)
		self.rightWindow.pack_start(hboxBelowEntries, False, True, 10)
		self.rightWindow.show()
		self.rightWindow.show_all()

		self.centerWindow.pack_start(self.rightWindow, True, True, 5)
		
		#self.rframe.add(self.rightWindow)
		#self.rframe.set_border_width(30)
		
		self.centerWindow.show()
		self.lframe.show()
		#self.rframe.show()
		self.rightTable.show()
		self.leftWindow.show()
		self.window.show()

def main():
	gtk.main()

if __name__ == "__main__":
	Client = hclient()
	main()