summaryrefslogtreecommitdiff
path: root/gui.py
blob: b3c138f8159dc688ef4f31afcac093be67bf0780 (plain)
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/usr/bin/python

import geneticone

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

# for doing emerge
from subprocess import *

# for the terminal
import pty
import vte

from portage_util import unique_array

class EmergeQueue:
	"""This class manages the emerge queue."""

	def __init__ (self, tree = None, console = None):
		""""tree" is a gtk.TreeStore to show the queue in; "console" is a vte.Terminal to print the output to."""
		self.mergequeue = {}
		self.unmergequeue = []
		self.tree = tree
		self.console = console

		if self.tree:
			self.emergeIt = self.tree.append(None, ["Emerge"])
			self.unmergeIt = self.tree.append(None, ["Unmerge"])
		else:
			self.emergeIt = self.unmergeIt = None

	def append (self, sth, unmerge = False):
		"""Appends a cpv either to the merge queue or to the unmerge-queue.
		Also update the tree-view."""
		if not unmerge:
			# insert dependencies
			pkg = geneticone.find_packages("="+sth)[0]
			try:
				self.mergequeue.update({sth : pkg.get_dep_packages()})
			except geneticone.BlockedException, e :
				blocks = e[0]
				blockedDialog(sth, blocks)
				return
			else:
				# update tree
				if self.emergeIt:
					pkgIt = self.tree.append(self.emergeIt, [sth])
					for p in self.mergequeue[sth]:
						self.tree.append(pkgIt, [p])
		else:
			self.unmergequeue.append(sth)
			if self.unmergeIt: # update tree
				self.tree.append(self.unmergeIt, [sth])

	def _emerge (self, options, it):
		"""Calls emerge and updates the terminal."""
		# open pty
		(master, slave) = pty.openpty()
		Popen("emerge "+options, stdout = slave, stderr = STDOUT, shell = True)
		self.removeAll(it)
		self.console.set_pty(master)

	def emerge (self, force = False):
		"""Emerges everything in the merge-queue. If force is 'False' (default) only 'emerge -pv' is called."""
		if len(self.mergequeue) == 0: return

		list = ""
		for k in self.mergequeue.keys():
			list += " '="+k+"'"
		
		s = ""
		print list
		if not force: s = "-pv "
		self._emerge(s+list, self.emergeIt)

	def unmerge (self, force = False):
		"""Unmerges everything in the umerge-queue. If force is 'False' (default) only "emerge -pv -C" is called."""
		if len(self.unmergequeue) == 0: return

		list = " ".join(self.unmergequeue)
		s = ""
		if not force: s = "-pv "
		self._emerge("-C "+s+list, self.unmergeIt)

	def removeAll (self, parentIt):
		"""Removes all children of a given parent TreeIter."""
		childIt = self.tree.iter_children(parentIt)

		while childIt:
			temp = childIt
			childIt = self.tree.iter_next(childIt)
			self.remove(temp)
	
	def remove (self, it):
		if self.tree.iter_parent(it): # NEVER remove our top stuff
			if self.tree.get_string_from_iter(it).split(":")[0] == self.tree.get_string_from_iter(self.emergeIt):
				del self.mergequeue[self.tree.get_value(it,0)]
			else:
				self.unmergequeue.remove(self.tree.get_value(it,0))
			
			self.tree.remove(it)

class PackageWindow:
	"""A window with data about a specfic package."""

	def cbChanged (self, combo, data = None):
		"""Callback for the changed ComboBox.
		It then rebuilds the useList and the checkboxes."""
		# remove old useList
		self.useListScroll.remove(self.useList)
		
		# build new
		self.useList = self.buildUseList()
		self.useListScroll.add(self.useList)
		self.updateCheckBoxes()

		self.useListScroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)

		# set emerge-button-label
		if not self.actualPackage().is_installed():
			self.emergeBtn.set_label("_Emerge")
		else:
			self.emergeBtn.set_label("_Unmerge")
		
		# refresh - make window as small as possible
		self.table.show_all()
		self.window.resize(1,1)
		return True

	def buildVersCombo (self):
		"""Creates the combo box with the different versions."""
		combo = gtk.combo_box_new_text()

		# append versions
		for s in [x.get_version() for x in self.packages]:
			combo.append_text(s)
		
		# activate the first one
		try:
			best_version = ""
			if self.version:
				best_version = self.version
			else:
				best_version = geneticone.find_best_match(self.packages[0].get_cp(), (self.instPackages != [])).get_version()
			for i in range(len(self.packages)):
				if self.packages[i].get_version() == best_version:
					combo.set_active(i)
					break
		except AttributeError: # no package found
			combo.set_active(0)

		combo.connect("changed", self.cbChanged)
		
		return combo

	def actualPackage (self):
		"""Returns the actual package (a geneticone.Package-object)."""
		return self.packages[self.vCombo.get_active()]

	def cbButtonPressed (self, b, event, data = None):
		"""Callback for pressed checkboxes. Just quits the event-loop - no redrawing."""
		b.emit_stop_by_name("button-press-event")
		return True

	def cbEmergeClicked (self, button, data = None):
		"""Adds the package to the EmergeQueue."""
		if not geneticone.am_i_root():
			errorMB = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "You cannot (un)merge without being root.")
			errorMB.run()
			errorMB.destroy()
		else:
			unmerge = self.actualPackage().is_installed()
			self.queue.append(self.actualPackage().get_cpv(), unmerge)
			self.window.destroy()
		return True

	def updateCheckBoxes (self):
		"""Updates the checkboxes."""
		self.installedCheck.set_active(self.actualPackage().is_installed())
		self.maskedCheck.set_active(self.actualPackage().is_masked())
		self.testingCheck.set_active((self.actualPackage().get_mask_status() % 3) == 1)

	def buildUseList (self):
		"""Builds the useList."""
		store = gtk.ListStore(bool, str, str)

		pkg = self.actualPackage()
		for use in pkg.get_all_useflags():
			if pkg.is_installed() and use in pkg.get_set_useflags(): # flags set during install
				enabled = True
			elif (not pkg.is_installed()) and use in pkg.get_settings("USE").split(): # flags that would be set
				enabled = True
			else:
				enabled = False
			store.append([enabled, use, geneticone.get_use_desc(use, self.cp)])

		# build view
		view = gtk.TreeView(store)
		cell = gtk.CellRendererText()
		tCell = gtk.CellRendererToggle()
		view.append_column(gtk.TreeViewColumn("Enabled", tCell, active = 0))
		view.append_column(gtk.TreeViewColumn("Flags", cell, text = 1))
		view.append_column(gtk.TreeViewColumn("Description", cell, text = 2))

		if store.iter_n_children(None) == 0:
			view.set_child_visible(False)
		else:
			view.set_child_visible(True)
		return view

	def cbSizeCheck (self, event, data = None):
		if self.useListScroll:
			width, height = self.window.get_size()
			if height > gtk.gdk.screen_height():
				self.window.set_default_size(width, gtk.gdk.screen_height())
				self.window.resize(width, gtk.gdk.screen_height())
				self.useListScroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)

		return False

	def __init__ (self, parent, cp, queue = None, version = None):
		"""Build up window contents."""
		self.parent = parent # parent window
		self.cp = cp # category/package
		self.version = version # version - if not None this is used
		self.queue = queue
		
		# window
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.set_title(cp)
		self.window.set_modal(True)
		self.window.set_transient_for(parent)
		self.window.set_destroy_with_parent(True)
		self.window.set_resizable(False)
		self.window.set_default_size(1,1) # as small as possible
		self.window.connect("delete-event", lambda a,b: False)
		#self.window.connect("configure-event", self.cbSizeCheck)
		
		# packages and installed packages
		self.packages = geneticone.sort_package_list(geneticone.find_packages(cp, masked=True))
		self.instPackages = geneticone.sort_package_list(geneticone.find_installed_packages(cp, masked=True))

		# main structure - the table
		self.table = gtk.Table(rows=4,columns=2)
		self.window.add(self.table)

		# version-combo-box
		self.vCombo = self.buildVersCombo()
		self.table.attach(self.vCombo, 0, 1, 1, 2, yoptions = gtk.FILL)

		# the label (must be here, because it depends on the combo box)
		desc = self.actualPackage().get_env_var("DESCRIPTION")
		use_markup = True
		if not desc: 
			desc = "<no description>"
			use_markup = False
		else:
			desc = "<b>"+desc+"</b>"
		self.descLabel = gtk.Label(desc)
		self.descLabel.set_line_wrap(True)
		self.descLabel.set_justify(gtk.JUSTIFY_CENTER)
		self.descLabel.set_use_markup(use_markup)
		self.table.attach(self.descLabel, 0, 2, 0, 1, xoptions = gtk.FILL, ypadding = 10)

		# the check boxes
		checkHB = gtk.HBox (True, 1)
		self.table.attach(checkHB, 1, 2, 1, 2, yoptions = gtk.FILL)

		self.installedCheck = gtk.CheckButton()
		self.installedCheck.connect("button-press-event", self.cbButtonPressed)
		self.installedCheck.set_label("Installed")		
		checkHB.pack_start(self.installedCheck, True, False)

		self.maskedCheck = gtk.CheckButton()
		self.maskedCheck.connect("button-press-event", self.cbButtonPressed)
		self.maskedCheck.set_label("Masked")		
		checkHB.pack_start(self.maskedCheck, True, False)

		self.testingCheck = gtk.CheckButton()
		self.testingCheck.connect("button-press-event", self.cbButtonPressed)
		self.testingCheck.set_label("Testing")
		checkHB.pack_start(self.testingCheck, True, False)

		# use list
		self.useList = self.buildUseList()
		self.useListScroll = gtk.ScrolledWindow()
		self.useListScroll.add(self.useList)
		self.useListScroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER) # XXX: make this work correctly
		self.table.attach(self.useListScroll, 0, 2, 2, 3, ypadding = 10)
		
		# buttons
		buttonHB = gtk.HButtonBox()
		buttonHB.set_layout(gtk.BUTTONBOX_SPREAD)
		self.table.attach(buttonHB, 0, 2, 3, 4)
		
		self.emergeBtn = gtk.Button("_Emerge")
		if not self.queue: self.emergeBtn.set_sensitive(False)
		self.cancelBtn = gtk.Button("_Cancel")
		self.cancelBtn.connect("clicked", lambda x: self.window.destroy())
		self.emergeBtn.connect("clicked", self.cbEmergeClicked)
		buttonHB.pack_start(self.emergeBtn)
		buttonHB.pack_start(self.cancelBtn)

		# current status
		self.cbChanged(self.vCombo)

		# show
		self.window.show_all()

class MainWindow:
	"""Application main window."""
	
	def cbDelete (self, widget, data = None):
		"""Returns false -> window is deleted."""
		return False

	def cbDestroy (self, widget, data = None):
		"""Calls main_quit()."""
		gtk.main_quit()

	def createMainMenu (self):
		"""Creates the main menu."""
		# the menu-list
		mainMenuDesc = [
				( "/_File", None, None, 0, "<Branch>"),
				( "/File/_Close", None, self.cbDestroy, 0, "")
				]
		self.itemFactory = gtk.ItemFactory(gtk.MenuBar, "<main>", None)
		self.itemFactory.create_items(mainMenuDesc)
		return self.itemFactory.get_widget("<main>")

	def cbCatListSelection (self, view, data = None):
		"""Callback for a category-list selection. Updates the package list with these packages in the category."""
		if view == self.catList: # be sure it is the catList
			# get the selected category
			sel = view.get_selection()
			store, it = sel.get_selected()
			
			if it:
				# remove old one
				self.scroll_2.remove(self.pkgList)
				
				# create new package list
				self.pkgList = self.createPkgList(store.get_value(it,0))
				self.scroll_2.add(self.pkgList)
				self.scroll_2.show_all()
		return False

	def cbRowActivated (self, view, path, col, store = None):
		"""Callback for an activated row in the pkgList. Opens a package window."""
		if view == self.pkgList:
			package = store.get_value(store.get_iter(path), 0)
			if package[-1] == '*': package = package[:-1]
			PackageWindow(self.window, self.selCatName+"/"+package, self.queue)
		elif view == self.emergeView:
			if len(path) > 1:
				package = store.get_value(store.get_iter(path), 0)
				cat, name, vers, rev = geneticone.split_package_name(package)
				PackageWindow(self.window, cat+"/"+name, queue = None, version = vers+"-"+rev)
		return True

	def createCatList (self):
		"""Creates the category list."""
		store = gtk.ListStore(str)

		# build categories
		for p in geneticone.list_categories():
			store.append([p])
		# sort them alphabetically
		store.set_sort_column_id(0, gtk.SORT_ASCENDING)

		view = gtk.TreeView(store)
		cell = gtk.CellRendererText()
		col = gtk.TreeViewColumn("Categories", cell, text = 0)
		view.append_column(col)
		view.connect("cursor-changed", self.cbCatListSelection)
		view.connect("row-activated", lambda v,p,c : self.cbCatListSelection(v))
		view.set_search_column(0)

		return view

	packages = {} # directory category -> [packages]
	def createPkgList (self, name = None):
		"""Creates the package list. Gets the name of the category."""
		self.selCatName = name # actual category
		
		store = gtk.ListStore(str)

		# calculate packages
		if name:
			if name not in self.packages: # only calc packages if not already done
				self.packages[name] = []
				for p in unique_array([x.get_name() for x in geneticone.find_all_packages(name+"/")]):
					if geneticone.find_installed_packages(name+"/"+p, masked=True) != []:
						p += "*" # append a '*' if the package is installed
					self.packages[name].append(p)

			for p in self.packages[name]:
				store.append([p])

			# sort alphabetically
			store.set_sort_column_id(0, gtk.SORT_ASCENDING)

		# build view
		pkgList = gtk.TreeView(store)
		cell = gtk.CellRendererText()
		col = gtk.TreeViewColumn("Packages", cell, text = 0)
		pkgList.append_column(col)
		pkgList.connect("row-activated", self.cbRowActivated, store)

		return pkgList

	def cbRemoveClicked (self, button, data = None):
		selected = self.emergeView.get_selection()

		if selected:
			model, iter = selected.get_selected()

			if not model.iter_parent(iter): # top-level
				if model.iter_n_children(iter) > 0: # and has children which can be removed :)
					askMB = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, "Do you really want to clear the whole queue?")
					if askMB.run() == gtk.RESPONSE_YES :
						self.queue.removeAll(iter)
					askMB.destroy()
			elif model.iter_parent(model.iter_parent(iter)): # this is in the 3rd level => dependency
				infoMB = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "You cannot remove dependencies. :)")
				infoMB.run()
				infoMB.destroy()
			else:
				self.queue.remove(iter)
		
		return True

	def __init__ (self):
		"""Build up window"""

		# window
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.set_title("Genetic")
		self.window.connect("delete_event", self.cbDelete)
		self.window.connect("destroy", self.cbDestroy)
		self.window.set_border_width(2)
		self.window.set_geometry_hints (self.window, min_width = 600, min_height = 700, max_height = gtk.gdk.screen_height(), max_width = gtk.gdk.screen_width())
		self.window.set_resizable(True)

		# main vb
		vb = gtk.VBox(False, 1)
		self.window.add(vb)

		# menubar
		menubar = self.createMainMenu()
		vb.pack_start(menubar, False)
		
		# VPaned holding the lists and the Terminal
		vpaned = gtk.VPaned()
		vpaned.set_position(400)
		vb.pack_start(vpaned, True, True)

		# a HB holding the lists
		hb = gtk.HBox(True, 5)
		hbFrame = gtk.Frame()
		hbFrame.add(hb)
		hbFrame.set_shadow_type(gtk.SHADOW_IN)
		vpaned.pack1(hbFrame, shrink = True, resize = True)
		
		self.scroll_1 = gtk.ScrolledWindow()
		self.scroll_2 = gtk.ScrolledWindow()
		self.scroll_1.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
		self.scroll_2.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
		hb.pack_start(self.scroll_1, True, True)
		hb.pack_start(self.scroll_2, True, True)
		
		# create cat List
		self.catList = self.createCatList()
		self.scroll_1.add(self.catList)
		
		# create pkg list
		self.pkgList = self.createPkgList()
		self.scroll_2.add(self.pkgList)
		
		# queue list
		queueVB = gtk.VBox(False, 0)
		hb.pack_start(queueVB, True, True)
		
		emergeStore = gtk.TreeStore(str)
		self.emergeView = gtk.TreeView(emergeStore)
		cell = gtk.CellRendererText()
		col = gtk.TreeViewColumn("Queue", cell, text = 0)
		self.emergeView.append_column(col)
		self.emergeView.connect("row-activated", self.cbRowActivated, emergeStore)
		queueVB.pack_start(self.emergeView, True, True)

		# buttons right unter the queue list
		buttonBox = gtk.HButtonBox()
		queueVB.pack_start(buttonBox, False)
		emergeBtn = gtk.Button("_Emerge")
		emergeBtn.connect("clicked", lambda a: self.queue.emerge(force=True))
		unmergeBtn = gtk.Button("_Unmerge")
		unmergeBtn.connect("clicked", lambda a: self.queue.unmerge(force=True))
		removeBtn = gtk.Button("_Remove")
		removeBtn.connect("clicked", self.cbRemoveClicked)
		buttonBox.pack_start(emergeBtn)
		buttonBox.pack_start(removeBtn)
		buttonBox.pack_start(unmergeBtn)
		
		# the terminal
		term = vte.Terminal()
		term.set_scrollback_lines(1024)
		term.set_scroll_on_output(True)
		termBox = gtk.HBox(False, 0)
		termScroll = gtk.VScrollbar(term.get_adjustment())
		termBox.pack_start(term, True, True)
		termBox.pack_start(termScroll, False)
		
		termFrame = gtk.Frame("Console")
		termFrame.set_shadow_type(gtk.SHADOW_IN)
		termFrame.add(termBox)
		vpaned.pack2(termFrame, shrink = True, resize = True)

		# show
		self.window.show_all()

		# set emerge queue
		self.queue = EmergeQueue(console=term, tree = emergeStore)
	
	def main (self):
		"""Main."""
		gtk.main()

def blockedDialog (blocked, blocks):
	dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, blocked+" is blocked by "+blocks+".\nPlease unmerge the blocking package.")
	dialog.run()
	dialog.destroy()

if __name__ == "__main__":
	m = MainWindow()
	m.main()