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