File indexing completed on 2024-05-19 05:06:49

0001 /*
0002     SPDX-FileCopyrightText: 2001-2003 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2001-2002 Felix Rodriguez <frodriguez@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2017 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0005     SPDX-FileCopyrightText: 2020 Thomas Baumgart <tbaumgart@kde.org>
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "kbackupdlg.h"
0010 
0011 // ----------------------------------------------------------------------------
0012 // QT Includes
0013 
0014 #include <QCheckBox>
0015 #include <QUrl>
0016 #include <QPushButton>
0017 #include <QIcon>
0018 #include <QFileDialog>
0019 
0020 // ----------------------------------------------------------------------------
0021 // KDE Includes
0022 
0023 #include <KConfig>
0024 #include <KConfigGroup>
0025 #include <KLocalizedString>
0026 #include <KSharedConfig>
0027 
0028 // ----------------------------------------------------------------------------
0029 // Project Includes
0030 
0031 #include "ui_kbackupdlg.h"
0032 
0033 #include "icons/icons.h"
0034 
0035 using namespace Icons;
0036 
0037 KBackupDlg::KBackupDlg(QWidget* parent) :
0038     QDialog(parent),
0039     ui(new Ui::KBackupDlg)
0040 {
0041     ui->setupUi(this);
0042     readConfig();
0043 
0044 #ifdef Q_OS_WIN
0045     // in windows we don't have a mount capability so
0046     // we hide that option from the user and deactivate it
0047     ui->mountCheckBox->setChecked(false);
0048     ui->mountCheckBox->hide();
0049     ui->txtAutoMount->hide();
0050     ui->lblMountPoint->setText(i18n("Backup location:"));
0051 #endif
0052 
0053     ui->chooseButton->setIcon(Icons::get(Icon::Folder));
0054 
0055     connect(ui->chooseButton, &QAbstractButton::clicked, this, &KBackupDlg::chooseButtonClicked);
0056 }
0057 
0058 KBackupDlg::~KBackupDlg()
0059 {
0060     writeConfig();
0061     delete ui;
0062 }
0063 
0064 QString KBackupDlg::mountPoint() const
0065 {
0066     return ui->txtMountPoint->text();
0067 }
0068 
0069 bool KBackupDlg::mountCheckBoxChecked() const
0070 {
0071     return ui->mountCheckBox->isChecked();
0072 }
0073 
0074 void KBackupDlg::chooseButtonClicked()
0075 {
0076     QUrl documentsLocation = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
0077     auto newDir = QFileDialog::getExistingDirectoryUrl(this, QString(), documentsLocation);
0078     if (!newDir.path().isEmpty())
0079         ui->txtMountPoint->setText(newDir.path());
0080 }
0081 
0082 void KBackupDlg::readConfig()
0083 {
0084     QString backupDefaultLocation;
0085 #ifdef Q_OS_WIN
0086     backupDefaultLocation = QDir::toNativeSeparators(QDir::homePath() + "/kmymoney/backup");
0087 #else
0088     backupDefaultLocation = "/mnt/floppy";
0089 #endif
0090     KSharedConfigPtr config = KSharedConfig::openConfig();
0091     KConfigGroup grp = config->group("Last Use Settings");
0092     ui->mountCheckBox->setChecked(grp.readEntry("KBackupDlg_mountDevice", false));
0093     ui->txtMountPoint->setText(grp.readEntry("KBackupDlg_BackupMountPoint", backupDefaultLocation));
0094 }
0095 
0096 void KBackupDlg::writeConfig()
0097 {
0098     KSharedConfigPtr config = KSharedConfig::openConfig();
0099     KConfigGroup grp = config->group("Last Use Settings");
0100     grp.writeEntry("KBackupDlg_mountDevice", ui->mountCheckBox->isChecked());
0101     grp.writeEntry("KBackupDlg_BackupMountPoint", ui->txtMountPoint->text());
0102     config->sync();
0103 }