1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
1 |
# -*- coding: utf-8 -*-
|
2 |
||
3 |
# Authors: Alejandro J. Cura <alecu@canonical.com>
|
|
4 |
# Authors: Natalia B. Bidart <nataliabidart@canonical.com>
|
|
5 |
#
|
|
6 |
# Copyright 2010 Canonical Ltd.
|
|
7 |
#
|
|
8 |
# This program is free software: you can redistribute it and/or modify it
|
|
9 |
# under the terms of the GNU General Public License version 3, as published
|
|
10 |
# by the Free Software Foundation.
|
|
11 |
#
|
|
12 |
# This program is distributed in the hope that it will be useful, but
|
|
13 |
# WITHOUT ANY WARRANTY; without even the implied warranties of
|
|
14 |
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
|
|
15 |
# PURPOSE. See the GNU General Public License for more details.
|
|
16 |
#
|
|
17 |
# You should have received a copy of the GNU General Public License along
|
|
18 |
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 |
||
20 |
"""Client to use other DBus services."""
|
|
21 |
||
22 |
import dbus.service |
|
23 |
import ubuntu_sso |
|
24 |
||
25 |
from twisted.internet import defer |
|
26 |
||
27 |
from ubuntuone.clientdefs import APP_NAME |
|
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
28 |
from ubuntuone.platform.linux import dbus_interface as sd_dbus_iface |
29 |
from ubuntuone.platform.linux.tools import SyncDaemonTool |
|
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
30 |
|
31 |
from ubuntuone.controlpanel.logger import setup_logging |
|
32 |
||
33 |
||
34 |
logger = setup_logging('dbus_client') |
|
35 |
||
36 |
||
37 |
class CredentialsError(Exception): |
|
38 |
"""No credentials could be retrieved."""
|
|
39 |
||
40 |
||
41 |
class VolumesError(Exception): |
|
42 |
"""An operation over a volume failed."""
|
|
43 |
||
44 |
||
45 |
def no_op(*a): |
|
46 |
"""Do nothing"""
|
|
47 |
||
48 |
||
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
49 |
def get_sso_proxy(): |
50 |
"""Get a DBus proxy for credentials management."""
|
|
51 |
bus = dbus.SessionBus() |
|
52 |
obj = bus.get_object(bus_name=ubuntu_sso.DBUS_BUS_NAME, |
|
53 |
object_path=ubuntu_sso.DBUS_CREDENTIALS_PATH, |
|
54 |
follow_name_owner_changes=True) |
|
55 |
proxy = dbus.Interface(object=obj, |
|
56 |
dbus_interface=ubuntu_sso.DBUS_CREDENTIALS_IFACE) |
|
57 |
return proxy |
|
58 |
||
59 |
||
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
60 |
def get_credentials(): |
61 |
"""Get the credentials from the ubuntu-sso-client."""
|
|
62 |
d = defer.Deferred() |
|
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
63 |
proxy = get_sso_proxy() |
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
64 |
|
65 |
def found_credentials(app_name, creds): |
|
66 |
"""Credentials have been found."""
|
|
67 |
if app_name == APP_NAME: |
|
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
68 |
logger.info('credentials were found! (%r).', APP_NAME) |
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
69 |
d.callback(creds) |
70 |
||
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
71 |
def credentials_error(app_name, error_dict=None): |
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
72 |
"""No credentials could be retrieved."""
|
73 |
if app_name == APP_NAME: |
|
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
74 |
if error_dict is None: |
75 |
error_dict = {'error_message': 'Credentials were not found.', |
|
76 |
'detailed_error': ''} |
|
77 |
||
78 |
logger.error('credentials error (%r, %r).', app_name, error_dict) |
|
79 |
error = CredentialsError(app_name, error_dict) |
|
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
80 |
d.errback(error) |
81 |
||
82 |
foundsig = proxy.connect_to_signal("CredentialsFound", found_credentials) |
|
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
83 |
notfoundsig = proxy.connect_to_signal("CredentialsNotFound", |
84 |
credentials_error) |
|
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
85 |
errorsig = proxy.connect_to_signal("CredentialsError", credentials_error) |
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
86 |
logger.debug('calling get_credentials.') |
87 |
proxy.find_credentials(APP_NAME, {'': ''}, |
|
88 |
reply_handler=no_op, error_handler=d.errback) |
|
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
89 |
|
90 |
def cleanup_signals(result): |
|
91 |
"""Remove signals after use."""
|
|
92 |
foundsig.remove() |
|
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
93 |
notfoundsig.remove() |
94 |
errorsig.remove() |
|
95 |
return result |
|
96 |
||
97 |
d.addBoth(cleanup_signals) |
|
98 |
return d |
|
99 |
||
100 |
||
101 |
def clear_credentials(): |
|
102 |
"""Clear the credentials using the ubuntu-sso-client."""
|
|
103 |
d = defer.Deferred() |
|
104 |
proxy = get_sso_proxy() |
|
105 |
||
106 |
def credentials_cleared(app_name): |
|
107 |
"""Credentials have been cleared."""
|
|
108 |
logger.debug('credentials were cleared for app_name %r.', app_name) |
|
109 |
if app_name == APP_NAME: |
|
110 |
logger.info('credentials were cleared! (%r).', APP_NAME) |
|
111 |
d.callback(app_name) |
|
112 |
||
113 |
def credentials_error(app_name, error_dict=None): |
|
114 |
"""No credentials could be retrieved."""
|
|
115 |
if app_name == APP_NAME: |
|
116 |
logger.error('credentials error (%r, %r).', app_name, error_dict) |
|
117 |
error = CredentialsError(app_name, error_dict) |
|
118 |
d.errback(error) |
|
119 |
||
120 |
clearedsig = proxy.connect_to_signal("CredentialsCleared", |
|
121 |
credentials_cleared) |
|
122 |
errorsig = proxy.connect_to_signal("CredentialsError", credentials_error) |
|
123 |
logger.warning('calling clear_credentials.') |
|
124 |
proxy.clear_credentials(APP_NAME, {'': ''}, |
|
125 |
reply_handler=no_op, error_handler=d.errback) |
|
126 |
||
127 |
def cleanup_signals(result): |
|
128 |
"""Remove signals after use."""
|
|
129 |
clearedsig.remove() |
|
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
130 |
errorsig.remove() |
131 |
return result |
|
132 |
||
133 |
d.addBoth(cleanup_signals) |
|
134 |
return d |
|
135 |
||
136 |
||
137 |
def get_syncdaemon_proxy(object_path, dbus_interface): |
|
138 |
"""Get a DBus proxy for syncdaemon at 'object_path':'dbus_interface'."""
|
|
139 |
logger.debug('get_syncdaemon_proxy: object_path %r, dbus_interface %r', |
|
140 |
object_path, dbus_interface) |
|
141 |
bus = dbus.SessionBus() |
|
142 |
obj = bus.get_object(bus_name=sd_dbus_iface.DBUS_IFACE_NAME, |
|
143 |
object_path=object_path, |
|
144 |
follow_name_owner_changes=True) |
|
145 |
proxy = dbus.Interface(object=obj, dbus_interface=dbus_interface) |
|
146 |
return proxy |
|
147 |
||
148 |
||
149 |
def get_config_syncdaemon_proxy(): |
|
150 |
"""Get a DBus proxy for syncdaemon config calls."""
|
|
151 |
return get_syncdaemon_proxy("/config", |
|
152 |
sd_dbus_iface.DBUS_IFACE_CONFIG_NAME) |
|
153 |
||
154 |
||
155 |
def get_folders_syncdaemon_proxy(): |
|
156 |
"""Get a DBus proxy for syncdaemon folders calls."""
|
|
157 |
return get_syncdaemon_proxy("/folders", |
|
158 |
sd_dbus_iface.DBUS_IFACE_FOLDERS_NAME) |
|
159 |
||
160 |
||
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
161 |
def get_status_syncdaemon_proxy(): |
162 |
"""Get a DBus proxy for syncdaemon status calls."""
|
|
163 |
return get_syncdaemon_proxy("/status", |
|
164 |
sd_dbus_iface.DBUS_IFACE_STATUS_NAME) |
|
165 |
||
166 |
||
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
167 |
def get_throttling_limits(): |
168 |
"""Get the speed limits from the syncdaemon."""
|
|
169 |
d = defer.Deferred() |
|
170 |
proxy = get_config_syncdaemon_proxy() |
|
171 |
proxy.get_throttling_limits(reply_handler=d.callback, |
|
172 |
error_handler=d.errback) |
|
173 |
return d |
|
174 |
||
175 |
||
176 |
def set_throttling_limits(limits): |
|
177 |
"""Set the speed limits on the syncdaemon."""
|
|
178 |
d = defer.Deferred() |
|
179 |
proxy = get_config_syncdaemon_proxy() |
|
180 |
download = int(limits["download"]) |
|
181 |
upload = int(limits["upload"]) |
|
182 |
proxy.set_throttling_limits(download, upload, |
|
183 |
reply_handler=lambda: d.callback("ok"), |
|
184 |
error_handler=d.errback) |
|
185 |
return d |
|
186 |
||
187 |
||
188 |
def bandwidth_throttling_enabled(): |
|
189 |
"""Get the state of throttling in the syncdaemon."""
|
|
190 |
d = defer.Deferred() |
|
191 |
proxy = get_config_syncdaemon_proxy() |
|
192 |
proxy.bandwidth_throttling_enabled(reply_handler=d.callback, |
|
193 |
error_handler=d.errback) |
|
194 |
return d |
|
195 |
||
196 |
||
197 |
def enable_bandwidth_throttling(): |
|
198 |
"""Enable the speed limits in the syncdaemon."""
|
|
199 |
d = defer.Deferred() |
|
200 |
proxy = get_config_syncdaemon_proxy() |
|
201 |
proxy.enable_bandwidth_throttling(reply_handler=lambda: d.callback("ok"), |
|
202 |
error_handler=d.errback) |
|
203 |
return d |
|
204 |
||
205 |
||
206 |
def disable_bandwidth_throttling(): |
|
207 |
"""Disable the speed limits in the syncdaemon."""
|
|
208 |
d = defer.Deferred() |
|
209 |
proxy = get_config_syncdaemon_proxy() |
|
210 |
proxy.disable_bandwidth_throttling(reply_handler=lambda: d.callback("ok"), |
|
211 |
error_handler=d.errback) |
|
212 |
return d |
|
213 |
||
214 |
||
1.1.7
by Natalia B. Bidart
Import upstream version 0.8.5 |
215 |
def show_all_notifications_enabled(): |
216 |
"""Get the state of show_all_notifications in the syncdaemon."""
|
|
217 |
d = defer.Deferred() |
|
218 |
proxy = get_config_syncdaemon_proxy() |
|
219 |
proxy.show_all_notifications_enabled(reply_handler=d.callback, |
|
220 |
error_handler=d.errback) |
|
221 |
return d |
|
222 |
||
223 |
||
224 |
def enable_show_all_notifications(): |
|
225 |
"""Enable show_all_notifications in the syncdaemon."""
|
|
226 |
d = defer.Deferred() |
|
227 |
proxy = get_config_syncdaemon_proxy() |
|
228 |
proxy.enable_show_all_notifications(reply_handler=lambda: d.callback("ok"), |
|
229 |
error_handler=d.errback) |
|
230 |
return d |
|
231 |
||
232 |
||
233 |
def disable_show_all_notifications(): |
|
234 |
"""Disable show_all_notifications in the syncdaemon."""
|
|
235 |
d = defer.Deferred() |
|
236 |
proxy = get_config_syncdaemon_proxy() |
|
237 |
handler = lambda: d.callback("ok") |
|
238 |
proxy.disable_show_all_notifications(reply_handler=handler, |
|
239 |
error_handler=d.errback) |
|
240 |
return d |
|
241 |
||
242 |
||
1.1.3
by Natalia B. Bidart
Import upstream version 0.8.0 |
243 |
def get_root_dir(): |
244 |
"""Retrieve the root information from syncdaemon."""
|
|
245 |
d = defer.Deferred() |
|
246 |
proxy = get_syncdaemon_proxy("/", sd_dbus_iface.DBUS_IFACE_SYNC_NAME) |
|
247 |
proxy.get_rootdir(reply_handler=d.callback, error_handler=d.errback) |
|
248 |
return d |
|
249 |
||
250 |
||
1.1.6
by Rodney Dawes
Import upstream version 0.8.3 |
251 |
def get_shares_dir(): |
252 |
"""Retrieve the shares information from syncdaemon."""
|
|
253 |
d = defer.Deferred() |
|
254 |
proxy = get_syncdaemon_proxy("/", sd_dbus_iface.DBUS_IFACE_SYNC_NAME) |
|
255 |
proxy.get_sharesdir(reply_handler=d.callback, error_handler=d.errback) |
|
256 |
return d |
|
257 |
||
258 |
||
259 |
def get_shares_dir_link(): |
|
260 |
"""Retrieve the shares information from syncdaemon."""
|
|
261 |
d = defer.Deferred() |
|
262 |
proxy = get_syncdaemon_proxy("/", sd_dbus_iface.DBUS_IFACE_SYNC_NAME) |
|
263 |
proxy.get_sharesdir_link(reply_handler=d.callback, error_handler=d.errback) |
|
264 |
return d |
|
265 |
||
266 |
||
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
267 |
def get_folders(): |
268 |
"""Retrieve the folders information from syncdaemon."""
|
|
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
269 |
d = defer.Deferred() |
270 |
proxy = get_folders_syncdaemon_proxy() |
|
271 |
proxy.get_folders(reply_handler=d.callback, error_handler=d.errback) |
|
272 |
return d |
|
273 |
||
274 |
||
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
275 |
def create_folder(path): |
276 |
"""Create a new folder through syncdaemon."""
|
|
1
by Natalia Bidart (nessita)
Import upstream version 0.0.9 |
277 |
d = defer.Deferred() |
278 |
proxy = get_folders_syncdaemon_proxy() |
|
279 |
||
280 |
sig = proxy.connect_to_signal('FolderCreated', d.callback) |
|
281 |
cb = lambda path, error: d.errback(VolumesError(path, error)) |
|
282 |
esig = proxy.connect_to_signal('FolderCreateError', cb) |
|
283 |
||
284 |
proxy.create(path, reply_handler=no_op, error_handler=d.errback) |
|
285 |
||
286 |
def cleanup_signals(result): |
|
287 |
"""Remove signals after use."""
|
|
288 |
sig.remove() |
|
289 |
esig.remove() |
|
290 |
return result |
|
291 |
||
292 |
d.addBoth(cleanup_signals) |
|
293 |
return d |
|
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
294 |
|
295 |
||
296 |
def subscribe_folder(folder_id): |
|
297 |
"""Subscribe to 'folder_id'."""
|
|
298 |
d = defer.Deferred() |
|
299 |
proxy = get_folders_syncdaemon_proxy() |
|
300 |
||
301 |
sig = proxy.connect_to_signal('FolderSubscribed', d.callback) |
|
1.1.6
by Rodney Dawes
Import upstream version 0.8.3 |
302 |
cb = lambda info, error: d.errback(VolumesError(info['id'], error)) |
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
303 |
esig = proxy.connect_to_signal('FolderSubscribeError', cb) |
304 |
||
305 |
proxy.subscribe(folder_id, reply_handler=no_op, error_handler=no_op) |
|
306 |
||
307 |
def cleanup_signals(result): |
|
308 |
"""Remove signals after use."""
|
|
309 |
sig.remove() |
|
310 |
esig.remove() |
|
311 |
return result |
|
312 |
||
313 |
d.addBoth(cleanup_signals) |
|
314 |
return d |
|
315 |
||
316 |
||
317 |
def unsubscribe_folder(folder_id): |
|
318 |
"""Unsubscribe 'folder_id'."""
|
|
319 |
d = defer.Deferred() |
|
320 |
proxy = get_folders_syncdaemon_proxy() |
|
321 |
||
322 |
sig = proxy.connect_to_signal('FolderUnSubscribed', d.callback) |
|
1.1.6
by Rodney Dawes
Import upstream version 0.8.3 |
323 |
cb = lambda info, error: d.errback(VolumesError(info['id'], error)) |
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
324 |
esig = proxy.connect_to_signal('FolderUnSubscribeError', cb) |
325 |
||
326 |
proxy.unsubscribe(folder_id, reply_handler=no_op, error_handler=no_op) |
|
327 |
||
328 |
def cleanup_signals(result): |
|
329 |
"""Remove signals after use."""
|
|
330 |
sig.remove() |
|
331 |
esig.remove() |
|
332 |
return result |
|
333 |
||
334 |
d.addBoth(cleanup_signals) |
|
335 |
return d |
|
336 |
||
337 |
||
1.1.6
by Rodney Dawes
Import upstream version 0.8.3 |
338 |
@defer.inlineCallbacks |
339 |
def get_shares(): |
|
340 |
"""Retrieve the shares information from syncdaemon."""
|
|
341 |
result = yield SyncDaemonTool(bus=dbus.SessionBus()).get_shares() |
|
342 |
defer.returnValue(result) |
|
343 |
||
344 |
||
345 |
@defer.inlineCallbacks |
|
346 |
def subscribe_share(share_id): |
|
347 |
"""Subscribe to 'share_id'."""
|
|
348 |
try: |
|
349 |
yield SyncDaemonTool(bus=dbus.SessionBus()).subscribe_share(share_id) |
|
350 |
except Exception, e: |
|
351 |
raise VolumesError(share_id, e) |
|
352 |
||
353 |
||
354 |
@defer.inlineCallbacks |
|
355 |
def unsubscribe_share(share_id): |
|
356 |
"""Unsubscribe 'share_id'."""
|
|
357 |
try: |
|
358 |
yield SyncDaemonTool(bus=dbus.SessionBus()).unsubscribe_share(share_id) |
|
359 |
except Exception, e: |
|
360 |
raise VolumesError(share_id, e) |
|
361 |
||
362 |
||
1.1.1
by Natalia B. Bidart
Import upstream version 0.1.0 |
363 |
def get_current_status(): |
364 |
"""Retrieve the current status from syncdaemon."""
|
|
365 |
d = defer.Deferred() |
|
366 |
proxy = get_status_syncdaemon_proxy() |
|
367 |
proxy.current_status(reply_handler=d.callback, error_handler=d.errback) |
|
368 |
return d |
|
369 |
||
370 |
||
371 |
def set_status_changed_handler(handler): |
|
372 |
"""Connect 'handler' with syncdaemon's StatusChanged signal."""
|
|
373 |
proxy = get_status_syncdaemon_proxy() |
|
374 |
sig = proxy.connect_to_signal('StatusChanged', handler) |
|
375 |
return proxy, sig |
|
376 |
||
377 |
||
378 |
def files_sync_enabled(): |
|
379 |
"""Get if file sync service is enabled."""
|
|
380 |
enabled = SyncDaemonTool(bus=None).is_files_sync_enabled() |
|
381 |
return defer.succeed(enabled) |
|
382 |
||
383 |
||
384 |
@defer.inlineCallbacks |
|
385 |
def set_files_sync_enabled(enabled): |
|
386 |
"""Set the file sync service to be 'enabled'."""
|
|
387 |
yield SyncDaemonTool(bus=dbus.SessionBus()).enable_files_sync(enabled) |
|
1.1.3
by Natalia B. Bidart
Import upstream version 0.8.0 |
388 |
|
389 |
||
390 |
@defer.inlineCallbacks |
|
391 |
def connect_file_sync(): |
|
392 |
"""Connect the file sync service."""
|
|
393 |
yield SyncDaemonTool(bus=dbus.SessionBus()).connect() |
|
394 |
||
395 |
||
396 |
@defer.inlineCallbacks |
|
397 |
def disconnect_file_sync(): |
|
398 |
"""Disconnect the file sync service."""
|
|
399 |
yield SyncDaemonTool(bus=dbus.SessionBus()).disconnect() |
|
400 |
||
401 |
||
402 |
@defer.inlineCallbacks |
|
403 |
def start_file_sync(): |
|
404 |
"""Start the file sync service."""
|
|
405 |
yield SyncDaemonTool(bus=dbus.SessionBus()).start() |
|
406 |
||
407 |
||
408 |
@defer.inlineCallbacks |
|
409 |
def stop_file_sync(): |
|
410 |
"""Stop the file sync service."""
|
|
411 |
yield SyncDaemonTool(bus=dbus.SessionBus()).quit() |