Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // create-online-sync-account.pro
- TEMPLATE = app
- TARGET = create-online-sync-account
- CONFIG += link_pkgconfig
- PKGCONFIG += accounts-qt5 libsignon-qt5
- QT += network
- SOURCES = main.cpp CreateOnlineSyncAccount.cpp
- HEADERS = CreateOnlineSyncAccount.h
- // main.cpp
- #include <QCoreApplication>
- #include <QObject>
- #include <QtDebug>
- #include "CreateOnlineSyncAccount.h"
- int main(int argc, char *argv[])
- {
- QCoreApplication app(argc, argv);
- CreateOnlineSyncAccount c;
- QString remotePath, mode, username, password;
- if (argc != 3 && argc != 5) {
- qWarning() << "usage: create-online-sync-account <remotePath> <legacy> <username> <password>";
- return 1;
- }
- remotePath = QString::fromLatin1(argv[1]);
- mode = QString::fromLatin1(argv[2]);
- if (argc == 5) username = QString::fromLatin1(argv[3]);
- if (argc == 5) password = QString::fromLatin1(argv[4]);
- if (mode != QStringLiteral("legacy")) {
- // actually we don't support oauth2 yet
- // to support that, we would also need as parameters:
- // auth path / token path / redirect uri / scopes etc.
- qWarning() << "oauth2 not currently supported";
- return 1;
- }
- QObject::connect(&c, SIGNAL(done()), &app, SLOT(quit()));
- c.createOnlineSyncAccount(remotePath, mode, username, password);
- return app.exec();
- }
- // createonlinesyncaccount.h
- #ifndef CREATEONLINESYNCACCOUNT_H
- #define CREATEONLINESYNCACCOUNT_H
- #include <QObject>
- #include <Accounts/Account>
- #include <Accounts/Manager>
- #include <Accounts/AccountService>
- #include <SignOn/Error>
- #include <SignOn/Identity>
- #include <SignOn/AuthSession>
- #include <SignOn/SessionData>
- class CreateOnlineSyncAccount : public QObject
- {
- Q_OBJECT
- public:
- CreateOnlineSyncAccount(QObject *parent = 0);
- ~CreateOnlineSyncAccount();
- void createOnlineSyncAccount(const QString &remotePath,
- const QString &mode,
- const QString &username,
- const QString &password);
- public Q_SLOTS:
- void credentialsStored(quint32 credentialsId);
- void credentialsFailed();
- Q_SIGNALS:
- void done();
- private:
- Accounts::Manager *m_accountManager;
- Accounts::Account *m_account;
- SignOn::Identity *m_identity;
- };
- #endif
- // createonlinesyncaccount.cpp
- #include "CreateOnlineSyncAccount.h"
- #include <QtDebug>
- #include <QUrl>
- #include <Accounts/Service>
- #include <SignOn/Identity>
- #include <SignOn/IdentityInfo>
- CreateOnlineSyncAccount::CreateOnlineSyncAccount(QObject *parent)
- : QObject(parent)
- , m_accountManager(new Accounts::Manager(this))
- , m_account(0)
- , m_identity(0)
- {
- }
- CreateOnlineSyncAccount::~CreateOnlineSyncAccount()
- {
- delete m_account;
- delete m_identity;
- }
- void CreateOnlineSyncAccount::createOnlineSyncAccount(const QString &remotePath,
- const QString &mode,
- const QString &username,
- const QString &password)
- {
- Q_UNUSED(mode) // until we take tokenpaths/redirecturis etc as parameters also...
- m_account = m_accountManager->createAccount("caldav"); // change this as required!
- if (!m_account) {
- qWarning() << "error: couldn't create onlinesync account";
- emit done();
- }
- Accounts::Service srv = m_accountManager->service("caldav-yahoo"); // change this as required!
- if (!srv.isValid()) {
- qWarning() << "error: couldn't retrieve caldav service";
- emit done();
- }
- m_account->setDisplayName("caldav account");
- m_account->selectService(srv);
- m_account->setValue("Remote database", remotePath);
- m_account->setValue("auth/method", "password");
- m_account->setValue("auth/mechanism", "password");
- m_account->setEnabled(true);
- m_account->selectService(Accounts::Service());
- m_account->setEnabled(false); // disable globally until credentials are set.
- m_account->syncAndBlock();
- QMap<QString, QStringList> methodMechs;
- methodMechs.insert("password", QStringList() << "password");
- SignOn::IdentityInfo *info = new SignOn::IdentityInfo(m_account->displayName(), username, methodMechs);
- info->setSecret(password, true);
- QUrl url(remotePath);
- info->setRealms(QStringList() << url.host());
- info->setType(SignOn::IdentityInfo::Application);
- m_identity = SignOn::Identity::newIdentity(*info);
- connect(m_identity, SIGNAL(error(const SignOn::Error &)),
- this, SLOT(credentialsFailed()));
- connect(m_identity, SIGNAL(credentialsStored(const quint32)),
- this, SLOT(credentialsStored(quint32)));
- m_identity->storeCredentials();
- }
- void CreateOnlineSyncAccount::credentialsFailed()
- {
- qWarning() << "error: failed to store credentials";
- emit done();
- }
- void CreateOnlineSyncAccount::credentialsStored(quint32 credentialsId)
- {
- Accounts::Service srv = m_accountManager->service("caldav-yahoo"); // change this as required!
- m_account->selectService(srv);
- m_account->setCredentialsId(credentialsId);
- m_account->selectService(Accounts::Service());
- m_account->setCredentialsId(credentialsId); // global service.
- m_account->setEnabled(true);
- m_account->syncAndBlock();
- qWarning() << "success!";
- emit done();
- }
Advertisement
Add Comment
Please, Sign In to add comment