~evfool/software-center/lp934414

« back to all changes in this revision

Viewing changes to softwarecenter/db/dataprovider.py

  • Committer: Michael Vogt
  • Date: 2012-10-05 06:54:05 UTC
  • mfrom: (3206.4.6 dbus-idle-timeout)
  • Revision ID: michael.vogt@ubuntu.com-20121005065405-3ejaw4iidohxlkmu
mergedĀ lp:~mvo/software-center/dbus-idle-timeout

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import dbus
20
20
import dbus.service
21
21
import logging
 
22
import time
22
23
 
23
24
from dbus.mainloop.glib import DBusGMainLoop
24
25
DBusGMainLoop(set_as_default=True)
50
51
DBUS_DATA_PROVIDER_PATH = '/com/ubuntu/SoftwareCenterDataProvider'
51
52
 
52
53
 
 
54
def update_activity_timestamp(fn):
 
55
    def wrapped(*args, **kwargs):
 
56
        self = args[0]
 
57
        self._update_activity_timestamp()
 
58
        return fn(*args, **kwargs)
 
59
    return wrapped
 
60
 
 
61
 
53
62
class SoftwareCenterDataProvider(dbus.service.Object):
54
63
 
55
 
    def __init__(self, bus_name, object_path=DBUS_DATA_PROVIDER_PATH):
 
64
    # 5 min by default
 
65
    IDLE_TIMEOUT = 60 * 5
 
66
    IDLE_CHECK_INTERVAL = 60
 
67
 
 
68
    def __init__(self, bus_name, object_path=DBUS_DATA_PROVIDER_PATH,
 
69
                 main_loop=None):
56
70
        dbus.service.Object.__init__(self, bus_name, object_path)
57
71
        self.bus_name = bus_name
 
72
        if main_loop is None:
 
73
            main_loop = GObject.MainLoop(GObject.main_context_default())
 
74
        self.main_loop = main_loop
58
75
        # the database
59
76
        self.db = StoreDatabase()
60
77
        self.db.open()
66
83
        self.review_loader.refresh_review_stats()
67
84
        # ensure we query new applications
68
85
        run_software_center_agent(self.db)
 
86
        # setup inactivity timer
 
87
        self._update_activity_timestamp()
 
88
        self._idle_timeout = GObject.timeout_add_seconds(
 
89
            self.IDLE_CHECK_INTERVAL, self._check_inactivity)
69
90
 
70
91
    def stop(self):
71
92
        """ stop the dbus controller and remove from the bus """
72
 
        self.remove_from_connection()
73
 
 
 
93
        LOG.debug("stop() called")
 
94
        self.main_loop.quit()
 
95
        LOG.debug("exited")
 
96
 
 
97
    # internal helper
 
98
    def _check_inactivity(self):
 
99
        """ Check for activity """
 
100
        now = time.time()
 
101
        if (self._activity_timestamp + self.IDLE_TIMEOUT) < now:
 
102
            LOG.info("stopping after %s inactivity" % self.IDLE_TIMEOUT)
 
103
            self.stop()
 
104
        return True
 
105
 
 
106
    def _update_activity_timestamp(self):
 
107
        self._activity_timestamp = time.time()
 
108
 
 
109
    # public dbus methods with their implementations, the dbus decorator
 
110
    # does not like additional decorators so we use a seperate function
 
111
    # for the actual implementation
74
112
    @dbus.service.method(DBUS_DATA_PROVIDER_IFACE,
75
113
                         in_signature='ss', out_signature='a{sv}')
76
114
    def GetAppDetails(self, appname, pkgname):
77
115
        LOG.debug("GetAppDetails() called with ('%s', '%s')" % (
78
116
                appname, pkgname))
 
117
        return self._get_app_details(appname, pkgname)
 
118
 
 
119
    @update_activity_timestamp
 
120
    def _get_app_details(self, appname, pkgname):
79
121
        app = Application(appname, pkgname)
80
122
        appdetails = app.get_details(self.db)
81
123
        return appdetails.as_dbus_property_dict()
84
126
                         in_signature='', out_signature='as')
85
127
    def GetAvailableCategories(self):
86
128
        LOG.debug("GetAvailableCategories() called")
 
129
        return self._get_available_categories()
 
130
 
 
131
    @update_activity_timestamp    
 
132
    def _get_available_categories(self):
87
133
        return [cat.name for cat in self.categories]
88
134
 
89
135
    @dbus.service.method(DBUS_DATA_PROVIDER_IFACE,
90
136
                         in_signature='s', out_signature='as')
91
137
    def GetAvailableSubcategories(self, category_name):
92
138
        LOG.debug("GetAvailableSubcategories() called")
 
139
        return self._get_available_subcategories(category_name)
 
140
 
 
141
    @update_activity_timestamp
 
142
    def _get_available_subcategories(self, category_name):
93
143
        cat = get_category_by_name(self.categories, category_name)
94
144
        return [subcat.name for subcat in cat.subcategories]
95
145
 
97
147
                         in_signature='s', out_signature='a(ssss)')
98
148
    def GetItemsForCategory(self, category_name):
99
149
        LOG.debug("GetItemsForCategory() called with ('%s')" % category_name)
 
150
        return self._get_items_for_category(category_name)
 
151
 
 
152
    @update_activity_timestamp
 
153
    def _get_items_for_category(self, category_name):
100
154
        result = []
101
155
        cat = get_category_by_name(self.categories, category_name)
102
156
        for doc in cat.get_documents(self.db):
112
166
def dbus_main(bus=None):
113
167
    if bus is None:
114
168
        bus = dbus.SessionBus()
 
169
 
 
170
    main_context = GObject.main_context_default()
 
171
    main_loop = GObject.MainLoop(main_context)
 
172
 
115
173
    bus_name = dbus.service.BusName(DBUS_BUS_NAME, bus)
116
 
    data_provider = SoftwareCenterDataProvider(bus_name)
 
174
    data_provider = SoftwareCenterDataProvider(bus_name, main_loop=main_loop)
117
175
    data_provider  # pyflakes
118
176
 
119
 
    main_context = GObject.main_context_default()
120
 
    main_loop = GObject.MainLoop(main_context)
 
177
    # run it
121
178
    main_loop.run()