File indexing completed on 2024-12-22 04:45:57

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "exportdatawizard.h"
0008 #include "exportdatafinishpage.h"
0009 #include "exportdataselectaccountpage.h"
0010 #include "importexportdata/importexportutils.h"
0011 #include "managerdatapaths.h"
0012 #include <KConfigGroup>
0013 #include <KLocalizedString>
0014 #include <KSharedConfig>
0015 #include <KWindowConfig>
0016 
0017 #include <QDirIterator>
0018 #include <QTimer>
0019 #include <QWindow>
0020 
0021 using namespace std::chrono_literals;
0022 
0023 namespace
0024 {
0025 static const char myConfigExportDataWizardGroupName[] = "ExportDataWizard";
0026 }
0027 ExportDataWizard::ExportDataWizard(QWidget *parent)
0028     : QWizard(parent)
0029     , mExportDataSelectAccountPage(new ExportDataSelectAccountPage(this))
0030     , mExportDataFinishPage(new ExportDataFinishPage(this))
0031 {
0032     setWindowTitle(i18nc("@title:window", "Export Accounts"));
0033 
0034     mExportDataSelectAccountPage->setObjectName(QStringLiteral("mExportDataSelectAccountPage"));
0035     mExportDataFinishPage->setObjectName(QStringLiteral("mExportDataFinishPage"));
0036 
0037     setPage(SelectAccountPage, mExportDataSelectAccountPage);
0038     setPage(FinishPage, mExportDataFinishPage);
0039 
0040     readConfig();
0041     loadAccountInfo();
0042     // Connect after loadAccountInfo
0043     connect(this, &ExportDataWizard::currentIdChanged, this, &ExportDataWizard::slotCurrentIdChanged);
0044 }
0045 
0046 ExportDataWizard::~ExportDataWizard()
0047 {
0048     writeConfig();
0049 }
0050 
0051 void ExportDataWizard::readConfig()
0052 {
0053     create(); // ensure a window is created
0054     windowHandle()->resize(QSize(800, 600));
0055     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1String(myConfigExportDataWizardGroupName));
0056     KWindowConfig::restoreWindowSize(windowHandle(), group);
0057     resize(windowHandle()->size()); // workaround for QTBUG-40584
0058 }
0059 
0060 void ExportDataWizard::writeConfig()
0061 {
0062     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1String(myConfigExportDataWizardGroupName));
0063     KWindowConfig::saveWindowSize(windowHandle(), group);
0064 }
0065 
0066 void ExportDataWizard::loadAccountInfo()
0067 {
0068     QDirIterator it(ManagerDataPaths::self()->path(ManagerDataPaths::Config, QString()),
0069                     QStringList() << QStringLiteral("ruqola.conf"),
0070                     QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot,
0071                     QDirIterator::Subdirectories);
0072     QVector<ImportExportUtils::AccountImportExportInfo> lstAccountInfo;
0073     while (it.hasNext()) {
0074         const QString val = it.next();
0075         ImportExportUtils::AccountImportExportInfo info;
0076         info.path = val;
0077         info.accountName = QFileInfo(val).dir().dirName();
0078         lstAccountInfo.append(std::move(info));
0079     }
0080     mExportDataSelectAccountPage->setAccountList(lstAccountInfo);
0081 }
0082 
0083 void ExportDataWizard::slotCurrentIdChanged(int id)
0084 {
0085     if (id == FinishPage) {
0086         mExportDataFinishPage->setListAccounts(mExportDataSelectAccountPage->selectedAccounts());
0087         // qDebug() << " selected account " << mExportDataSelectAccountPage->selectedAccounts();
0088         QTimer::singleShot(200ms, this, &ExportDataWizard::exportAccounts);
0089     }
0090 }
0091 
0092 void ExportDataWizard::exportAccounts()
0093 {
0094     mExportDataFinishPage->exportAccounts();
0095 }
0096 
0097 #include "moc_exportdatawizard.cpp"