2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2017, 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 "torrentcategorydialog.h"
31 #include <QMessageBox>
32 #include <QPushButton>
34 #include "base/bittorrent/session.h"
35 #include "base/utils/fs.h"
36 #include "ui_torrentcategorydialog.h"
38 TorrentCategoryDialog::TorrentCategoryDialog(QWidget
*parent
)
40 , m_ui
{new Ui::TorrentCategoryDialog
}
44 m_ui
->comboSavePath
->setMode(FileSystemPathEdit::Mode::DirectorySave
);
45 m_ui
->comboSavePath
->setDialogCaption(tr("Choose save path"));
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);
52 // disable save button
53 m_ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setEnabled(false);
54 connect(m_ui
->buttonBox
, &QDialogButtonBox::accepted
, this, &QDialog::accept
);
55 connect(m_ui
->buttonBox
, &QDialogButtonBox::rejected
, this, &QDialog::reject
);
57 connect(m_ui
->textCategoryName
, &QLineEdit::textChanged
, this, &TorrentCategoryDialog::categoryNameChanged
);
58 connect(m_ui
->comboUseDownloadPath
, &QComboBox::currentIndexChanged
, this, &TorrentCategoryDialog::useDownloadPathChanged
);
61 TorrentCategoryDialog::~TorrentCategoryDialog()
66 QString
TorrentCategoryDialog::createCategory(QWidget
*parent
, const QString
&parentCategoryName
)
68 using BitTorrent::CategoryOptions
;
69 using BitTorrent::Session
;
71 QString newCategoryName
= parentCategoryName
;
72 if (!newCategoryName
.isEmpty())
73 newCategoryName
+= u
'/';
74 newCategoryName
+= tr("New Category");
76 TorrentCategoryDialog dialog
{parent
};
77 dialog
.setCategoryName(newCategoryName
);
78 while (dialog
.exec() == TorrentCategoryDialog::Accepted
)
80 newCategoryName
= dialog
.categoryName();
82 if (!Session::isValidCategoryName(newCategoryName
))
84 QMessageBox::critical(
85 parent
, tr("Invalid category name")
86 , tr("Category name cannot contain '\\'.\n"
87 "Category name cannot start/end with '/'.\n"
88 "Category name cannot contain '//' sequence."));
90 else if (Session::instance()->categories().contains(newCategoryName
))
92 QMessageBox::critical(
93 parent
, tr("Category creation error")
94 , tr("Category with the given name already exists.\n"
95 "Please choose a different name and try again."));
99 Session::instance()->addCategory(newCategoryName
, dialog
.categoryOptions());
100 return newCategoryName
;
107 void TorrentCategoryDialog::editCategory(QWidget
*parent
, const QString
&categoryName
)
109 using BitTorrent::Session
;
111 Q_ASSERT(Session::instance()->categories().contains(categoryName
));
113 auto *dialog
= new TorrentCategoryDialog(parent
);
114 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
115 dialog
->setCategoryNameEditable(false);
116 dialog
->setCategoryName(categoryName
);
117 dialog
->setCategoryOptions(Session::instance()->categoryOptions(categoryName
));
118 connect(dialog
, &TorrentCategoryDialog::accepted
, parent
, [dialog
, categoryName
]()
120 Session::instance()->editCategory(categoryName
, dialog
->categoryOptions());
125 void TorrentCategoryDialog::setCategoryNameEditable(bool editable
)
127 m_ui
->textCategoryName
->setEnabled(editable
);
130 QString
TorrentCategoryDialog::categoryName() const
132 return m_ui
->textCategoryName
->text();
135 void TorrentCategoryDialog::setCategoryName(const QString
&categoryName
)
137 m_ui
->textCategoryName
->setText(categoryName
);
139 const int subcategoryNameStart
= categoryName
.lastIndexOf(u
"/") + 1;
140 m_ui
->textCategoryName
->setSelection(subcategoryNameStart
, (categoryName
.size() - subcategoryNameStart
));
143 BitTorrent::CategoryOptions
TorrentCategoryDialog::categoryOptions() const
145 BitTorrent::CategoryOptions categoryOptions
;
146 categoryOptions
.savePath
= m_ui
->comboSavePath
->selectedPath();
147 if (m_ui
->comboUseDownloadPath
->currentIndex() == 1)
148 categoryOptions
.downloadPath
= {true, m_ui
->comboDownloadPath
->selectedPath()};
149 else if (m_ui
->comboUseDownloadPath
->currentIndex() == 2)
150 categoryOptions
.downloadPath
= {false, {}};
152 return categoryOptions
;
155 void TorrentCategoryDialog::setCategoryOptions(const BitTorrent::CategoryOptions
&categoryOptions
)
157 m_ui
->comboSavePath
->setSelectedPath(categoryOptions
.savePath
);
158 if (categoryOptions
.downloadPath
)
160 m_ui
->comboUseDownloadPath
->setCurrentIndex(categoryOptions
.downloadPath
->enabled
? 1 : 2);
161 m_ui
->comboDownloadPath
->setSelectedPath(categoryOptions
.downloadPath
->enabled
? categoryOptions
.downloadPath
->path
: Path());
165 m_ui
->comboUseDownloadPath
->setCurrentIndex(0);
166 m_ui
->comboDownloadPath
->setSelectedPath({});
170 void TorrentCategoryDialog::categoryNameChanged(const QString
&categoryName
)
172 const Path categoryPath
= Utils::Fs::toValidPath(categoryName
);
173 const auto *btSession
= BitTorrent::Session::instance();
174 m_ui
->comboSavePath
->setPlaceholder(btSession
->savePath() / categoryPath
);
176 const int index
= m_ui
->comboUseDownloadPath
->currentIndex();
177 const bool useDownloadPath
= (index
== 1) || ((index
== 0) && BitTorrent::Session::instance()->isDownloadPathEnabled());
179 m_ui
->comboDownloadPath
->setPlaceholder(btSession
->downloadPath() / categoryPath
);
181 m_ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setEnabled(!categoryName
.isEmpty());
184 void TorrentCategoryDialog::useDownloadPathChanged(const int index
)
186 if (const Path selectedPath
= m_ui
->comboDownloadPath
->selectedPath(); !selectedPath
.isEmpty())
187 m_lastEnteredDownloadPath
= selectedPath
;
189 m_ui
->labelDownloadPath
->setEnabled(index
== 1);
190 m_ui
->comboDownloadPath
->setEnabled(index
== 1);
191 m_ui
->comboDownloadPath
->setSelectedPath((index
== 1) ? m_lastEnteredDownloadPath
: Path());
193 const QString categoryName
= m_ui
->textCategoryName
->text();
194 const Path categoryPath
= BitTorrent::Session::instance()->downloadPath() / Utils::Fs::toValidPath(categoryName
);
195 const bool useDownloadPath
= (index
== 1) || ((index
== 0) && BitTorrent::Session::instance()->isDownloadPathEnabled());
196 m_ui
->comboDownloadPath
->setPlaceholder(useDownloadPath
? categoryPath
: Path());