From 8fb673457b42e9bf37562568410a7382ed50448d Mon Sep 17 00:00:00 2001 From: Kevin Ottens Date: Mon, 5 Oct 2020 19:45:40 +0200 Subject: [PATCH] Add a button to create a debug archive This will harvest everything we might need for debugging purposes: * config file * sync journal dbs * log files Signed-off-by: Kevin Ottens --- src/gui/generalsettings.cpp | 93 +++++++++++++++++++++++++++++++++++++ src/gui/generalsettings.h | 1 + src/gui/generalsettings.ui | 11 ++++- 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/src/gui/generalsettings.cpp b/src/gui/generalsettings.cpp index fff5c9076..f910601ac 100644 --- a/src/gui/generalsettings.cpp +++ b/src/gui/generalsettings.cpp @@ -30,21 +30,102 @@ #include "ignorelisteditor.h" #include "common/utility.h" +#include "logger.h" #include "config.h" #include "legalnotice.h" +#include +#include #include #include #include +#include + #define QTLEGACY (QT_VERSION < QT_VERSION_CHECK(5,9,0)) #if !(QTLEGACY) #include #endif +namespace { +struct ZipEntry { + QString localFilename; + QString zipFilename; +}; + +ZipEntry fileInfoToZipEntry(const QFileInfo &info) +{ + return { + info.absoluteFilePath(), + info.fileName() + }; +} + +ZipEntry fileInfoToLogZipEntry(const QFileInfo &info) +{ + auto entry = fileInfoToZipEntry(info); + entry.zipFilename.prepend(QStringLiteral("logs/")); + return entry; +} + +ZipEntry syncFolderToZipEntry(OCC::Folder *f) +{ + const auto journalPath = f->journalDb()->databaseFilePath(); + const auto journalInfo = QFileInfo(journalPath); + return fileInfoToZipEntry(journalInfo); +} + +QVector createFileList() +{ + auto list = QVector(); + OCC::ConfigFile cfg; + + list.append(fileInfoToZipEntry(QFileInfo(cfg.configFile()))); + + const auto logger = OCC::Logger::instance(); + + if (!logger->logDir().isEmpty()) { + list.append({QString(), QStringLiteral("logs")}); + + QDir dir(logger->logDir()); + const auto infoList = dir.entryInfoList(QDir::Files); + std::transform(std::cbegin(infoList), std::cend(infoList), + std::back_inserter(list), + fileInfoToLogZipEntry); + } else if (!logger->logFile().isEmpty()) { + list.append(fileInfoToZipEntry(QFileInfo(logger->logFile()))); + } + + const auto folders = OCC::FolderMan::instance()->map().values(); + std::transform(std::cbegin(folders), std::cend(folders), + std::back_inserter(list), + syncFolderToZipEntry); + + return list; +} + +void createDebugArchive(const QString &filename) +{ + const auto entries = createFileList(); + + QZipWriter zip(filename); + for (const auto &entry : entries) { + if (entry.localFilename.isEmpty()) { + zip.addDirectory(entry.zipFilename); + } else { + QFile file(entry.localFilename); + if (!file.open(QFile::ReadOnly)) { + continue; + } + zip.addFile(entry.zipFilename, &file); + } + } +} +} + namespace OCC { GeneralSettings::GeneralSettings(QWidget *parent) @@ -122,6 +203,7 @@ GeneralSettings::GeneralSettings(QWidget *parent) _ui->monoIconsCheckBox->setVisible(Theme::instance()->monoIconsAvailable()); connect(_ui->ignoredFilesButton, &QAbstractButton::clicked, this, &GeneralSettings::slotIgnoreFilesEditor); + connect(_ui->debugArchiveButton, &QAbstractButton::clicked, this, &GeneralSettings::slotCreateDebugArchive); // accountAdded means the wizard was finished and the wizard might change some options. connect(AccountManager::instance(), &AccountManager::accountAdded, this, &GeneralSettings::loadMiscSettings); @@ -260,6 +342,17 @@ void GeneralSettings::slotIgnoreFilesEditor() } } +void GeneralSettings::slotCreateDebugArchive() +{ + const auto filename = QFileDialog::getSaveFileName(this, tr("Create Debug Archive"), QString(), tr("Zip Archives") + " (*.zip)"); + if (filename.isEmpty()) { + return; + } + + createDebugArchive(filename); + QMessageBox::information(this, tr("Debug Archive Created"), tr("Debug archive is created at %1").arg(filename)); +} + void GeneralSettings::slotShowLegalNotice() { auto notice = new LegalNotice(); diff --git a/src/gui/generalsettings.h b/src/gui/generalsettings.h index 4b5e6a538..fa6fe11b9 100644 --- a/src/gui/generalsettings.h +++ b/src/gui/generalsettings.h @@ -48,6 +48,7 @@ private slots: void slotToggleOptionalServerNotifications(bool); void slotShowInExplorerNavigationPane(bool); void slotIgnoreFilesEditor(); + void slotCreateDebugArchive(); void loadMiscSettings(); void slotShowLegalNotice(); #if defined(BUILD_UPDATER) diff --git a/src/gui/generalsettings.ui b/src/gui/generalsettings.ui index ebc69ebd6..67e9af6ee 100644 --- a/src/gui/generalsettings.ui +++ b/src/gui/generalsettings.ui @@ -6,8 +6,8 @@ 0 0 - 516 - 523 + 553 + 558 @@ -219,6 +219,13 @@ + + + + Create Debug Archive... + + +