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