aboutsummaryrefslogtreecommitdiff
blob: 4d64e59a0b1325ebc8a48aa87ebf0625b089fe53 (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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# Copyright 2005-2007 Red Hat, Inc.
#
# Jeremy Katz <katzj@redhat.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 2 only
#
# 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import os, sys
import logging
import gettext

import gtk
import gtk.glade
import gtk.gdk as gdk
import gobject

import yum
import yum.Errors
try:
    import repomd.mdErrors as mdErrors
except ImportError: # yum 2.9.x
    mdErrors = yum.Errors
from yum.constants import *
from compssort import *

I18N_DOMAIN="anaconda"

import rpm

def sanitizeString(s, translate = True):
    if len(s) == 0:
        return s

    if not translate:
        i18ndomains = []
    elif hasattr(rpm, "expandMacro"):
        i18ndomains = rpm.expandMacro("%_i18ndomains").split(":")
    else:
        i18ndomains = ["redhat-dist"]
        
    # iterate over i18ndomains to find the translation
    for d in i18ndomains:
        r = gettext.dgettext(d, s)
        if r != s:
            s = r
            break
        
    s = s.replace("\n\n", "\x00")
    s = s.replace("\n", " ")
    s = s.replace("\x00", "\n\n")
    s = s.replace("&", "&amp;")
    s = s.replace("<", "&lt;")
    s = s.replace(">", "&gt;")
    if type(s) != unicode:
        try:
            s = unicode(s, "utf-8")
        except UnicodeDecodeError, e:
            sys.stderr.write("Unable to convert %s to a unicode object: %s\n" % (s, e))
            return ""
    return s

# given a package object, spit out a string reasonable for the list widgets
def listEntryString(po):
    desc = po.returnSimple('summary') or ''
    pkgStr = "%s-%s-%s.%s" % (po.name, po.version, po.release, po.arch)
    desc = "<b>%s</b> - %s" %(pkgStr, sanitizeString(desc))
    return desc

GLADE_FILE = "GroupSelector.glade"

def _getgladefile(fn):
    if os.path.exists(fn):
        return fn
    elif os.path.exists("data/%s" %(fn,)):
        return "data/%s" %(fn,)
    else:
        return "/usr/share/pirut/ui/%s" %(fn,)

t = gettext.translation(I18N_DOMAIN, "/usr/share/locale", fallback = True)
_ = t.lgettext


def _deselectPackage(ayum, group, pkg):
    grpid = group.groupid
    try:
        pkgs = ayum.pkgSack.returnNewestByName(pkg)
    except mdErrors.PackageSackError:
        log = logging.getLogger("yum.verbose")
        log.debug("no such package %s from group %s" % (pkg, grpid))
    if pkgs:
        pkgs = ayum.bestPackagesFromList(pkgs)
    for po in pkgs:
        txmbrs = ayum.tsInfo.getMembers(pkgtup = po.pkgtup)
        for txmbr in txmbrs:
            try:
                txmbr.groups.remove(grpid)
            except ValueError:
                log = logging.getLogger("yum.verbose")                
                log.debug("package %s was not marked in group %s" %(po, grpid))
            if len(txmbr.groups) == 0:
                ayum.tsInfo.remove(po.pkgtup)

def _selectPackage(ayum, group, pkg):
    grpid = group.groupid
    try:
        txmbrs = ayum.install(name = pkg)
    except yum.Errors.InstallError, e:
        log = logging.getLogger("yum.verbose")
        log.info("No package named %s available to be installed: %s" %(pkg, e))
    else:
        map(lambda x: x.groups.append(grpid), txmbrs)

def _catHasGroupWithPackages(cat, ayum):
    grps = map(lambda x: ayum.comps.return_group(x),
                   filter(lambda x: ayum.comps.has_group(x), cat.groups))
    for g in grps:
        if ayum._groupHasPackages(g):
            return True
    return False

class OptionalPackageSelector:
    def __init__(self, yumobj, group, parent = None, getgladefunc = None):
        self.ayum = yumobj
        self.group = group

        if getgladefunc:
            xmlfn = getgladefunc(GLADE_FILE)
        else:
            xmlfn = _getgladefile(GLADE_FILE)

        self.xml = gtk.glade.XML(xmlfn, "groupDetailsDialog",
                                 domain=I18N_DOMAIN)

        self.window = self.xml.get_widget("groupDetailsDialog")
        if parent:
            self.window.set_transient_for(parent)
        self.window.set_title(_("Packages in %s") %
                               xmltrans(group.name, group.translated_name))
        self.window.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
        self.window.set_size_request(600, 400)
        self._createStore()
        self._populate()

    def __search_pkgs(self, model, col, key, i):
        val = model.get_value(i, 2).returnSimple('name')
        if val.lower().startswith(key.lower()):
            return False
        return True

    def _createStore(self):
        self.pkgstore = gtk.ListStore(gobject.TYPE_BOOLEAN,
                                      gobject.TYPE_STRING,
                                      gobject.TYPE_PYOBJECT)
        tree = self.xml.get_widget("packageList")
        tree.set_model(self.pkgstore)

        column = gtk.TreeViewColumn(None, None)
        cbr = gtk.CellRendererToggle()
        cbr.connect ("toggled", self._pkgToggled)
        column.pack_start(cbr, False)
        column.add_attribute(cbr, 'active', 0)
        tree.append_column(column)
        
        column = gtk.TreeViewColumn(None, None)
        renderer = gtk.CellRendererText()
        column.pack_start(renderer, True)
        column.add_attribute(renderer, 'markup', 1)
        tree.append_column(column)
        tree.set_search_equal_func(self.__search_pkgs)
        tree.connect("row-activated", self._rowToggle)

        self.pkgstore.set_sort_column_id(1, gtk.SORT_ASCENDING)

    def _rowToggle(self, tree, path, col):
        self._pkgToggled(None, path)

    def _pkgToggled(self, widget, path):
        if type(path) == type(str):
            i = self.pkgstore.get_iter_from_string(path)
        else:
            i = self.pkgstore.get_iter(path)
        sel = self.pkgstore.get_value(i, 0)
        pkg = self.pkgstore.get_value(i, 2).returnSimple('name')
        if sel and not self.ayum.simpleDBInstalled(name = pkg):
            _deselectPackage(self.ayum, self.group, pkg)
        elif sel:
            self.ayum.remove(name = pkg)
        elif self.ayum.simpleDBInstalled(name = pkg):
            txmbrs = self.ayum.tsInfo.matchNaevr(name = pkg)
            for tx in txmbrs:
                if tx.output_state == TS_ERASE:
                    self.ayum.tsInfo.remove(tx.pkgtup)
        else:
            _selectPackage(self.ayum, self.group, pkg)
        self.pkgstore.set_value(i, 0, not sel)
            

    def __getPackageObject(self, pkgname):
        pos = self.ayum.pkgSack.searchNevra(name=pkgname)
        if len(pos) > 0:
            return pos[0]
        return None

    def _populate(self):
        pkgs = self.group.default_packages.keys() + \
               self.group.optional_packages.keys()
        for pkg in pkgs:
            po = self.__getPackageObject(pkg)
            if not po:
                continue

            # Don't display obsolete packages in the UI
            if self.ayum.up.checkForObsolete([po.pkgtup]).has_key(po.pkgtup):
                continue

            self.pkgstore.append([self.ayum.isPackageInstalled(pkg), listEntryString(po), po])

    def run(self):
        self.window.show_all()
        return self.window.run()

    def destroy(self):
        return self.window.destroy()

# the GroupSelector requires a YumBase object which also implements the
# following additional methods:
# * isPackageInstalled(p): is there a package named p installed or selected
# * isGroupInstalled(grp): is there a group grp installed or selected
class GroupSelector:
    def __init__(self, yumobj, getgladefunc = None, framefunc = None):
        self.ayum = yumobj

        self.getgladefunc = getgladefunc
        self.framefunc = framefunc
        if getgladefunc:
            xmlfn = getgladefunc(GLADE_FILE)
        else:
            xmlfn = _getgladefile(GLADE_FILE)

        self.xml = gtk.glade.XML(xmlfn, "groupSelectionBox",
                                 domain=I18N_DOMAIN)
        self.vbox = self.xml.get_widget("groupSelectionBox")
        self.xml.get_widget("detailsButton").set_sensitive(False)

        self.menuxml = gtk.glade.XML(xmlfn, "groupPopupMenu",
                                     domain=I18N_DOMAIN)
        self.groupMenu = self.menuxml.get_widget("groupPopupMenu")

        self._connectSignals()
        self._createStores()
        self.vbox.show()

    def _connectSignals(self):
        sigs = { "on_detailsButton_clicked": self._optionalPackagesDialog,
                 "on_groupList_button_press": self._groupListButtonPress,
                 "on_groupList_popup_menu": self._groupListPopup, }
        self.xml.signal_autoconnect(sigs)

        menusigs = { "on_select_activate": self._selectAllPackages,
                     "on_selectgrp_activate": self._groupSelect,
                     "on_deselectgrp_activate": self._groupDeselect,
                     "on_deselect_activate": self._deselectAllPackages }
        self.menuxml.signal_autoconnect(menusigs)

    def _createStores(self):
        self._createCategoryStore()
        self._createGroupStore()

        b = gtk.TextBuffer()
        self.xml.get_widget("groupDescriptionTextView").set_buffer(b)

    def _createCategoryStore(self):        
        # display string, category object
        self.catstore = gtk.TreeStore(gobject.TYPE_STRING,
                                      gobject.TYPE_PYOBJECT)
        tree = self.xml.get_widget("categoryList")
        tree.set_model(self.catstore)

        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn('Text', renderer, markup=0)
        column.set_clickable(False)
        tree.append_column(column)
        tree.columns_autosize()
        tree.set_enable_search(False)

        selection = tree.get_selection()
        selection.connect("changed", self._categorySelected)

    def _createGroupStore(self):
        # checkbox, display string, object
        self.groupstore = gtk.TreeStore(gobject.TYPE_BOOLEAN,
                                        gobject.TYPE_STRING,
                                        gobject.TYPE_PYOBJECT,
                                        gobject.TYPE_OBJECT)
        tree = self.xml.get_widget("groupList")
        tree.set_model(self.groupstore)

        column = gtk.TreeViewColumn(None, None)
        column.set_clickable(True)
        pixr = gtk.CellRendererPixbuf()
        pixr.set_property('stock-size', 1)
        column.pack_start(pixr, False)
        column.add_attribute(pixr, 'pixbuf', 3)
        cbr = gtk.CellRendererToggle()
        column.pack_start(cbr, False)
        column.add_attribute(cbr, 'active', 0)
        cbr.connect ("toggled", self._groupToggled)
        tree.append_column(column)
        
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn('Text', renderer, markup=1)
        column.set_clickable(False)
        tree.append_column(column)
        tree.columns_autosize()
        tree.set_enable_search(False)
        tree.grab_focus()

        selection = tree.get_selection()
        selection.connect("changed", self._groupSelected)
        selection.set_mode(gtk.SELECTION_MULTIPLE)

    def _get_pix(self, fn):
        imgsize = 24
        pix = gtk.gdk.pixbuf_new_from_file(fn)
        if pix.get_height() != imgsize or pix.get_width() != imgsize:
            pix = pix.scale_simple(imgsize, imgsize,
                                   gtk.gdk.INTERP_BILINEAR)
        return pix

    def _categorySelected(self, selection):
        self.groupstore.clear()
        (model, i) = selection.get_selected()
        if not i:
            return
        cat = model.get_value(i, 1)

        # fall back to the category pixbuf
        fbpix = None
        fn = "/usr/share/pixmaps/comps/%s.png" %(cat.categoryid,)
        if os.access(fn, os.R_OK):
            fbpix = self._get_pix(fn)
        self._populateGroups(cat.groups, fbpix)

    def _populateGroups(self, groups, defaultpix = None):
        grps = map(lambda x: self.ayum.comps.return_group(x),
                   filter(lambda x: self.ayum.comps.has_group(x), groups))
        grps.sort(ui_comps_sort)
        for grp in grps:
            if not self.ayum._groupHasPackages(grp):
                continue
            s = "<span size=\"large\" weight=\"bold\">%s</span>" % xmltrans(grp.name, grp.translated_name)

            fn = "/usr/share/pixmaps/comps/%s.png" % grp.groupid
            if os.access(fn, os.R_OK):
                pix = self._get_pix(fn)
            elif defaultpix:
                pix = defaultpix
            else:
                pix = None
            self.groupstore.append(None,
                                   [self.ayum.isGroupInstalled(grp),s,grp,pix])

        tree = self.xml.get_widget("groupList")
        gobject.idle_add(lambda x: x.flags() & gtk.REALIZED and x.scroll_to_point(0, 0), tree)
        self.xml.get_widget("optionalLabel").set_text("")
        self.xml.get_widget("detailsButton").set_sensitive(False)

        # select the first group
        i = self.groupstore.get_iter_first()
        if i is not None:
            sel = self.xml.get_widget("groupList").get_selection()
            sel.select_iter(i)

    def _groupSelected(self, selection):
        if selection.count_selected_rows() != 1:
            # if we have more groups (or no group) selected, then
            # we can't show a description or allow selecting optional
            self.__setGroupDescription(None)
            return
        (model, paths) = selection.get_selected_rows()
        grp = model.get_value(model.get_iter(paths[0]), 2)
        self.__setGroupDescription(grp)

    def __setGroupDescription(self, grp):
        b = self.xml.get_widget("groupDescriptionTextView").get_buffer()
        b.set_text("")
        if grp is None:
            return
        
        if grp.description:
            txt = xmltrans(grp.description, grp.translated_description)
        else:
            txt = xmltrans(grp.name, grp.translated_name)

        inst = 0
        cnt = 0
        pkgs = grp.default_packages.keys() + grp.optional_packages.keys()
        for p in pkgs:
            if self.ayum.isPackageInstalled(p):
                cnt += 1
                inst += 1
            elif self.ayum.pkgSack.searchNevra(name=p):
                cnt += 1
            else:
                log = logging.getLogger("yum.verbose")
                log.debug("no such package %s for %s" %(p, grp.groupid))

        b.set_text(txt)
        if cnt == 0 or not self.ayum.isGroupInstalled(grp):
            self.xml.get_widget("detailsButton").set_sensitive(False)
            self.xml.get_widget("optionalLabel").set_text("")
        else:
            self.xml.get_widget("detailsButton").set_sensitive(True)
            txt = _("Optional packages selected: %(inst)d of %(cnt)d") \
                    % {'inst': inst, 'cnt': cnt}
            self.xml.get_widget("optionalLabel").set_markup(_("<i>%s</i>") %(txt,))

    def _groupToggled(self, widget, path, sel = None, updateText = True):
        if type(path) == type(str):
            i = self.groupstore.get_iter_from_string(path)
        else:
            i = self.groupstore.get_iter(path)
        if sel is None:
            sel = not self.groupstore.get_value(i, 0)
            
        self.groupstore.set_value(i, 0, sel)
        grp = self.groupstore.get_value(i, 2)

        self.vbox.window.set_cursor(gdk.Cursor(gdk.WATCH))
        
        if sel:
            self.ayum.selectGroup(grp.groupid)
        else:
            self.ayum.deselectGroup(grp.groupid)
            # FIXME: this doesn't mark installed packages for removal.
            # we probably want that behavior with s-c-p, but not anaconda

        if updateText:
            self.__setGroupDescription(grp)

        self.vbox.window.set_cursor(None)

    def populateCategories(self):
        self.catstore.clear()
        cats = self.ayum.comps.categories
        cats.sort(ui_comps_sort)
        for cat in cats:
            if not _catHasGroupWithPackages(cat, self.ayum):
                continue
            s = "<span size=\"large\" weight=\"bold\">%s</span>" % xmltrans(cat.name, cat.translated_name)
            self.catstore.append(None, [s, cat])

        # select the first category
        i = self.catstore.get_iter_first()
        if i is not None:
            sel = self.xml.get_widget("categoryList").get_selection()
            sel.select_iter(i)

    def _setupCatchallCategory(self):
        # FIXME: this is a bad hack, but catch groups which aren't in
        # a category yet are supposed to be user-visible somehow.
        # conceivably should be handled by yum
        grps = {}
        for g in self.ayum.comps.groups:
            if g.user_visible and self.ayum._groupHasPackages(g):
                grps[g.groupid] = g

        for cat in self.ayum.comps.categories:
            for g in cat.groups:
                if grps.has_key(g):
                    del grps[g]

        if len(grps.keys()) == 0:
            return
        c = yum.comps.Category()
        c.name = _("Uncategorized")
        c._groups = grps
        c.categoryid = "uncategorized"

        self.ayum.comps._categories[c.categoryid] = c

    def doRefresh(self):
        if len(self.ayum.comps.categories) == 0:
            self.xml.get_widget("categorySW").hide()
            self._populateGroups(map(lambda x: x.groupid,
                                     self.ayum.comps.groups))
        else:
            self._setupCatchallCategory()
            self.populateCategories()

    def _getSelectedGroup(self):
        """Return the selected group.
        NOTE: this only ever returns one group."""
        selection = self.xml.get_widget("groupList").get_selection()
        (model, paths) = selection.get_selected_rows()
        for p in paths:
            return model.get_value(model.get_iter(p), 2)
        return None
    
    def _optionalPackagesDialog(self, *args):
        group = self._getSelectedGroup()
        if group is None:
            return

        pwin = self.vbox.get_parent() # hack to find the parent window...
        while not isinstance(pwin, gtk.Window):
            pwin = pwin.get_parent()
        d = OptionalPackageSelector(self.ayum, group, pwin, self.getgladefunc)
        if self.framefunc:
            self.framefunc(d.window)
        rc = d.run()
        d.destroy()
        self.__setGroupDescription(group)

    def _groupSelect(self, *args):
        selection = self.xml.get_widget("groupList").get_selection()
        if selection.count_selected_rows() == 0:
            return

        (model, paths) = selection.get_selected_rows()
        for p in paths:
            self._groupToggled(model, p, True, updateText=(len(paths) == 1))

    def _groupDeselect(self, *args):
        selection = self.xml.get_widget("groupList").get_selection()
        if selection.count_selected_rows() == 0:
            return

        (model, paths) = selection.get_selected_rows()
        for p in paths:
            self._groupToggled(model, p, False, updateText=(len(paths) == 1))

    def _selectAllPackages(self, *args):
        selection = self.xml.get_widget("groupList").get_selection()
        if selection.count_selected_rows() == 0:
            return
        (model, paths) = selection.get_selected_rows()

        self.vbox.window.set_cursor(gdk.Cursor(gdk.WATCH))

        for p in paths:
            i = model.get_iter(p)
            grp = model.get_value(i, 2)

            # ensure the group is selected
            self.ayum.selectGroup(grp.groupid)
            model.set_value(i, 0, True)
        
            for pkg in grp.default_packages.keys() + \
                    grp.optional_packages.keys():
                if self.ayum.isPackageInstalled(pkg):
                    continue
                elif self.ayum.simpleDBInstalled(name = pkg):
                    txmbrs = self.ayum.tsInfo.matchNaevr(name = pkg)
                    for tx in txmbrs:
                        if tx.output_state == TS_ERASE:
                            self.ayum.tsInfo.remove(tx.pkgtup)
                else:
                    _selectPackage(self.ayum, grp, pkg)

        if len(paths) == 1:
            self.__setGroupDescription(grp)
        self.vbox.window.set_cursor(None)

    def _deselectAllPackages(self, *args):
        selection = self.xml.get_widget("groupList").get_selection()
        if selection.count_selected_rows() == 0:
            return
        (model, paths) = selection.get_selected_rows()
        
        for p in paths:
            i = model.get_iter(p)
            grp = model.get_value(i, 2)

            for pkg in grp.default_packages.keys() + \
                    grp.optional_packages.keys():
                if not self.ayum.isPackageInstalled(pkg):
                    continue
                elif self.ayum.simpleDBInstalled(name=pkg):
                    self.ayum.remove(name=pkg)
                else:
                    _deselectPackage(self.ayum, grp, pkg)
        if len(paths) == 1:
            self.__setGroupDescription(grp)

    def __doGroupPopup(self, button, time):
        menu = self.groupMenu
        menu.popup(None, None, None, button, time)
        menu.show_all()

    def _groupListButtonPress(self, widget, event):
        if event.button == 3:
            x = int(event.x)
            y = int(event.y)
            pthinfo = widget.get_path_at_pos(x, y)
            if pthinfo is not None:
                sel = widget.get_selection()
                if sel.count_selected_rows() == 1:
                    path, col, cellx, celly = pthinfo                    
                    widget.grab_focus()
                    widget.set_cursor(path, col, 0)
                self.__doGroupPopup(event.button, event.time)
            return 1

    def _groupListPopup(self, widget):
        sel = widget.get_selection()
        if sel.count_selected_rows() > 0:
            self.__doGroupPopup(0, 0)