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);
55 connect(m_ui
->textCategoryName
, &QLineEdit::textChanged
, this, &TorrentCategoryDialog::categoryNameChanged
);
56 connect(m_ui
->comboUseDownloadPath
, &QComboBox::currentIndexChanged
, this, &TorrentCategoryDialog::useDownloadPathChanged
);
59 TorrentCategoryDialog::~TorrentCategoryDialog()
64 QString
TorrentCategoryDialog::createCategory(QWidget
*parent
, const QString
&parentCategoryName
)
66 using BitTorrent::CategoryOptions
;
67 using BitTorrent::Session
;
69 QString newCategoryName
= parentCategoryName
;
70 if (!newCategoryName
.isEmpty())
71 newCategoryName
+= u
'/';
72 newCategoryName
+= tr("New Category");
74 TorrentCategoryDialog dialog
{parent
};
75 dialog
.setCategoryName(newCategoryName
);
76 while (dialog
.exec() == TorrentCategoryDialog::Accepted
)
78 newCategoryName
= dialog
.categoryName();
80 if (!Session::isValidCategoryName(newCategoryName
))
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."));
88 else if (Session::instance()->categories().contains(newCategoryName
))
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."));
97 Session::instance()->addCategory(newCategoryName
, dialog
.categoryOptions());
98 return newCategoryName
;
105 void TorrentCategoryDialog::editCategory(QWidget
*parent
, const QString
&categoryName
)
107 using BitTorrent::Session
;
109 Q_ASSERT(Session::instance()->categories().contains(categoryName
));
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
]()
118 Session::instance()->editCategory(categoryName
, dialog
->categoryOptions());
123 void TorrentCategoryDialog::setCategoryNameEditable(bool editable
)
125 m_ui
->textCategoryName
->setEnabled(editable
);
128 QString
TorrentCategoryDialog::categoryName() const
130 return m_ui
->textCategoryName
->text();
133 void TorrentCategoryDialog::setCategoryName(const QString
&categoryName
)
135 m_ui
->textCategoryName
->setText(categoryName
);
137 const int subcategoryNameStart
= categoryName
.lastIndexOf(u
"/") + 1;
138 m_ui
->textCategoryName
->setSelection(subcategoryNameStart
, (categoryName
.size() - subcategoryNameStart
));
141 BitTorrent::CategoryOptions
TorrentCategoryDialog::categoryOptions() const
143 BitTorrent::CategoryOptions categoryOptions
;
144 categoryOptions
.savePath
= m_ui
->comboSavePath
->selectedPath();
145 if (m_ui
->comboUseDownloadPath
->currentIndex() == 1)
146 categoryOptions
.downloadPath
= {true, m_ui
->comboDownloadPath
->selectedPath()};
147 else if (m_ui
->comboUseDownloadPath
->currentIndex() == 2)
148 categoryOptions
.downloadPath
= {false, {}};
150 return categoryOptions
;
153 void TorrentCategoryDialog::setCategoryOptions(const BitTorrent::CategoryOptions
&categoryOptions
)
155 m_ui
->comboSavePath
->setSelectedPath(categoryOptions
.savePath
);
156 if (categoryOptions
.downloadPath
)
158 m_ui
->comboUseDownloadPath
->setCurrentIndex(categoryOptions
.downloadPath
->enabled
? 1 : 2);
159 m_ui
->comboDownloadPath
->setSelectedPath(categoryOptions
.downloadPath
->enabled
? categoryOptions
.downloadPath
->path
: Path());
163 m_ui
->comboUseDownloadPath
->setCurrentIndex(0);
164 m_ui
->comboDownloadPath
->setSelectedPath({});
168 void TorrentCategoryDialog::categoryNameChanged(const QString
&categoryName
)
170 const Path categoryPath
= Utils::Fs::toValidPath(categoryName
);
171 const auto *btSession
= BitTorrent::Session::instance();
172 m_ui
->comboSavePath
->setPlaceholder(btSession
->savePath() / categoryPath
);
174 const int index
= m_ui
->comboUseDownloadPath
->currentIndex();
175 const bool useDownloadPath
= (index
== 1) || ((index
== 0) && BitTorrent::Session::instance()->isDownloadPathEnabled());
177 m_ui
->comboDownloadPath
->setPlaceholder(btSession
->downloadPath() / categoryPath
);
179 m_ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setEnabled(!categoryName
.isEmpty());
182 void TorrentCategoryDialog::useDownloadPathChanged(const int index
)
184 if (const Path selectedPath
= m_ui
->comboDownloadPath
->selectedPath(); !selectedPath
.isEmpty())
185 m_lastEnteredDownloadPath
= selectedPath
;
187 m_ui
->labelDownloadPath
->setEnabled(index
== 1);
188 m_ui
->comboDownloadPath
->setEnabled(index
== 1);
189 m_ui
->comboDownloadPath
->setSelectedPath((index
== 1) ? m_lastEnteredDownloadPath
: Path());
191 const QString categoryName
= m_ui
->textCategoryName
->text();
192 const Path categoryPath
= BitTorrent::Session::instance()->downloadPath() / Utils::Fs::toValidPath(categoryName
);
193 const bool useDownloadPath
= (index
== 1) || ((index
== 0) && BitTorrent::Session::instance()->isDownloadPathEnabled());
194 m_ui
->comboDownloadPath
->setPlaceholder(useDownloadPath
? categoryPath
: Path());