| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/dialogs/formbackupdatabasesettings.h" |
| 4 | |
| 5 | #include "exceptions/applicationexception.h" |
| 6 | #include "gui/dialogs/filedialog.h" |
| 7 | #include "gui/guiutilities.h" |
| 8 | #include "miscellaneous/application.h" |
| 9 | #include "miscellaneous/iconfactory.h" |
| 10 | #include "miscellaneous/settings.h" |
| 11 | |
| 12 | #include <QCheckBox> |
| 13 | #include <QDateTime> |
| 14 | #include <QDialogButtonBox> |
| 15 | #include <QPushButton> |
| 16 | |
| 17 | FormBackupDatabaseSettings::FormBackupDatabaseSettings(QWidget* parent) |
| 18 | : QDialog(parent), m_ui(new Ui::FormBackupDatabaseSettings) { |
| 19 | m_ui->setupUi(this); |
| 20 | m_ui->m_txtBackupName->lineEdit()->setPlaceholderText(tr("Common name for backup files")); |
| 21 | |
| 22 | GuiUtilities::applyDialogProperties(*this, qApp->icons()->fromTheme(QSL("document-export"))); |
| 23 | |
| 24 | connect(m_ui->m_checkBackupDatabase, &QCheckBox::toggled, this, &FormBackupDatabaseSettings::checkOkButton); |
| 25 | connect(m_ui->m_checkBackupSettings, &QCheckBox::toggled, this, &FormBackupDatabaseSettings::checkOkButton); |
| 26 | connect(m_ui->m_buttonBox->button(QDialogButtonBox::StandardButton::Ok), |
| 27 | &QPushButton::clicked, |
| 28 | this, |
| 29 | &FormBackupDatabaseSettings::performBackup); |
| 30 | connect(m_ui->m_txtBackupName->lineEdit(), |
| 31 | &BaseLineEdit::textChanged, |
| 32 | this, |
| 33 | &FormBackupDatabaseSettings::checkBackupNames); |
| 34 | connect(m_ui->m_txtBackupName->lineEdit(), |
| 35 | &BaseLineEdit::textChanged, |
| 36 | this, |
| 37 | &FormBackupDatabaseSettings::checkOkButton); |
| 38 | connect(m_ui->m_btnSelectFolder, &QPushButton::clicked, this, &FormBackupDatabaseSettings::selectFolderInitial); |
| 39 | selectFolder(FileDialog::storedFolder(GENERAL_REMEMBERED_PATH, qApp->documentsFolder())); |
| 40 | m_ui->m_txtBackupName->lineEdit()->setText(QSL(APP_LOW_NAME) + QL1S("_") + |
| 41 | QDateTime::currentDateTime().toString(QSL("yyyyMMddHHmm"))); |
| 42 | m_ui->m_lblResult->setStatus(WidgetWithStatus::StatusType::Warning, |
| 43 | tr("No operation executed yet."), |
| 44 | tr("No operation executed yet.")); |
| 45 | |
| 46 | if (qApp->database()->activeDatabaseDriver() != DatabaseDriver::DriverType::SQLite) { |
| 47 | m_ui->m_checkBackupDatabase->setDisabled(true); |
| 48 | } |
| 49 | |
| 50 | GuiUtilities::restoreState(this, qApp->settings()->value(GROUP(GUI), objectName(), QByteArray()).toByteArray()); |
| 51 | } |
| 52 | |
| 53 | FormBackupDatabaseSettings::~FormBackupDatabaseSettings() { |
| 54 | qDebugNN << LOGSEC_GUI << "Destroying FormBackupDatabaseSettings instance."; |
| 55 | } |
| 56 | |
| 57 | void FormBackupDatabaseSettings::performBackup() { |
| 58 | try { |
| 59 | qApp->backupDatabaseSettings(m_ui->m_checkBackupDatabase->isChecked(), |
| 60 | m_ui->m_checkBackupSettings->isChecked(), |
| 61 | m_ui->m_lblSelectFolder->label()->text(), |
| 62 | m_ui->m_txtBackupName->lineEdit()->text()); |
| 63 | m_ui->m_lblResult->setStatus(WidgetWithStatus::StatusType::Ok, |
| 64 | tr("Backup was created successfully and stored in target directory."), |
| 65 | tr("Backup was created successfully.")); |
| 66 | } |
| 67 | catch (const ApplicationException& ex) { |
| 68 | m_ui->m_lblResult->setStatus(WidgetWithStatus::StatusType::Error, ex.message(), tr("Backup failed.")); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void FormBackupDatabaseSettings::selectFolderInitial() { |
| 73 | selectFolder(); |
| 74 | } |
| 75 | |
| 76 | void FormBackupDatabaseSettings::selectFolder(QString path) { |
| 77 | if (path.isEmpty()) { |
| 78 | path = FileDialog::existingDirectory(this, |
| 79 | tr("Select destination directory"), |
| 80 | m_ui->m_lblSelectFolder->label()->text(), |
| 81 | GENERAL_REMEMBERED_PATH); |
| 82 | } |
| 83 | |
| 84 | if (!path.isEmpty()) { |
| 85 | m_ui->m_lblSelectFolder->setStatus(WidgetWithStatus::StatusType::Ok, |
| 86 | QDir::toNativeSeparators(path), |
| 87 | tr("Good destination directory is specified.")); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void FormBackupDatabaseSettings::checkBackupNames(const QString& name) { |
| 92 | if (name.simplified().isEmpty()) { |
| 93 | m_ui->m_txtBackupName->setStatus(WidgetWithStatus::StatusType::Error, tr("Backup name cannot be empty.")); |
| 94 | } |
| 95 | else { |
| 96 | m_ui->m_txtBackupName->setStatus(WidgetWithStatus::StatusType::Ok, tr("Backup name looks okay.")); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void FormBackupDatabaseSettings::checkOkButton() { |
| 101 | m_ui->m_buttonBox->button(QDialogButtonBox::StandardButton::Ok) |
| 102 | ->setDisabled(m_ui->m_txtBackupName->lineEdit()->text().simplified().isEmpty() || |
| 103 | m_ui->m_lblSelectFolder->label()->text().simplified().isEmpty() || |
| 104 | (!m_ui->m_checkBackupDatabase->isChecked() && !m_ui->m_checkBackupSettings->isChecked())); |
| 105 | } |
| 106 | |
| 107 | void FormBackupDatabaseSettings::hideEvent(QHideEvent* event) { |
| 108 | QByteArray state = GuiUtilities::saveState(this); |
| 109 | |
| 110 | qApp->settings()->setValue(GROUP(GUI), objectName(), state); |
| 111 | QDialog::hideEvent(event); |
| 112 | } |