| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/dialogs/formdatabasecleanup.h" |
| 4 | |
| 5 | #include "database/databasefactory.h" |
| 6 | #include "definitions/definitions.h" |
| 7 | #include "gui/guiutilities.h" |
| 8 | #include "miscellaneous/application.h" |
| 9 | #include "miscellaneous/iconfactory.h" |
| 10 | #include "miscellaneous/settings.h" |
| 11 | |
| 12 | #include <QCloseEvent> |
| 13 | #include <QDialogButtonBox> |
| 14 | #include <QPushButton> |
| 15 | |
| 16 | FormDatabaseCleanup::FormDatabaseCleanup(QWidget* parent) |
| 17 | : QDialog(parent), m_ui(new Ui::FormDatabaseCleanup), m_cleaner(nullptr) { |
| 18 | m_ui->setupUi(this); |
| 19 | |
| 20 | setObjectName(QSL("form_db_cleanup")); |
| 21 | |
| 22 | GuiUtilities::applyDialogProperties(*this, qApp->icons()->fromTheme(QSL("edit-clear"))); |
| 23 | |
| 24 | connect(m_ui->m_spinDays, |
| 25 | QOverload<int>::of(&QSpinBox::valueChanged), |
| 26 | this, |
| 27 | &FormDatabaseCleanup::updateDaysSuffix); |
| 28 | connect(m_ui->m_btnBox->button(QDialogButtonBox::StandardButton::Ok), |
| 29 | &QPushButton::clicked, |
| 30 | this, |
| 31 | &FormDatabaseCleanup::startPurging); |
| 32 | connect(this, &FormDatabaseCleanup::purgeRequested, &m_cleaner, &DatabaseCleaner::purgeDatabaseData); |
| 33 | connect(&m_cleaner, &DatabaseCleaner::purgeStarted, this, &FormDatabaseCleanup::onPurgeStarted); |
| 34 | connect(&m_cleaner, &DatabaseCleaner::purgeProgress, this, &FormDatabaseCleanup::onPurgeProgress); |
| 35 | connect(&m_cleaner, &DatabaseCleaner::purgeFinished, this, &FormDatabaseCleanup::onPurgeFinished); |
| 36 | |
| 37 | m_ui->m_spinDays->setValue(DEFAULT_DAYS_TO_DELETE_MSG); |
| 38 | m_ui->m_lblResult->setStatus(WidgetWithStatus::StatusType::Information, tr("I am ready."), tr("I am ready.")); |
| 39 | |
| 40 | loadDatabaseInfo(); |
| 41 | |
| 42 | GuiUtilities::restoreState(this, qApp->settings()->value(GROUP(GUI), objectName(), QByteArray()).toByteArray()); |
| 43 | } |
| 44 | |
| 45 | void FormDatabaseCleanup::closeEvent(QCloseEvent* event) { |
| 46 | if (!m_ui->m_btnBox->isEnabled()) { |
| 47 | event->ignore(); |
| 48 | } |
| 49 | else { |
| 50 | QDialog::closeEvent(event); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void FormDatabaseCleanup::keyPressEvent(QKeyEvent* event) { |
| 55 | if (!m_ui->m_btnBox->isEnabled()) { |
| 56 | event->ignore(); |
| 57 | } |
| 58 | else { |
| 59 | QDialog::keyPressEvent(event); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void FormDatabaseCleanup::updateDaysSuffix(int number) { |
| 64 | m_ui->m_spinDays->setSuffix(tr(" day(s)", nullptr, number)); |
| 65 | } |
| 66 | |
| 67 | void FormDatabaseCleanup::startPurging() { |
| 68 | CleanerOrders orders; |
| 69 | |
| 70 | orders.m_removeRecycleBin = m_ui->m_checkRemoveRecycleBin->isChecked(); |
| 71 | orders.m_removeOldMessages = m_ui->m_checkRemoveOldMessages->isChecked(); |
| 72 | orders.m_barrierForRemovingOldMessagesInDays = m_ui->m_spinDays->value(); |
| 73 | orders.m_removeReadMessages = m_ui->m_checkRemoveReadMessages->isChecked(); |
| 74 | orders.m_shrinkDatabase = m_ui->m_checkShrink->isEnabled() && m_ui->m_checkShrink->isChecked(); |
| 75 | orders.m_removeStarredMessages = m_ui->m_checkRemoveStarredMessages->isChecked(); |
| 76 | |
| 77 | emit purgeRequested(orders); |
| 78 | } |
| 79 | |
| 80 | void FormDatabaseCleanup::onPurgeStarted() { |
| 81 | m_ui->m_progressBar->setValue(0); |
| 82 | m_ui->m_btnBox->setEnabled(false); |
| 83 | m_ui->m_lblResult->setStatus(WidgetWithStatus::StatusType::Information, |
| 84 | tr("Database cleanup is running."), |
| 85 | tr("Database cleanup is running.")); |
| 86 | } |
| 87 | |
| 88 | void FormDatabaseCleanup::onPurgeProgress(int progress, const QString& description) { |
| 89 | m_ui->m_progressBar->setValue(progress); |
| 90 | m_ui->m_lblResult->setStatus(WidgetWithStatus::StatusType::Information, description, description); |
| 91 | } |
| 92 | |
| 93 | void FormDatabaseCleanup::onPurgeFinished(bool finished) { |
| 94 | m_ui->m_progressBar->setValue(100); |
| 95 | m_ui->m_btnBox->setEnabled(true); |
| 96 | |
| 97 | if (finished) { |
| 98 | m_ui->m_lblResult->setStatus(WidgetWithStatus::StatusType::Ok, |
| 99 | tr("Database cleanup is completed."), |
| 100 | tr("Database cleanup is completed.")); |
| 101 | } |
| 102 | else { |
| 103 | m_ui->m_lblResult->setStatus(WidgetWithStatus::StatusType::Error, |
| 104 | tr("Database cleanup failed."), |
| 105 | tr("Database cleanup failed.")); |
| 106 | } |
| 107 | |
| 108 | loadDatabaseInfo(); |
| 109 | } |
| 110 | |
| 111 | void FormDatabaseCleanup::loadDatabaseInfo() { |
| 112 | qint64 data_size = qApp->database()->driver()->databaseDataSize(); |
| 113 | QString data_size_str = data_size > 0 ? QString::number(data_size / 1000000.0) + QL1S(" MB") : tr("unknown"); |
| 114 | |
| 115 | m_ui->m_txtFileSize->setText(data_size_str); |
| 116 | m_ui->m_txtDatabaseType->setText(qApp->database()->driver()->humanDriverType()); |
| 117 | } |
| 118 | |
| 119 | void FormDatabaseCleanup::hideEvent(QHideEvent* event) { |
| 120 | QByteArray state = GuiUtilities::saveState(this); |
| 121 | |
| 122 | qApp->settings()->setValue(GROUP(GUI), objectName(), state); |
| 123 | QDialog::hideEvent(event); |
| 124 | } |