Replace prio namespace with FilePriority enum class

This commit is contained in:
Thomas Piccirello 2018-10-21 18:58:11 -04:00
parent 6bb4eb825b
commit f27dc977e9
14 changed files with 159 additions and 69 deletions

View file

@ -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
@ -77,6 +78,7 @@ types.h
unicodestrings.h
# sources
bittorrent/filepriority.cpp
bittorrent/infohash.cpp
bittorrent/magneturi.cpp
bittorrent/peerinfo.cpp

View file

@ -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 \
@ -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 \

View file

@ -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;
}
}
}

View file

@ -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);
}

View file

@ -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"
@ -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));
}
}
}

View file

@ -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"
@ -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();

View file

@ -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"
@ -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();
}
@ -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:
@ -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:
@ -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);
@ -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();
}

View file

@ -48,6 +48,7 @@
#include <QPixmapCache>
#endif
#include "base/bittorrent/filepriority.h"
#include "base/global.h"
#include "base/utils/misc.h"
#include "base/utils/fs.h"
@ -245,7 +246,7 @@ void TorrentContentModel::updateFilesPriorities(const QVector<int> &fprio)
emit layoutAboutToBeChanged();
for (int i = 0; i < fprio.size(); ++i)
m_filesIndex[i]->setPriority(fprio[i]);
m_filesIndex[i]->setPriority(static_cast<BitTorrent::FilePriority>(fprio[i]));
emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
}
@ -268,14 +269,14 @@ QVector<int> TorrentContentModel::getFilePriorities() const
QVector<int> prio;
prio.reserve(m_filesIndex.size());
for (const TorrentContentModelFile *file : asConst(m_filesIndex))
prio.push_back(file->priority());
prio.push_back(static_cast<int>(file->priority()));
return prio;
}
bool TorrentContentModel::allFiltered() const
{
for (const TorrentContentModelFile *fileItem : asConst(m_filesIndex))
if (fileItem->priority() != prio::IGNORED)
if (fileItem->priority() != BitTorrent::FilePriority::Ignored)
return false;
return true;
}
@ -296,13 +297,14 @@ bool TorrentContentModel::setData(const QModelIndex &index, const QVariant &valu
if ((index.column() == TorrentContentModelItem::COL_NAME) && (role == Qt::CheckStateRole)) {
TorrentContentModelItem *item = static_cast<TorrentContentModelItem*>(index.internalPointer());
qDebug("setData(%s, %d", qUtf8Printable(item->name()), value.toInt());
if (item->priority() != value.toInt()) {
if (static_cast<int>(item->priority()) != value.toInt()) {
BitTorrent::FilePriority prio = BitTorrent::FilePriority::Normal;
if (value.toInt() == Qt::PartiallyChecked)
item->setPriority(prio::MIXED);
prio = BitTorrent::FilePriority::Mixed;
else if (value.toInt() == Qt::Unchecked)
item->setPriority(prio::IGNORED);
else
item->setPriority(prio::NORMAL);
prio = BitTorrent::FilePriority::Ignored;
item->setPriority(prio);
// Update folders progress in the tree
m_rootItem->recalculateProgress();
m_rootItem->recalculateAvailability();
@ -320,7 +322,7 @@ bool TorrentContentModel::setData(const QModelIndex &index, const QVariant &valu
item->setName(value.toString());
break;
case TorrentContentModelItem::COL_PRIO:
item->setPriority(value.toInt());
item->setPriority(static_cast<BitTorrent::FilePriority>(value.toInt()));
break;
default:
return false;
@ -362,9 +364,9 @@ QVariant TorrentContentModel::data(const QModelIndex &index, int role) const
}
if ((index.column() == TorrentContentModelItem::COL_NAME) && (role == Qt::CheckStateRole)) {
if (item->data(TorrentContentModelItem::COL_PRIO).toInt() == prio::IGNORED)
if (item->data(TorrentContentModelItem::COL_PRIO).toInt() == static_cast<int>(BitTorrent::FilePriority::Ignored))
return Qt::Unchecked;
if (item->data(TorrentContentModelItem::COL_PRIO).toInt() == prio::MIXED)
if (item->data(TorrentContentModelItem::COL_PRIO).toInt() == static_cast<int>(BitTorrent::FilePriority::Mixed))
return Qt::PartiallyChecked;
return Qt::Checked;
}
@ -499,8 +501,8 @@ void TorrentContentModel::selectAll()
{
for (int i = 0; i < m_rootItem->childCount(); ++i) {
TorrentContentModelItem* child = m_rootItem->child(i);
if (child->priority() == prio::IGNORED)
child->setPriority(prio::NORMAL);
if (child->priority() == BitTorrent::FilePriority::Ignored)
child->setPriority(BitTorrent::FilePriority::Normal);
}
emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
}
@ -508,6 +510,6 @@ void TorrentContentModel::selectAll()
void TorrentContentModel::selectNone()
{
for (int i = 0; i < m_rootItem->childCount(); ++i)
m_rootItem->child(i)->setPriority(prio::IGNORED);
m_rootItem->child(i)->setPriority(BitTorrent::FilePriority::Ignored);
emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
}

View file

@ -52,9 +52,9 @@ int TorrentContentModelFile::fileIndex() const
return m_fileIndex;
}
void TorrentContentModelFile::setPriority(int newPriority, bool updateParent)
void TorrentContentModelFile::setPriority(BitTorrent::FilePriority newPriority, bool updateParent)
{
Q_ASSERT(newPriority != prio::MIXED);
Q_ASSERT(newPriority != BitTorrent::FilePriority::Mixed);
if (m_priority == newPriority)
return;

View file

@ -29,6 +29,7 @@
#ifndef TORRENTCONTENTMODELFILE_H
#define TORRENTCONTENTMODELFILE_H
#include "base/bittorrent/filepriority.h"
#include "torrentcontentmodelitem.h"
class TorrentContentModelFile : public TorrentContentModelItem
@ -38,7 +39,7 @@ public:
TorrentContentModelFolder *parent, int fileIndex);
int fileIndex() const;
void setPriority(int newPriority, bool updateParent = true) override;
void setPriority(BitTorrent::FilePriority newPriority, bool updateParent = true) override;
void setProgress(qreal progress);
void setAvailability(qreal availability);
ItemType itemType() const override;

View file

@ -108,10 +108,10 @@ void TorrentContentModelFolder::updatePriority()
// If all children have the same priority
// then the folder should have the same
// priority
const int prio = m_childItems.first()->priority();
const BitTorrent::FilePriority prio = m_childItems.first()->priority();
for (int i = 1; i < m_childItems.size(); ++i) {
if (m_childItems.at(i)->priority() != prio) {
setPriority(prio::MIXED);
setPriority(BitTorrent::FilePriority::Mixed);
return;
}
}
@ -120,7 +120,7 @@ void TorrentContentModelFolder::updatePriority()
setPriority(prio);
}
void TorrentContentModelFolder::setPriority(int newPriority, bool updateParent)
void TorrentContentModelFolder::setPriority(BitTorrent::FilePriority newPriority, bool updateParent)
{
if (m_priority == newPriority)
return;
@ -132,7 +132,7 @@ void TorrentContentModelFolder::setPriority(int newPriority, bool updateParent)
m_parentItem->updatePriority();
// Update children
if (m_priority != prio::MIXED)
if (m_priority != BitTorrent::FilePriority::Mixed)
for (TorrentContentModelItem *child : asConst(m_childItems))
child->setPriority(m_priority, false);
}
@ -143,7 +143,7 @@ void TorrentContentModelFolder::recalculateProgress()
qulonglong tSize = 0;
qulonglong tRemaining = 0;
for (TorrentContentModelItem *child : asConst(m_childItems)) {
if (child->priority() == prio::IGNORED)
if (child->priority() == BitTorrent::FilePriority::Ignored)
continue;
if (child->itemType() == FolderType)
@ -166,7 +166,7 @@ void TorrentContentModelFolder::recalculateAvailability()
qulonglong tSize = 0;
bool foundAnyData = false;
for (TorrentContentModelItem *child : asConst(m_childItems)) {
if (child->priority() == prio::IGNORED)
if (child->priority() == BitTorrent::FilePriority::Ignored)
continue;
if (child->itemType() == FolderType)

View file

@ -29,6 +29,7 @@
#ifndef TORRENTCONTENTMODELFOLDER_H
#define TORRENTCONTENTMODELFOLDER_H
#include "base/bittorrent/filepriority.h"
#include "torrentcontentmodelitem.h"
class TorrentContentModelFolder : public TorrentContentModelItem
@ -49,7 +50,7 @@ public:
void recalculateAvailability();
void updatePriority();
void setPriority(int newPriority, bool updateParent = true) override;
void setPriority(BitTorrent::FilePriority newPriority, bool updateParent = true) override;
void deleteAllChildren();
const QList<TorrentContentModelItem*> &children() const;

View file

@ -38,7 +38,7 @@ TorrentContentModelItem::TorrentContentModelItem(TorrentContentModelFolder *pare
: m_parentItem(parent)
, m_size(0)
, m_remaining(0)
, m_priority(prio::NORMAL)
, m_priority(BitTorrent::FilePriority::Normal)
, m_progress(0)
, m_availability(-1.)
{
@ -90,7 +90,7 @@ qreal TorrentContentModelItem::availability() const
return (m_size > 0) ? m_availability : 0;
}
int TorrentContentModelItem::priority() const
BitTorrent::FilePriority TorrentContentModelItem::priority() const
{
Q_ASSERT(!isRootItem());
return m_priority;
@ -110,7 +110,7 @@ QVariant TorrentContentModelItem::data(int column) const
case COL_NAME:
return m_name;
case COL_PRIO:
return m_priority;
return static_cast<int>(m_priority);
case COL_PROGRESS:
return progress();
case COL_SIZE:

View file

@ -32,17 +32,7 @@
#include <QList>
#include <QVariant>
namespace prio
{
enum FilePriority
{
IGNORED=0,
NORMAL=1,
HIGH=6,
MAXIMUM=7,
MIXED=-1
};
}
#include "base/bittorrent/filepriority.h"
class TorrentContentModelFolder;
@ -82,8 +72,8 @@ public:
qreal availability() const;
int priority() const;
virtual void setPriority(int newPriority, bool updateParent = true) = 0;
BitTorrent::FilePriority priority() const;
virtual void setPriority(BitTorrent::FilePriority newPriority, bool updateParent = true) = 0;
int columnCount() const;
QVariant data(int column) const;
@ -97,7 +87,7 @@ protected:
QString m_name;
qulonglong m_size;
qulonglong m_remaining;
int m_priority;
BitTorrent::FilePriority m_priority;
qreal m_progress;
qreal m_availability;
};