Guest User

Untitled

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