~dpm/qreator/snap

« back to all changes in this revision

Viewing changes to qreator/qrcodes/QRCodeSoftwareCenterAppGtk.py

  • Committer: David Planella
  • Date: 2012-05-30 08:33:06 UTC
  • Revision ID: david.planella@ubuntu.com-20120530083306-2naqmakzg56jh5mx
Decoupled QR code types from the QR window and from a particular GUI toolkit (right now only GTK is supported)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
### BEGIN LICENSE
 
3
# Copyright (C) 2012 David Planella <david.planella@ubuntu.com>
 
4
# This program is free software: you can redistribute it and/or modify it
 
5
# under the terms of the GNU General Public License version 3, as published
 
6
# by the Free Software Foundation.
 
7
#
 
8
# This program is distributed in the hope that it will be useful, but
 
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
# PURPOSE.  See the GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License along
 
14
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
### END LICENSE
 
16
 
 
17
from qreator_lib.i18n import _
 
18
from qreator_lib.helpers import get_data_file
 
19
from gi.repository import Gtk, GdkPixbuf, GLib
 
20
import sys
 
21
sys.path.insert(0, "/usr/share/software-center/")
 
22
import softwarecenter.db.database
 
23
import defer
 
24
 
 
25
 
 
26
class QRCodeSoftwareCenterAppGtk(object):
 
27
    def __init__(self, qr_code_update_func):
 
28
        self.qr_code_update_func = qr_code_update_func
 
29
        self.builder = Gtk.Builder()
 
30
 
 
31
        self.builder.add_from_file(
 
32
            get_data_file('ui', '%s.ui' % ('QrCodeSoftwareCentreApp',)))
 
33
        self.grid = self.builder.get_object('qr_code_softwarecenterapp')
 
34
        self.builder.connect_signals(self)
 
35
        print 'Builder!'
 
36
 
 
37
        # Initialize the Software Center apps autocompletion
 
38
        self.softwarecenterapps = None
 
39
 
 
40
        self.usc_apps_complete = Complete()
 
41
 
 
42
        self.usc_apps_complete.set_hexpand(True)
 
43
        self.usc_apps_complete.connect("changed",
 
44
                                    self.on_usc_apps_complete_changed, None)
 
45
        #self.usc_apps_complete.connect("icon-press",
 
46
        #                            self.on_usc_apps_complete_icon_pressed)
 
47
        self.usc_apps_complete.set_icon_from_icon_name(
 
48
            Gtk.EntryIconPosition.PRIMARY, 'edit-find-symbolic')
 
49
        self.usc_apps_complete.set_icon_from_stock(
 
50
            Gtk.EntryIconPosition.SECONDARY, None)
 
51
        
 
52
        self.grid.attach(self.usc_apps_complete, 1, 0, 1, 1)
 
53
        self.softwarecenter_apps_init = False
 
54
        self.grid.show_all()
 
55
 
 
56
    def on_activated(self):
 
57
        pass
 
58
        #if not self.softwarecenter_apps_init:
 
59
        #    GLib.idle_add(self.init_softwarecenter_apps)
 
60
 
 
61
    def init_softwarecenter_apps(self):
 
62
        deferred = defer.defer(self.get_softwarecenterapps)
 
63
        deferred.add_callback(self.on_get_softwarecenterapps)
 
64
        self.softwarecenter_apps_init = True
 
65
 
 
66
    def on_usc_apps_complete_changed(self, widget, data=None):
 
67
        #self._check_style(widget)
 
68
        USC_APPS_BASE_URL = 'https://apps.ubuntu.com/cat/applications/'
 
69
        self.qr_code_update_func(USC_APPS_BASE_URL + widget.get_text())
 
70
 
 
71
    def get_softwarecenterapps(self):
 
72
        print 'Task STARTED!'
 
73
        db = softwarecenter.db.database.StoreDatabase()
 
74
        db._aptcache.open()
 
75
        db.open(use_axi=False, use_agent=False)
 
76
        apps = []
 
77
        for doc in db:
 
78
            app = db.get_application(doc)
 
79
            appdetails = app.get_details(db)
 
80
            appinfo = App(app.appname, app.pkgname, appdetails.icon)
 
81
            apps.append(appinfo)
 
82
        self.softwarecenterapps = apps
 
83
        return apps
 
84
 
 
85
    def on_get_softwarecenterapps(self, result):
 
86
        print 'Task FINISHED!'
 
87
        self.usc_apps_complete.add(self.softwarecenterapps)
 
88
 
 
89
 
 
90
class App(object):
 
91
    def __init__(self, name, package, icon):
 
92
        self.name = name
 
93
        self.package = package
 
94
        self.icon = icon
 
95
 
 
96
 
 
97
class Complete(Gtk.Entry):
 
98
    '''a class to autocomplete'''
 
99
 
 
100
    def __init__(self, apps=None):
 
101
        Gtk.Entry.__init__(self)
 
102
        self.completion = Gtk.EntryCompletion()
 
103
        self.set_completion(self.completion)
 
104
        self.model = Gtk.ListStore(str, GdkPixbuf.Pixbuf, str)
 
105
        self.completion.set_model(self.model)
 
106
        self.completion_img = Gtk.CellRendererPixbuf()
 
107
        self.completion.pack_start(self.completion_img, True)
 
108
        self.completion.add_attribute(self.completion_img, "pixbuf", 1)
 
109
        self.completion.set_text_column(0)
 
110
        self.completion.set_popup_set_width(False)
 
111
        self.completion.set_inline_completion(True)
 
112
        self.icons = Gtk.IconTheme.get_default()
 
113
        self.icons.append_search_path("/usr/share/app-install/icons/")
 
114
 
 
115
        if apps:
 
116
            self.add(apps)
 
117
        else:
 
118
            # TRANSLATORS: this message appears for a few seconds on the
 
119
            # Software Center apps text entry until the app names to
 
120
            # suggest for autocompletion have been read from the Software
 
121
            # Center database
 
122
            self.set_placeholder_text(
 
123
                _('[Wait a few seconds to enable autocompletion...]'))
 
124
 
 
125
    def add(self, apps):
 
126
        for app in apps:
 
127
            self._remember(app)
 
128
        self.set_placeholder_text(
 
129
                _('[Type the name of an app]'))
 
130
 
 
131
    def _remember(self, app):
 
132
        '''Add a value to the list of strings to suggest'''
 
133
        try:
 
134
            icon = self.icons.load_icon(
 
135
                "{0}".format(app.icon),
 
136
                16, Gtk.IconLookupFlags.USE_BUILTIN)
 
137
        except:
 
138
            icon = self.icons.load_icon(
 
139
                "stock_not",
 
140
                16, Gtk.IconLookupFlags.USE_BUILTIN)
 
141
        self.model.append([app.package, icon, app.name])