diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index a92ee0424f..9c16fa412b 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -153,6 +153,8 @@ set(client_SRCS userinfo.cpp vfsdownloaderrordialog.h vfsdownloaderrordialog.cpp + legacyaccountselectiondialog.h + legacyaccountselectiondialog.cpp accountstate.h accountstate.cpp addcertificatedialog.h diff --git a/src/gui/accountmanager.cpp b/src/gui/accountmanager.cpp index 8be37e45d3..ac1eaa7e68 100644 --- a/src/gui/accountmanager.cpp +++ b/src/gui/accountmanager.cpp @@ -16,6 +16,7 @@ #include "libsync/cookiejar.h" #include "libsync/theme.h" #include "libsync/clientproxy.h" +#include "legacyaccountselectiondialog.h" #include #include @@ -176,6 +177,7 @@ bool AccountManager::restoreFromLegacySettings() auto wasLegacyImportDialogDisplayed = false; const auto displayLegacyImportDialog = Theme::instance()->displayLegacyImportDialog(); + QStringList selectedAccountIds; // if the settings file could not be opened, the childKeys list is empty // then try to load settings from a very old place @@ -215,25 +217,51 @@ bool AccountManager::restoreFromLegacySettings() } oCSettings->beginGroup(QLatin1String(accountsC)); - const auto accountsListSize = oCSettings->childGroups().size(); - oCSettings->endGroup(); + const auto childGroups = oCSettings->childGroups(); + const auto accountsListSize = childGroups.size(); + oCSettings->endGroup(); //accountsC if (const QFileInfo configFileInfo(configFile); configFileInfo.exists() && configFileInfo.isReadable()) { + qCInfo(lcAccountManager) << "Migrate: checking old config " << configFile; if (!forceLegacyImport() && accountsListSize > 0 && displayLegacyImportDialog) { wasLegacyImportDialogDisplayed = true; - const auto importQuestion = accountsListSize > 1 - ? tr("%1 accounts were detected from a legacy desktop client.\n" - "Should the accounts be imported?").arg(QString::number(accountsListSize)) - : tr("1 account was detected from a legacy desktop client.\n" - "Should the account be imported?"); - const auto importMessageBox = new QMessageBox(QMessageBox::Question, tr("Legacy import"), importQuestion); - importMessageBox->addButton(tr("Import"), QMessageBox::AcceptRole); - const auto skipButton = importMessageBox->addButton(tr("Skip"), QMessageBox::DestructiveRole); - importMessageBox->exec(); - if (importMessageBox->clickedButton() == skipButton) { - return false; + if (accountsListSize == 1) { + const auto importQuestion = + tr("An account was detected from a legacy desktop client.\n" + "Should the account be imported?"); + QMessageBox importMessageBox(QMessageBox::Question, tr("Legacy import"), importQuestion); + importMessageBox.addButton(tr("Import"), QMessageBox::AcceptRole); + const auto skipButton = importMessageBox.addButton(tr("Skip"), QMessageBox::DestructiveRole); + importMessageBox.exec(); + if (importMessageBox.clickedButton() == skipButton) { + return false; + } + selectedAccountIds = childGroups; + } else { + QVector accountsToDisplay; + oCSettings->beginGroup(QLatin1String(accountsC)); + for (const auto &accId : childGroups) { + oCSettings->beginGroup(accId); + const auto displayName = oCSettings->value(QLatin1String(displayNameC)).toString(); + const auto urlStr = oCSettings->value(QLatin1String(urlC)).toString(); + oCSettings->endGroup(); //accId + const auto label = QString("%1 - %2").arg(displayName, urlStr); + accountsToDisplay.push_back({accId, label}); + } + oCSettings->endGroup(); //accountsC + + LegacyAccountSelectionDialog accountSelectionDialog(accountsToDisplay); + if (accountSelectionDialog.exec() != QDialog::Accepted) { + return false; + } + selectedAccountIds = accountSelectionDialog.selectedAccountIds(); + if (selectedAccountIds.isEmpty()) { + return false; + } } + } else { + selectedAccountIds = childGroups; } qCInfo(lcAccountManager) << "Copy settings" << oCSettings->allKeys().join(", "); @@ -264,7 +292,7 @@ bool AccountManager::restoreFromLegacySettings() // Try to load the single account. if (!settings->childKeys().isEmpty()) { settings->beginGroup(accountsC); - const auto childGroups = settings->childGroups(); + const auto childGroups = selectedAccountIds.isEmpty() ? settings->childGroups() : selectedAccountIds; for (const auto &accountId : childGroups) { settings->beginGroup(accountId); const auto acc = loadAccountHelper(*settings); diff --git a/src/gui/legacyaccountselectiondialog.cpp b/src/gui/legacyaccountselectiondialog.cpp new file mode 100644 index 0000000000..b841535729 --- /dev/null +++ b/src/gui/legacyaccountselectiondialog.cpp @@ -0,0 +1,45 @@ +#include "legacyaccountselectiondialog.h" + +#include +#include +#include +#include + +namespace OCC +{ + +LegacyAccountSelectionDialog::LegacyAccountSelectionDialog(const QVector &accounts, QWidget *parent) + : QDialog(parent) +{ + setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); + setWindowTitle(tr("Legacy import")); + + auto layout = new QVBoxLayout(this); + layout->addWidget(new QLabel(tr("Select the accounts to import from the legacy configuration:"), this)); + + for (const auto &account : accounts) { + auto checkbox = new QCheckBox(account.label, this); + checkbox->setChecked(true); + layout->addWidget(checkbox); + _checkBoxes.insert(account.id, checkbox); + } + + auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); + connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + layout->addWidget(buttonBox); +} + +QStringList LegacyAccountSelectionDialog::selectedAccountIds() const +{ + QStringList selectedAccount; + for (auto it = _checkBoxes.constBegin(); it != _checkBoxes.constEnd(); ++it) { + if (it.value()->isChecked()) { + selectedAccount.push_back(it.key()); + } + } + return selectedAccount; +} + +} // namespace OCC + diff --git a/src/gui/legacyaccountselectiondialog.h b/src/gui/legacyaccountselectiondialog.h new file mode 100644 index 0000000000..d2e9f8c029 --- /dev/null +++ b/src/gui/legacyaccountselectiondialog.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include + +class QCheckBox; + +namespace OCC +{ + +class LegacyAccountSelectionDialog : public QDialog +{ + Q_OBJECT +public: + struct AccountItem { + QString id; + QString label; + }; + + explicit LegacyAccountSelectionDialog(const QVector &accounts, QWidget *parent = nullptr); + + [[nodiscard]] QStringList selectedAccountIds() const; + +private: + QMap _checkBoxes; +}; + +} // namespace OCC +