Skip to content

Commit

Permalink
Merge pull request #9541 from Piccirello/webui-file-priority
Browse files Browse the repository at this point in the history
 Set priority for multiple files in one WebAPI request
  • Loading branch information
Chocobo1 authored Dec 11, 2018
2 parents e1f19b7 + 57e6254 commit cf9d903
Show file tree
Hide file tree
Showing 21 changed files with 289 additions and 183 deletions.
2 changes: 2 additions & 0 deletions src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_library(qbt_base STATIC
# headers
bittorrent/addtorrentparams.h
bittorrent/cachestatus.h
bittorrent/filepriority.h
bittorrent/infohash.h
bittorrent/magneturi.h
bittorrent/peerinfo.h
Expand Down Expand Up @@ -77,6 +78,7 @@ types.h
unicodestrings.h

# sources
bittorrent/filepriority.cpp
bittorrent/infohash.cpp
bittorrent/magneturi.cpp
bittorrent/peerinfo.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/base/base.pri
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ HEADERS += \
$$PWD/asyncfilestorage.h \
$$PWD/bittorrent/addtorrentparams.h \
$$PWD/bittorrent/cachestatus.h \
$$PWD/bittorrent/filepriority.h \
$$PWD/bittorrent/infohash.h \
$$PWD/bittorrent/magneturi.h \
$$PWD/bittorrent/peerinfo.h \
Expand Down Expand Up @@ -76,6 +77,7 @@ HEADERS += \

SOURCES += \
$$PWD/asyncfilestorage.cpp \
$$PWD/bittorrent/filepriority.cpp \
$$PWD/bittorrent/infohash.cpp \
$$PWD/bittorrent/magneturi.cpp \
$$PWD/bittorrent/peerinfo.cpp \
Expand Down
46 changes: 46 additions & 0 deletions src/base/bittorrent/filepriority.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2018 Thomas Piccirello <thomas.piccirello@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/

#include "filepriority.h"

namespace BitTorrent
{
bool isValidFilePriority(const BitTorrent::FilePriority priority)
{
switch (priority) {
case BitTorrent::FilePriority::Ignored:
case BitTorrent::FilePriority::Normal:
case BitTorrent::FilePriority::High:
case BitTorrent::FilePriority::Maximum:
case BitTorrent::FilePriority::Mixed:
return true;
default:
return false;
}
}
}
43 changes: 43 additions & 0 deletions src/base/bittorrent/filepriority.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2018 Thomas Piccirello <thomas.piccirello@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/

#pragma once

namespace BitTorrent
{
enum class FilePriority : int
{
Ignored = 0,
Normal = 1,
High = 6,
Maximum = 7,
Mixed = -1
};

bool isValidFilePriority(BitTorrent::FilePriority priority);
}
10 changes: 0 additions & 10 deletions src/base/bittorrent/torrenthandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1424,16 +1424,6 @@ bool TorrentHandle::saveTorrentFile(const QString &path)
return false;
}

void TorrentHandle::setFilePriority(int index, int priority)
{
std::vector<int> priorities = m_nativeHandle.file_priorities();

if ((priorities.size() > static_cast<quint64>(index)) && (priorities[index] != priority)) {
priorities[index] = priority;
prioritizeFiles(QVector<int>::fromStdVector(priorities));
}
}

void TorrentHandle::handleStateUpdate(const libt::torrent_status &nativeStatus)
{
updateStatus(nativeStatus);
Expand Down
1 change: 0 additions & 1 deletion src/base/bittorrent/torrenthandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ namespace BitTorrent
void renameFile(int index, const QString &name);
bool saveTorrentFile(const QString &path);
void prioritizeFiles(const QVector<int> &priorities);
void setFilePriority(int index, int priority);
void setRatioLimit(qreal limit);
void setSeedingTimeLimit(int limit);
void setUploadLimit(int limit);
Expand Down
13 changes: 7 additions & 6 deletions src/gui/addnewtorrentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <QUrl>
#include <QVector>

#include "base/bittorrent/filepriority.h"
#include "base/bittorrent/magneturi.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrenthandle.h"
Expand Down Expand Up @@ -622,18 +623,18 @@ void AddNewTorrentDialog::displayContentTreeMenu(const QPoint &)
renameSelectedFile();
}
else {
int prio = prio::NORMAL;
BitTorrent::FilePriority prio = BitTorrent::FilePriority::Normal;
if (act == m_ui->actionHigh)
prio = prio::HIGH;
prio = BitTorrent::FilePriority::High;
else if (act == m_ui->actionMaximum)
prio = prio::MAXIMUM;
prio = BitTorrent::FilePriority::Maximum;
else if (act == m_ui->actionNotDownloaded)
prio = prio::IGNORED;
prio = BitTorrent::FilePriority::Ignored;

qDebug("Setting files priority");
for (const QModelIndex &index : selectedRows) {
qDebug("Setting priority(%d) for file at row %d", prio, index.row());
m_contentModel->setData(m_contentModel->index(index.row(), PRIORITY, index.parent()), prio);
qDebug("Setting priority(%d) for file at row %d", static_cast<int>(prio), index.row());
m_contentModel->setData(m_contentModel->index(index.row(), PRIORITY, index.parent()), static_cast<int>(prio));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/ipsubnetwhitelistoptionsdialog.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2017 Thomas Piccirello <thomas@piccirello.com>
* Copyright (C) 2017 Thomas Piccirello <thomas.piccirello@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down
2 changes: 1 addition & 1 deletion src/gui/ipsubnetwhitelistoptionsdialog.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2017 Thomas Piccirello <thomas@piccirello.com>
* Copyright (C) 2017 Thomas Piccirello <thomas.piccirello@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down
13 changes: 7 additions & 6 deletions src/gui/properties/propertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <QThread>
#include <QTimer>

#include "base/bittorrent/filepriority.h"
#include "base/bittorrent/session.h"
#include "base/preferences.h"
#include "base/unicodestrings.h"
Expand Down Expand Up @@ -621,18 +622,18 @@ void PropertiesWidget::displayFilesListMenu(const QPoint &)
renameSelectedFile();
}
else {
int prio = prio::NORMAL;
BitTorrent::FilePriority prio = BitTorrent::FilePriority::Normal;
if (act == m_ui->actionHigh)
prio = prio::HIGH;
prio = BitTorrent::FilePriority::High;
else if (act == m_ui->actionMaximum)
prio = prio::MAXIMUM;
prio = BitTorrent::FilePriority::Maximum;
else if (act == m_ui->actionNotDownloaded)
prio = prio::IGNORED;
prio = BitTorrent::FilePriority::Ignored;

qDebug("Setting files priority");
for (const QModelIndex &index : selectedRows) {
qDebug("Setting priority(%d) for file at row %d", prio, index.row());
m_propListModel->setData(m_propListModel->index(index.row(), PRIORITY, index.parent()), prio);
qDebug("Setting priority(%d) for file at row %d", static_cast<int>(prio), index.row());
m_propListModel->setData(m_propListModel->index(index.row(), PRIORITY, index.parent()), static_cast<int>(prio));
}
// Save changes
filteredFilesChanged();
Expand Down
33 changes: 17 additions & 16 deletions src/gui/properties/proplistdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <QProxyStyle>
#endif

#include "base/bittorrent/filepriority.h"
#include "base/unicodestrings.h"
#include "base/utils/misc.h"
#include "base/utils/string.h"
Expand Down Expand Up @@ -92,7 +93,7 @@ void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
newopt.maximum = 100;
newopt.minimum = 0;
newopt.textVisible = true;
if (index.sibling(index.row(), PRIORITY).data().toInt() == prio::IGNORED) {
if (index.sibling(index.row(), PRIORITY).data().toInt() == static_cast<int>(BitTorrent::FilePriority::Ignored)) {
newopt.state &= ~QStyle::State_Enabled;
newopt.palette = progressBarDisabledPalette();
}
Expand All @@ -110,17 +111,17 @@ void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
break;
case PRIORITY: {
QString text = "";
switch (index.data().toInt()) {
case prio::MIXED:
switch (static_cast<BitTorrent::FilePriority>(index.data().toInt())) {
case BitTorrent::FilePriority::Mixed:
text = tr("Mixed", "Mixed (priorities");
break;
case prio::IGNORED:
case BitTorrent::FilePriority::Ignored:
text = tr("Not downloaded");
break;
case prio::HIGH:
case BitTorrent::FilePriority::High:
text = tr("High", "High (priority)");
break;
case prio::MAXIMUM:
case BitTorrent::FilePriority::Maximum:
text = tr("Maximum", "Maximum (priority)");
break;
default:
Expand Down Expand Up @@ -154,14 +155,14 @@ void PropListDelegate::setEditorData(QWidget *editor, const QModelIndex &index)
{
QComboBox *combobox = static_cast<QComboBox *>(editor);
// Set combobox index
switch (index.data().toInt()) {
case prio::IGNORED:
switch (static_cast<BitTorrent::FilePriority>(index.data().toInt())) {
case BitTorrent::FilePriority::Ignored:
combobox->setCurrentIndex(0);
break;
case prio::HIGH:
case BitTorrent::FilePriority::High:
combobox->setCurrentIndex(2);
break;
case prio::MAXIMUM:
case BitTorrent::FilePriority::Maximum:
combobox->setCurrentIndex(3);
break;
default:
Expand All @@ -180,7 +181,7 @@ QWidget *PropListDelegate::createEditor(QWidget *parent, const QStyleOptionViewI
return nullptr;
}

if (index.data().toInt() == prio::MIXED)
if (index.data().toInt() == static_cast<int>(BitTorrent::FilePriority::Mixed))
return nullptr;

QComboBox *editor = new QComboBox(parent);
Expand All @@ -198,20 +199,20 @@ void PropListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
int value = combobox->currentIndex();
qDebug("PropListDelegate: setModelData(%d)", value);

BitTorrent::FilePriority prio = BitTorrent::FilePriority::Normal; // NORMAL
switch (value) {
case 0:
model->setData(index, prio::IGNORED); // IGNORED
prio = BitTorrent::FilePriority::Ignored; // IGNORED
break;
case 2:
model->setData(index, prio::HIGH); // HIGH
prio = BitTorrent::FilePriority::High; // HIGH
break;
case 3:
model->setData(index, prio::MAXIMUM); // MAX
prio = BitTorrent::FilePriority::Maximum; // MAX
break;
default:
model->setData(index, prio::NORMAL); // NORMAL
}

model->setData(index, static_cast<int>(prio));
emit filteredFilesChanged();
}

Expand Down
Loading

0 comments on commit cf9d903

Please sign in to comment.