File indexing completed on 2024-04-28 15:14:02

0001 /***************************************************************************
0002     File                 : SettingsDatasetsPage.cpp
0003     Project              : LabPlot
0004     Description          : settings page for Datasets
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2019 Alexander Semke (alexander.semke@web.de)
0007 
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 
0029 #include "SettingsDatasetsPage.h"
0030 #include "backend/lib/macros.h"
0031 
0032 #include <KLocalizedString>
0033 
0034 #include <QDir>
0035 #include <QMessageBox>
0036 #include <QStandardPaths>
0037 
0038 /**
0039  * \brief Page for the 'General' settings of the Labplot settings dialog.
0040  */
0041 SettingsDatasetsPage::SettingsDatasetsPage(QWidget* parent) : SettingsPage(parent) {
0042     ui.setupUi(this);
0043 
0044     ui.bClearCache->setIcon(QIcon::fromTheme(QLatin1String("edit-clear")));
0045     ui.bClearCache->setToolTip(i18n("Clear downloaded files"));
0046     ui.bClearCache->setEnabled(false);
0047 
0048     connect(ui.bClearCache, &QPushButton::clicked, this, &SettingsDatasetsPage::clearCache);
0049 
0050     loadSettings();
0051 }
0052 
0053 void SettingsDatasetsPage::applySettings() {
0054 
0055 }
0056 
0057 void SettingsDatasetsPage::restoreDefaults() {
0058 }
0059 
0060 void SettingsDatasetsPage::loadSettings() {
0061     QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1String("/datasets_local/"));
0062     if (dir.exists()) {
0063         int count = dir.count() - 2; //subtract 2 for . and ..
0064         ui.lFiles->setText(i18n("Files - %1", count));
0065 
0066         if (count > 0) {
0067             ui.bClearCache->setEnabled(true);
0068 
0069             //calculate the size
0070             int size = 0;
0071             for (auto file : dir.entryList()) {
0072                 if (file == QLatin1Char('.') || file == QLatin1String(".."))
0073                     continue;
0074 
0075                 size += QFileInfo(dir, file).size();
0076             }
0077 
0078             SET_NUMBER_LOCALE
0079             QString sizeStr;
0080             if (size > 1024*1024)
0081                 sizeStr = numberLocale.toString(size/1024/1024) + QLatin1String("MB");
0082             if (size > 1024)
0083                 sizeStr = numberLocale.toString(size/1024) + QLatin1String("kB");
0084             else
0085                 sizeStr = numberLocale.toString(size) + QLatin1String("B");
0086 
0087             ui.lSize->setText(i18n("Total size - %1", sizeStr));
0088         } else
0089             ui.lSize->setText(i18n("Total size - 0B"));
0090     }
0091 }
0092 
0093 void SettingsDatasetsPage::clearCache() {
0094     QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1String("/datasets_local/"));
0095     if (dir.exists()) {
0096         for (auto fileName : dir.entryList()) {
0097             if (fileName == QLatin1Char('.') || fileName == QLatin1String(".."))
0098                 continue;
0099 
0100             QFile file(dir.path() + QLatin1String("/") + fileName);
0101             file.remove();
0102         }
0103 
0104         ui.lFiles->setText(i18n("Files - 0"));
0105         ui.lSize->setText(i18n("Total size - 0B"));
0106         ui.bClearCache->setEnabled(false);
0107         QMessageBox::information(this, i18n("Datasets cache"), i18n("Downloaded files successfully deleted."));
0108     }
0109 }