qBittorrent
torrentcategorydialog.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2017, 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 
29 #include "torrentcategorydialog.h"
30 
31 #include <QMessageBox>
32 #include <QPushButton>
33 
35 #include "base/utils/fs.h"
36 #include "ui_torrentcategorydialog.h"
37 
39  : QDialog {parent}
40  , m_ui {new Ui::TorrentCategoryDialog}
41 {
42  m_ui->setupUi(this);
43 
44  m_ui->comboSavePath->setMode(FileSystemPathEdit::Mode::DirectorySave);
45  m_ui->comboSavePath->setDialogCaption(tr("Choose save path"));
46 
47  m_ui->comboDownloadPath->setMode(FileSystemPathEdit::Mode::DirectorySave);
48  m_ui->comboDownloadPath->setDialogCaption(tr("Choose download path"));
49  m_ui->comboDownloadPath->setEnabled(false);
50  m_ui->labelDownloadPath->setEnabled(false);
51 
52  // disable save button
53  m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
54 
55  connect(m_ui->textCategoryName, &QLineEdit::textChanged, this, &TorrentCategoryDialog::categoryNameChanged);
56  connect(m_ui->comboUseDownloadPath, &QComboBox::currentIndexChanged, this, &TorrentCategoryDialog::useDownloadPathChanged);
57 }
58 
60 {
61  delete m_ui;
62 }
63 
64 QString TorrentCategoryDialog::createCategory(QWidget *parent, const QString &parentCategoryName)
65 {
67  using BitTorrent::Session;
68 
69  QString newCategoryName = parentCategoryName;
70  if (!newCategoryName.isEmpty())
71  newCategoryName += QLatin1Char('/');
72  newCategoryName += tr("New Category");
73 
74  TorrentCategoryDialog dialog {parent};
75  dialog.setCategoryName(newCategoryName);
76  while (dialog.exec() == TorrentCategoryDialog::Accepted)
77  {
78  newCategoryName = dialog.categoryName();
79 
80  if (!Session::isValidCategoryName(newCategoryName))
81  {
82  QMessageBox::critical(
83  parent, tr("Invalid category name")
84  , tr("Category name cannot contain '\\'.\n"
85  "Category name cannot start/end with '/'.\n"
86  "Category name cannot contain '//' sequence."));
87  }
88  else if (Session::instance()->categories().contains(newCategoryName))
89  {
90  QMessageBox::critical(
91  parent, tr("Category creation error")
92  , tr("Category with the given name already exists.\n"
93  "Please choose a different name and try again."));
94  }
95  else
96  {
97  Session::instance()->addCategory(newCategoryName, dialog.categoryOptions());
98  return newCategoryName;
99  }
100  }
101 
102  return {};
103 }
104 
105 void TorrentCategoryDialog::editCategory(QWidget *parent, const QString &categoryName)
106 {
107  using BitTorrent::Session;
108 
109  Q_ASSERT(Session::instance()->categories().contains(categoryName));
110 
111  auto dialog = new TorrentCategoryDialog(parent);
112  dialog->setAttribute(Qt::WA_DeleteOnClose);
113  dialog->setCategoryNameEditable(false);
114  dialog->setCategoryName(categoryName);
115  dialog->setCategoryOptions(Session::instance()->categoryOptions(categoryName));
116  connect(dialog, &TorrentCategoryDialog::accepted, parent, [dialog, categoryName]()
117  {
118  Session::instance()->editCategory(categoryName, dialog->categoryOptions());
119  });
120  dialog->open();
121 }
122 
124 {
125  m_ui->textCategoryName->setEnabled(editable);
126 }
127 
129 {
130  return m_ui->textCategoryName->text();
131 }
132 
133 void TorrentCategoryDialog::setCategoryName(const QString &categoryName)
134 {
135  m_ui->textCategoryName->setText(categoryName);
136 }
137 
139 {
141  categoryOptions.savePath = m_ui->comboSavePath->selectedPath();
142  if (m_ui->comboUseDownloadPath->currentIndex() == 1)
143  categoryOptions.downloadPath = {true, m_ui->comboDownloadPath->selectedPath()};
144  else if (m_ui->comboUseDownloadPath->currentIndex() == 2)
145  categoryOptions.downloadPath = {false, {}};
146 
147  return categoryOptions;
148 }
149 
151 {
152  m_ui->comboSavePath->setSelectedPath(categoryOptions.savePath);
154  {
155  m_ui->comboUseDownloadPath->setCurrentIndex(categoryOptions.downloadPath->enabled ? 1 : 2);
156  m_ui->comboDownloadPath->setSelectedPath(categoryOptions.downloadPath->enabled ? categoryOptions.downloadPath->path : QString());
157  }
158  else
159  {
160  m_ui->comboUseDownloadPath->setCurrentIndex(0);
161  m_ui->comboDownloadPath->setSelectedPath({});
162  }
163 }
164 
165 void TorrentCategoryDialog::categoryNameChanged(const QString &categoryName)
166 {
167  const QString categoryPath = Utils::Fs::toValidFileSystemName(categoryName, true);
168  const auto *btSession = BitTorrent::Session::instance();
169  m_ui->comboSavePath->setPlaceholder(Utils::Fs::resolvePath(categoryPath, btSession->savePath()));
170 
171  const int index = m_ui->comboUseDownloadPath->currentIndex();
172  const bool useDownloadPath = (index == 1) || ((index == 0) && BitTorrent::Session::instance()->isDownloadPathEnabled());
173  if (useDownloadPath)
174  m_ui->comboDownloadPath->setPlaceholder(Utils::Fs::resolvePath(categoryPath, btSession->downloadPath()));
175 
176  m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!categoryName.isEmpty());
177 }
178 
180 {
181  if (const QString selectedPath = m_ui->comboDownloadPath->selectedPath(); !selectedPath.isEmpty())
182  m_lastEnteredDownloadPath = selectedPath;
183 
184  m_ui->labelDownloadPath->setEnabled(index == 1);
185  m_ui->comboDownloadPath->setEnabled(index == 1);
186  m_ui->comboDownloadPath->setSelectedPath((index == 1) ? m_lastEnteredDownloadPath : QString());
187 
188  const QString categoryName = m_ui->textCategoryName->text();
189  const QString categoryPath = Utils::Fs::resolvePath(Utils::Fs::toValidFileSystemName(categoryName, true)
190  , BitTorrent::Session::instance()->downloadPath());
191  const bool useDownloadPath = (index == 1) || ((index == 0) && BitTorrent::Session::instance()->isDownloadPathEnabled());
192  m_ui->comboDownloadPath->setPlaceholder(useDownloadPath ? categoryPath : QString());
193 }
static Session * instance()
Definition: session.cpp:997
bool isDownloadPathEnabled() const
Definition: session.cpp:541
@ DirectorySave
selecting directories for saving
Ui::TorrentCategoryDialog * m_ui
static void editCategory(QWidget *parent, const QString &categoryName)
void categoryNameChanged(const QString &categoryName)
void setCategoryOptions(const BitTorrent::CategoryOptions &categoryOptions)
BitTorrent::CategoryOptions categoryOptions() const
void setCategoryName(const QString &categoryName)
TorrentCategoryDialog(QWidget *parent=nullptr)
void setCategoryNameEditable(bool editable)
static QString createCategory(QWidget *parent, const QString &parentCategoryName={})
void useDownloadPathChanged(int index)
QString toValidFileSystemName(const QString &name, bool allowSeparators=false, const QString &pad=QLatin1String(" "))
Definition: fs.cpp:237
QString resolvePath(const QString &relativePath, const QString &basePath)
Definition: fs.cpp:74
std::optional< DownloadPathOption > downloadPath