Combine all the column filter related widgets
[qBittorrent.git] / src / gui / watchedfolderoptionsdialog.cpp
blob30b513c68ee20e6d4e8b43b2dcbd50a2271041e8
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2021 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "watchedfolderoptionsdialog.h"
31 #include <QDir>
32 #include <QPushButton>
34 #include "base/bittorrent/session.h"
35 #include "base/global.h"
36 #include "base/utils/fs.h"
37 #include "ui_watchedfolderoptionsdialog.h"
38 #include "utils.h"
40 #define SETTINGS_KEY(name) u"WatchedFolderOptionsDialog/" name
42 WatchedFolderOptionsDialog::WatchedFolderOptionsDialog(
43 const TorrentFilesWatcher::WatchedFolderOptions &watchedFolderOptions, QWidget *parent)
44 : QDialog {parent}
45 , m_ui {new Ui::WatchedFolderOptionsDialog}
46 , m_savePath {watchedFolderOptions.addTorrentParams.savePath}
47 , m_downloadPath {watchedFolderOptions.addTorrentParams.downloadPath}
48 , m_storeDialogSize {SETTINGS_KEY(u"DialogSize"_qs)}
50 m_ui->setupUi(this);
52 m_ui->savePath->setMode(FileSystemPathEdit::Mode::DirectorySave);
53 m_ui->savePath->setDialogCaption(tr("Choose save path"));
55 m_ui->downloadPath->setMode(FileSystemPathEdit::Mode::DirectorySave);
56 m_ui->downloadPath->setDialogCaption(tr("Choose save path"));
58 const auto *session = BitTorrent::Session::instance();
59 m_useDownloadPath = watchedFolderOptions.addTorrentParams.useDownloadPath.value_or(session->isDownloadPathEnabled());
61 connect(m_ui->comboTTM, qOverload<int>(&QComboBox::currentIndexChanged), this, &WatchedFolderOptionsDialog::onTMMChanged);
62 connect(m_ui->categoryComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &WatchedFolderOptionsDialog::onCategoryChanged);
64 m_ui->checkBoxRecursive->setChecked(watchedFolderOptions.recursive);
65 populateSavePaths();
67 const BitTorrent::AddTorrentParams &torrentParams = watchedFolderOptions.addTorrentParams;
68 m_ui->addToQueueTopCheckBox->setChecked(torrentParams.addToQueueTop.value_or(session->isAddTorrentToQueueTop()));
69 m_ui->startTorrentCheckBox->setChecked(!torrentParams.addPaused.value_or(session->isAddTorrentPaused()));
70 m_ui->skipCheckingCheckBox->setChecked(torrentParams.skipChecking);
71 m_ui->comboTTM->setCurrentIndex(torrentParams.useAutoTMM.value_or(!session->isAutoTMMDisabledByDefault()));
72 m_ui->contentLayoutComboBox->setCurrentIndex(
73 static_cast<int>(torrentParams.contentLayout.value_or(session->torrentContentLayout())));
75 // Load categories
76 QStringList categories = session->categories();
77 std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
79 if (!torrentParams.category.isEmpty())
80 m_ui->categoryComboBox->addItem(torrentParams.category);
81 m_ui->categoryComboBox->addItem(u""_qs);
83 for (const QString &category : asConst(categories))
85 if (category != torrentParams.category)
86 m_ui->categoryComboBox->addItem(category);
89 loadState();
91 // Default focus
92 if (m_ui->comboTTM->currentIndex() == 0) // 0 is Manual mode
93 m_ui->savePath->setFocus();
94 else
95 m_ui->categoryComboBox->setFocus();
98 WatchedFolderOptionsDialog::~WatchedFolderOptionsDialog()
100 saveState();
101 delete m_ui;
104 TorrentFilesWatcher::WatchedFolderOptions WatchedFolderOptionsDialog::watchedFolderOptions() const
106 TorrentFilesWatcher::WatchedFolderOptions watchedFolderOptions;
107 watchedFolderOptions.recursive = m_ui->checkBoxRecursive->isChecked();
109 BitTorrent::AddTorrentParams &params = watchedFolderOptions.addTorrentParams;
110 const bool useAutoTMM = (m_ui->comboTTM->currentIndex() == 1);
111 if (!useAutoTMM)
113 params.savePath = m_ui->savePath->selectedPath();
114 params.useDownloadPath = m_ui->groupBoxDownloadPath->isChecked();
115 if (params.useDownloadPath)
116 params.downloadPath = m_ui->downloadPath->selectedPath();
118 params.useAutoTMM = useAutoTMM;
119 params.category = m_ui->categoryComboBox->currentText();
120 params.addToQueueTop = m_ui->addToQueueTopCheckBox->isChecked();
121 params.addPaused = !m_ui->startTorrentCheckBox->isChecked();
122 params.skipChecking = m_ui->skipCheckingCheckBox->isChecked();
123 params.contentLayout = static_cast<BitTorrent::TorrentContentLayout>(m_ui->contentLayoutComboBox->currentIndex());
125 return watchedFolderOptions;
128 void WatchedFolderOptionsDialog::loadState()
130 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
131 resize(dialogSize);
134 void WatchedFolderOptionsDialog::saveState()
136 m_storeDialogSize = size();
139 void WatchedFolderOptionsDialog::onCategoryChanged(const int index)
141 Q_UNUSED(index);
143 if (m_ui->comboTTM->currentIndex() == 1)
145 const auto *btSession = BitTorrent::Session::instance();
146 const QString categoryName = m_ui->categoryComboBox->currentText();
148 const Path savePath = btSession->categorySavePath(categoryName);
149 m_ui->savePath->setSelectedPath(savePath);
151 const Path downloadPath = btSession->categoryDownloadPath(categoryName);
152 m_ui->downloadPath->setSelectedPath(downloadPath);
154 m_ui->groupBoxDownloadPath->setChecked(!downloadPath.isEmpty());
158 void WatchedFolderOptionsDialog::populateSavePaths()
160 const auto *btSession = BitTorrent::Session::instance();
162 const Path defaultSavePath {btSession->savePath()};
163 m_ui->savePath->setSelectedPath(!m_savePath.isEmpty() ? m_savePath : defaultSavePath);
165 const Path defaultDownloadPath {btSession->downloadPath()};
166 m_ui->downloadPath->setSelectedPath(!m_downloadPath.isEmpty() ? m_downloadPath : defaultDownloadPath);
168 m_ui->groupBoxDownloadPath->setChecked(m_useDownloadPath);
171 void WatchedFolderOptionsDialog::onTMMChanged(const int index)
173 if (index != 1)
174 { // 0 is Manual mode and 1 is Automatic mode. Handle all non 1 values as manual mode.
175 populateSavePaths();
176 m_ui->groupBoxSavePath->setEnabled(true);
177 m_ui->savePath->blockSignals(false);
178 m_ui->downloadPath->blockSignals(false);
180 else
182 m_ui->groupBoxSavePath->setEnabled(false);
184 const auto *btSession = BitTorrent::Session::instance();
186 m_ui->savePath->blockSignals(true);
187 m_savePath = m_ui->savePath->selectedPath();
188 const Path savePath = btSession->categorySavePath(m_ui->categoryComboBox->currentText());
189 m_ui->savePath->setSelectedPath(savePath);
191 m_ui->downloadPath->blockSignals(true);
192 m_downloadPath = m_ui->downloadPath->selectedPath();
193 const Path downloadPath = btSession->categoryDownloadPath(m_ui->categoryComboBox->currentText());
194 m_ui->downloadPath->setSelectedPath(downloadPath);
196 m_useDownloadPath = m_ui->groupBoxDownloadPath->isChecked();
197 m_ui->groupBoxDownloadPath->setChecked(!downloadPath.isEmpty());