File indexing completed on 2024-05-12 11:41:21

0001 /*
0002     File                 : SettingsDatasetsPage.cpp
0003     Project              : LabPlot
0004     Description          : settings page for Datasets
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2019 Alexander Semke <alexander.semke@web.de>
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "SettingsDatasetsPage.h"
0011 #include "backend/lib/macros.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <QDir>
0016 #include <QMessageBox>
0017 #include <QStandardPaths>
0018 
0019 /**
0020  * \brief Page for the 'General' settings of the Labplot settings dialog.
0021  */
0022 SettingsDatasetsPage::SettingsDatasetsPage(QWidget* parent)
0023     : SettingsPage(parent) {
0024     ui.setupUi(this);
0025 
0026     ui.bClearCache->setIcon(QIcon::fromTheme(QLatin1String("edit-clear")));
0027     ui.bClearCache->setToolTip(i18n("Clear downloaded files"));
0028     ui.bClearCache->setEnabled(false);
0029 
0030     connect(ui.bClearCache, &QPushButton::clicked, this, &SettingsDatasetsPage::clearCache);
0031 
0032     loadSettings();
0033 }
0034 
0035 void SettingsDatasetsPage::applySettings() {
0036 }
0037 
0038 void SettingsDatasetsPage::restoreDefaults() {
0039 }
0040 
0041 void SettingsDatasetsPage::loadSettings() {
0042     QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1String("/datasets_local/"));
0043     if (dir.exists()) {
0044         int count = dir.count() - 2; // subtract 2 for . and ..
0045         ui.lFiles->setText(i18n("Files - %1", count));
0046 
0047         if (count > 0) {
0048             ui.bClearCache->setEnabled(true);
0049 
0050             // calculate the size
0051             int size = 0;
0052             for (auto& file : dir.entryList()) {
0053                 if (file == QLatin1Char('.') || file == QLatin1String(".."))
0054                     continue;
0055 
0056                 size += QFileInfo(dir, file).size();
0057             }
0058 
0059             const auto numberLocale = QLocale();
0060             QString sizeStr;
0061             if (size > 1024 * 1024)
0062                 sizeStr = numberLocale.toString(size / 1024 / 1024) + QLatin1String("MB");
0063             else if (size > 1024)
0064                 sizeStr = numberLocale.toString(size / 1024) + QLatin1String("kB");
0065             else
0066                 sizeStr = numberLocale.toString(size) + QLatin1String("B");
0067 
0068             ui.lSize->setText(i18n("Total size - %1", sizeStr));
0069         } else
0070             ui.lSize->setText(i18n("Total size - 0B"));
0071     }
0072 }
0073 
0074 void SettingsDatasetsPage::clearCache() {
0075     QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1String("/datasets_local/"));
0076     if (dir.exists()) {
0077         for (auto& fileName : dir.entryList()) {
0078             if (fileName == QLatin1Char('.') || fileName == QLatin1String(".."))
0079                 continue;
0080 
0081             QFile file(dir.path() + QLatin1String("/") + fileName);
0082             file.remove();
0083         }
0084 
0085         ui.lFiles->setText(i18n("Files - 0"));
0086         ui.lSize->setText(i18n("Total size - 0B"));
0087         ui.bClearCache->setEnabled(false);
0088         QMessageBox::information(this, i18n("Datasets cache"), i18n("Downloaded files successfully deleted."));
0089     }
0090 }