~dpm/qreator/snap

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
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2012 David Planella <david.planella@ubuntu.com>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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 this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

from qreator_lib.i18n import _
from qreator_lib.helpers import get_data_file
from gi.repository import Gtk, GdkPixbuf, GLib
import sys
sys.path.insert(0, "/usr/share/software-center/")
import softwarecenter.db.database
import defer


class QRCodeSoftwareCenterAppGtk(object):
    def __init__(self, qr_code_update_func):
        self.qr_code_update_func = qr_code_update_func
        self.builder = Gtk.Builder()

        self.builder.add_from_file(
            get_data_file('ui', '%s.ui' % ('QrCodeSoftwareCentreApp',)))
        self.grid = self.builder.get_object('qr_code_softwarecenterapp')
        self.builder.connect_signals(self)
        print 'Builder!'

        # Initialize the Software Center apps autocompletion
        self.softwarecenterapps = None

        self.usc_apps_complete = Complete()

        self.usc_apps_complete.set_hexpand(True)
        self.usc_apps_complete.connect("changed",
                                    self.on_usc_apps_complete_changed, None)
        #self.usc_apps_complete.connect("icon-press",
        #                            self.on_usc_apps_complete_icon_pressed)
        self.usc_apps_complete.set_icon_from_icon_name(
            Gtk.EntryIconPosition.PRIMARY, 'edit-find-symbolic')
        self.usc_apps_complete.set_icon_from_stock(
            Gtk.EntryIconPosition.SECONDARY, None)
        
        self.grid.attach(self.usc_apps_complete, 1, 0, 1, 1)
        self.softwarecenter_apps_init = False
        self.grid.show_all()

    def on_activated(self):
        pass
        #if not self.softwarecenter_apps_init:
        #    GLib.idle_add(self.init_softwarecenter_apps)

    def init_softwarecenter_apps(self):
        deferred = defer.defer(self.get_softwarecenterapps)
        deferred.add_callback(self.on_get_softwarecenterapps)
        self.softwarecenter_apps_init = True

    def on_usc_apps_complete_changed(self, widget, data=None):
        #self._check_style(widget)
        USC_APPS_BASE_URL = 'https://apps.ubuntu.com/cat/applications/'
        self.qr_code_update_func(USC_APPS_BASE_URL + widget.get_text())

    def get_softwarecenterapps(self):
        print 'Task STARTED!'
        db = softwarecenter.db.database.StoreDatabase()
        db._aptcache.open()
        db.open(use_axi=False, use_agent=False)
        apps = []
        for doc in db:
            app = db.get_application(doc)
            appdetails = app.get_details(db)
            appinfo = App(app.appname, app.pkgname, appdetails.icon)
            apps.append(appinfo)
        self.softwarecenterapps = apps
        return apps

    def on_get_softwarecenterapps(self, result):
        print 'Task FINISHED!'
        self.usc_apps_complete.add(self.softwarecenterapps)


class App(object):
    def __init__(self, name, package, icon):
        self.name = name
        self.package = package
        self.icon = icon


class Complete(Gtk.Entry):
    '''a class to autocomplete'''

    def __init__(self, apps=None):
        Gtk.Entry.__init__(self)
        self.completion = Gtk.EntryCompletion()
        self.set_completion(self.completion)
        self.model = Gtk.ListStore(str, GdkPixbuf.Pixbuf, str)
        self.completion.set_model(self.model)
        self.completion_img = Gtk.CellRendererPixbuf()
        self.completion.pack_start(self.completion_img, True)
        self.completion.add_attribute(self.completion_img, "pixbuf", 1)
        self.completion.set_text_column(0)
        self.completion.set_popup_set_width(False)
        self.completion.set_inline_completion(True)
        self.icons = Gtk.IconTheme.get_default()
        self.icons.append_search_path("/usr/share/app-install/icons/")

        if apps:
            self.add(apps)
        else:
            # TRANSLATORS: this message appears for a few seconds on the
            # Software Center apps text entry until the app names to
            # suggest for autocompletion have been read from the Software
            # Center database
            self.set_placeholder_text(
                _('[Wait a few seconds to enable autocompletion...]'))

    def add(self, apps):
        for app in apps:
            self._remember(app)
        self.set_placeholder_text(
                _('[Type the name of an app]'))

    def _remember(self, app):
        '''Add a value to the list of strings to suggest'''
        try:
            icon = self.icons.load_icon(
                "{0}".format(app.icon),
                16, Gtk.IconLookupFlags.USE_BUILTIN)
        except:
            icon = self.icons.load_icon(
                "stock_not",
                16, Gtk.IconLookupFlags.USE_BUILTIN)
        self.model.append([app.package, icon, app.name])