Skip to content

Commit 637a565

Browse files
committed
refactor(migration): move logic from Application to related classes.
- Separate logic from widgets. - Functions handling account migration go into AccountManager and functions handling legacy config check go in ConfigFile. Signed-off-by: Camila Ayres <[email protected]>
1 parent b662232 commit 637a565

File tree

7 files changed

+257
-235
lines changed

7 files changed

+257
-235
lines changed

src/gui/accountmanager.cpp

Lines changed: 41 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
#include "accountmanager.h"
88

99
#include "sslerrordialog.h"
10-
#include "proxyauthhandler.h"
1110
#include "creds/credentialsfactory.h"
1211
#include "creds/abstractcredentials.h"
13-
#include "creds/keychainchunk.h"
1412
#include "libsync/clientsideencryption.h"
1513
#include "libsync/configfile.h"
1614
#include "libsync/cookiejar.h"
1715
#include "libsync/theme.h"
1816
#include "libsync/clientproxy.h"
17+
#include "proxyauthhandler.h"
1918

2019
#include <QSettings>
2120
#include <QDir>
@@ -525,9 +524,9 @@ AccountPtr AccountManager::createAccount()
525524
const auto acc = Account::create();
526525
acc->setSslErrorHandler(new SslDialogErrorHandler);
527526
connect(acc.data(), &Account::proxyAuthenticationRequired,
528-
ProxyAuthHandler::instance(), &ProxyAuthHandler::handleProxyAuthenticationRequired);
527+
ProxyAuthHandler::instance(), &ProxyAuthHandler::handleProxyAuthenticationRequired);
529528
connect(acc.data(), &Account::lockFileError,
530-
Systray::instance(), &Systray::showErrorMessageDialog);
529+
Systray::instance(), &Systray::showErrorMessageDialog);
531530

532531
return acc;
533532
}
@@ -604,82 +603,57 @@ void AccountManager::setForceLegacyImport(const bool forceLegacyImport)
604603
Q_EMIT forceLegacyImportChanged();
605604
}
606605

607-
void AccountManager::setupAccountsAndFolders()
608-
{
609-
const auto accountsRestoreResult = restoreExistingAccounts();
610606

611-
const auto foldersListSize = FolderMan::instance()->setupFolders();
607+
int AccountManager::setupAccounts()
608+
{
609+
if (!confirmRestoreExistingAccounts()) {
610+
return 0;
611+
}
612612

613-
const auto prettyNamesList = [](const QList<AccountStatePtr> &accounts) {
614-
QStringList list;
615-
for (const auto &account : accounts) {
616-
list << account->account()->prettyName().prepend("- ");
617-
}
618-
return list.join("\n");
619-
};
620-
621-
if (const auto accounts = AccountManager::instance()->accounts();
622-
accountsRestoreResult == AccountManager::AccountsRestoreSuccessFromLegacyVersion
623-
&& !accounts.isEmpty()) {
624-
625-
const auto accountsListSize = accounts.size();
626-
if (Theme::instance()->displayLegacyImportDialog()) {
627-
const auto accountsRestoreMessage = accountsListSize > 1
628-
? tr("%1 accounts", "number of accounts imported").arg(QString::number(accountsListSize))
629-
: tr("1 account");
630-
const auto foldersRestoreMessage = foldersListSize > 1
631-
? tr("%1 folders", "number of folders imported").arg(QString::number(foldersListSize))
632-
: tr("1 folder");
633-
const auto messageBox = new QMessageBox(QMessageBox::Information,
634-
tr("Legacy import"),
635-
tr("Imported %1 and %2 from a legacy desktop client.\n%3",
636-
"number of accounts and folders imported. list of users.")
637-
.arg(accountsRestoreMessage,
638-
foldersRestoreMessage,
639-
prettyNamesList(accounts))
640-
);
641-
messageBox->setWindowModality(Qt::NonModal);
642-
messageBox->open();
613+
ConfigFile configFile;
614+
const auto tryMigrate = configFile.overrideServerUrl().isEmpty();
615+
const auto configFileToRestore = configFile.configFileToRestore();
616+
auto accountsRestoreResult = AccountManager::AccountsRestoreResult::AccountsRestoreSuccess;
617+
if (accountsRestoreResult = restore(configFileToRestore, tryMigrate);
618+
accountsRestoreResult == AccountManager::AccountsRestoreFailure) {
619+
// If there is an error reading the account settings, try again
620+
// after a couple of seconds, if that fails, give up.
621+
// (non-existence is not an error)
622+
Utility::sleep(5);
623+
if (accountsRestoreResult = restore(configFileToRestore, tryMigrate);
624+
accountsRestoreResult == AccountManager::AccountsRestoreFailure) {
625+
qCCritical(lcAccountManager) << "Could not read the account settings, quitting";
626+
QMessageBox::critical(
627+
nullptr,
628+
tr("Error accessing the configuration file"),
629+
tr("There was an error while accessing the configuration "
630+
"file at %1. Please make sure the file can be accessed by your system account.")
631+
.arg(ConfigFile().configFile()),
632+
QMessageBox::Ok
633+
);
634+
QTimer::singleShot(0, qApp, &QCoreApplication::quit);
635+
return 0;
643636
}
637+
}
644638

645-
qCWarning(lcAccountManager) << "Migration result AccountManager::AccountsRestoreResult:" << accountsRestoreResult;
646-
qCWarning(lcAccountManager) << "Folders migrated: " << foldersListSize;
647-
qCWarning(lcAccountManager) << accountsListSize << "account(s) were migrated:" << prettyNamesList(accounts);
639+
const auto accountsMigrated = AccountManager::instance()->accounts().size();
640+
qCWarning(lcAccountManager) << "Migration result AccountManager::AccountsRestoreResult:" << accountsRestoreResult;
641+
qCWarning(lcAccountManager) << "Accounts migrated:" << accountsMigrated;
648642

649-
} else {
650-
qCWarning(lcAccountManager) << "Migration result AccountManager::AccountsRestoreResult: " << accountsRestoreResult;
651-
qCWarning(lcAccountManager) << "Folders migrated: " << foldersListSize;
652-
qCWarning(lcAccountManager) << "No accounts were migrated, prompting user to set up accounts and folders from scratch.";
653-
}
643+
return accountsMigrated;
654644
}
655645

656-
AccountManager::AccountsRestoreResult AccountManager::restoreExistingAccounts()
646+
bool AccountManager::confirmRestoreExistingAccounts() const
657647
{
658648
ConfigFile configFile;
659-
const auto tryMigrate = configFile.overrideServerUrl().isEmpty();
660-
661-
auto isALegacyClientRestore = false;
662-
const auto whichConfigFileToRestore = [&](){
663-
const auto legacyConfigFile = configFile.discoveredLegacyConfigFile();
664-
if (legacyConfigFile.isEmpty()) {
665-
return configFile.configFile();
666-
}
667-
668-
isALegacyClientRestore = true;
669-
return legacyConfigFile;
670-
};
671-
672-
auto accountsRestoreResult = AccountManager::AccountsRestoreFailure;
673-
const auto configFileToRestore = whichConfigFileToRestore();
649+
const auto configFileToRestore = configFile.configFileToRestore();
674650
auto oCSettings = std::make_unique<QSettings>(configFileToRestore, QSettings::IniFormat);
675651
oCSettings->beginGroup(QLatin1String(accountsC));
676652
const auto accountsListSize = oCSettings->childGroups().size();
677653
oCSettings->endGroup();
678-
auto showDialogs = false;
679654
if (!forceLegacyImport() && accountsListSize > 0
680655
&& Theme::instance()->displayLegacyImportDialog()
681-
&& isALegacyClientRestore) {
682-
showDialogs = true;
656+
&& !configFile.discoveredLegacyConfigFile().isEmpty()) {
683657
const auto importQuestion = accountsListSize > 1
684658
? tr("%1 accounts were detected from a legacy desktop client.\n"
685659
"Should the accounts be imported?").arg(QString::number(accountsListSize))
@@ -693,33 +667,11 @@ AccountManager::AccountsRestoreResult AccountManager::restoreExistingAccounts()
693667
QMessageBox::information(nullptr,
694668
tr("Legacy import"),
695669
tr("Could not import accounts from legacy client configuration."));
696-
return accountsRestoreResult;
697-
}
698-
}
699-
700-
if (accountsRestoreResult = restore(configFileToRestore, tryMigrate);
701-
accountsRestoreResult == AccountManager::AccountsRestoreFailure) {
702-
// If there is an error reading the account settings, try again
703-
// after a couple of seconds, if that fails, give up.
704-
// (non-existence is not an error)
705-
Utility::sleep(5);
706-
if (accountsRestoreResult = AccountManager::instance()->restore(configFileToRestore, tryMigrate);
707-
accountsRestoreResult == AccountManager::AccountsRestoreFailure
708-
&& showDialogs) {
709-
qCCritical(lcAccountManager) << "Could not read the account settings, quitting";
710-
QMessageBox::critical(
711-
nullptr,
712-
tr("Error accessing the configuration file"),
713-
tr("There was an error while accessing the configuration "
714-
"file at %1. Please make sure the file can be accessed by your system account.")
715-
.arg(ConfigFile().configFile()),
716-
QMessageBox::Ok
717-
);
718-
QTimer::singleShot(0, qApp, &QCoreApplication::quit);
670+
return false;
719671
}
720672
}
721673

722-
return accountsRestoreResult;
674+
return true;
723675
}
724676

725677
}

src/gui/accountmanager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ class AccountManager : public QObject
8484
*/
8585
static void backwardMigrationSettingsKeys(QStringList *deleteKeys, QStringList *ignoreKeys);
8686

87-
AccountManager::AccountsRestoreResult restoreExistingAccounts();
88-
void setupAccountsAndFolders();
87+
bool confirmRestoreExistingAccounts() const;
88+
int setupAccounts();
89+
8990
public slots:
9091
/// Saves account data when adding user, when updating e.g. dav user, not including the credentials
9192
void saveAccount(const OCC::AccountPtr &newAccountData);

0 commit comments

Comments
 (0)