diff --git a/src/jobs/accountservicetogglejob.cpp b/src/jobs/accountservicetogglejob.cpp index 27e85d0..1819a47 100644 --- a/src/jobs/accountservicetogglejob.cpp +++ b/src/jobs/accountservicetogglejob.cpp @@ -1,134 +1,134 @@ /************************************************************************************* * Copyright (C) 2020 by Dan Leinir Turthra Jensen * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ -#include "accountservicetoggle.h" +#include "accountservicetogglejob.h" #include #include "core.h" #include -class AccountServiceToggle::Private { +class AccountServiceToggleJob::Private { public: Private() {} QString accountId; QString serviceId; bool serviceEnabled{false}; }; -AccountServiceToggle::AccountServiceToggle(QObject* parent) +AccountServiceToggleJob::AccountServiceToggleJob(QObject* parent) : KJob(parent) , d(new Private) { } -AccountServiceToggle::~AccountServiceToggle() +AccountServiceToggleJob::~AccountServiceToggleJob() { delete d; } -QString AccountServiceToggle::accountId() const +QString AccountServiceToggleJob::accountId() const { return d->accountId; } -void AccountServiceToggle::setAccountId(const QString& accountId) +void AccountServiceToggleJob::setAccountId(const QString& accountId) { d->accountId = accountId; Q_EMIT accountIdChanged(); } -QString AccountServiceToggle::serviceId() const +QString AccountServiceToggleJob::serviceId() const { return d->serviceId; } -void AccountServiceToggle::setServiceId(const QString& serviceId) +void AccountServiceToggleJob::setServiceId(const QString& serviceId) { d->serviceId = serviceId; Q_EMIT serviceIdChanged(); } -bool AccountServiceToggle::serviceEnabled() const +bool AccountServiceToggleJob::serviceEnabled() const { return d->serviceEnabled; } -void AccountServiceToggle::setServiceEnabled(bool serviceEnabled) +void AccountServiceToggleJob::setServiceEnabled(bool serviceEnabled) { d->serviceEnabled = serviceEnabled; Q_EMIT serviceEnabledChanged(); } -void AccountServiceToggle::start() +void AccountServiceToggleJob::start() { Accounts::Manager* accountsManager = KAccounts::accountsManager(); if (accountsManager) { Accounts::Account *account = accountsManager->account(d->accountId.toInt()); if (account) { Accounts::Service service = accountsManager->service(d->serviceId); if (!service.isValid()) { // qWarning() << "Looks like we might have been given a name instead of an ID for the service, which will be expected when using the Ubuntu AccountServiceModel, which only gives you the name"; for (const Accounts::Service& aService : account->services()) { if (aService.displayName() == d->serviceId) { service = aService; break; } } } if (service.isValid()) { account->selectService(service); account->setEnabled(d->serviceEnabled); if (d->serviceEnabled) { account->selectService(); account->setEnabled(true); } else { bool shouldStayEnabled = false; for (const Accounts::Service &accountService : account->services()) { // Skip the current service, that is not synced to the account yet // so it would return the state before the user clicked the checkbox if (accountService == service) { continue; } account->selectService(accountService); if (account->isEnabled()) { // At least one service is enabled, leave the account enabled shouldStayEnabled = true; break; } } // Make sure we're operating on the global account account->selectService(); account->setEnabled(shouldStayEnabled); } connect(account, &Accounts::Account::synced, this, [this](){ emitResult(); }); account->sync(); } else { qWarning() << "No service found with the ID" << d->serviceId << "on account" << account->displayName(); emitResult(); } } else { qWarning() << "No account found with the ID" << d->accountId; emitResult(); } } else { qWarning() << "No accounts manager, this is not awesome."; emitResult(); } } diff --git a/src/jobs/accountservicetogglejob.h b/src/jobs/accountservicetogglejob.h index 9b91173..8f0a06e 100644 --- a/src/jobs/accountservicetogglejob.h +++ b/src/jobs/accountservicetogglejob.h @@ -1,54 +1,56 @@ /************************************************************************************* * Copyright (C) 2020 by Dan Leinir Turthra Jensen * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ #ifndef ACCOUNTSERVICETOGGLE_H #define ACCOUNTSERVICETOGGLE_H +#include "kaccounts_export.h" + #include #include -class AccountServiceToggle : public KJob +class KACCOUNTS_EXPORT AccountServiceToggleJob : public KJob { Q_OBJECT Q_PROPERTY(QString accountId READ accountId WRITE setAccountId NOTIFY accountIdChanged) Q_PROPERTY(QString serviceId READ serviceId WRITE setServiceId NOTIFY serviceIdChanged) Q_PROPERTY(bool serviceEnabled READ serviceEnabled WRITE setServiceEnabled NOTIFY serviceEnabledChanged) public: - explicit AccountServiceToggle(QObject* parent = nullptr); - virtual ~AccountServiceToggle(); + explicit AccountServiceToggleJob(QObject* parent = nullptr); + virtual ~AccountServiceToggleJob(); void start() override; QString accountId() const; void setAccountId(const QString& accountId); Q_SIGNAL void accountIdChanged(); QString serviceId() const; void setServiceId(const QString& serviceId); Q_SIGNAL void serviceIdChanged(); bool serviceEnabled() const; void setServiceEnabled(bool serviceEnabled); Q_SIGNAL void serviceEnabledChanged(); private: class Private; Private* d; }; #endif//ACCOUNTSERVICETOGGLE_H diff --git a/src/jobs/createaccountjob.cpp b/src/jobs/createaccountjob.cpp index 429bb62..965d8b0 100644 --- a/src/jobs/createaccountjob.cpp +++ b/src/jobs/createaccountjob.cpp @@ -1,239 +1,240 @@ /************************************************************************************* * Copyright (C) 2013 by Alejandro Fiestas Olivares * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ -#include "createaccount.h" -#include "lib/kaccountsuiplugin.h" -#include "lib/core.h" +#include "createaccountjob.h" + +#include "kaccountsuiplugin.h" +#include "core.h" #include "uipluginsmanager.h" #include #include #include #include #include #include #include #include -CreateAccount::CreateAccount(QObject* parent) - : CreateAccount(QString(), parent) +CreateAccountJob::CreateAccountJob(QObject* parent) + : CreateAccountJob(QString(), parent) { } -CreateAccount::CreateAccount(const QString &providerName, QObject* parent) +CreateAccountJob::CreateAccountJob(const QString &providerName, QObject* parent) : KJob(parent) , m_providerName(providerName) , m_manager(new Accounts::Manager(this)) , m_account(nullptr) , m_accInfo(nullptr) , m_identity(nullptr) , m_done(false) { } -void CreateAccount::start() +void CreateAccountJob::start() { qDebug() << m_providerName; QMetaObject::invokeMethod(this, "processSession"); } -void CreateAccount::processSession() +void CreateAccountJob::processSession() { m_account = m_manager->createAccount(m_providerName); Accounts::Service service; if (m_account->services().size() == 1) { service = m_account->services().at(0); } m_accInfo = new Accounts::AccountService(m_account, service, this); const QString pluginName = m_account->provider().pluginName(); qDebug() << "Looking for plugin" << pluginName; if (!pluginName.isEmpty()) { loadPluginAndShowDialog(pluginName); } else { SignOn::IdentityInfo info; info.setCaption(m_providerName); info.setAccessControlList(QStringList("*")); info.setType(SignOn::IdentityInfo::Application); info.setStoreSecret(true); m_identity = SignOn::Identity::newIdentity(info, this); m_identity->storeCredentials(); - connect(m_identity, &SignOn::Identity::info, this, &CreateAccount::info); + connect(m_identity, &SignOn::Identity::info, this, &CreateAccountJob::info); connect(m_identity, &SignOn::Identity::error, [=](const SignOn::Error &err) { qDebug() << "Error storing identity:" << err.message(); }); QVariantMap data = m_accInfo->authData().parameters(); data.insert("Embedded", false); SignOn::SessionData sessionData(data); SignOn::AuthSessionP session = m_identity->createSession(m_accInfo->authData().method()); qDebug() << "Starting auth session with" << m_accInfo->authData().method(); - connect(session, &SignOn::AuthSession::error, this, &CreateAccount::sessionError); - connect(session, &SignOn::AuthSession::response, this, &CreateAccount::sessionResponse); + connect(session, &SignOn::AuthSession::error, this, &CreateAccountJob::sessionError); + connect(session, &SignOn::AuthSession::response, this, &CreateAccountJob::sessionResponse); session->process(sessionData, m_accInfo->authData().mechanism()); } } -void CreateAccount::loadPluginAndShowDialog(const QString &pluginName) +void CreateAccountJob::loadPluginAndShowDialog(const QString &pluginName) { KAccountsUiPlugin *ui = KAccounts::UiPluginsManager::pluginForName(pluginName); if (!ui) { qDebug() << "Plugin could not be loaded"; pluginError(i18nc("The %1 is for plugin name, eg. Could not load UI plugin", "Could not load %1 plugin, please check your installation", pluginName)); return; } - connect(ui, &KAccountsUiPlugin::success, this, &CreateAccount::pluginFinished, Qt::UniqueConnection); - connect(ui, &KAccountsUiPlugin::error, this, &CreateAccount::pluginError, Qt::UniqueConnection); + connect(ui, &KAccountsUiPlugin::success, this, &CreateAccountJob::pluginFinished, Qt::UniqueConnection); + connect(ui, &KAccountsUiPlugin::error, this, &CreateAccountJob::pluginError, Qt::UniqueConnection); ui->setProviderName(m_providerName); ui->init(KAccountsUiPlugin::NewAccountDialog); } -void CreateAccount::pluginFinished(const QString &screenName, const QString &secret, const QVariantMap &data) +void CreateAccountJob::pluginFinished(const QString &screenName, const QString &secret, const QVariantMap &data) { // Set up the new identity SignOn::IdentityInfo info; info.setStoreSecret(true); info.setUserName(screenName); info.setSecret(secret, true); info.setCaption(m_providerName); info.setAccessControlList(QStringList(QLatin1String("*"))); info.setType(SignOn::IdentityInfo::Application); const auto keys = data.keys(); for (const QString &key : keys) { // If a key with __service/ prefix exists and its value is false, // add it to m_disabledServices which will later be used for disabling // the services contained in that list if (key.startsWith(QLatin1String("__service/")) && !data.value(key).toBool()) { m_disabledServices << key.mid(10); } m_account->setValue(key, data.value(key).toString()); } m_identity = SignOn::Identity::newIdentity(info, this); - connect(m_identity, &SignOn::Identity::info, this, &CreateAccount::info); + connect(m_identity, &SignOn::Identity::info, this, &CreateAccountJob::info); m_done = true; connect(m_identity, &SignOn::Identity::credentialsStored, m_identity, &SignOn::Identity::queryInfo); m_identity->storeCredentials(); } -void CreateAccount::pluginError(const QString &error) +void CreateAccountJob::pluginError(const QString &error) { if (error.isEmpty()) { setError(-1); } else { setError(KJob::UserDefinedError); } setErrorText(error); // Delete the dialog emitResult(); } -void CreateAccount::sessionResponse(const SignOn::SessionData &/*data*/) +void CreateAccountJob::sessionResponse(const SignOn::SessionData &/*data*/) { qDebug() << "Received session response"; m_done = true; m_identity->queryInfo(); } -void CreateAccount::info(const SignOn::IdentityInfo &info) +void CreateAccountJob::info(const SignOn::IdentityInfo &info) { qDebug() << "Info:"; qDebug() << "\tId:" << info.id(); qDebug() << "\tcaption:" << info.caption(); qDebug() << "\towner:" << info.owner(); qDebug() << "\tuserName:" << info.userName(); if (!m_done) { return; } m_account->selectService(); if (m_account->displayName().isEmpty()) { m_account->setDisplayName(info.userName()); } m_account->setValue("username", info.userName()); m_account->setCredentialsId(info.id()); Accounts::AuthData authData = m_accInfo->authData(); m_account->setValue("auth/mechanism", authData.mechanism()); m_account->setValue("auth/method", authData.method()); QString base("auth/"); base.append(authData.method()); base.append("/"); base.append(authData.mechanism()); base.append("/"); QVariantMap data = authData.parameters(); QMapIterator i(data); while (i.hasNext()) { i.next(); m_account->setValue(base + i.key(), i.value()); } const Accounts::ServiceList services = m_account->services(); for (const Accounts::Service &service : services) { m_account->selectService(service); m_account->setEnabled(m_disabledServices.contains(service.name()) ? false : true); } m_account->selectService(); m_account->setEnabled(true); m_account->sync(); - connect(m_account, &Accounts::Account::synced, this, &CreateAccount::emitResult); + connect(m_account, &Accounts::Account::synced, this, &CreateAccountJob::emitResult); } -void CreateAccount::sessionError(const SignOn::Error &signOnError) +void CreateAccountJob::sessionError(const SignOn::Error &signOnError) { if (error()) { // Guard against SignOn sending two error() signals return; } qWarning() << "Error:"; qWarning() << "\t" << signOnError.message(); setError(KJob::UserDefinedError); setErrorText(i18n("There was an error while trying to process the request: %1", signOnError.message())); emitResult(); } -void CreateAccount::setProviderName(const QString &name) +void CreateAccountJob::setProviderName(const QString &name) { if (m_providerName != name) { m_providerName = name; Q_EMIT providerNameChanged(); } } diff --git a/src/jobs/createaccountjob.h b/src/jobs/createaccountjob.h index 137a277..a86635b 100644 --- a/src/jobs/createaccountjob.h +++ b/src/jobs/createaccountjob.h @@ -1,74 +1,76 @@ /************************************************************************************* * Copyright (C) 2013 by Alejandro Fiestas Olivares * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ #ifndef CREATE_ACCOUNT_JOB_H #define CREATE_ACCOUNT_JOB_H +#include "kaccounts_export.h" + #include #include namespace Accounts { class Account; class Manager; class AccountService; } namespace SignOn { class Error; class Identity; class SessionData; class IdentityInfo; } -class CreateAccount : public KJob +class KACCOUNTS_EXPORT CreateAccountJob : public KJob { Q_OBJECT Q_PROPERTY(QString providerName READ providerName WRITE setProviderName NOTIFY providerNameChanged) public: - explicit CreateAccount(QObject* parent = nullptr); - explicit CreateAccount(const QString &providerName, QObject* parent = nullptr); + explicit CreateAccountJob(QObject* parent = nullptr); + explicit CreateAccountJob(const QString &providerName, QObject* parent = nullptr); QString providerName() const { return m_providerName; } void setProviderName(const QString &name); void start() override; private Q_SLOTS: void processSession(); void sessionError(const SignOn::Error &signOnError); void sessionResponse(const SignOn::SessionData &data); void info(const SignOn::IdentityInfo &info); void pluginFinished(const QString &screenName, const QString &secret, const QVariantMap &map); void pluginError(const QString &error); Q_SIGNALS: void providerNameChanged(); private: void loadPluginAndShowDialog(const QString &pluginName); QString m_providerName; QStringList m_disabledServices; Accounts::Manager *m_manager; Accounts::Account *m_account; Accounts::AccountService *m_accInfo; SignOn::Identity *m_identity; bool m_done; }; #endif //CREATE_ACCOUNT_JOB_H