Index: subversion/libsvn_auth_kwallet/kwallet.cpp
===================================================================
--- subversion/libsvn_auth_kwallet/kwallet.cpp	(revision 31821)
+++ subversion/libsvn_auth_kwallet/kwallet.cpp	(working copy)
@@ -33,6 +33,7 @@
 #include "svn_private_config.h"
 
 #include <QtCore/QString>
+#include <QtGui/QWidget>
 
 #include <kapplication.h>
 #include <kcmdlineargs.h>
@@ -43,16 +44,28 @@
 /* KWallet simple provider, puts passwords in KWallet                    */
 /*-----------------------------------------------------------------------*/
 
-/* Implementation of svn_auth__password_get_t that retrieves
-   the password from KWallet. */
-static svn_boolean_t
-kwallet_password_get(const char **password,
-                     apr_hash_t *creds,
-                     const char *realmstring,
-                     const char *username,
-                     svn_boolean_t non_interactive,
-                     apr_pool_t *pool)
+class SvnKWallet
 {
+public:
+  static svn_boolean_t init(svn_boolean_t non_interactive);
+  SvnKWallet(const char *realmstring, const char *username);
+  ~SvnKWallet();
+  svn_boolean_t get(const char **password, apr_pool_t *pool);
+  svn_boolean_t set(const char *password);
+private:
+  bool open(bool check_key, bool create_wallet);
+
+  KApplication application;
+  QWidget widget;
+  WId wid;
+  KWallet::Wallet *wallet;
+  QString wallet_name;
+  QString folder;
+  QString key;
+};
+
+svn_boolean_t SvnKWallet::init( svn_boolean_t non_interactive )
+{
   if (non_interactive)
     {
       return FALSE;
@@ -63,52 +76,93 @@
       return FALSE;
     }
 
+  char kdearg0[] = "svn";
+  char *kdeargs[1] = { kdearg0 };
   KCmdLineArgs::init(1,
-                     (char *[1]) { "svn" },
+                     kdeargs,
                      "Subversion",
                      "subversion",
                      ki18n("Subversion"),
                      SVN_VER_NUMBER,
                      ki18n("Version control system"),
                      KCmdLineArgs::CmdLineArgKDE);
-  KApplication application;
-  svn_boolean_t ret = FALSE;
-  QString wallet_name = KWallet::Wallet::NetworkWallet();
-  QString folder = QString::fromUtf8("Subversion");
-  QString key =
-    QString::fromUtf8(username) + "@" + QString::fromUtf8(realmstring);
-  if (! KWallet::Wallet::keyDoesNotExist(wallet_name, folder, key))
-    {
-      KWallet::Wallet *wallet =
-        KWallet::Wallet::openWallet(wallet_name,
-                                    -1,
-                                    KWallet::Wallet::Synchronous);
-      if (wallet)
-        {
-          if (wallet->hasFolder(folder))
-            {
-              if (wallet->setFolder(folder))
-                {
-                  QString q_password;
-                  if (wallet->readPassword(key, q_password) == 0);
-                    {
-                      *password = apr_pstrmemdup(pool,
-                                                 q_password.toUtf8().data(),
-                                                 q_password.size());
-                      ret = TRUE;
-                    }
-                }
-            }
-        }
-    }
+  return TRUE;
+}
+
+SvnKWallet::SvnKWallet(const char *realmstring, const char *username)
+  : wallet(0)
+{
+  wallet_name = KWallet::Wallet::NetworkWallet();
+  folder = QString::fromUtf8("Subversion");
+  key = QString::fromUtf8(username) + "@" + QString::fromUtf8(realmstring);
+}
+
+SvnKWallet::~SvnKWallet()
+{
 // This function currently closes the wallet if no other application
 // is connected to the wallet. We're waiting for this to be fixed
 // upstream, see https://bugs.kde.org/show_bug.cgi?id=162570
 //  KWallet::Wallet::disconnectApplication(wallet_name,
 //                                         QString::fromUtf8("Subversion"));
-  return ret;
 }
 
+svn_boolean_t SvnKWallet::get(const char **password, apr_pool_t *pool)
+{
+  if (!open(true, false))
+    return FALSE;
+  QString q_password;
+  if (wallet->readPassword(key, q_password) != 0)
+    return FALSE;
+  *password = apr_pstrmemdup(pool,
+                             q_password.toUtf8().data(),
+                             q_password.size());
+  return TRUE;
+}
+
+svn_boolean_t SvnKWallet::set(const char *password)
+{
+  if (!open(false, true))
+    return FALSE;
+  QString q_password = QString::fromUtf8(password);
+  if (wallet->writePassword(key, q_password) != 0)
+    return FALSE;
+  return TRUE;
+}
+
+bool SvnKWallet::open(bool check_key, bool create_wallet)
+{
+  if (check_key && KWallet::Wallet::keyDoesNotExist(wallet_name, folder, key))
+    return false;
+  wallet = KWallet::Wallet::openWallet(wallet_name,
+                                       wid,
+                                       KWallet::Wallet::Synchronous);
+  if (!wallet)
+    return false;
+  if (!wallet->hasFolder(folder))
+    {
+      if (!create_wallet)
+        return false;
+      if (!wallet->createFolder(folder))
+        return false;
+    }
+  return wallet->setFolder(folder);
+}
+
+/* Implementation of svn_auth__password_get_t that retrieves
+   the password from KWallet. */
+static svn_boolean_t
+kwallet_password_get(const char **password,
+                     apr_hash_t *creds,
+                     const char *realmstring,
+                     const char *username,
+                     svn_boolean_t non_interactive,
+                     apr_pool_t *pool)
+{
+  if (!SvnKWallet::init(non_interactive))
+    return FALSE;
+  return SvnKWallet(realmstring, username).get(password, pool);
+}
+
 /* Implementation of svn_auth__password_set_t that stores
    the password in KWallet. */
 static svn_boolean_t
@@ -119,58 +173,9 @@
                      svn_boolean_t non_interactive,
                      apr_pool_t *pool)
 {
-  if (non_interactive)
-    {
-      return FALSE;
-    }
-
-  if (! KWallet::Wallet::isEnabled())
-    {
-      return FALSE;
-    }
-
-  KCmdLineArgs::init(1,
-                     (char *[1]) { "svn" },
-                     "Subversion",
-                     "subversion",
-                     ki18n("Subversion"),
-                     SVN_VER_NUMBER,
-                     ki18n("Version control system"),
-                     KCmdLineArgs::CmdLineArgKDE);
-  KApplication application;
-  svn_boolean_t ret = FALSE;
-  QString q_password = QString::fromUtf8(password);
-  QString wallet_name = KWallet::Wallet::NetworkWallet();
-  QString folder = QString::fromUtf8("Subversion");
-  KWallet::Wallet *wallet =
-    KWallet::Wallet::openWallet(wallet_name,
-                                -1,
-                                KWallet::Wallet::Synchronous);
-  if (wallet)
-    {
-      if (! wallet->hasFolder(folder))
-        {
-          wallet->createFolder(folder);
-        }
-      if (wallet->hasFolder(folder))
-        {
-          if (wallet->setFolder(folder))
-            {
-              QString key = QString::fromUtf8(username) + "@"
-                + QString::fromUtf8(realmstring);
-              if (wallet->writePassword(key, q_password) == 0)
-                {
-                  ret = TRUE;
-                }
-            }
-        }
-    }
-// This function currently closes the wallet if no other application
-// is connected to the wallet. We're waiting for this to be fixed
-// upstream, see https://bugs.kde.org/show_bug.cgi?id=162570
-//  KWallet::Wallet::disconnectApplication(wallet_name,
-//                                         QString::fromUtf8("Subversion"));
-  return ret;
+  if (!SvnKWallet::init(non_interactive))
+    return FALSE;
+  return SvnKWallet(realmstring, username).set(password);
 }
 
 /* Get cached encrypted credentials from the simple provider's cache. */
