~verterok/ubuntuone-client/vm-pre-refactor-api-3

« back to all changes in this revision

Viewing changes to bin/ubuntuone-client-applet

  • Committer: Tarmac
  • Author(s): Rodney Dawes
  • Date: 2010-01-06 22:37:42 UTC
  • mfrom: (300.3.2 appletless-oauthdesktop)
  • Revision ID: dobey@gnome.org-20100106223742-fds3r9eoqrcjn71f
Split out OAuth login handling into a separate libexec script

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
pygtk.require('2.0')
25
25
import gobject
26
26
import gtk
27
 
import pango
28
27
import os
29
28
import subprocess
30
29
import sys
39
38
from ConfigParser import ConfigParser
40
39
from dbus.exceptions import DBusException
41
40
from dbus.mainloop.glib import DBusGMainLoop
42
 
from ubuntuone.oauthdesktop.main import Login
43
41
from xdg.BaseDirectory import xdg_config_home
44
42
from threading import Lock, Thread
45
43
from urllib import quote
90
88
 
91
89
      def __init__(self, *args, **kw):
92
90
            """Initializes the child threads and dbus monitor."""
93
 
            from twisted.internet import gtk2reactor
94
 
            gtk2reactor.install()
95
 
            login = Login(dbus.service.BusName(DBUS_IFACE_AUTH_NAME,
96
 
                                               bus=dbus.SessionBus()))
97
 
 
98
91
            logger.info(_("Starting Ubuntu One client version %s") %
99
92
                        clientdefs.VERSION)
100
93
 
145
138
                  handler_function=self.__no_credentials,
146
139
                  signal_name="NoCredentials",
147
140
                  dbus_interface=DBUS_IFACE_AUTH_NAME)
148
 
            self.__bus.add_signal_receiver(
149
 
                  handler_function=self.__got_oauth_error,
150
 
                  signal_name="OAuthError",
151
 
                  dbus_interface=DBUS_IFACE_AUTH_NAME)
152
141
 
153
142
            self.__icon = AppletIcon(main=self, config=self.config)
154
143
 
159
148
            self.__start_storage_daemon()
160
149
            self.add_to_autostart()
161
150
 
162
 
            self.set_up_desktopcouch_pairing(consumer_key)
163
 
 
164
151
            if self.connect == 2:
165
152
                  return
166
153
 
194
181
            except DBusException, e:
195
182
                  quit_error(e)
196
183
 
197
 
            from twisted.internet import reactor
198
 
            reactor.stop()
 
184
            gtk.main_quit()
199
185
 
200
186
      def __no_credentials(self):
201
187
            """Signal callback for when no credentials exist in the keyring."""
202
188
            self.is_authorized = False
203
189
 
204
 
      def __got_oauth_error(self, message=None):
205
 
            """Signal callback for when an OAuth error occured."""
206
 
            def dialog_response(dialog, response):
207
 
                  """Handle the dialog closing."""
208
 
                  dialog.destroy()
209
 
 
210
 
            if message:
211
 
                  logger.error(message)
212
 
                  dialog = gtk.Dialog(title=_("Ubuntu One: Error"),
213
 
                                      flags=gtk.DIALOG_NO_SEPARATOR,
214
 
                                      buttons=(gtk.STOCK_CLOSE,
215
 
                                               gtk.RESPONSE_CLOSE))
216
 
                  dialog.set_default_response(gtk.RESPONSE_CLOSE)
217
 
                  dialog.set_icon_name("ubuntuone-client")
218
 
 
219
 
                  area = dialog.get_content_area()
220
 
 
221
 
                  hbox = gtk.HBox(spacing=12)
222
 
                  hbox.set_border_width(12)
223
 
                  area.pack_start(hbox)
224
 
                  hbox.show()
225
 
 
226
 
                  image = gtk.Image()
227
 
                  image.set_from_icon_name("dialog-error", gtk.ICON_SIZE_DIALOG)
228
 
                  image.set_alignment(0.5, 0.0)
229
 
                  image.show()
230
 
                  hbox.pack_start(image, False, False)
231
 
 
232
 
                  vbox = gtk.VBox(spacing=12)
233
 
                  vbox.show()
234
 
                  hbox.pack_start(vbox)
235
 
 
236
 
                  label = gtk.Label("<b>%s</b>" % _("Authorization Error"))
237
 
                  label.set_use_markup(True)
238
 
                  label.set_alignment(0.0, 0.5)
239
 
                  label.show()
240
 
                  vbox.pack_start(label, False, False)
241
 
 
242
 
                  label = gtk.Label(message)
243
 
                  label.set_line_wrap(True)
244
 
                  label.set_max_width_chars(64)
245
 
                  label.set_ellipsize(pango.ELLIPSIZE_MIDDLE)
246
 
                  label.set_alignment(0.0, 0.0)
247
 
                  label.show()
248
 
                  vbox.pack_start(label, True, True)
249
 
 
250
 
                  dialog.connect('close', dialog_response, gtk.RESPONSE_CLOSE)
251
 
                  dialog.connect('response', dialog_response)
252
 
 
253
 
                  dialog.show()
254
 
            else:
255
 
                  logger.error(_("Got an OAuth error with no message."))
256
 
 
257
190
      def check_for_token(self, do_login=False):
258
191
            """Method to check for an existing token."""
259
192
            def local_dbus_error(e):
322
255
            except OSError:
323
256
                  pass
324
257
 
325
 
      def set_up_desktopcouch_pairing(self, consumer_key):
326
 
          """Add a pairing record between desktopcouch and Ubuntu One"""
327
 
          try:
328
 
              from desktopcouch.pair.couchdb_pairing.couchdb_io import \
329
 
                   put_static_paired_service, PAIRED_SERVER_RECORD_TYPE
330
 
              from desktopcouch.records.server import CouchDatabase
331
 
          except ImportError:
332
 
              # desktopcouch is not installed
333
 
              logger.debug(_("Not adding desktopcouch pairing since"
334
 
                  " desktopcouch is not installed"))
335
 
              return
336
 
          # Check whether there is already a record of the Ubuntu One service
337
 
          db = CouchDatabase("management", create=True)
338
 
          if not db.view_exists("ubuntu_one_pair_record","ubuntu_one_pair_record"):
339
 
              map_js = """function(doc) {
340
 
                  if (doc.service_name == "ubuntuone") {
341
 
                      if (doc.application_annotations &&
342
 
                          doc.application_annotations["Ubuntu One"] &&
343
 
                          doc.application_annotations["Ubuntu One"]["private_application_annotations"] &&
344
 
                          doc.application_annotations["Ubuntu One"]["private_application_annotations"]["deleted"]) {
345
 
                          emit(doc._id, 1);
346
 
                      } else {
347
 
                          emit(doc._id, 0)
348
 
                      }
349
 
                  }
350
 
              }"""
351
 
              db.add_view("ubuntu_one_pair_record", map_js, None,
352
 
                  "ubuntu_one_pair_record")
353
 
          results = db.execute_view("ubuntu_one_pair_record",
354
 
              "ubuntu_one_pair_record")
355
 
          found = False
356
 
          deleted = False
357
 
          # Results should contain either one row or no rows
358
 
          # If there is one row, its value will be 0, meaning that there is
359
 
          #   already an Ubuntu One pairing record, or 1, meaning that there
360
 
          #   was an Ubuntu One pairing record but it has since been unpaired
361
 
          # Only create a new record if there is not one already. Specifically,
362
 
          #   do not add the record if there is a deleted one, as this means
363
 
          #   that the user explicitly unpaired it!
364
 
          for row in results:
365
 
              found = True
366
 
              if row.value == 1:
367
 
                  deleted = True
368
 
                  logger.debug(_("Not adding desktopcouch pairing since"
369
 
                  " the user has explicitly unpaired with Ubuntu One"))
370
 
              else:
371
 
                  logger.debug(_("Not adding desktopcouch pairing since"
372
 
                  " we are already paired"))
373
 
          if not found:
374
 
              put_static_paired_service(None, "ubuntuone")
375
 
              logger.debug(_("Pairing desktopcouch with Ubuntu One"))
376
 
 
377
258
      def main(self):
378
259
            """Starts the gtk main loop."""
379
 
            from twisted.internet import reactor
380
260
            if self.connect != 2 or (self.connect == 1 and self.connected):
381
261
                  gobject.idle_add(self.check_for_token, True)
382
262
 
383
 
            reactor.run()
 
263
            gtk.main()
384
264
 
385
265
      @property
386
266
      def authorized(self):
928
808
            """Quit the daemon and closes the applet."""
929
809
            self.__stop_syncdaemon()
930
810
 
931
 
            from twisted.internet import reactor
932
 
            reactor.stop()
 
811
            gtk.main_quit()
933
812
 
934
813
      def __toggle_state(self, menuitem, data=None):
935
814
            """Connects or disconnects the storage sync process."""