Correctly handle share limits in torrent options dialog
[qBittorrent.git] / src / gui / torrentsharelimitswidget.cpp
blob6300d5f18a36a190d33def6430bf9ca7cde62952
1 /*
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"
34 namespace
36 enum ShareLimitModeIndex
38 UninitializedModeIndex = -1,
39 DefaultModeIndex,
40 UnlimitedModeIndex,
41 AssignedModeIndex
45 TorrentShareLimitsWidget::TorrentShareLimitsWidget(QWidget *parent)
46 : QWidget(parent)
47 , m_ui {new Ui::TorrentShareLimitsWidget}
49 m_ui->setupUi(this);
51 m_ui->spinBoxRatioValue->setEnabled(false);
52 m_ui->spinBoxRatioValue->setSuffix({});
53 m_ui->spinBoxRatioValue->clear();
54 m_ui->spinBoxSeedingTimeValue->setEnabled(false);
55 m_ui->spinBoxSeedingTimeValue->setSuffix({});
56 m_ui->spinBoxSeedingTimeValue->clear();
57 m_ui->spinBoxInactiveSeedingTimeValue->setEnabled(false);
58 m_ui->spinBoxInactiveSeedingTimeValue->setSuffix({});
59 m_ui->spinBoxInactiveSeedingTimeValue->clear();
61 connect(m_ui->comboBoxRatioMode, &QComboBox::currentIndexChanged, this, &TorrentShareLimitsWidget::refreshRatioLimitControls);
62 connect(m_ui->comboBoxSeedingTimeMode, &QComboBox::currentIndexChanged, this, &TorrentShareLimitsWidget::refreshSeedingTimeLimitControls);
63 connect(m_ui->comboBoxInactiveSeedingTimeMode, &QComboBox::currentIndexChanged, this, &TorrentShareLimitsWidget::refreshInactiveSeedingTimeLimitControls);
66 TorrentShareLimitsWidget::~TorrentShareLimitsWidget()
68 delete m_ui;
71 void TorrentShareLimitsWidget::setRatioLimit(const qreal ratioLimit)
73 if (ratioLimit == BitTorrent::Torrent::USE_GLOBAL_RATIO)
75 m_ui->comboBoxRatioMode->setCurrentIndex(DefaultModeIndex);
77 else if (ratioLimit == BitTorrent::Torrent::NO_RATIO_LIMIT)
79 m_ui->comboBoxRatioMode->setCurrentIndex(UnlimitedModeIndex);
81 else
83 m_ui->comboBoxRatioMode->setCurrentIndex(AssignedModeIndex);
84 m_ui->spinBoxRatioValue->setValue(ratioLimit);
88 void TorrentShareLimitsWidget::setSeedingTimeLimit(const int seedingTimeLimit)
90 if (seedingTimeLimit == BitTorrent::Torrent::USE_GLOBAL_SEEDING_TIME)
92 m_ui->comboBoxSeedingTimeMode->setCurrentIndex(DefaultModeIndex);
94 else if (seedingTimeLimit == BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT)
96 m_ui->comboBoxSeedingTimeMode->setCurrentIndex(UnlimitedModeIndex);
98 else
100 m_ui->comboBoxSeedingTimeMode->setCurrentIndex(AssignedModeIndex);
101 m_ui->spinBoxSeedingTimeValue->setValue(seedingTimeLimit);
105 void TorrentShareLimitsWidget::setInactiveSeedingTimeLimit(const int inactiveSeedingTimeLimit)
107 if (inactiveSeedingTimeLimit == BitTorrent::Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME)
109 m_ui->comboBoxInactiveSeedingTimeMode->setCurrentIndex(DefaultModeIndex);
111 else if (inactiveSeedingTimeLimit == BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT)
113 m_ui->comboBoxInactiveSeedingTimeMode->setCurrentIndex(UnlimitedModeIndex);
115 else
117 m_ui->comboBoxInactiveSeedingTimeMode->setCurrentIndex(AssignedModeIndex);
118 m_ui->spinBoxInactiveSeedingTimeValue->setValue(inactiveSeedingTimeLimit);
122 void TorrentShareLimitsWidget::setDefaultLimits(const qreal ratioLimit, const int seedingTimeLimit, const int inactiveSeedingTimeLimit)
124 if (m_defaultRatioLimit != ratioLimit)
126 m_defaultRatioLimit = ratioLimit;
127 refreshRatioLimitControls();
130 if (m_defaultSeedingTimeLimit != seedingTimeLimit)
132 m_defaultSeedingTimeLimit = seedingTimeLimit;
133 refreshSeedingTimeLimitControls();
136 if (m_defaultInactiveSeedingTimeLimit != inactiveSeedingTimeLimit)
138 m_defaultInactiveSeedingTimeLimit = inactiveSeedingTimeLimit;
139 refreshInactiveSeedingTimeLimitControls();
143 std::optional<qreal> TorrentShareLimitsWidget::ratioLimit() const
145 switch (m_ui->comboBoxRatioMode->currentIndex())
147 case DefaultModeIndex:
148 return BitTorrent::Torrent::USE_GLOBAL_RATIO;
149 case UnlimitedModeIndex:
150 return BitTorrent::Torrent::NO_RATIO_LIMIT;
151 case AssignedModeIndex:
152 return m_ui->spinBoxRatioValue->value();
153 default:
154 return std::nullopt;
158 std::optional<int> TorrentShareLimitsWidget::seedingTimeLimit() const
160 switch (m_ui->comboBoxSeedingTimeMode->currentIndex())
162 case DefaultModeIndex:
163 return BitTorrent::Torrent::USE_GLOBAL_SEEDING_TIME;
164 case UnlimitedModeIndex:
165 return BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT;
166 case AssignedModeIndex:
167 return m_ui->spinBoxSeedingTimeValue->value();
168 default:
169 return std::nullopt;
173 std::optional<int> TorrentShareLimitsWidget::inactiveSeedingTimeLimit() const
175 switch (m_ui->comboBoxInactiveSeedingTimeMode->currentIndex())
177 case DefaultModeIndex:
178 return BitTorrent::Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME;
179 case UnlimitedModeIndex:
180 return BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT;
181 case AssignedModeIndex:
182 return m_ui->spinBoxInactiveSeedingTimeValue->value();
183 default:
184 return std::nullopt;
188 void TorrentShareLimitsWidget::refreshRatioLimitControls()
190 const auto index = m_ui->comboBoxRatioMode->currentIndex();
192 m_ui->spinBoxRatioValue->setEnabled(index == AssignedModeIndex);
193 if (index == AssignedModeIndex)
195 m_ui->spinBoxRatioValue->setValue(m_ratioLimit);
197 else if ((index == DefaultModeIndex) && (m_defaultRatioLimit >= 0))
199 m_ui->spinBoxRatioValue->setValue(m_defaultRatioLimit);
201 else
203 m_ratioLimit = m_ui->spinBoxRatioValue->value();
204 m_ui->spinBoxRatioValue->clear();
208 void TorrentShareLimitsWidget::refreshSeedingTimeLimitControls()
210 const auto index = m_ui->comboBoxSeedingTimeMode->currentIndex();
212 m_ui->spinBoxSeedingTimeValue->setEnabled(index == AssignedModeIndex);
213 if (index == AssignedModeIndex)
215 m_ui->spinBoxSeedingTimeValue->setValue(m_seedingTimeLimit);
216 m_ui->spinBoxSeedingTimeValue->setSuffix(tr(" min"));
218 else if ((index == DefaultModeIndex) && (m_defaultSeedingTimeLimit >= 0))
220 m_ui->spinBoxSeedingTimeValue->setValue(m_defaultSeedingTimeLimit);
221 m_ui->spinBoxSeedingTimeValue->setSuffix(tr(" min"));
223 else
225 m_seedingTimeLimit = m_ui->spinBoxSeedingTimeValue->value();
226 m_ui->spinBoxSeedingTimeValue->setSuffix({});
227 m_ui->spinBoxSeedingTimeValue->clear();
231 void TorrentShareLimitsWidget::refreshInactiveSeedingTimeLimitControls()
233 const auto index = m_ui->comboBoxInactiveSeedingTimeMode->currentIndex();
235 m_ui->spinBoxInactiveSeedingTimeValue->setEnabled(index == AssignedModeIndex);
236 if (index == AssignedModeIndex)
238 m_ui->spinBoxInactiveSeedingTimeValue->setValue(m_inactiveSeedingTimeLimit);
239 m_ui->spinBoxInactiveSeedingTimeValue->setSuffix(tr(" min"));
241 else if ((index == DefaultModeIndex) && (m_defaultInactiveSeedingTimeLimit >= 0))
243 m_ui->spinBoxInactiveSeedingTimeValue->setValue(m_defaultInactiveSeedingTimeLimit);
244 m_ui->spinBoxInactiveSeedingTimeValue->setSuffix(tr(" min"));
246 else
248 m_inactiveSeedingTimeLimit = m_ui->spinBoxInactiveSeedingTimeValue->value();
249 m_ui->spinBoxInactiveSeedingTimeValue->setSuffix({});
250 m_ui->spinBoxInactiveSeedingTimeValue->clear();