qBittorrent
watchedfolderoptionsdialog.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2021 Vladimir Golovnev <glassez@yandex.ru>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
28 
30 
31 #include <QDir>
32 #include <QPushButton>
33 
35 #include "base/global.h"
36 #include "base/utils/fs.h"
37 #include "ui_watchedfolderoptionsdialog.h"
38 #include "utils.h"
39 
40 #define SETTINGS_KEY(name) "WatchedFolderOptionsDialog/" name
41 
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("DialogSize")}
49 {
50  m_ui->setupUi(this);
51 
53  m_ui->savePath->setDialogCaption(tr("Choose save path"));
54 
55  const auto *session = BitTorrent::Session::instance();
56 
57  m_ui->downloadPath->setMode(FileSystemPathEdit::Mode::DirectorySave);
58  m_ui->downloadPath->setDialogCaption(tr("Choose save path"));
59  m_ui->groupBoxDownloadPath->setChecked(watchedFolderOptions.addTorrentParams.useDownloadPath.value_or(session->isDownloadPathEnabled()));
60 
61  connect(m_ui->comboTTM, qOverload<int>(&QComboBox::currentIndexChanged), this, &WatchedFolderOptionsDialog::onTMMChanged);
62  connect(m_ui->categoryComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &WatchedFolderOptionsDialog::onCategoryChanged);
63 
64  m_ui->checkBoxRecursive->setChecked(watchedFolderOptions.recursive);
66 
68  m_ui->startTorrentCheckBox->setChecked(!torrentParams.addPaused.value_or(session->isAddTorrentPaused()));
69  m_ui->skipCheckingCheckBox->setChecked(torrentParams.skipChecking);
70  m_ui->comboTTM->setCurrentIndex(torrentParams.useAutoTMM.value_or(!session->isAutoTMMDisabledByDefault()));
71  m_ui->contentLayoutComboBox->setCurrentIndex(
72  static_cast<int>(torrentParams.contentLayout.value_or(session->torrentContentLayout())));
73 
74  // Load categories
75  QStringList categories = session->categories();
76  std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
77 
78  if (!torrentParams.category.isEmpty())
79  m_ui->categoryComboBox->addItem(torrentParams.category);
80  m_ui->categoryComboBox->addItem("");
81 
82  for (const QString &category : asConst(categories))
83  {
84  if (category != torrentParams.category)
85  m_ui->categoryComboBox->addItem(category);
86  }
87 
88  loadState();
89 
90  m_ui->buttonBox->button(QDialogButtonBox::Ok)->setFocus();
91 }
92 
94 {
95  saveState();
96  delete m_ui;
97 }
98 
100 {
102  watchedFolderOptions.recursive = m_ui->checkBoxRecursive->isChecked();
103 
105  const bool useAutoTMM = (m_ui->comboTTM->currentIndex() == 1);
106  if (!useAutoTMM)
107  {
108  params.savePath = m_ui->savePath->selectedPath();
109  params.useDownloadPath = m_ui->groupBoxDownloadPath->isChecked();
110  if (params.useDownloadPath)
111  params.downloadPath = m_ui->downloadPath->selectedPath();
112  }
113  params.useAutoTMM = useAutoTMM;
114  params.category = m_ui->categoryComboBox->currentText();
115  params.addPaused = !m_ui->startTorrentCheckBox->isChecked();
116  params.skipChecking = m_ui->skipCheckingCheckBox->isChecked();
117  params.contentLayout = static_cast<BitTorrent::TorrentContentLayout>(m_ui->contentLayoutComboBox->currentIndex());
118 
119  return watchedFolderOptions;
120 }
121 
123 {
125 }
126 
128 {
129  m_storeDialogSize = size();
130 }
131 
133 {
134  Q_UNUSED(index);
135 
136  if (m_ui->comboTTM->currentIndex() == 1)
137  {
138  const auto *btSession = BitTorrent::Session::instance();
139  const QString categoryName = m_ui->categoryComboBox->currentText();
140 
141  const QString savePath = btSession->categorySavePath(categoryName);
142  m_ui->savePath->setSelectedPath(Utils::Fs::toNativePath(savePath));
143 
144  const QString finishedSavePath = btSession->categoryDownloadPath(categoryName);
145  m_ui->downloadPath->setSelectedPath(Utils::Fs::toNativePath(finishedSavePath));
146 
147  m_ui->groupBoxDownloadPath->setChecked(!finishedSavePath.isEmpty());
148  }
149 }
150 
152 {
153  const auto *btSession = BitTorrent::Session::instance();
154 
155  const QString defaultSavePath {btSession->savePath()};
156  m_ui->savePath->setSelectedPath(!m_savePath.isEmpty() ? m_savePath : defaultSavePath);
157 
158  const QString defaultFinishedSavePath {btSession->downloadPath()};
159  m_ui->downloadPath->setSelectedPath(!m_downloadPath.isEmpty() ? m_downloadPath : defaultFinishedSavePath);
160 
161  m_ui->groupBoxDownloadPath->setChecked(m_useDownloadPath);
162 }
163 
165 {
166  if (index != 1)
167  { // 0 is Manual mode and 1 is Automatic mode. Handle all non 1 values as manual mode.
169  m_ui->groupBoxSavePath->setEnabled(true);
170  m_ui->savePath->blockSignals(false);
171  m_ui->downloadPath->blockSignals(false);
172  }
173  else
174  {
175  m_ui->groupBoxSavePath->setEnabled(false);
176 
177  const auto *btSession = BitTorrent::Session::instance();
178 
179  m_ui->savePath->blockSignals(true);
180  m_savePath = m_ui->savePath->selectedPath();
181  const QString savePath = btSession->categorySavePath(m_ui->categoryComboBox->currentText());
182  m_ui->savePath->setSelectedPath(savePath);
183 
184  m_ui->downloadPath->blockSignals(true);
185  m_downloadPath = m_ui->downloadPath->selectedPath();
186  const QString finishedSavePath = btSession->categoryDownloadPath(m_ui->categoryComboBox->currentText());
187  m_ui->downloadPath->setSelectedPath(finishedSavePath);
188 
189  m_useDownloadPath = m_ui->groupBoxDownloadPath->isChecked();
190  m_ui->groupBoxDownloadPath->setChecked(!finishedSavePath.isEmpty());
191  }
192 }
static Session * instance()
Definition: session.cpp:997
@ DirectorySave
selecting directories for saving
Ui::WatchedFolderOptionsDialog * m_ui
SettingValue< QSize > m_storeDialogSize
TorrentFilesWatcher::WatchedFolderOptions watchedFolderOptions() const
WatchedFolderOptionsDialog(const TorrentFilesWatcher::WatchedFolderOptions &watchedFolderOptions, QWidget *parent)
constexpr std::add_const_t< T > & asConst(T &t) noexcept
Definition: global.h:42
QString toNativePath(const QString &path)
Definition: fs.cpp:64
void resize(QWidget *widget, const QSize &newSize={})
Definition: utils.cpp:54
std::optional< bool > useDownloadPath
std::optional< bool > useAutoTMM
std::optional< bool > addPaused
std::optional< BitTorrent::TorrentContentLayout > contentLayout
BitTorrent::AddTorrentParams addTorrentParams
#define SETTINGS_KEY(name)