~ubuntuone-control-tower/desktopcouch/trunk

« back to all changes in this revision

Viewing changes to desktopcouch/application/plugins/ubuntuone_pairing.py

When running plugin code in initializing the desktopcouch service, do not call any functions that require that the service be running and answering DBus method calls. In particular, we already know the port couchdb is listening on, so do not ask for that via DBus client call, but explicity pass it into the plugin function calls. (LP: #706939)

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
"""
34
34
 
35
35
 
36
 
def pair_with_ubuntuone(management_db=None):
 
36
def pair_with_ubuntuone(couchdb_port, management_db=None,
 
37
                        db_class=DesktopDatabase,
 
38
                        put_service_fn=put_static_paired_service):
37
39
    """Adds a pairing record with ubuntu one when needed."""
 
40
    # Use explicit uri so that we do not access dbus service.
 
41
    uri = "http://localhost:%s" % (couchdb_port,)
38
42
    if not management_db:
39
 
        management_db = DesktopDatabase("management", create=True)
 
43
        management_db = db_class("management", uri=uri, create=True, ctx=None)
40
44
    # we indeed have credentials to add to the pairing records
41
45
    # but first we ensure that the required view is present
42
46
    if not management_db.view_exists(U1_PAIR_RECORD, U1_PAIR_RECORD):
60
64
            logging.debug("Not adding desktopcouch pairing since we are "
61
65
                "already paired")
62
66
    if not pairing_found:
63
 
        put_static_paired_service(None, "ubuntuone")
 
67
        put_service_fn(None, "ubuntuone", uri=uri, ctx=None)
64
68
        logging.debug("Pairing desktopcouch with Ubuntu One")
65
69
 
66
70
 
67
 
def got_new_credentials(app_name, credentials):
 
71
def got_new_credentials(couchdb_port, app_name, credentials):
68
72
    """Pair with Ubuntu One when we get the new credentials."""
69
73
    if app_name == APP_NAME:
70
 
        pair_with_ubuntuone()
71
 
 
72
 
 
73
 
def listen_to_dbus():
 
74
        pair_with_ubuntuone(couchdb_port)
 
75
 
 
76
 
 
77
def listen_to_dbus(couchdb_port):
74
78
    """Set up the signal handler on D-Bus for Ubuntu One pairing."""
75
79
    import dbus
76
80
    bus = dbus.SessionBus()
78
82
    try:
79
83
        import ubuntu_sso
80
84
 
 
85
        receiver = lambda *args: got_new_credentials(couchdb_port, *args)
 
86
 
81
87
        iface = ubuntu_sso.DBUS_CREDENTIALS_IFACE
82
 
        bus.add_signal_receiver(handler_function=got_new_credentials,
 
88
        bus.add_signal_receiver(handler_function=receiver,
83
89
                                signal_name='CredentialsFound',
84
90
                                dbus_interface=iface)
85
91
 
92
98
        logging.info('Ubuntu SSO is not available.')
93
99
 
94
100
 
95
 
def plugin_init():
 
101
def plugin_init(couchdb_port):
96
102
    """Set up the signal handler for pairing with Ubuntu One."""
97
103
    logging.info('Loaded Ubuntu One extension for desktopcouch.')
98
104
    if sys.platform == 'win32':
99
105
        logging.warning('Windows support for Ubuntu One is not yet ready.')
100
106
    else:
101
107
        import gobject
102
 
        gobject.idle_add(listen_to_dbus)
 
108
        gobject.idle_add(listen_to_dbus, couchdb_port)