AppDescView.py 12 KB
Newer Older
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
import gtk
from ReleaseNotesViewer import ReleaseNotesViewer

import re
import urllib
import pango
import gobject

# The URL to a debshots instance (without trailing '/')
DEBSHOTS = "http://screenshots.debian.net"


def show_big_screenshot(eventbox, event, pkgname):
    """Show a big screenshot, in a window."""        
    res = urllib.urlretrieve("%s/screenshot/%s" % (DEBSHOTS, pkgname))
    # %s is the name of the package.
    win = gtk.Dialog(_("Screenshot of %s") % pkgname)
    image = gtk.image_new_from_file(res[0])
    image.show()
    win.vbox.add(image)
    win.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE)
    win.run()
    win.destroy()

class AppDescView(ReleaseNotesViewer):
    def __init__(self):
        ReleaseNotesViewer.__init__(self)
        self.set_wrap_mode(gtk.WRAP_WORD)
        self.set_pixels_below_lines(3)
        self.set_right_margin(6)
        self.set_left_margin(6)
        atk_desc = self.get_accessible()
        atk_desc.set_name(_("Description"))

    def hook(self, cache, menu, icons, tooltips, distro):
        self.cache = cache
        self.menu = menu
        self.icons = icons
        self.tooltips = tooltips
        self.distro = distro

    def show_thumb(self, button, anchor, pkgname):
        """Show a thumbnail."""
        # TODO: Run in a parallel thread to not lock the UI.
        button.hide()
        res = urllib.urlretrieve("%s/thumbnail/%s" % (DEBSHOTS, pkgname))
        event = gtk.EventBox()
        image = gtk.image_new_from_file(res[0])
        event.connect("button-press-event", show_big_screenshot, pkgname)
        event.add(image)
        event.show_all()
        self.add_child_at_anchor(event, anchor)

    def show_description(self, item):
        """Collect and show some information about the package that 
           contains the selected application"""
        details = []
        clean_desc = ""
        short_desc = ""
        version = ""
        desktop_environment = ""
        icons = []
        homepage = ""
        style = self.get_style()

        
        if self.menu.itemAvailable(item):
            pkg = self.cache[item.pkgname]
            # Get the candidate version (fallback to pkg for older python-apt)
            candidate = getattr(pkg, 'candidate', pkg)
            homepage = candidate and candidate.homepage
            short_desc = candidate and candidate.summary
            try:
                # short_desc = ... already looked up the correct translation
                # for us.
                rough_desc = candidate.package._pcache._records.LongDesc.split("\n",1)[1]
            except AttributeError:
                rough_desc = candidate and candidate.raw_description.split("\n",1)[1]
            
            # Split into sections, parse each section on its own.
            sections = rough_desc.split("\n .")

            for section in sections:
                # Take care of lists and use UTF-8 bullets.
                section = re.sub("\n( |\t)+(-|\*)", u"\n\r\t\u2022 ", section)
                # There should be no new lines within a section.
                section = section.replace("\n", "")
                # Hack to get the lists working again.
                section = section.replace("\r", "\n")
                # Multiple whitespace gets merged into a single one.
                section = re.sub("\ \ +", " ", section)
                # Append it to the complete description.
                if section.startswith(u"\n\t\u2022"):
                    clean_desc += section
                else:
                    clean_desc += "\n%s" % section.strip()

            # Remove the leading newline.
            clean_desc = clean_desc[1:]
            
            if not clean_desc:
                clean_desc = item.comment
        else:
            msg = _("%s cannot be installed" % item.name)
            # check if we have seen the component
            for it in self.cache._cache.FileList:
                # FIXME: we need to exclude cdroms here. the problem is
                # how to detect if a PkgFileIterator is pointing to a cdrom
                if (it.Component != "" and it.Component == item.component) or\
                   (item.architectures and \
                    not self.cache.getArch() in item.architectures):
                    # warn that this app is not available on this plattform
                    details.append(_("%s cannot be installed on your "
                                     "computer type (%s). Either the "
                                     "application requires special "
                                     "hardware features or the vendor "
                                     "decided to not support your "
                                     "computer type.") % (item.name, 
                                     self.cache.getArch()))
                    break
            # check if it comes from a third party repository and
            # display message then
            if item.channel:
                details.append(_("%s is available in the third party software " 
                                 "channel '%s'. To install it, please "
                                 "click on the checkbox to activate the "
                                 "software channel.") % 
                               (item.name, item.channel))

        # mutliple apps per pkg check
        s = ""
        if self.menu.pkg_to_app.has_key(item.name) and \
           len(self.menu.pkg_to_app[item.name]) > 1:
            s = _("This application is bundled with "
                  "the following applications: ")
            apps = self.menu.pkg_to_app[item.name]
            s += ", ".join([pkg.name for pkg in apps])
            details.append(s)

        # init the textview that shows the description of the app
        buffer = self.get_buffer()
        buffer.set_text("")
        # remove all old tags
        tag_table = buffer.get_tag_table()
        tag_table.foreach((lambda tag, table: table.remove(tag)), tag_table)
        iter = buffer.get_start_iter()
        # we need the default font size for the vertically justification
        pango_context = self.get_pango_context()
        font_desc = pango_context.get_font_description()
        font_size = font_desc.get_size() / pango.SCALE
151
        if item.iconname == "":
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
            # justify the icon and the app name
            if font_size * pango.SCALE_LARGE > 32:
                icon_size = 32
                adjust_vertically = 0
            elif font_size * pango.SCALE_LARGE < 10:
                icon_size = 24
                adjust_vertically = (icon_size - font_size * pango.SCALE_LARGE)\
                                    /2
            else:
                icon_size = 32
                adjust_vertically = (icon_size - font_size * pango.SCALE_LARGE)\
                                    / 2
            tag_name = buffer.create_tag("app-icon",
                                         right_margin=6,
                                         pixels_above_lines=6)
            tag_name = buffer.create_tag("app-name",
                                         rise=adjust_vertically * pango.SCALE,
                                         pixels_above_lines=6,
                                         weight=pango.WEIGHT_BOLD,
                                         scale=pango.SCALE_LARGE)
            try:
                icon_pixbuf = self.icons.load_icon(item.iconname, icon_size, 0)
            except gobject.GError:
                try:
                    icon_pixbuf = self.icons.load_icon("applications-other", 
                                                       icon_size, 0)
                except gobject.GError:
                    icon_pixbuf = self.icons.load_icon(gtk.STOCK_MISSING_IMAGE,
                                                       icon_size, 0)
            buffer.insert_pixbuf(iter, icon_pixbuf)
            (start_iter, end_iter,) = buffer.get_bounds()
            buffer.apply_tag_by_name("app-icon", start_iter, end_iter)
            buffer.insert_with_tags_by_name(iter, " %s" % item.name, "app-name")
        else:
            tag_name = buffer.create_tag("app-name",
                                         weight=pango.WEIGHT_BOLD,
                                         pixels_above_lines=6,
                                         scale=pango.SCALE_LARGE)
            buffer.insert_with_tags_by_name(iter, "%s" % item.name, "app-name")

        emblems = self.distro.get_app_emblems(item, self.cache)
193
        if short_desc == "": 
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
            tag_name = buffer.create_tag("short-desc",
                                         weight=pango.WEIGHT_BOLD)
            buffer.insert_with_tags_by_name(iter,
                                            "\n%s" % short_desc,
                                            "short-desc")
            for emblem in emblems:
                # not all emblems have got an icon
                if not emblem[0]:
                    continue
                image_emblem = gtk.Image()
                image_emblem.set_from_icon_name(emblem[0],
                                                gtk.ICON_SIZE_MENU)
                image_emblem.set_pixel_size(16)
                event = gtk.EventBox()
                # use the base color of the textview for the image
                # FIXME: send the selected signal from the anchor 
                #        to the widget
                for state in [gtk.STATE_NORMAL, gtk.STATE_ACTIVE,
                              gtk.STATE_PRELIGHT, gtk.STATE_SELECTED,
                              gtk.STATE_INSENSITIVE]:
                    event.modify_bg(state,
                                    style.base[state])
                event.add(image_emblem)
                if emblem[1] != None:
                    self.tooltips.set_tip(event, emblem[1])
                buffer.insert(iter, " ")
                anchor = buffer.create_child_anchor(iter)
                self.add_child_at_anchor(event,
                                         anchor)
                event.show()
                image_emblem.show()

        buffer.insert(iter,"\n")
        anchor = buffer.create_child_anchor(iter)
        button = gtk.Button(_("Screenshot"))
        self.add_child_at_anchor(button, anchor)
        button.connect("clicked", self.show_thumb, anchor, item.pkgname)
        button.show()
        
        if clean_desc != "": 
            buffer.insert(iter, "\n%s" % clean_desc)
        if homepage:
            buffer.insert(iter, _("\nHomepage: %s\n") % homepage)
        if version != "":
            buffer.insert(iter, _("Version: %s (%s)") % (version, item.pkgname))
        if len(details) > 0:
            for x in details:
                buffer.insert(iter, "\n%s" % x)
        # add maintenance status
        #buffer.insert(iter, " ")
        #anchor = buffer.create_child_anchor(iter)
        #foo = gtk.Frame(self.distro.get_maintenance_status(item, self.cache))
        #foo.show()
        #self.add_child_at_anchor(foo, anchor)
248 249 250 251
        #buffer.create_tag("maint-status", 
        #                  scale_set=True,
        #                  scale=pango.SCALE_SMALL,
        #                  foreground="#888")
252
        #                  foreground_gdk=style.base[gtk.STATE_INSENSITIVE])
253 254
        #m = self.distro.get_maintenance_status(item, self.cache)
        #buffer.insert_with_tags_by_name(iter, "\n%s" % m, "maint-status")
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

    def show_message(self, header, msg):
        """ Show a quick introduction to gnome-app-install 
            in the description view"""
        buffer = self.get_buffer()
        buffer.set_text("")
        iter = buffer.get_start_iter()
        tag_header = buffer.get_tag_table().lookup("header")
        if not tag_header:
            tag_header = buffer.create_tag("header",
                                           scale = pango.SCALE_LARGE,
                                           weight = pango.WEIGHT_BOLD,
                                           pixels_above_lines=6)
        if header:
            buffer.insert_with_tags(iter, "%s\n" % header, 
                                    tag_header)
        buffer.insert(iter, msg)

    def clear_description(self):
        buffer = self.get_buffer()
        buffer.set_text("")