~apachelogger/ubuntuone-client/stable-1-2-lucid+kwallet

33 by Harald Sitter
Add kwallet support
1
--- a/ubuntuone/syncdaemon/main.py
2
+++ b/ubuntuone/syncdaemon/main.py
3
@@ -19,7 +19,6 @@
4
 """ SyncDaemon Main"""
5
 import dbus
6
 from dbus.mainloop.glib import DBusGMainLoop
7
-import gnomekeyring
8
 import logging
9
 import os
10
 import sys
37 by Harald Sitter
Only ever use one QApp instance, otherwise someone will start crying and
11
@@ -307,6 +306,51 @@
33 by Harald Sitter
Add kwallet support
12
 
13
         If no token is available in the keyring, `NoAccessToken` is raised.
14
         """
35 by Harald Sitter
Make patch use kcheckrunning instead of env var KDE_FULL_SESSION, latter
15
+        import subprocess
16
+        try:
17
+            # kcheckrunning returns 0 iff KDE is running
18
+            k = subprocess.Popen(["kcheckrunning"]).wait()
19
+        except OSError:
20
+            # kcheckrunning not found -> no KDE session
21
+            k = 1
22
+
23
+        if k == 0:
33 by Harald Sitter
Add kwallet support
24
+            try:
25
+                from PyKDE4.kdeui import KWallet
26
+                from PyQt4.QtCore import QCoreApplication
27
+
28
+                # KWallet uses a QEventLoop. This only works if a QApp is present.
37 by Harald Sitter
Only ever use one QApp instance, otherwise someone will start crying and
29
+                # We only can have one instance - ever!
30
+                a = QCoreApplication.instance()
31
+                if a == None:
32
+                    a = QCoreApplication([""])
33 by Harald Sitter
Add kwallet support
33
+
34
+                wallet = KWallet.Wallet.openWallet(KWallet.Wallet.LocalWallet(), 0, KWallet.Wallet.Synchronous)
35
+                if wallet.isOpen():
36
+                    if wallet.setFolder("ubuntuone"):
37
+                        fail, token = wallet.readPassword("token")
38
+                        if not fail and not token.isEmpty(): # For some reason fail is not really fail?
39
+                            return oauth.OAuthToken.from_string(str(token))
40
+                        else:
41
+                            raise NoAccessToken("No access token found.")
42
+                    else:
43
+                        raise NoAccessToken("Could not access ubuntuone folder.")
44
+                else:
45
+                    raise NoAccessToken("Could not access wallet.")
35 by Harald Sitter
Make patch use kcheckrunning instead of env var KDE_FULL_SESSION, latter
46
+            # If PyKDE is missing or KWallet did not contain a token, try to use gnomekeyring:
47
+            except ImportError as error:
48
+                try:
49
+                    import gnomekeyring
50
+                except ImportError:
51
+                    raise error
33 by Harald Sitter
Add kwallet support
52
+            except NoAccessToken as error:
53
+                try:
54
+                    import gnomekeyring
55
+                except ImportError:
56
+                    raise error
35 by Harald Sitter
Make patch use kcheckrunning instead of env var KDE_FULL_SESSION, latter
57
+        else: # If KDE is available we fall back to gnomekeyring in appropriate excepts.
33 by Harald Sitter
Add kwallet support
58
+            import gnomekeyring
35 by Harald Sitter
Make patch use kcheckrunning instead of env var KDE_FULL_SESSION, latter
59
+
33 by Harald Sitter
Add kwallet support
60
         try:
61
             items = gnomekeyring.find_items_sync(
62
                 gnomekeyring.ITEM_GENERIC_SECRET,