~ubuntuone-control-tower/ubuntuone-control-panel/trunk

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/backend.py

  • Committer: Tarmac
  • Author(s): Natalia B. Bidart
  • Date: 2011-03-29 17:46:31 UTC
  • mfrom: (114.1.6 decouple-devices)
  • Revision ID: tarmac-20110329174631-li53mudna9g50zlo
- Decoupled device list retrieved from the web from the local settings retrieved from syncdaemon (LP: #720704).

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from ubuntuone.controlpanel import dbus_client
27
27
from ubuntuone.controlpanel import replication_client
28
28
from ubuntuone.controlpanel.logger import setup_logging, log_call
29
 
from ubuntuone.controlpanel.webclient import WebClient
 
29
from ubuntuone.controlpanel.webclient import WebClient, WebClientError
30
30
 
31
31
 
32
32
logger = setup_logging('backend')
143
143
                                      _set_status_changed_handler)
144
144
 
145
145
    @inlineCallbacks
 
146
    def _process_device_web_info(self, devices, enabled, limit_bw, limits,
 
147
                                 show_notifs):
 
148
        """Return a lis of processed devices."""
 
149
        result = []
 
150
        for d in devices:
 
151
            di = {}
 
152
            di["type"] = d["kind"]
 
153
            di["name"] = d["description"]
 
154
            di["configurable"] = ''
 
155
            if di["type"] == DEVICE_TYPE_COMPUTER:
 
156
                di["device_id"] = di["type"] + d["token"]
 
157
            if di["type"] == DEVICE_TYPE_PHONE:
 
158
                di["device_id"] = di["type"] + str(d["id"])
 
159
 
 
160
            is_local = yield self.device_is_local(di["device_id"])
 
161
            di["is_local"] = bool_str(is_local)
 
162
            # currently, only local devices are configurable.
 
163
            # eventually, more devices will be configurable.
 
164
            di["configurable"] = bool_str(is_local and enabled)
 
165
 
 
166
            if bool(di["configurable"]):
 
167
                di["limit_bandwidth"] = bool_str(limit_bw)
 
168
                di["show_all_notifications"] = bool_str(show_notifs)
 
169
                upload = limits["upload"]
 
170
                download = limits["download"]
 
171
                di[UPLOAD_KEY] = str(upload)
 
172
                di[DOWNLOAD_KEY] = str(download)
 
173
 
 
174
            # date_added is not in the webservice yet (LP: #673668)
 
175
            # di["date_added"] = ""
 
176
 
 
177
            # missing values (LP: #673668)
 
178
            # di["available_services"] = ""
 
179
            # di["enabled_services"] = ""
 
180
 
 
181
            if is_local:  # prepend the local device!
 
182
                result.insert(0, di)
 
183
            else:
 
184
                result.append(di)
 
185
 
 
186
        returnValue(result)
 
187
 
 
188
    @inlineCallbacks
146
189
    def get_token(self):
147
190
        """Return the token from the credentials."""
148
191
        credentials = yield dbus_client.get_credentials()
188
231
    @inlineCallbacks
189
232
    def devices_info(self):
190
233
        """Get the user devices info."""
191
 
        result = []
192
 
        limit_bw = yield dbus_client.bandwidth_throttling_enabled()
193
 
        show_all_notif = yield dbus_client.show_all_notifications_enabled()
194
 
        limits = yield dbus_client.get_throttling_limits()
195
 
 
196
 
        devices = yield self.wc.call_api(DEVICES_API)
197
 
        for d in devices:
198
 
            di = {}
199
 
            di["type"] = d["kind"]
200
 
            di["name"] = d["description"]
201
 
            di["configurable"] = ''
202
 
            if di["type"] == DEVICE_TYPE_COMPUTER:
203
 
                di["device_id"] = di["type"] + d["token"]
204
 
            if di["type"] == DEVICE_TYPE_PHONE:
205
 
                di["device_id"] = di["type"] + str(d["id"])
206
 
 
207
 
            is_local = yield self.device_is_local(di["device_id"])
208
 
            di["is_local"] = bool_str(is_local)
209
 
            # currently, only local devices are configurable.
210
 
            # eventually, more the devices will be configurable.
211
 
            di["configurable"] = bool_str(is_local)
212
 
 
213
 
            if bool(di["configurable"]):
214
 
                di["limit_bandwidth"] = bool_str(limit_bw)
215
 
                di["show_all_notifications"] = bool_str(show_all_notif)
 
234
        result = limit_bw = limits = show_notifs = None
 
235
        enabled = yield dbus_client.files_sync_enabled()
 
236
        if enabled:
 
237
            limit_bw = yield dbus_client.bandwidth_throttling_enabled()
 
238
            show_notifs = yield dbus_client.show_all_notifications_enabled()
 
239
            limits = yield dbus_client.get_throttling_limits()
 
240
 
 
241
        try:
 
242
            devices = yield self.wc.call_api(DEVICES_API)
 
243
        except WebClientError:
 
244
            logger.exception('devices_info: web client failure:')
 
245
        else:
 
246
            result = yield self._process_device_web_info(devices, enabled,
 
247
                                                         limit_bw, limits,
 
248
                                                         show_notifs)
 
249
        if result is None:
 
250
            credentials = yield dbus_client.get_credentials()
 
251
            local_device = {}
 
252
            local_device["type"] = DEVICE_TYPE_COMPUTER
 
253
            local_device["name"] = credentials['name']
 
254
            device_id = local_device["type"] + credentials["token"]
 
255
            local_device["device_id"] = device_id
 
256
            local_device["is_local"] = bool_str(True)
 
257
            local_device["configurable"] = bool_str(enabled)
 
258
            if bool(local_device["configurable"]):
 
259
                local_device["limit_bandwidth"] = bool_str(limit_bw)
 
260
                show_notifs = bool_str(show_notifs)
 
261
                local_device["show_all_notifications"] = show_notifs
216
262
                upload = limits["upload"]
217
263
                download = limits["download"]
218
 
                di[UPLOAD_KEY] = str(upload)
219
 
                di[DOWNLOAD_KEY] = str(download)
220
 
 
221
 
            # date_added is not in the webservice yet (LP: #673668)
222
 
            # di["date_added"] = ""
223
 
 
224
 
            # missing values (LP: #673668)
225
 
            # di["available_services"] = ""
226
 
            # di["enabled_services"] = ""
227
 
 
228
 
            if is_local:  # prepend the local device!
229
 
                result.insert(0, di)
230
 
            else:
231
 
                result.append(di)
 
264
                local_device[UPLOAD_KEY] = str(upload)
 
265
                local_device[DOWNLOAD_KEY] = str(download)
 
266
            result = [local_device]
232
267
 
233
268
        returnValue(result)
234
269