2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2024 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 "torrentsharelimitswidget.h"
31 #include "base/bittorrent/torrent.h"
32 #include "ui_torrentsharelimitswidget.h"
36 enum ShareLimitModeIndex
38 UninitializedModeIndex
= -1,
44 enum ShareLimitActionIndex
46 UninitializedActionIndex
= -1,
50 RemoveWithContentActionIndex
,
51 SuperSeedingActionIndex
55 TorrentShareLimitsWidget::TorrentShareLimitsWidget(QWidget
*parent
)
57 , m_ui
{new Ui::TorrentShareLimitsWidget
}
61 m_ui
->spinBoxRatioValue
->setEnabled(false);
62 m_ui
->spinBoxRatioValue
->setSuffix({});
63 m_ui
->spinBoxRatioValue
->clear();
64 m_ui
->spinBoxSeedingTimeValue
->setEnabled(false);
65 m_ui
->spinBoxSeedingTimeValue
->setSuffix({});
66 m_ui
->spinBoxSeedingTimeValue
->clear();
67 m_ui
->spinBoxInactiveSeedingTimeValue
->setEnabled(false);
68 m_ui
->spinBoxInactiveSeedingTimeValue
->setSuffix({});
69 m_ui
->spinBoxInactiveSeedingTimeValue
->clear();
71 connect(m_ui
->comboBoxRatioMode
, &QComboBox::currentIndexChanged
, this, &TorrentShareLimitsWidget::refreshRatioLimitControls
);
72 connect(m_ui
->comboBoxSeedingTimeMode
, &QComboBox::currentIndexChanged
, this, &TorrentShareLimitsWidget::refreshSeedingTimeLimitControls
);
73 connect(m_ui
->comboBoxInactiveSeedingTimeMode
, &QComboBox::currentIndexChanged
, this, &TorrentShareLimitsWidget::refreshInactiveSeedingTimeLimitControls
);
76 TorrentShareLimitsWidget::~TorrentShareLimitsWidget()
81 void TorrentShareLimitsWidget::setRatioLimit(const qreal ratioLimit
)
83 if (ratioLimit
== BitTorrent::Torrent::USE_GLOBAL_RATIO
)
85 m_ui
->comboBoxRatioMode
->setCurrentIndex(DefaultModeIndex
);
87 else if (ratioLimit
== BitTorrent::Torrent::NO_RATIO_LIMIT
)
89 m_ui
->comboBoxRatioMode
->setCurrentIndex(UnlimitedModeIndex
);
93 m_ui
->comboBoxRatioMode
->setCurrentIndex(AssignedModeIndex
);
94 m_ui
->spinBoxRatioValue
->setValue(ratioLimit
);
98 void TorrentShareLimitsWidget::setSeedingTimeLimit(const int seedingTimeLimit
)
100 if (seedingTimeLimit
== BitTorrent::Torrent::USE_GLOBAL_SEEDING_TIME
)
102 m_ui
->comboBoxSeedingTimeMode
->setCurrentIndex(DefaultModeIndex
);
104 else if (seedingTimeLimit
== BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT
)
106 m_ui
->comboBoxSeedingTimeMode
->setCurrentIndex(UnlimitedModeIndex
);
110 m_ui
->comboBoxSeedingTimeMode
->setCurrentIndex(AssignedModeIndex
);
111 m_ui
->spinBoxSeedingTimeValue
->setValue(seedingTimeLimit
);
115 void TorrentShareLimitsWidget::setInactiveSeedingTimeLimit(const int inactiveSeedingTimeLimit
)
117 if (inactiveSeedingTimeLimit
== BitTorrent::Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME
)
119 m_ui
->comboBoxInactiveSeedingTimeMode
->setCurrentIndex(DefaultModeIndex
);
121 else if (inactiveSeedingTimeLimit
== BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT
)
123 m_ui
->comboBoxInactiveSeedingTimeMode
->setCurrentIndex(UnlimitedModeIndex
);
127 m_ui
->comboBoxInactiveSeedingTimeMode
->setCurrentIndex(AssignedModeIndex
);
128 m_ui
->spinBoxInactiveSeedingTimeValue
->setValue(inactiveSeedingTimeLimit
);
132 void TorrentShareLimitsWidget::setShareLimitAction(const BitTorrent::ShareLimitAction action
)
136 case BitTorrent::ShareLimitAction::Default
:
138 m_ui
->comboBoxAction
->setCurrentIndex(DefaultActionIndex
);
140 case BitTorrent::ShareLimitAction::Stop
:
141 m_ui
->comboBoxAction
->setCurrentIndex(StopActionIndex
);
143 case BitTorrent::ShareLimitAction::Remove
:
144 m_ui
->comboBoxAction
->setCurrentIndex(RemoveActionIndex
);
146 case BitTorrent::ShareLimitAction::RemoveWithContent
:
147 m_ui
->comboBoxAction
->setCurrentIndex(RemoveWithContentActionIndex
);
149 case BitTorrent::ShareLimitAction::EnableSuperSeeding
:
150 m_ui
->comboBoxAction
->setCurrentIndex(SuperSeedingActionIndex
);
155 void TorrentShareLimitsWidget::setDefaultLimits(const qreal ratioLimit
, const int seedingTimeLimit
, const int inactiveSeedingTimeLimit
)
157 if (m_defaultRatioLimit
!= ratioLimit
)
159 m_defaultRatioLimit
= ratioLimit
;
160 refreshRatioLimitControls();
163 if (m_defaultSeedingTimeLimit
!= seedingTimeLimit
)
165 m_defaultSeedingTimeLimit
= seedingTimeLimit
;
166 refreshSeedingTimeLimitControls();
169 if (m_defaultInactiveSeedingTimeLimit
!= inactiveSeedingTimeLimit
)
171 m_defaultInactiveSeedingTimeLimit
= inactiveSeedingTimeLimit
;
172 refreshInactiveSeedingTimeLimitControls();
176 std::optional
<qreal
> TorrentShareLimitsWidget::ratioLimit() const
178 switch (m_ui
->comboBoxRatioMode
->currentIndex())
180 case DefaultModeIndex
:
181 return BitTorrent::Torrent::USE_GLOBAL_RATIO
;
182 case UnlimitedModeIndex
:
183 return BitTorrent::Torrent::NO_RATIO_LIMIT
;
184 case AssignedModeIndex
:
185 return m_ui
->spinBoxRatioValue
->value();
191 std::optional
<int> TorrentShareLimitsWidget::seedingTimeLimit() const
193 switch (m_ui
->comboBoxSeedingTimeMode
->currentIndex())
195 case DefaultModeIndex
:
196 return BitTorrent::Torrent::USE_GLOBAL_SEEDING_TIME
;
197 case UnlimitedModeIndex
:
198 return BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT
;
199 case AssignedModeIndex
:
200 return m_ui
->spinBoxSeedingTimeValue
->value();
206 std::optional
<int> TorrentShareLimitsWidget::inactiveSeedingTimeLimit() const
208 switch (m_ui
->comboBoxInactiveSeedingTimeMode
->currentIndex())
210 case DefaultModeIndex
:
211 return BitTorrent::Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME
;
212 case UnlimitedModeIndex
:
213 return BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT
;
214 case AssignedModeIndex
:
215 return m_ui
->spinBoxInactiveSeedingTimeValue
->value();
221 std::optional
<BitTorrent::ShareLimitAction
> TorrentShareLimitsWidget::shareLimitAction() const
223 switch (m_ui
->comboBoxAction
->currentIndex())
225 case DefaultActionIndex
:
226 return BitTorrent::ShareLimitAction::Default
;
227 case StopActionIndex
:
228 return BitTorrent::ShareLimitAction::Stop
;
229 case RemoveActionIndex
:
230 return BitTorrent::ShareLimitAction::Remove
;
231 case RemoveWithContentActionIndex
:
232 return BitTorrent::ShareLimitAction::RemoveWithContent
;
233 case SuperSeedingActionIndex
:
234 return BitTorrent::ShareLimitAction::EnableSuperSeeding
;
240 void TorrentShareLimitsWidget::refreshRatioLimitControls()
242 const auto index
= m_ui
->comboBoxRatioMode
->currentIndex();
244 m_ui
->spinBoxRatioValue
->setEnabled(index
== AssignedModeIndex
);
245 if (index
== AssignedModeIndex
)
247 m_ui
->spinBoxRatioValue
->setValue(m_ratioLimit
);
249 else if ((index
== DefaultModeIndex
) && (m_defaultRatioLimit
>= 0))
251 m_ui
->spinBoxRatioValue
->setValue(m_defaultRatioLimit
);
255 m_ratioLimit
= m_ui
->spinBoxRatioValue
->value();
256 m_ui
->spinBoxRatioValue
->clear();
260 void TorrentShareLimitsWidget::refreshSeedingTimeLimitControls()
262 const auto index
= m_ui
->comboBoxSeedingTimeMode
->currentIndex();
264 m_ui
->spinBoxSeedingTimeValue
->setEnabled(index
== AssignedModeIndex
);
265 if (index
== AssignedModeIndex
)
267 m_ui
->spinBoxSeedingTimeValue
->setValue(m_seedingTimeLimit
);
268 m_ui
->spinBoxSeedingTimeValue
->setSuffix(tr(" min"));
270 else if ((index
== DefaultModeIndex
) && (m_defaultSeedingTimeLimit
>= 0))
272 m_ui
->spinBoxSeedingTimeValue
->setValue(m_defaultSeedingTimeLimit
);
273 m_ui
->spinBoxSeedingTimeValue
->setSuffix(tr(" min"));
277 m_seedingTimeLimit
= m_ui
->spinBoxSeedingTimeValue
->value();
278 m_ui
->spinBoxSeedingTimeValue
->setSuffix({});
279 m_ui
->spinBoxSeedingTimeValue
->clear();
283 void TorrentShareLimitsWidget::refreshInactiveSeedingTimeLimitControls()
285 const auto index
= m_ui
->comboBoxInactiveSeedingTimeMode
->currentIndex();
287 m_ui
->spinBoxInactiveSeedingTimeValue
->setEnabled(index
== AssignedModeIndex
);
288 if (index
== AssignedModeIndex
)
290 m_ui
->spinBoxInactiveSeedingTimeValue
->setValue(m_inactiveSeedingTimeLimit
);
291 m_ui
->spinBoxInactiveSeedingTimeValue
->setSuffix(tr(" min"));
293 else if ((index
== DefaultModeIndex
) && (m_defaultInactiveSeedingTimeLimit
>= 0))
295 m_ui
->spinBoxInactiveSeedingTimeValue
->setValue(m_defaultInactiveSeedingTimeLimit
);
296 m_ui
->spinBoxInactiveSeedingTimeValue
->setSuffix(tr(" min"));
300 m_inactiveSeedingTimeLimit
= m_ui
->spinBoxInactiveSeedingTimeValue
->value();
301 m_ui
->spinBoxInactiveSeedingTimeValue
->setSuffix({});
302 m_ui
->spinBoxInactiveSeedingTimeValue
->clear();