| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/settings/settingsdownloads.h" |
| 4 | |
| 5 | #include "gui/dialogs/filedialog.h" |
| 6 | #include "miscellaneous/application.h" |
| 7 | #include "miscellaneous/iconfactory.h" |
| 8 | #include "miscellaneous/settings.h" |
| 9 | #include "network-web/downloadmanager.h" |
| 10 | |
| 11 | SettingsDownloads::SettingsDownloads(Settings* settings, QWidget* parent) |
| 12 | : SettingsPanel(settings, parent), m_ui(new Ui::SettingsDownloads) { |
| 13 | m_ui->setupUi(this); |
| 14 | connect(m_ui->m_checkOpenManagerWhenDownloadStarts, &QCheckBox::toggled, this, &SettingsDownloads::dirtifySettings); |
| 15 | connect(m_ui->m_txtDownloadsTargetDirectory, &QLineEdit::textChanged, this, &SettingsDownloads::dirtifySettings); |
| 16 | connect(m_ui->m_rbDownloadsAskEachFile, &QRadioButton::toggled, this, &SettingsDownloads::dirtifySettings); |
| 17 | connect(m_ui->m_btnDownloadsTargetDirectory, |
| 18 | &QPushButton::clicked, |
| 19 | this, |
| 20 | &SettingsDownloads::selectDownloadsDirectory); |
| 21 | } |
| 22 | |
| 23 | SettingsDownloads::~SettingsDownloads() { |
| 24 | delete m_ui; |
| 25 | } |
| 26 | |
| 27 | QIcon SettingsDownloads::icon() const { |
| 28 | return qApp->icons()->fromTheme(QSL("browser-downloads"), QSL("download")); |
| 29 | } |
| 30 | |
| 31 | void SettingsDownloads::selectDownloadsDirectory() { |
| 32 | const QString target_directory = FileDialog::existingDirectory(this, |
| 33 | tr("Select downloads target directory"), |
| 34 | m_ui->m_txtDownloadsTargetDirectory->text()); |
| 35 | |
| 36 | if (!target_directory.isEmpty()) { |
| 37 | m_ui->m_txtDownloadsTargetDirectory->setText(QDir::toNativeSeparators(target_directory)); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void SettingsDownloads::loadSettings() { |
| 42 | onBeginLoadSettings(); |
| 43 | m_ui->m_checkOpenManagerWhenDownloadStarts |
| 44 | ->setChecked(settings()->value(GROUP(Downloads), SETTING(Downloads::ShowDownloadsWhenNewDownloadStarts)).toBool()); |
| 45 | m_ui->m_txtDownloadsTargetDirectory->setText(QDir::toNativeSeparators(settings() |
| 46 | ->value(GROUP(Downloads), |
| 47 | SETTING(Downloads::TargetDirectory)) |
| 48 | .toString())); |
| 49 | m_ui->m_rbDownloadsAskEachFile |
| 50 | ->setChecked(settings()->value(GROUP(Downloads), SETTING(Downloads::AlwaysPromptForFilename)).toBool()); |
| 51 | onEndLoadSettings(); |
| 52 | } |
| 53 | |
| 54 | void SettingsDownloads::saveSettings() { |
| 55 | onBeginSaveSettings(); |
| 56 | settings()->setValue(GROUP(Downloads), |
| 57 | Downloads::ShowDownloadsWhenNewDownloadStarts, |
| 58 | m_ui->m_checkOpenManagerWhenDownloadStarts->isChecked()); |
| 59 | settings()->setValue(GROUP(Downloads), Downloads::TargetDirectory, m_ui->m_txtDownloadsTargetDirectory->text()); |
| 60 | settings()->setValue(GROUP(Downloads), |
| 61 | Downloads::AlwaysPromptForFilename, |
| 62 | m_ui->m_rbDownloadsAskEachFile->isChecked()); |
| 63 | qApp->downloadManager()->setDownloadDirectory(m_ui->m_txtDownloadsTargetDirectory->text()); |
| 64 | onEndSaveSettings(); |
| 65 | } |