File size: 12,906 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/***************************************************************************
 *   Copyright (c) 2021 Werner Mayer <wmayer[at]users.sourceforge.net>     *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/

#include <cmath>
#include <limits>
#include <vector>
#include <QCoreApplication>
#include <QDate>
#include <QDesktopServices>
#include <QDir>
#include <QLocale>
#include <QMessageBox>
#include <QSettings>
#include <QUrl>

#include <App/Application.h>
#include <App/Document.h>
#include <Gui/DocumentRecovery.h>
#include <Gui/MainWindow.h>

#include "DlgSettingsCacheDirectory.h"
#include "ui_DlgSettingsCacheDirectory.h"


using namespace Gui::Dialog;

/* TRANSLATOR Gui::Dialog::DlgSettingsCacheDirectory */

QString DlgSettingsCacheDirectory::currentSize;

DlgSettingsCacheDirectory::DlgSettingsCacheDirectory(QWidget* parent)
    : PreferencePage(parent)
    , ui(new Ui_DlgSettingsCacheDirectory)
{
    ui->setupUi(this);
    ui->labelCache->setToolTip(tr("Notify the user if the cache size exceeds the specified limit"));
    if (currentSize.isEmpty()) {
        currentSize = tr("Unknown");
    }
    setCurrentCacheSize(currentSize);

    QString path = QString::fromStdString(App::Application::getUserCachePath());
    ui->cacheLocation->setText(path);

    ui->comboBoxLimit->addItem(QStringLiteral("100 MB"), 100);
    ui->comboBoxLimit->addItem(QStringLiteral("300 MB"), 300);
    ui->comboBoxLimit->addItem(QStringLiteral("500 MB"), 500);
    ui->comboBoxLimit->addItem(QStringLiteral("1 GB"), 1024);
    ui->comboBoxLimit->addItem(QStringLiteral("2 GB"), 2048);
    ui->comboBoxLimit->addItem(QStringLiteral("3 GB"), 3072);

    connect(ui->pushButtonCheck, &QPushButton::clicked, this, &DlgSettingsCacheDirectory::runCheck);
    connect(ui->openButton, &QPushButton::clicked, this, &DlgSettingsCacheDirectory::openDirectory);
}

DlgSettingsCacheDirectory::~DlgSettingsCacheDirectory() = default;

void DlgSettingsCacheDirectory::saveSettings()
{
    ApplicationCacheSettings::setCheckPeriod(ui->comboBoxPeriod->currentIndex());
    ApplicationCacheSettings::setCacheSizeLimit(ui->comboBoxLimit->currentData().toUInt());
}

void DlgSettingsCacheDirectory::loadSettings()
{
    int period = ApplicationCacheSettings::getCheckPeriod();
    if (period >= 0 && period < ui->comboBoxPeriod->count()) {
        ui->comboBoxPeriod->setCurrentIndex(period);
    }
    unsigned int limit = ApplicationCacheSettings::getCacheSizeLimit();
    int index = ui->comboBoxLimit->findData(limit);

    // if not found then add a new item with this value
    if (index < 0) {
        ui->comboBoxLimit->addItem(QStringLiteral("%1 MB").arg(limit), limit);
        index = ui->comboBoxLimit->count() - 1;
    }
    ui->comboBoxLimit->setCurrentIndex(index);
}

void DlgSettingsCacheDirectory::resetSettingsToDefaults()
{
    ParameterGrp::handle hGrp;
    hGrp = WindowParameter::getDefaultParameter()->GetGroup("CacheDirectory");
    // reset "Limit" parameter
    hGrp->RemoveUnsigned("Limit");
    // reset "Period" parameter
    hGrp->RemoveInt("Period");

    // finally reset all the parameters associated to Gui::Pref* widgets
    PreferencePage::resetSettingsToDefaults();
}

void DlgSettingsCacheDirectory::changeEvent(QEvent* e)
{
    if (e->type() == QEvent::LanguageChange) {
        int period = ui->comboBoxPeriod->currentIndex();
        ui->retranslateUi(this);
        ui->comboBoxPeriod->setCurrentIndex(period);
        setCurrentCacheSize(currentSize);
    }
    QWidget::changeEvent(e);
}

void DlgSettingsCacheDirectory::setCurrentCacheSize(const QString& str)
{
    currentSize = str;
    ui->labelCurrentCache->setText(tr("Current cache size: %1").arg(str));
}

void DlgSettingsCacheDirectory::runCheck()
{
    Gui::Dialog::ApplicationCache cache;
    unsigned int sizeInMB = ui->comboBoxLimit->currentData().toUInt();
    cache.setLimit(ApplicationCache::toBytes(sizeInMB));
    qint64 total = cache.size();
    setCurrentCacheSize(ApplicationCache::toString(total));

    // When performing the clean-up then recompute the new cache size
    if (cache.performAction(total)) {
        total = cache.size();
        setCurrentCacheSize(ApplicationCache::toString(total));
    }
}

void DlgSettingsCacheDirectory::openDirectory()
{
    QString path = QString::fromStdString(App::Application::getUserCachePath());
    QUrl url = QUrl::fromLocalFile(path);
    QDesktopServices::openUrl(url);
}

// ----------------------------------------------------------------------------

ApplicationCache::ApplicationCache()
{
    limit = std::pow(1024, 3);
    setPeriod(Period::Weekly);
}

/*!
 * \brief ApplicationCache::setPeriod
 * Set the period to check for the cache size
 * \param period
 */
void ApplicationCache::setPeriod(ApplicationCache::Period period)
{
    switch (period) {
        case Period::Always:
            numDays = -1;
            break;
        case Period::Daily:
            numDays = 1;
            break;
        case Period::Weekly:
            numDays = 7;
            break;
        case Period::Monthly:
            numDays = 31;
            break;
        case Period::Yearly:
            numDays = 365;
            break;
        case Period::Never:
            numDays = std::numeric_limits<int>::max();
            break;
    }
}

/*!
 * \brief ApplicationCache::setLimit
 * Set the limit in bytes to perform a check
 * \param value
 */
void ApplicationCache::setLimit(qint64 value)
{
    limit = value;
}

/*!
 * \brief ApplicationCache::periodicCheckOfSize
 * Checks if the periodic check should be performed now
 * \return
 */
bool ApplicationCache::periodicCheckOfSize() const
{
    QString vendor = QString::fromLatin1(App::Application::Config()["ExeVendor"].c_str());
    QString application = QString::fromStdString(App::Application::getExecutableName());

    QSettings settings(vendor, application);
    QString key = QStringLiteral("LastCacheCheck");
    QDate date = settings.value(key).toDate();
    QDate now = QDate::currentDate();

    // get the days since the last check
    int days = date.daysTo(now);
    if (date.isNull()) {
        days = 1000;
    }

    if (days >= numDays) {
        settings.setValue(key, now);
        return true;
    }

    return false;
}

/*!
 * \brief ApplicationCache::performAction
 * If the cache size \a total is higher than the limit then show a dialog to the user
 * \param total
 */
bool ApplicationCache::performAction(qint64 total)
{
    bool performed = false;
    if (total > limit) {
        QString path = QString::fromStdString(App::Application::getUserCachePath());
        QMessageBox msgBox(Gui::getMainWindow());
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setWindowTitle(tr("Cache Directory"));

        QString hint = tr("The cache directory %1 exceeds the size of %2.")
                           .arg(path, ApplicationCache::toString(limit));
        QString ask = tr("Clear it now?");
        QString warn = tr("Warning: Make sure that this is the only running %1 instance "
                          "and that no documents are opened as this may result into data loss!")
                           .arg(QCoreApplication::applicationName());

        msgBox.setText(QStringLiteral("%1 %2\n\n\n%3").arg(hint, ask, warn));
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Open);
        msgBox.setDefaultButton(QMessageBox::No);

        while (true) {
            int ret = msgBox.exec();
            if (ret == QMessageBox::Open) {
                QUrl url = QUrl::fromLocalFile(path);
                QDesktopServices::openUrl(url);
            }
            else {
                if (ret == QMessageBox::Yes) {
                    clearDirectory(path);
                    performed = true;
                }
                break;
            }
        }
    }

    return performed;
}

/*!
 * \brief ApplicationCache::size
 * Determines the size of the cache.
 * \return
 */
qint64 ApplicationCache::size() const
{
    qint64 total = dirSize(QString::fromStdString(App::Application::getUserCachePath()));
    return total;
}

/*!
 * \internal
 */
void ApplicationCache::clearDirectory(const QString& path)
{
    // Add the transient directories and the lock files to the ignore list
    QDir tmp = QString::fromUtf8(App::Application::getUserCachePath().c_str());
    tmp.setNameFilters(QStringList() << QStringLiteral("*.lock"));
    tmp.setFilter(QDir::Files);

    QList<QFileInfo> dirs;
    std::vector<App::Document*> docs = App::GetApplication().getDocuments();
    for (auto it : docs) {
        QDir dir(QString::fromStdString(it->TransientDir.getStrValue()));
        QFileInfo fi(dir.absolutePath());
        dirs.append(fi);
    }

    DocumentRecoveryCleaner cleaner;
    cleaner.setIgnoreFiles(tmp.entryList());
    cleaner.setIgnoreDirectories(dirs);
    cleaner.clearDirectory(QFileInfo(path));
}

/*!
 * \internal
 */
qint64 ApplicationCache::dirSize(QString dirPath) const
{
    qint64 total = 0;
    QDir dir(dirPath);

    QDir::Filters fileFilters = QDir::Files;
    const auto& files = dir.entryList(fileFilters);
    for (const QString& filePath : files) {
        QFileInfo fi(dir, filePath);
        total += fi.size();
    }

    // traverse sub-directories recursively
    QDir::Filters dirFilters = QDir::Dirs | QDir::NoDotAndDotDot;
    const auto& dirs = dir.entryList(dirFilters);
    for (const QString& subDirPath : dirs) {
        total += dirSize(dirPath + QDir::separator() + subDirPath);
    }
    return total;
}

/*!
 * \brief ApplicationCache::applyUserSettings
 * Set period and limit according to user settings
 */
void ApplicationCache::applyUserSettings()
{
    int period = ApplicationCacheSettings::getCheckPeriod();
    setPeriod(static_cast<Period>(period));

    unsigned int sizeInMB = ApplicationCacheSettings::getCacheSizeLimit();
    setLimit(ApplicationCache::toBytes(sizeInMB));
}

qint64 ApplicationCache::toBytes(unsigned int sizeInMB)
{
    qint64 value = static_cast<qint64>(sizeInMB) * 1024 * 1024;
    return value;
}

QString ApplicationCache::toString(qint64 size)
{
    QStringList units
        = {QStringLiteral("Bytes"), QStringLiteral("KB"), QStringLiteral("MB"), QStringLiteral("GB")};
    int i;
    double outputSize = size;
    for (i = 0; i < units.size() - 1; i++) {
        if (outputSize < 1024) {
            break;
        }
        outputSize /= 1024;
    }

    return QStringLiteral("%1 %2").arg(QLocale().toString(outputSize, 'f', 2), units[i]);
}

// ----------------------------------------------------------------------------

unsigned int ApplicationCacheSettings::getCacheSizeLimit()
{
    ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("CacheDirectory");
    return hGrp->GetUnsigned("Limit", 500);
}

void ApplicationCacheSettings::setCacheSizeLimit(unsigned int limit)
{
    ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("CacheDirectory");
    hGrp->SetUnsigned("Limit", limit);
}

int ApplicationCacheSettings::getCheckPeriod()
{
    ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("CacheDirectory");
    return hGrp->GetInt("Period", static_cast<int>(ApplicationCache::Period::Weekly));
}

void ApplicationCacheSettings::setCheckPeriod(int period)
{
    ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("CacheDirectory");
    hGrp->SetInt("Period", period);
}


#include "moc_DlgSettingsCacheDirectory.cpp"