Changeset View
Changeset View
Standalone View
Standalone View
src/lib/createaccountjob.cpp
- This file was moved from src/jobs/createaccount.cpp.
Show All 10 Lines | |||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
12 | * GNU General Public License for more details. * | 12 | * GNU General Public License for more details. * | ||
13 | * * | 13 | * * | ||
14 | * You should have received a copy of the GNU General Public License * | 14 | * You should have received a copy of the GNU General Public License * | ||
15 | * along with this program; if not, write to the Free Software * | 15 | * along with this program; if not, write to the Free Software * | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * | ||
17 | *************************************************************************************/ | 17 | *************************************************************************************/ | ||
18 | 18 | | |||
19 | #include "createaccount.h" | 19 | #include "createaccountjob.h" | ||
20 | #include "lib/kaccountsuiplugin.h" | 20 | #include "kaccountsuiplugin.h" | ||
21 | #include "lib/core.h" | 21 | #include "core.h" | ||
22 | #include "uipluginsmanager.h" | 22 | #include "uipluginsmanager.h" | ||
23 | 23 | | |||
24 | #include <QDebug> | 24 | #include <QDebug> | ||
25 | #include <QPluginLoader> | 25 | #include <QPluginLoader> | ||
26 | #include <QDir> | 26 | #include <QDir> | ||
27 | 27 | | |||
28 | #include <Accounts/Manager> | 28 | #include <Accounts/Manager> | ||
29 | #include <Accounts/AccountService> | 29 | #include <Accounts/AccountService> | ||
30 | 30 | | |||
31 | #include <SignOn/Identity> | 31 | #include <SignOn/Identity> | ||
32 | #include <SignOn/AuthSession> | 32 | #include <SignOn/AuthSession> | ||
33 | 33 | | |||
34 | #include <KLocalizedString> | 34 | #include <KLocalizedString> | ||
35 | 35 | | |||
36 | CreateAccount::CreateAccount(QObject* parent) | 36 | CreateAccountJob::CreateAccountJob(QObject* parent) | ||
37 | : CreateAccount(QString(), parent) | 37 | : CreateAccountJob(QString(), parent) | ||
38 | { | 38 | { | ||
39 | 39 | | |||
40 | } | 40 | } | ||
41 | 41 | | |||
42 | CreateAccount::CreateAccount(const QString &providerName, QObject* parent) | 42 | CreateAccountJob::CreateAccountJob(const QString &providerName, QObject* parent) | ||
43 | : KJob(parent) | 43 | : KJob(parent) | ||
44 | , m_providerName(providerName) | 44 | , m_providerName(providerName) | ||
45 | , m_manager(new Accounts::Manager(this)) | 45 | , m_manager(new Accounts::Manager(this)) | ||
46 | , m_account(nullptr) | 46 | , m_account(nullptr) | ||
47 | , m_accInfo(nullptr) | 47 | , m_accInfo(nullptr) | ||
48 | , m_identity(nullptr) | 48 | , m_identity(nullptr) | ||
49 | , m_done(false) | 49 | , m_done(false) | ||
50 | { | 50 | { | ||
51 | 51 | | |||
52 | } | 52 | } | ||
53 | 53 | | |||
54 | void CreateAccount::start() | 54 | void CreateAccountJob::start() | ||
55 | { | 55 | { | ||
56 | qDebug() << m_providerName; | 56 | qDebug() << m_providerName; | ||
57 | QMetaObject::invokeMethod(this, "processSession"); | 57 | QMetaObject::invokeMethod(this, "processSession"); | ||
58 | } | 58 | } | ||
59 | 59 | | |||
60 | void CreateAccount::processSession() | 60 | void CreateAccountJob::processSession() | ||
61 | { | 61 | { | ||
62 | m_account = m_manager->createAccount(m_providerName); | 62 | m_account = m_manager->createAccount(m_providerName); | ||
63 | Accounts::Service service; | 63 | Accounts::Service service; | ||
64 | if (m_account->services().size() == 1) { | 64 | if (m_account->services().size() == 1) { | ||
65 | service = m_account->services().at(0); | 65 | service = m_account->services().at(0); | ||
66 | } | 66 | } | ||
67 | m_accInfo = new Accounts::AccountService(m_account, service, this); | 67 | m_accInfo = new Accounts::AccountService(m_account, service, this); | ||
68 | 68 | | |||
69 | const QString pluginName = m_account->provider().pluginName(); | 69 | const QString pluginName = m_account->provider().pluginName(); | ||
70 | qDebug() << "Looking for plugin" << pluginName; | 70 | qDebug() << "Looking for plugin" << pluginName; | ||
71 | if (!pluginName.isEmpty()) { | 71 | if (!pluginName.isEmpty()) { | ||
72 | loadPluginAndShowDialog(pluginName); | 72 | loadPluginAndShowDialog(pluginName); | ||
73 | } else { | 73 | } else { | ||
74 | SignOn::IdentityInfo info; | 74 | SignOn::IdentityInfo info; | ||
75 | info.setCaption(m_providerName); | 75 | info.setCaption(m_providerName); | ||
76 | info.setAccessControlList(QStringList("*")); | 76 | info.setAccessControlList(QStringList("*")); | ||
77 | info.setType(SignOn::IdentityInfo::Application); | 77 | info.setType(SignOn::IdentityInfo::Application); | ||
78 | info.setStoreSecret(true); | 78 | info.setStoreSecret(true); | ||
79 | 79 | | |||
80 | m_identity = SignOn::Identity::newIdentity(info, this); | 80 | m_identity = SignOn::Identity::newIdentity(info, this); | ||
81 | m_identity->storeCredentials(); | 81 | m_identity->storeCredentials(); | ||
82 | 82 | | |||
83 | connect(m_identity, &SignOn::Identity::info, this, &CreateAccount::info); | 83 | connect(m_identity, &SignOn::Identity::info, this, &CreateAccountJob::info); | ||
84 | connect(m_identity, &SignOn::Identity::error, [=](const SignOn::Error &err) { | 84 | connect(m_identity, &SignOn::Identity::error, [=](const SignOn::Error &err) { | ||
85 | qDebug() << "Error storing identity:" << err.message(); | 85 | qDebug() << "Error storing identity:" << err.message(); | ||
86 | }); | 86 | }); | ||
87 | 87 | | |||
88 | QVariantMap data = m_accInfo->authData().parameters(); | 88 | QVariantMap data = m_accInfo->authData().parameters(); | ||
89 | data.insert("Embedded", false); | 89 | data.insert("Embedded", false); | ||
90 | 90 | | |||
91 | SignOn::SessionData sessionData(data); | 91 | SignOn::SessionData sessionData(data); | ||
92 | SignOn::AuthSessionP session = m_identity->createSession(m_accInfo->authData().method()); | 92 | SignOn::AuthSessionP session = m_identity->createSession(m_accInfo->authData().method()); | ||
93 | qDebug() << "Starting auth session with" << m_accInfo->authData().method(); | 93 | qDebug() << "Starting auth session with" << m_accInfo->authData().method(); | ||
94 | connect(session, &SignOn::AuthSession::error, this, &CreateAccount::sessionError); | 94 | connect(session, &SignOn::AuthSession::error, this, &CreateAccountJob::sessionError); | ||
95 | connect(session, &SignOn::AuthSession::response, this, &CreateAccount::sessionResponse); | 95 | connect(session, &SignOn::AuthSession::response, this, &CreateAccountJob::sessionResponse); | ||
96 | 96 | | |||
97 | session->process(sessionData, m_accInfo->authData().mechanism()); | 97 | session->process(sessionData, m_accInfo->authData().mechanism()); | ||
98 | } | 98 | } | ||
99 | } | 99 | } | ||
100 | 100 | | |||
101 | void CreateAccount::loadPluginAndShowDialog(const QString &pluginName) | 101 | void CreateAccountJob::loadPluginAndShowDialog(const QString &pluginName) | ||
102 | { | 102 | { | ||
103 | KAccountsUiPlugin *ui = KAccounts::UiPluginsManager::pluginForName(pluginName); | 103 | KAccountsUiPlugin *ui = KAccounts::UiPluginsManager::pluginForName(pluginName); | ||
104 | 104 | | |||
105 | if (!ui) { | 105 | if (!ui) { | ||
106 | qDebug() << "Plugin could not be loaded"; | 106 | qDebug() << "Plugin could not be loaded"; | ||
107 | pluginError(i18nc("The %1 is for plugin name, eg. Could not load UI plugin", "Could not load %1 plugin, please check your installation", pluginName)); | 107 | pluginError(i18nc("The %1 is for plugin name, eg. Could not load UI plugin", "Could not load %1 plugin, please check your installation", pluginName)); | ||
108 | return; | 108 | return; | ||
109 | } | 109 | } | ||
110 | 110 | | |||
111 | connect(ui, &KAccountsUiPlugin::success, this, &CreateAccount::pluginFinished, Qt::UniqueConnection); | 111 | connect(ui, &KAccountsUiPlugin::success, this, &CreateAccountJob::pluginFinished, Qt::UniqueConnection); | ||
112 | connect(ui, &KAccountsUiPlugin::error, this, &CreateAccount::pluginError, Qt::UniqueConnection); | 112 | connect(ui, &KAccountsUiPlugin::error, this, &CreateAccountJob::pluginError, Qt::UniqueConnection); | ||
113 | 113 | | |||
114 | ui->setProviderName(m_providerName); | 114 | ui->setProviderName(m_providerName); | ||
115 | ui->init(KAccountsUiPlugin::NewAccountDialog); | 115 | ui->init(KAccountsUiPlugin::NewAccountDialog); | ||
116 | } | 116 | } | ||
117 | 117 | | |||
118 | void CreateAccount::pluginFinished(const QString &screenName, const QString &secret, const QVariantMap &data) | 118 | void CreateAccountJob::pluginFinished(const QString &screenName, const QString &secret, const QVariantMap &data) | ||
119 | { | 119 | { | ||
120 | // Set up the new identity | 120 | // Set up the new identity | ||
121 | SignOn::IdentityInfo info; | 121 | SignOn::IdentityInfo info; | ||
122 | info.setStoreSecret(true); | 122 | info.setStoreSecret(true); | ||
123 | info.setUserName(screenName); | 123 | info.setUserName(screenName); | ||
124 | info.setSecret(secret, true); | 124 | info.setSecret(secret, true); | ||
125 | info.setCaption(m_providerName); | 125 | info.setCaption(m_providerName); | ||
126 | info.setAccessControlList(QStringList(QLatin1String("*"))); | 126 | info.setAccessControlList(QStringList(QLatin1String("*"))); | ||
127 | info.setType(SignOn::IdentityInfo::Application); | 127 | info.setType(SignOn::IdentityInfo::Application); | ||
128 | 128 | | |||
129 | const auto keys = data.keys(); | 129 | const auto keys = data.keys(); | ||
130 | for (const QString &key : keys) { | 130 | for (const QString &key : keys) { | ||
131 | // If a key with __service/ prefix exists and its value is false, | 131 | // If a key with __service/ prefix exists and its value is false, | ||
132 | // add it to m_disabledServices which will later be used for disabling | 132 | // add it to m_disabledServices which will later be used for disabling | ||
133 | // the services contained in that list | 133 | // the services contained in that list | ||
134 | if (key.startsWith(QLatin1String("__service/")) && !data.value(key).toBool()) { | 134 | if (key.startsWith(QLatin1String("__service/")) && !data.value(key).toBool()) { | ||
135 | m_disabledServices << key.mid(10); | 135 | m_disabledServices << key.mid(10); | ||
136 | } | 136 | } | ||
137 | m_account->setValue(key, data.value(key).toString()); | 137 | m_account->setValue(key, data.value(key).toString()); | ||
138 | } | 138 | } | ||
139 | 139 | | |||
140 | m_identity = SignOn::Identity::newIdentity(info, this); | 140 | m_identity = SignOn::Identity::newIdentity(info, this); | ||
141 | connect(m_identity, &SignOn::Identity::info, this, &CreateAccount::info); | 141 | connect(m_identity, &SignOn::Identity::info, this, &CreateAccountJob::info); | ||
142 | 142 | | |||
143 | m_done = true; | 143 | m_done = true; | ||
144 | 144 | | |||
145 | connect(m_identity, &SignOn::Identity::credentialsStored, m_identity, &SignOn::Identity::queryInfo); | 145 | connect(m_identity, &SignOn::Identity::credentialsStored, m_identity, &SignOn::Identity::queryInfo); | ||
146 | m_identity->storeCredentials(); | 146 | m_identity->storeCredentials(); | ||
147 | } | 147 | } | ||
148 | 148 | | |||
149 | void CreateAccount::pluginError(const QString &error) | 149 | void CreateAccountJob::pluginError(const QString &error) | ||
150 | { | 150 | { | ||
151 | if (error.isEmpty()) { | 151 | if (error.isEmpty()) { | ||
152 | setError(-1); | 152 | setError(-1); | ||
153 | } else { | 153 | } else { | ||
154 | setError(KJob::UserDefinedError); | 154 | setError(KJob::UserDefinedError); | ||
155 | } | 155 | } | ||
156 | setErrorText(error); | 156 | setErrorText(error); | ||
157 | // Delete the dialog | 157 | // Delete the dialog | ||
158 | emitResult(); | 158 | emitResult(); | ||
159 | } | 159 | } | ||
160 | 160 | | |||
161 | void CreateAccount::sessionResponse(const SignOn::SessionData &/*data*/) | 161 | void CreateAccountJob::sessionResponse(const SignOn::SessionData &/*data*/) | ||
162 | { | 162 | { | ||
163 | qDebug() << "Received session response"; | 163 | qDebug() << "Received session response"; | ||
164 | 164 | | |||
165 | m_done = true; | 165 | m_done = true; | ||
166 | m_identity->queryInfo(); | 166 | m_identity->queryInfo(); | ||
167 | } | 167 | } | ||
168 | 168 | | |||
169 | void CreateAccount::info(const SignOn::IdentityInfo &info) | 169 | void CreateAccountJob::info(const SignOn::IdentityInfo &info) | ||
170 | { | 170 | { | ||
171 | qDebug() << "Info:"; | 171 | qDebug() << "Info:"; | ||
172 | qDebug() << "\tId:" << info.id(); | 172 | qDebug() << "\tId:" << info.id(); | ||
173 | qDebug() << "\tcaption:" << info.caption(); | 173 | qDebug() << "\tcaption:" << info.caption(); | ||
174 | qDebug() << "\towner:" << info.owner(); | 174 | qDebug() << "\towner:" << info.owner(); | ||
175 | qDebug() << "\tuserName:" << info.userName(); | 175 | qDebug() << "\tuserName:" << info.userName(); | ||
176 | 176 | | |||
177 | if (!m_done) { | 177 | if (!m_done) { | ||
Show All 30 Lines | |||||
208 | for (const Accounts::Service &service : services) { | 208 | for (const Accounts::Service &service : services) { | ||
209 | m_account->selectService(service); | 209 | m_account->selectService(service); | ||
210 | m_account->setEnabled(m_disabledServices.contains(service.name()) ? false : true); | 210 | m_account->setEnabled(m_disabledServices.contains(service.name()) ? false : true); | ||
211 | } | 211 | } | ||
212 | 212 | | |||
213 | m_account->selectService(); | 213 | m_account->selectService(); | ||
214 | m_account->setEnabled(true); | 214 | m_account->setEnabled(true); | ||
215 | m_account->sync(); | 215 | m_account->sync(); | ||
216 | connect(m_account, &Accounts::Account::synced, this, &CreateAccount::emitResult); | 216 | connect(m_account, &Accounts::Account::synced, this, &CreateAccountJob::emitResult); | ||
217 | } | 217 | } | ||
218 | 218 | | |||
219 | void CreateAccount::sessionError(const SignOn::Error &signOnError) | 219 | void CreateAccountJob::sessionError(const SignOn::Error &signOnError) | ||
220 | { | 220 | { | ||
221 | if (error()) { | 221 | if (error()) { | ||
222 | // Guard against SignOn sending two error() signals | 222 | // Guard against SignOn sending two error() signals | ||
223 | return; | 223 | return; | ||
224 | } | 224 | } | ||
225 | qWarning() << "Error:"; | 225 | qWarning() << "Error:"; | ||
226 | qWarning() << "\t" << signOnError.message(); | 226 | qWarning() << "\t" << signOnError.message(); | ||
227 | 227 | | |||
228 | setError(KJob::UserDefinedError); | 228 | setError(KJob::UserDefinedError); | ||
229 | setErrorText(i18n("There was an error while trying to process the request: %1", signOnError.message())); | 229 | setErrorText(i18n("There was an error while trying to process the request: %1", signOnError.message())); | ||
230 | emitResult(); | 230 | emitResult(); | ||
231 | } | 231 | } | ||
232 | 232 | | |||
233 | void CreateAccount::setProviderName(const QString &name) | 233 | void CreateAccountJob::setProviderName(const QString &name) | ||
234 | { | 234 | { | ||
235 | if (m_providerName != name) { | 235 | if (m_providerName != name) { | ||
236 | m_providerName = name; | 236 | m_providerName = name; | ||
237 | Q_EMIT providerNameChanged(); | 237 | Q_EMIT providerNameChanged(); | ||
238 | } | 238 | } | ||
239 | } | 239 | } |