Guest User

Untitled

a guest
Mar 19th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. // create-online-sync-account.pro
  2. TEMPLATE = app
  3. TARGET = create-online-sync-account
  4. CONFIG += link_pkgconfig
  5. PKGCONFIG += accounts-qt5 libsignon-qt5
  6.  
  7. QT += network
  8. SOURCES = main.cpp CreateOnlineSyncAccount.cpp
  9. HEADERS = CreateOnlineSyncAccount.h
  10.  
  11. // main.cpp
  12. #include <QCoreApplication>
  13. #include <QObject>
  14. #include <QtDebug>
  15. #include "CreateOnlineSyncAccount.h"
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19. QCoreApplication app(argc, argv);
  20. CreateOnlineSyncAccount c;
  21. QString remotePath, mode, username, password;
  22.  
  23. if (argc != 3 && argc != 5) {
  24. qWarning() << "usage: create-online-sync-account <remotePath> <legacy> <username> <password>";
  25. return 1;
  26. }
  27.  
  28. remotePath = QString::fromLatin1(argv[1]);
  29. mode = QString::fromLatin1(argv[2]);
  30. if (argc == 5) username = QString::fromLatin1(argv[3]);
  31. if (argc == 5) password = QString::fromLatin1(argv[4]);
  32.  
  33. if (mode != QStringLiteral("legacy")) {
  34. // actually we don't support oauth2 yet
  35. // to support that, we would also need as parameters:
  36. // auth path / token path / redirect uri / scopes etc.
  37. qWarning() << "oauth2 not currently supported";
  38. return 1;
  39. }
  40.  
  41. QObject::connect(&c, SIGNAL(done()), &app, SLOT(quit()));
  42. c.createOnlineSyncAccount(remotePath, mode, username, password);
  43. return app.exec();
  44. }
  45.  
  46.  
  47. // createonlinesyncaccount.h
  48. #ifndef CREATEONLINESYNCACCOUNT_H
  49. #define CREATEONLINESYNCACCOUNT_H
  50.  
  51. #include <QObject>
  52.  
  53. #include <Accounts/Account>
  54. #include <Accounts/Manager>
  55. #include <Accounts/AccountService>
  56. #include <SignOn/Error>
  57. #include <SignOn/Identity>
  58. #include <SignOn/AuthSession>
  59. #include <SignOn/SessionData>
  60.  
  61. class CreateOnlineSyncAccount : public QObject
  62. {
  63. Q_OBJECT
  64.  
  65. public:
  66. CreateOnlineSyncAccount(QObject *parent = 0);
  67. ~CreateOnlineSyncAccount();
  68.  
  69. void createOnlineSyncAccount(const QString &remotePath,
  70. const QString &mode,
  71. const QString &username,
  72. const QString &password);
  73.  
  74. public Q_SLOTS:
  75. void credentialsStored();
  76. void credentialsFailed();
  77.  
  78. Q_SIGNALS:
  79. void done();
  80.  
  81. private:
  82. Accounts::Manager *m_accountManager;
  83. Accounts::Account *m_account;
  84. SignOn::Identity *m_identity;
  85. };
  86.  
  87. #endif
  88.  
  89. // createonlinesyncaccount.cpp
  90. #include "CreateOnlineSyncAccount.h"
  91. #include <QtDebug>
  92. #include <QUrl>
  93. #include <Accounts/Service>
  94. #include <SignOn/Identity>
  95. #include <SignOn/IdentityInfo>
  96.  
  97.  
  98. CreateOnlineSyncAccount::CreateOnlineSyncAccount(QObject *parent)
  99. : QObject(parent)
  100. , m_accountManager(new Accounts::Manager(this))
  101. , m_account(0)
  102. , m_identity(0)
  103. {
  104. }
  105.  
  106. CreateOnlineSyncAccount::~CreateOnlineSyncAccount()
  107. {
  108. delete m_account;
  109. delete m_identity;
  110. }
  111.  
  112. void CreateOnlineSyncAccount::createOnlineSyncAccount(const QString &remotePath,
  113. const QString &mode,
  114. const QString &username,
  115. const QString &password)
  116. {
  117. Q_UNUSED(mode) // until we take tokenpaths/redirecturis etc as parameters also...
  118.  
  119. m_account = m_accountManager->createAccount("caldav"); // change this as required!
  120. if (!m_account) {
  121. qWarning() << "error: couldn't create onlinesync account";
  122. emit done();
  123. }
  124.  
  125. Accounts::Service srv = m_accountManager->service("caldav-yahoo"); // change this as required!
  126. if (!srv.isValid()) {
  127. qWarning() << "error: couldn't retrieve caldav service";
  128. emit done();
  129. }
  130.  
  131. m_account->setDisplayName("caldav account");
  132. m_account->selectService(srv);
  133. m_account->setValue("Remote database", remotePath);
  134. m_account->setValue("auth/method", "password");
  135. m_account->setValue("auth/mechanism", "password");
  136. m_account->setEnabled(true);
  137. m_account->selectService(Accounts::Service());
  138. m_account->setEnabled(false); // disable globally until credentials are set.
  139.  
  140. m_account->syncAndBlock();
  141.  
  142. QMap<QString, QStringList> methodMechs;
  143. methodMechs.insert("password", QStringList() << "password");
  144. SignOn::IdentityInfo *info = new SignOn::IdentityInfo(m_account->displayName(), username, methodMechs);
  145. info->setSecret(password, true);
  146. QUrl url(remotePath);
  147. info->setRealms(QStringList() << url.host());
  148. info->setType(SignOn::IdentityInfo::Application);
  149.  
  150. m_identity = SignOn::Identity::newIdentity(*info);
  151. connect(m_identity, SIGNAL(error(const SignOn::Error &)),
  152. this, SLOT(credentialsFailed()));
  153. connect(m_identity, SIGNAL(credentialsStored(const quint32)),
  154. this, SLOT(credentialsStored()));
  155.  
  156. m_identity->storeCredentials();
  157. }
  158.  
  159. void CreateOnlineSyncAccount::credentialsFailed()
  160. {
  161. qWarning() << "error: failed to store credentials";
  162. emit done();
  163. }
  164.  
  165. void CreateOnlineSyncAccount::credentialsStored()
  166. {
  167. m_account->setEnabled(true);
  168. m_account->syncAndBlock();
  169.  
  170. qWarning() << "success!";
  171. emit done();
  172. }
Advertisement
Add Comment
Please, Sign In to add comment