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 "exportaccountjob.h" 0008 #include "ruqola_importexport_accounts_debug.h" 0009 #include <KLocalizedString> 0010 #include <KZip> 0011 #include <QDir> 0012 #include <QStandardPaths> 0013 #include <QTemporaryFile> 0014 0015 ExportAccountJob::ExportAccountJob(const QString &fileName, QObject *parent) 0016 : QThread{parent} 0017 , mArchive(new KZip(fileName)) 0018 { 0019 connect(this, &ExportAccountJob::exportCacheData, this, &ExportAccountJob::exportCache); 0020 connect(this, &ExportAccountJob::exportLogsData, this, &ExportAccountJob::exportLogs); 0021 } 0022 0023 ExportAccountJob::~ExportAccountJob() 0024 { 0025 if (mArchive && mArchive->isOpen()) { 0026 mArchive->close(); 0027 } 0028 delete mArchive; 0029 } 0030 0031 void ExportAccountJob::run() 0032 { 0033 if (!canStart()) { 0034 deleteLater(); 0035 Q_EMIT exportFailed(i18n("Impossible to export data.")); 0036 qCDebug(RUQOLA_IMPORT_EXPORT_ACCOUNTS_LOG) << " Account list is empty! "; 0037 return; 0038 } 0039 const bool result = mArchive->open(QIODevice::WriteOnly); 0040 if (!result) { 0041 deleteLater(); 0042 Q_EMIT exportFailed(i18n("Impossible to create zip file.")); 0043 qCDebug(RUQOLA_IMPORT_EXPORT_ACCOUNTS_LOG) << "Impossible to open zip file"; 0044 return; 0045 } 0046 0047 exportAccount(); 0048 } 0049 0050 void ExportAccountJob::exportAccount() 0051 { 0052 if (mAccountIndex < mListAccounts.count()) { 0053 const auto account = mListAccounts.at(mAccountIndex); 0054 mAccountNames.append(account.accountName); 0055 exportAccount(account); 0056 } else { 0057 finishExportAccount(); 0058 } 0059 } 0060 0061 void ExportAccountJob::finishExportAccount() 0062 { 0063 QTemporaryFile tmp; 0064 tmp.open(); 0065 QTextStream text(&tmp); 0066 text << mAccountNames.join(QLatin1Char('\n')); 0067 tmp.close(); 0068 mArchive->addLocalFile(tmp.fileName(), QStringLiteral("accounts")); 0069 0070 Q_EMIT exportInfo(i18n("Export Done.") + QLatin1Char('\n')); 0071 Q_EMIT exportDone(); 0072 deleteLater(); 0073 } 0074 0075 QVector<ImportExportUtils::AccountImportExportInfo> ExportAccountJob::listAccounts() const 0076 { 0077 return mListAccounts; 0078 } 0079 0080 void ExportAccountJob::exportAccount(const ImportExportUtils::AccountImportExportInfo &info) 0081 { 0082 exportConfig(info); 0083 } 0084 0085 void ExportAccountJob::exportConfig(const ImportExportUtils::AccountImportExportInfo &info) 0086 { 0087 // config files 0088 const QString configPath = info.accountName + QLatin1Char('/') + ImportExportUtils::configPath(); 0089 qCDebug(RUQOLA_IMPORT_EXPORT_ACCOUNTS_LOG) << " configPath " << configPath; 0090 storeDirectory(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/ruqola/") + info.accountName, configPath); 0091 Q_EMIT exportInfo(i18n("Account %1: export config done.", info.accountName)); 0092 Q_EMIT exportCacheData(info); 0093 } 0094 0095 void ExportAccountJob::exportCache(const ImportExportUtils::AccountImportExportInfo &info) 0096 { 0097 // cache files 0098 const QString cachePath = info.accountName + QLatin1Char('/') + ImportExportUtils::cachePath(); 0099 qCDebug(RUQOLA_IMPORT_EXPORT_ACCOUNTS_LOG) << " cachePath " << cachePath; 0100 const QString storeCachePath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1Char('/') + info.accountName + QLatin1Char('/'); 0101 qCDebug(RUQOLA_IMPORT_EXPORT_ACCOUNTS_LOG) << "QStandardPaths::writableLocation(QStandardPaths::CacheLocation) " << storeCachePath; 0102 storeDirectory(storeCachePath, cachePath); 0103 Q_EMIT exportInfo(i18n("Account %1: export cache done.", info.accountName)); 0104 Q_EMIT exportLogsData(info); 0105 } 0106 0107 void ExportAccountJob::exportLogs(const ImportExportUtils::AccountImportExportInfo &info) 0108 { 0109 // local files 0110 const QString localPath = info.accountName + QLatin1Char('/') + ImportExportUtils::logsPath(); 0111 qCDebug(RUQOLA_IMPORT_EXPORT_ACCOUNTS_LOG) << " localPath " << localPath; 0112 storeDirectory(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + QStringLiteral("/logs/") + info.accountName, localPath); 0113 Q_EMIT exportInfo(i18n("Account %1: export logs done.", info.accountName)); 0114 mAccountIndex++; 0115 exportAccount(); 0116 } 0117 0118 void ExportAccountJob::exportDatabase(const ImportExportUtils::AccountImportExportInfo &info) 0119 { 0120 // TODO 0121 } 0122 0123 void ExportAccountJob::setListAccounts(const QVector<ImportExportUtils::AccountImportExportInfo> &newListAccounts) 0124 { 0125 mListAccounts = newListAccounts; 0126 } 0127 0128 bool ExportAccountJob::canStart() const 0129 { 0130 return !mListAccounts.isEmpty(); 0131 } 0132 0133 void ExportAccountJob::storeDirectory(const QString &subDirectory, const QString &subfolderPath) 0134 { 0135 const QDir directoryToStore(subDirectory); 0136 // qDebug() << " directoryToStore " << directoryToStore; 0137 if (directoryToStore.exists()) { 0138 const bool addFolder = mArchive->addLocalDirectory(directoryToStore.path(), subfolderPath); 0139 if (!addFolder) { 0140 Q_EMIT exportFailed(i18n("Directory \"%1\" cannot be added to backup file.", directoryToStore.path())); 0141 } 0142 } 0143 } 0144 0145 #include "moc_exportaccountjob.cpp"