2015-04-09 17:19:17 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Olivier Goffart <ogoffart@woboq.com>
|
|
|
|
*
|
|
|
|
* 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; version 2 of the License.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "accountmanager.h"
|
2015-04-17 18:56:17 +03:00
|
|
|
#include "sslerrordialog.h"
|
2015-07-16 15:21:51 +03:00
|
|
|
#include "proxyauthhandler.h"
|
2015-04-09 17:19:17 +03:00
|
|
|
#include <theme.h>
|
|
|
|
#include <creds/credentialsfactory.h>
|
|
|
|
#include <creds/abstractcredentials.h>
|
|
|
|
#include <cookiejar.h>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
static const char urlC[] = "url";
|
|
|
|
static const char authTypeC[] = "authType";
|
|
|
|
static const char userC[] = "user";
|
|
|
|
static const char httpUserC[] = "http_user";
|
|
|
|
static const char caCertsKeyC[] = "CaCertificates";
|
2015-04-24 11:18:33 +03:00
|
|
|
static const char accountsC[] = "Accounts";
|
2015-09-03 07:23:19 +03:00
|
|
|
static const char versionC[] = "version";
|
2015-04-09 17:19:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace OCC {
|
|
|
|
|
|
|
|
AccountManager *AccountManager::instance()
|
|
|
|
{
|
|
|
|
static AccountManager instance;
|
|
|
|
return &instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AccountManager::restore()
|
2015-04-23 16:42:18 +03:00
|
|
|
{
|
2015-06-15 16:04:39 +03:00
|
|
|
auto settings = Account::settingsWithGroup(QLatin1String(accountsC));
|
2015-04-23 16:42:18 +03:00
|
|
|
|
|
|
|
// If there are no accounts, check the old format.
|
2015-09-03 07:23:19 +03:00
|
|
|
if (settings->childGroups().isEmpty()
|
|
|
|
&& !settings->contains(QLatin1String(versionC))) {
|
2015-04-23 16:42:18 +03:00
|
|
|
return restoreFromLegacySettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const auto& accountId, settings->childGroups()) {
|
|
|
|
settings->beginGroup(accountId);
|
2015-04-24 11:18:33 +03:00
|
|
|
if (auto acc = load(*settings)) {
|
2015-04-23 16:42:18 +03:00
|
|
|
acc->_id = accountId;
|
|
|
|
addAccount(acc);
|
|
|
|
}
|
|
|
|
settings->endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AccountManager::restoreFromLegacySettings()
|
2015-04-09 17:19:17 +03:00
|
|
|
{
|
|
|
|
// try to open the correctly themed settings
|
2015-06-15 16:04:39 +03:00
|
|
|
auto settings = Account::settingsWithGroup(Theme::instance()->appName());
|
2015-04-09 17:19:17 +03:00
|
|
|
|
|
|
|
bool migratedCreds = false;
|
|
|
|
|
|
|
|
// if the settings file could not be opened, the childKeys list is empty
|
2015-04-23 16:42:18 +03:00
|
|
|
// then try to load settings from a very old place
|
2015-04-09 17:19:17 +03:00
|
|
|
if( settings->childKeys().isEmpty() ) {
|
|
|
|
// Now try to open the original ownCloud settings to see if they exist.
|
|
|
|
QString oCCfgFile = QDir::fromNativeSeparators( settings->fileName() );
|
|
|
|
// replace the last two segments with ownCloud/owncloud.cfg
|
|
|
|
oCCfgFile = oCCfgFile.left( oCCfgFile.lastIndexOf('/'));
|
|
|
|
oCCfgFile = oCCfgFile.left( oCCfgFile.lastIndexOf('/'));
|
|
|
|
oCCfgFile += QLatin1String("/ownCloud/owncloud.cfg");
|
|
|
|
|
|
|
|
qDebug() << "Migrate: checking old config " << oCCfgFile;
|
|
|
|
|
|
|
|
QFileInfo fi( oCCfgFile );
|
|
|
|
if( fi.isReadable() ) {
|
|
|
|
QSettings *oCSettings = new QSettings(oCCfgFile, QSettings::IniFormat);
|
|
|
|
oCSettings->beginGroup(QLatin1String("ownCloud"));
|
|
|
|
|
|
|
|
// Check the theme url to see if it is the same url that the oC config was for
|
|
|
|
QString overrideUrl = Theme::instance()->overrideServerUrl();
|
|
|
|
if( !overrideUrl.isEmpty() ) {
|
|
|
|
if (overrideUrl.endsWith('/')) { overrideUrl.chop(1); }
|
|
|
|
QString oCUrl = oCSettings->value(QLatin1String(urlC)).toString();
|
|
|
|
if (oCUrl.endsWith('/')) { oCUrl.chop(1); }
|
|
|
|
|
|
|
|
// in case the urls are equal reset the settings object to read from
|
|
|
|
// the ownCloud settings object
|
|
|
|
qDebug() << "Migrate oC config if " << oCUrl << " == " << overrideUrl << ":"
|
2015-04-23 16:42:18 +03:00
|
|
|
<< (oCUrl == overrideUrl ? "Yes" : "No");
|
2015-04-09 17:19:17 +03:00
|
|
|
if( oCUrl == overrideUrl ) {
|
|
|
|
migratedCreds = true;
|
|
|
|
settings.reset( oCSettings );
|
|
|
|
} else {
|
|
|
|
delete oCSettings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 16:42:18 +03:00
|
|
|
// Try to load the single account.
|
2015-04-09 17:19:17 +03:00
|
|
|
if (!settings->childKeys().isEmpty()) {
|
2015-04-24 11:18:33 +03:00
|
|
|
if (auto acc = load(*settings)) {
|
2015-04-23 16:42:18 +03:00
|
|
|
if (migratedCreds) {
|
|
|
|
acc->setMigrated(true);
|
|
|
|
}
|
|
|
|
addAccount(acc);
|
|
|
|
return true;
|
2015-04-09 17:19:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-06 13:49:18 +03:00
|
|
|
void AccountManager::save(bool saveCredentials)
|
2015-04-09 17:19:17 +03:00
|
|
|
{
|
2015-06-15 16:04:39 +03:00
|
|
|
auto settings = Account::settingsWithGroup(QLatin1String(accountsC));
|
2015-09-03 07:23:19 +03:00
|
|
|
settings->setValue(QLatin1String(versionC), 2);
|
2015-04-23 16:42:18 +03:00
|
|
|
foreach (const auto &acc, _accounts) {
|
|
|
|
settings->beginGroup(acc->account()->id());
|
2015-08-06 13:49:18 +03:00
|
|
|
save(acc->account(), *settings, saveCredentials);
|
2015-04-23 16:42:18 +03:00
|
|
|
settings->endGroup();
|
2015-04-17 18:56:17 +03:00
|
|
|
}
|
2015-09-17 14:48:05 +03:00
|
|
|
|
|
|
|
settings->sync();
|
|
|
|
qDebug() << "Saved all account settings, status:" << settings->status();
|
2015-04-17 18:56:17 +03:00
|
|
|
}
|
|
|
|
|
2015-08-14 12:31:01 +03:00
|
|
|
void AccountManager::wantsAccountSavedSlot(AccountPtr a)
|
|
|
|
{
|
2015-08-14 13:37:19 +03:00
|
|
|
qDebug() << "Saving account" << a->url().toString();
|
2015-08-14 12:31:01 +03:00
|
|
|
auto settings = Account::settingsWithGroup(QLatin1String(accountsC));
|
|
|
|
settings->beginGroup(a->id());
|
2015-08-14 13:37:19 +03:00
|
|
|
save(a, *settings, false); // don't save credentials they might not have been loaded yet
|
2015-08-14 12:31:01 +03:00
|
|
|
settings->endGroup();
|
2015-09-17 14:48:05 +03:00
|
|
|
|
|
|
|
settings->sync();
|
|
|
|
qDebug() << "Saved account settings, status:" << settings->status();
|
2015-08-14 12:31:01 +03:00
|
|
|
}
|
|
|
|
|
2015-08-06 13:49:18 +03:00
|
|
|
void AccountManager::save(const AccountPtr& acc, QSettings& settings, bool saveCredentials)
|
2015-04-17 18:56:17 +03:00
|
|
|
{
|
2015-04-24 11:18:33 +03:00
|
|
|
settings.setValue(QLatin1String(urlC), acc->_url.toString());
|
2015-04-09 17:19:17 +03:00
|
|
|
if (acc->_credentials) {
|
2015-08-06 13:49:18 +03:00
|
|
|
if (saveCredentials) {
|
|
|
|
// Only persist the credentials if the parameter is set, on migration from 1.8.x
|
|
|
|
// we want to save the accounts but not overwrite the credentials
|
|
|
|
// (This is easier than asynchronously fetching the credentials from keychain and then
|
|
|
|
// re-persisting them)
|
|
|
|
acc->_credentials->persist();
|
|
|
|
}
|
2015-04-09 17:19:17 +03:00
|
|
|
Q_FOREACH(QString key, acc->_settingsMap.keys()) {
|
2015-04-24 11:18:33 +03:00
|
|
|
settings.setValue(key, acc->_settingsMap.value(key));
|
2015-04-09 17:19:17 +03:00
|
|
|
}
|
2015-04-24 11:18:33 +03:00
|
|
|
settings.setValue(QLatin1String(authTypeC), acc->_credentials->authType());
|
2015-04-09 17:19:17 +03:00
|
|
|
|
|
|
|
// HACK: Save http_user also as user
|
|
|
|
if (acc->_settingsMap.contains(httpUserC))
|
2015-04-24 11:18:33 +03:00
|
|
|
settings.setValue(userC, acc->_settingsMap.value(httpUserC));
|
2015-04-09 17:19:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save accepted certificates.
|
2015-04-24 11:18:33 +03:00
|
|
|
settings.beginGroup(QLatin1String("General"));
|
2015-04-09 17:19:17 +03:00
|
|
|
qDebug() << "Saving " << acc->approvedCerts().count() << " unknown certs.";
|
|
|
|
QByteArray certs;
|
|
|
|
Q_FOREACH( const QSslCertificate& cert, acc->approvedCerts() ) {
|
|
|
|
certs += cert.toPem() + '\n';
|
|
|
|
}
|
|
|
|
if (!certs.isEmpty()) {
|
2015-04-24 11:18:33 +03:00
|
|
|
settings.setValue( QLatin1String(caCertsKeyC), certs );
|
2015-04-09 17:19:17 +03:00
|
|
|
}
|
2015-04-24 11:18:33 +03:00
|
|
|
settings.endGroup();
|
2015-04-09 17:19:17 +03:00
|
|
|
|
|
|
|
// Save cookies.
|
|
|
|
if (acc->_am) {
|
|
|
|
CookieJar* jar = qobject_cast<CookieJar*>(acc->_am->cookieJar());
|
|
|
|
if (jar) {
|
|
|
|
qDebug() << "Saving cookies.";
|
|
|
|
jar->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 11:18:33 +03:00
|
|
|
AccountPtr AccountManager::load(QSettings& settings)
|
2015-04-23 16:42:18 +03:00
|
|
|
{
|
2015-07-16 15:21:51 +03:00
|
|
|
auto acc = createAccount();
|
2015-04-23 16:42:18 +03:00
|
|
|
|
2015-04-24 11:18:33 +03:00
|
|
|
acc->setUrl(settings.value(QLatin1String(urlC)).toUrl());
|
2015-04-23 16:42:18 +03:00
|
|
|
|
|
|
|
// We want to only restore settings for that auth type and the user value
|
2015-04-24 11:18:33 +03:00
|
|
|
acc->_settingsMap.insert(QLatin1String(userC), settings.value(userC));
|
|
|
|
QString authTypePrefix = settings.value(authTypeC).toString() + "_";
|
|
|
|
Q_FOREACH(QString key, settings.childKeys()) {
|
2015-04-23 16:42:18 +03:00
|
|
|
if (!key.startsWith(authTypePrefix))
|
|
|
|
continue;
|
2015-04-24 11:18:33 +03:00
|
|
|
acc->_settingsMap.insert(key, settings.value(key));
|
2015-04-23 16:42:18 +03:00
|
|
|
}
|
|
|
|
|
2015-04-24 11:18:33 +03:00
|
|
|
acc->setCredentials(CredentialsFactory::create(settings.value(QLatin1String(authTypeC)).toString()));
|
2015-04-23 16:42:18 +03:00
|
|
|
|
|
|
|
// now the cert, it is in the general group
|
2015-04-24 11:18:33 +03:00
|
|
|
settings.beginGroup(QLatin1String("General"));
|
|
|
|
acc->setApprovedCerts(QSslCertificate::fromData(settings.value(caCertsKeyC).toByteArray()));
|
|
|
|
settings.endGroup();
|
|
|
|
|
2015-04-23 16:42:18 +03:00
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
2015-11-12 19:50:00 +03:00
|
|
|
AccountStatePtr AccountManager::account(const QString& name)
|
|
|
|
{
|
|
|
|
foreach (const auto& acc, _accounts) {
|
|
|
|
if (acc->account()->displayName() == name) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AccountStatePtr();
|
|
|
|
}
|
|
|
|
|
2015-04-27 18:43:07 +03:00
|
|
|
AccountState *AccountManager::addAccount(const AccountPtr& newAccount)
|
2015-04-17 18:56:17 +03:00
|
|
|
{
|
2015-04-24 08:02:51 +03:00
|
|
|
auto id = newAccount->id();
|
|
|
|
if (id.isEmpty() || !isAccountIdAvailable(id)) {
|
|
|
|
id = generateFreeAccountId();
|
|
|
|
}
|
|
|
|
newAccount->_id = id;
|
|
|
|
|
2015-08-14 12:31:01 +03:00
|
|
|
QObject::connect(newAccount.data(), SIGNAL(wantsAccountSaved(AccountPtr)),
|
|
|
|
this, SLOT(wantsAccountSavedSlot(AccountPtr)));
|
|
|
|
|
2015-04-17 18:56:17 +03:00
|
|
|
AccountStatePtr newAccountState(new AccountState(newAccount));
|
|
|
|
_accounts << newAccountState;
|
|
|
|
emit accountAdded(newAccountState.data());
|
2015-04-27 18:43:07 +03:00
|
|
|
return newAccountState.data();
|
2015-04-17 18:56:17 +03:00
|
|
|
}
|
|
|
|
|
2015-05-12 16:16:32 +03:00
|
|
|
void AccountManager::deleteAccount(AccountState* account)
|
|
|
|
{
|
|
|
|
auto it = std::find(_accounts.begin(), _accounts.end(), account);
|
|
|
|
if (it == _accounts.end()) { return; }
|
|
|
|
auto copy = *it; // keep a reference to the shared pointer so it does not delete it just yet
|
|
|
|
_accounts.erase(it);
|
2015-05-12 16:37:16 +03:00
|
|
|
|
2015-06-15 16:04:39 +03:00
|
|
|
auto settings = Account::settingsWithGroup(QLatin1String(accountsC));
|
2015-05-12 16:37:16 +03:00
|
|
|
settings->remove(account->account()->id());
|
|
|
|
|
2015-07-03 12:13:19 +03:00
|
|
|
emit accountRemoved(account);
|
2015-05-12 16:16:32 +03:00
|
|
|
}
|
|
|
|
|
2015-07-16 15:21:51 +03:00
|
|
|
AccountPtr AccountManager::createAccount()
|
|
|
|
{
|
|
|
|
AccountPtr acc = Account::create();
|
|
|
|
acc->setSslErrorHandler(new SslDialogErrorHandler);
|
|
|
|
connect(acc.data(), SIGNAL(proxyAuthenticationRequired(QNetworkProxy, QAuthenticator*)),
|
|
|
|
ProxyAuthHandler::instance(), SLOT(handleProxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
2015-05-12 16:16:32 +03:00
|
|
|
|
2015-04-17 18:56:17 +03:00
|
|
|
void AccountManager::shutdown()
|
|
|
|
{
|
|
|
|
auto accountsCopy = _accounts;
|
|
|
|
_accounts.clear();
|
|
|
|
foreach (const auto &acc, accountsCopy) {
|
|
|
|
emit accountRemoved(acc.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 08:02:51 +03:00
|
|
|
bool AccountManager::isAccountIdAvailable(const QString& id) const
|
|
|
|
{
|
|
|
|
foreach (const auto& acc, _accounts) {
|
|
|
|
if (acc->account()->id() == id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString AccountManager::generateFreeAccountId() const
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
forever {
|
|
|
|
QString id = QString::number(i);
|
|
|
|
if (isAccountIdAvailable(id)) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2015-04-09 17:19:17 +03:00
|
|
|
|
2015-04-23 15:47:31 +03:00
|
|
|
}
|