summaryrefslogtreecommitdiff
path: root/portato/gui/qt/windows.py
blob: 46d9ec68418ce4000bbdc2243a7ab0ea7085812f (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
# -*- coding: utf-8 -*-
#
# File: portato/gui/qt/windows.py
# This file is part of the Portato-Project, a graphical portage-frontend.
#
# Copyright (C) 2007 René 'Necoro' Neumann
# This is free software.  You may redistribute copies of it under the terms of
# the GNU General Public License version 2.
# There is NO WARRANTY, to the extent permitted by law.
#
# Written by René 'Necoro' Neumann <necoro@necoro.net>

# qt4
from PyQt4 import QtGui, uic, QtCore
import sip

# our backend stuff
from portato.helper import *
from portato.constants import CONFIG_LOCATION, VERSION, DATA_DIR
from portato.backend import flags, system
from portato.backend.exceptions import *

from portato.gui.gui_helper import Database, Config, EmergeQueue

# own GUI stuff
from terminal import QtConsole
from tree import QtTree
from dialogs import *

UI_DIR = DATA_DIR+"ui/"

#XXX: global variables are bad
app = QtGui.QApplication([])

def qCheck (check):
	if check:
		return QtCore.Qt.Checked
	else:
		return QtCore.Qt.Unchecked

class WindowMeta (sip.wrappertype, type):

	def __new__ (cls, name, bases, dict):
		new_bases = uic.loadUiType(UI_DIR+name+".ui")
		dict.update(_bases = new_bases)
		dict.update(_qt_base = new_bases[1])
		return super(WindowMeta, cls).__new__(cls, name, new_bases+bases, dict)

	def __init__ (cls, name, bases, dict):
		b = dict["_bases"]
		del dict["_bases"]
		super(WindowMeta, cls).__init__(name, b+bases, dict)

class Window:

	def __init__(self, parent = None):
		self._qt_base.__init__(self, parent)
		self.setupUi(self)

class AboutDialog (Window):
	"""A window showing the "about"-informations."""
	__metaclass__ = WindowMeta

	def __init__ (self, parent = None):
		Window.__init__(self, parent)

		self.label.setText("""
<font size=5><b>Portato v.%s</b></font><br><br>
A Portage-GUI<br>
<br>		
This software is licensed under the terms of the GPLv2.<br>
Copyright (C) 2006-2007 Ren&eacute; 'Necoro' Neumann &lt;necoro@necoro.net&gt;<br>
<br>
<font size=1>Thanks to Fred for support and ideas :P</font>""" % VERSION)

		self.adjustSize()

class SearchDialog (Window):
	"""A window showing the results of a search process."""
	__metaclass__ = WindowMeta

	def __init__ (self, parent, list, jumpTo):
		"""Constructor.

		@param parent: parent-window
		@type parent: QtGui.QWidget
		@param list: list of results to show
		@type list: string[]
		@param jumpTo: function to call if "OK"-Button is hit
		@type jumpTo: function(string)"""

		Window.__init__(self, parent)

		self.comboBox.addItems(list)
		self.comboBox.setCurrentIndex(0)
		self.jumpTo = jumpTo

		QtCore.QObject.connect(self, QtCore.SIGNAL("accepted()"), self.finish)

	def finish (self):
		s = str(self.comboBox.currentText())
		self.done(0)
		self.jumpTo(s)

class PackageDetails:

	def __init__ (self, window):
		self.window = window
		self.window.pkgTab.setHidden(True)
		self.window.tabWidget.removeTab(0)

		self.window.installedCheck.blockSignals(True)

		QtCore.QObject.connect(self.window.versCombo, QtCore.SIGNAL("currentIndexChanged(int)"), self.cb_combo_changed)
		
		QtCore.QObject.connect(self.window.pkgEmergeBtn, QtCore.SIGNAL("clicked()"), self.cb_emerge_clicked)
		QtCore.QObject.connect(self.window.pkgUnmergeBtn, QtCore.SIGNAL("clicked()"), self.cb_unmerge_clicked)
		QtCore.QObject.connect(self.window.pkgRevertBtn, QtCore.SIGNAL("clicked()"), self.cb_revert_clicked)

		#QtCore.QObject.connect(self.window.maskedCheck, QtCore.SIGNAL("clicked(bool)"), self.cb_masked_clicked)
		#QtCore.QObject.connect(self.window.testingCheck, QtCore.SIGNAL("clicked(bool)"), self.cb_testing_clicked)

	def update (self, cp, queue = None, version = None, doEmerge = True, instantChange = False):
		"""Updates the table to show the contents for the package.
		
		@param cp: the selected package
		@type cp: string (cp)
		@param queue: emerge-queue (if None the emerge-buttons are disabled)
		@type queue: EmergeQueue
		@param version: if not None, specifies the version to select
		@type version: string
		@param doEmerge: if False, the emerge buttons are disabled
		@type doEmerge: False
		@param instantChange: if True the changed keywords are updated instantly
		@type instantChange: boolean"""

		self.cp = cp
		self.version = version
		self.queue = queue
		self.doEmerge = doEmerge
		self.instantChange = instantChange
		
		# packages and installed packages
		self.packages = system.sort_package_list(system.find_packages(cp, masked = True))
		self.instPackages = system.sort_package_list(system.find_installed_packages(cp, masked = True))

		# comboBox
		self.set_combo()

		# the labels
		desc = self.actual_package().get_package_settings("DESCRIPTION").replace("&","&amp;")
		
		if not desc: 
			desc = "<no description>"
		else:
			desc = "<b>%s</b>" % desc
		
		self.window.descLabel.setText(desc)
		self.window.nameLabel.setText("<i><u>%s</i></u>" % self.actual_package().get_cp())

		# disable buttons when emerging is not allowed
		if not self.queue or not self.doEmerge: 
			self.window.pkgEmergeBtn.setEnabled(False)
			self.window.pkgUnmergeBtn.setEnabled(False)
		
		# first update -> show
		if self.window.pkgTab.isHidden():
			self.window.tabWidget.insertTab(0, self.window.pkgTab, "Package")
			self.window.pkgTab.setHidden(False)

		self.window.tabWidget.setCurrentIndex(self.window.PKG_PAGE)

	def set_combo (self):
		self.window.versCombo.clear()
		self.window.versCombo.addItems([x.get_version() for x in self.packages])

		try:
			best_version = ""
			if self.version:
				best_version = self.version
			else:
				best_version = system.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:
					self.window.versCombo.setCurrentIndex(i)
		except AttributeError:
			self.window.versCombo.setCurrentIndex(0)

	def build_use_list (self):
		self.window.useList.clear()
		self.window.useList.setHeaderLabels(["Enabled","Flag","Description"])
		
		pkg = self.actual_package()
		pkg_flags = pkg.get_all_use_flags()
		pkg_flags.sort()
		
		actual_exp = None
		actual_exp_it = self.window.useList
		
		for use in pkg_flags:
			exp = pkg.use_expanded(use, suggest = actual_exp)
			if exp is not None:
				if exp != actual_exp:
					actual_exp_it = QtGui.QTreeWidgetItem(self.window.useList, ["", exp, ""])
					actual_exp = exp
			else:
				actual_exp_it = self.window.useList
				actual_exp = None

			item = QtGui.QTreeWidgetItem(actual_exp_it, ["", use, system.get_use_desc(use, self.cp)])
			item.setCheckState(0, qCheck(pkg.is_use_flag_enabled(use)))

	def _update_keywords (self, emerge, update = False):
		if emerge:
			try:
				try:
					self.queue.append(self.actual_package().get_cpv(), unmerge = False, update = update)
				except PackageNotFoundException, e:
					if unmask_dialog(self.window, e[0]) == QtGui.QMessageBox.Yes :
						self.queue.append(self.actual_package().get_cpv(), unmerge = False, unmask = True, update = update)
			except BlockedException, e:
				blocked_dialog(self.window, e[0], e[1])
		else:
			try:
				self.queue.append(self.actual_package().get_cpv(), unmerge = True)
			except PackageNotFoundException, e:
				debug("Package could not be found",e[0], error = 1)


	def actual_package (self):
		"""Returns the actual selected package.
		
		@returns: the actual selected package
		@rtype: backend.Package"""
		
		return self.packages[self.window.versCombo.currentIndex()]

	def cb_emerge_clicked (self):
		"""Callback for pressed emerge-button. Adds the package to the EmergeQueue."""
		if not am_i_root():
			not_root_dialog(self.window)
		else:
			self._update_keywords(True)
			self.window.tabWidget.setCurrentIndex(self.window.QUEUE_PAGE)
		return True

	def cb_unmerge_clicked (self):
		"""Callback for pressed unmerge-button. Adds the package to the EmergeQueue."""
		if not am_i_root():
			not_root_dialog(self.window)
		else:
			self._update_keywords(False)
			self.window.tabWidget.setCurrentIndex(self.window.QUEUE_PAGE)
		return True

	def cb_revert_clicked (self, button):
		"""Callback for pressed revert-button."""
		self.actual_package().remove_new_use_flags()
		self.actual_package().remove_new_masked()
		self.actual_package().remove_new_testing()
		self.cb_combo_changed()
		if self.instantChange:
			self._update_keywords(True, update = True)
		return True

	def cb_combo_changed (self):
		"""Callback for the changed ComboBox.
		It then rebuilds the useList and the checkboxes."""
		
		# build new
		self.build_use_list()
		pkg = self.actual_package()

		shown = QtGui.QSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Fixed)
		hidden = QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Fixed)
		
		#
		# rebuild the buttons and checkboxes in all the different manners which are possible
		#
		if (not pkg.is_in_system()) or pkg.is_missing_keyword():
			if not pkg.is_in_system():
				self.window.missingLabel.setSizePolicy(hidden)
				self.window.notInSysLabel.setSizePolicy(shown)
			else: # missing keyword
				self.window.missingLabel.setSizePolicy(shown)
				self.window.notInSysLabel.setSizePolicy(hidden)
			
			self.window.installedCheck.setSizePolicy(hidden)
			self.window.maskedCheck.setSizePolicy(hidden)
			self.window.testingCheck.setSizePolicy(hidden)
			self.window.pkgEmergeBtn.setEnabled(False)
		else: # normal package
			self.window.missingLabel.setSizePolicy(hidden)
			self.window.notInSysLabel.setSizePolicy(hidden)
			self.window.installedCheck.setSizePolicy(shown)
			self.window.maskedCheck.setSizePolicy(shown)
			self.window.testingCheck.setSizePolicy(shown)
			if self.doEmerge:
				self.window.pkgEmergeBtn.setEnabled(True)
			self.window.installedCheck.setCheckState(qCheck(pkg.is_installed()))
			
			if pkg.is_masked(use_changed = False) and not pkg.is_masked(use_changed = True):
				self.window.maskedCheck.setText("(Masked)")
			else:
				self.window.maskedCheck.setText("Masked")
			
			self.window.maskedCheck.setCheckState(qCheck(pkg.is_masked(use_changed = False)))
			
			if pkg.is_testing(use_keywords = False) and not pkg.is_testing(use_keywords = True):
				self.window.testingCheck.setText("(Testing)")
			else:
				self.window.testingCheck.setText("Testing")
			
			self.window.testingCheck.setCheckState(qCheck(pkg.is_testing(use_keywords = False)))

		if self.doEmerge:
			# set emerge-button-label
			if not self.actual_package().is_installed():
				self.window.pkgEmergeBtn.setText("E&merge")
				self.window.pkgUnmergeBtn.setEnabled(False)
			else:
				self.window.pkgEmergeBtn.setText("Re&merge")
				self.window.pkgUnmergeBtn.setEnabled(True)
		
class MainWindow (Window):

	__metaclass__ = WindowMeta

	# NOTEBOOK PAGE CONSTANTS
	PKG_PAGE = 0
	QUEUE_PAGE = 1
	CONSOLE_PAGE = 2
	
	def __init__ (self):
		Window.__init__(self)

		self.setWindowTitle(("Portato (%s)" % VERSION))
		self.statusbar.showMessage("Portato - A Portage GUI")

		self.doUpdate = False
		self.pkgDetails = PackageDetails(self)
		
		# package db
		self.db = Database()
		self.db.populate()

		# config
		try:
			self.cfg = Config(CONFIG_LOCATION)
		except IOError, e:
			io_ex_dialog(self, e)
			raise

		self.cfg.modify_external_configs()

		# the two lists
		self.build_pkg_list()
		self.build_cat_list()
		QtCore.QObject.connect(self.selCatListModel, QtCore.SIGNAL("currentChanged(QModelIndex, QModelIndex)"), self.cb_cat_list_selected)
		QtCore.QObject.connect(self.selPkgListModel, QtCore.SIGNAL("currentChanged(QModelIndex, QModelIndex)"), self.cb_pkg_list_selected)

		# build console
		self.console = QtConsole(self.consoleTab)
		self.consoleLayout = QtGui.QVBoxLayout()
		self.consoleLayout.setMargin(0)
		self.consoleLayout.setSpacing(0)
		self.consoleTab.setLayout(self.consoleLayout)
		self.consoleLayout.addWidget(self.console)

		# build queueList
		self.queueList.setHeaderLabels(["Package", "Additional infos"])
		self.queueTree = QtTree(self.queueList)

		QtCore.QObject.connect(self, QtCore.SIGNAL("doTitleUpdate"), self._title_update)

		# set emerge queue
		self.queue = EmergeQueue(console = self.console, tree = self.queueTree, db = self.db, title_update = self.title_update)

		self.show()
	
	def title_update (self, title):
		self.emit(QtCore.SIGNAL("doTitleUpdate"), title)

	def _title_update (self, title):
		if title == None: title = "Console"
		else: title = ("Console (%s)" % title)

		self.tabWidget.setTabText(self.CONSOLE_PAGE, title)

	def jump_to (self, cp):
		"""Is called when we want to jump to a specific package."""
		self.pkgDetails.update(cp, self.queue)

	def fill_pkg_list (self, cat):
		self.pkgListModel.setStringList([name for (name,inst) in self.db.get_cat(cat)])

	def build_pkg_list (self):
		self.pkgListModel = QtGui.QStringListModel([])
		self.pkgListModel.sort(0)
		self.selPkgListModel = QtGui.QItemSelectionModel(self.pkgListModel)
		self.pkgList.setModel(self.pkgListModel)
		self.pkgList.setSelectionModel(self.selPkgListModel)

	def build_cat_list (self):
		self.catListModel = QtGui.QStringListModel(system.list_categories())
		self.catListModel.sort(0)
		self.selCatListModel = QtGui.QItemSelectionModel(self.catListModel)
		self.catList.setModel(self.catListModel)
		self.catList.setSelectionModel(self.selCatListModel)

	@QtCore.pyqtSignature("")
	def on_aboutAction_triggered (self):
		AboutDialog(self).exec_()

	@QtCore.pyqtSignature("")
	def on_searchBtn_clicked (self):
		"""Do a search."""
		text = str(self.searchEdit.text())
		if text != "":
			packages = system.find_all_packages(text, withVersion = False)

			if packages == []:
				nothing_found_dialog(self)
			else:
				if len(packages) == 1:
					self.jump_to(packages[0])
				else:
					SearchDialog(self, packages, self.jump_to).exec_()

	@QtCore.pyqtSignature("")
	def on_removeBtn_clicked (self):
		"""Removes a selected item in the (un)emerge-queue if possible."""
		selected = self.queueList.currentItem()

		if selected:
			if not selected.parent(): # top-level
				if self.queueTree.iter_has_children(selected): # and has children which can be removed :)
					if remove_queue_dialog(self) == QtGui.QMessageBox.Yes :
						self.queue.remove_children(selected)
						self.doUpdate = False
			
			elif selected.parent().parent(): # this is in the 3rd level => dependency
				remove_deps_dialog(self)
			else:
				self.queue.remove_with_children(selected)
				self.doUpdate = False

	@QtCore.pyqtSignature("")
	def on_emergeBtn_clicked (self):
		"""Do emerge."""
		
		self.tabWidget.setCurrentIndex(self.CONSOLE_PAGE)
		
		if len(flags.newUseFlags) > 0:
			changed_flags_dialog(self, "use flags")
			flags.write_use_flags()
		
		if len(flags.new_masked)>0 or len(flags.new_unmasked)>0 or len(flags.newTesting)>0:
			debug("new masked:",flags.new_masked)
			debug("new unmasked:", flags.new_unmasked)
			debug("new testing:", flags.newTesting)
			changed_flags_dialog(self, "masking keywords")
			flags.write_masked()
			flags.write_testing()
			system.reload_settings()
		
		if not self.doUpdate:
			self.queue.emerge(force=True, options = ["--nospinner"])
		else:
			self.queue.update_world(force=True, newuse = self.cfg.get_boolean("newuse_opt"), deep = self.cfg.get_boolean("deep_opt"), options = ["--nospinner"])
			self.doUpdate = False

	@QtCore.pyqtSignature("")
	def on_unmergeBtn_clicked (self):
		"""Do unmerge."""

		self.tabWidget.setCurrentIndex(self.CONSOLE_PAGE)
		self.queue.unmerge(force = True)

	def cb_cat_list_selected (self, index, prev):
		self.selCatName = str(index.data().toString())
		self.fill_pkg_list(self.selCatName)

	def cb_pkg_list_selected (self, index, prev):
		self.pkgDetails.update(self.selCatName+"/"+str(index.data().toString()), self.queue)

	def main (self):
		app.exec_()