Browse Source

Clean up and optimize transferlist class

adaptive-webui-19844
Christophe Dumez 15 years ago
parent
commit
28a6afeb02
  1. 185
      src/transferlistwidget.cpp
  2. 86
      src/transferlistwidget.h

185
src/transferlistwidget.cpp

@ -161,7 +161,7 @@ void TransferListWidget::addTorrent(const QTorrentHandle& h) {
// Check that the torrent is not already there // Check that the torrent is not already there
if(getRowFromHash(h.hash()) >= 0) return; if(getRowFromHash(h.hash()) >= 0) return;
// Actuall add the torrent // Actuall add the torrent
int row = listModel->rowCount(); const int row = listModel->rowCount();
try { try {
// Adding torrent to transfer list // Adding torrent to transfer list
listModel->insertRow(row); listModel->insertRow(row);
@ -174,7 +174,7 @@ void TransferListWidget::addTorrent(const QTorrentHandle& h) {
listModel->setData(listModel->index(row, TR_SEED_DATE), QVariant(TorrentPersistentData::getSeedDate(h.hash()))); listModel->setData(listModel->index(row, TR_SEED_DATE), QVariant(TorrentPersistentData::getSeedDate(h.hash())));
listModel->setData(listModel->index(row, TR_UPLIMIT), QVariant(h.upload_limit())); listModel->setData(listModel->index(row, TR_UPLIMIT), QVariant(h.upload_limit()));
listModel->setData(listModel->index(row, TR_DLLIMIT), QVariant(h.download_limit())); listModel->setData(listModel->index(row, TR_DLLIMIT), QVariant(h.download_limit()));
QString label = TorrentPersistentData::getLabel(h.hash()); const QString &label = TorrentPersistentData::getLabel(h.hash());
listModel->setData(listModel->index(row, TR_LABEL), QVariant(label)); listModel->setData(listModel->index(row, TR_LABEL), QVariant(label));
if(BTSession->isQueueingEnabled()) if(BTSession->isQueueingEnabled())
listModel->setData(listModel->index(row, TR_PRIORITY), QVariant((int)h.queue_position())); listModel->setData(listModel->index(row, TR_PRIORITY), QVariant((int)h.queue_position()));
@ -217,7 +217,7 @@ QStandardItemModel* TransferListWidget::getSourceModel() const {
} }
void TransferListWidget::setRowColor(int row, QColor color) { void TransferListWidget::setRowColor(int row, QColor color) {
unsigned int nbColumns = listModel->columnCount()-1; const unsigned int nbColumns = listModel->columnCount()-1;
for(unsigned int i=0; i<nbColumns; ++i) { for(unsigned int i=0; i<nbColumns; ++i) {
listModel->setData(listModel->index(row, i), QVariant(color), Qt::ForegroundRole); listModel->setData(listModel->index(row, i), QVariant(color), Qt::ForegroundRole);
} }
@ -225,7 +225,7 @@ void TransferListWidget::setRowColor(int row, QColor color) {
void TransferListWidget::deleteTorrent(int row, bool refresh_list) { void TransferListWidget::deleteTorrent(int row, bool refresh_list) {
Q_ASSERT(row >= 0); Q_ASSERT(row >= 0);
QModelIndex index = listModel->index(row, 0); const QModelIndex &index = listModel->index(row, 0);
Q_ASSERT(index.isValid()); Q_ASSERT(index.isValid());
if(!index.isValid()) return; if(!index.isValid()) return;
emit torrentAboutToBeRemoved(index); emit torrentAboutToBeRemoved(index);
@ -235,12 +235,12 @@ void TransferListWidget::deleteTorrent(int row, bool refresh_list) {
} }
// Wrapper slot for bittorrent signal // Wrapper slot for bittorrent signal
void TransferListWidget::pauseTorrent(QTorrentHandle &h) { void TransferListWidget::pauseTorrent(const QTorrentHandle &h) {
pauseTorrent(getRowFromHash(h.hash())); pauseTorrent(getRowFromHash(h.hash()));
} }
void TransferListWidget::pauseTorrent(int row, bool refresh_list) { void TransferListWidget::pauseTorrent(int row, bool refresh_list) {
QTorrentHandle h = BTSession->getTorrentHandle(getHashFromRow(row)); const QTorrentHandle &h = BTSession->getTorrentHandle(getHashFromRow(row));
listModel->setData(listModel->index(row, TR_DLSPEED), QVariant((double)0.0)); listModel->setData(listModel->index(row, TR_DLSPEED), QVariant((double)0.0));
listModel->setData(listModel->index(row, TR_UPSPEED), QVariant((double)0.0)); listModel->setData(listModel->index(row, TR_UPSPEED), QVariant((double)0.0));
listModel->setData(listModel->index(row, TR_ETA), QVariant((qlonglong)-1)); listModel->setData(listModel->index(row, TR_ETA), QVariant((qlonglong)-1));
@ -264,12 +264,12 @@ int TransferListWidget::getNbTorrents() const {
} }
// Wrapper slot for bittorrent signal // Wrapper slot for bittorrent signal
void TransferListWidget::resumeTorrent(QTorrentHandle &h) { void TransferListWidget::resumeTorrent(const QTorrentHandle &h) {
resumeTorrent(getRowFromHash(h.hash())); resumeTorrent(getRowFromHash(h.hash()));
} }
void TransferListWidget::resumeTorrent(int row, bool refresh_list) { void TransferListWidget::resumeTorrent(int row, bool refresh_list) {
QTorrentHandle h = BTSession->getTorrentHandle(getHashFromRow(row)); const QTorrentHandle &h = BTSession->getTorrentHandle(getHashFromRow(row));
if(!h.is_valid()) return; if(!h.is_valid()) return;
if(h.is_seed()) { if(h.is_seed()) {
listModel->setData(listModel->index(row, TR_NAME), QVariant(QIcon(":/Icons/skin/stalledUP.png")), Qt::DecorationRole); listModel->setData(listModel->index(row, TR_NAME), QVariant(QIcon(":/Icons/skin/stalledUP.png")), Qt::DecorationRole);
@ -283,9 +283,9 @@ void TransferListWidget::resumeTorrent(int row, bool refresh_list) {
refreshList(); refreshList();
} }
void TransferListWidget::updateMetadata(QTorrentHandle &h) { void TransferListWidget::updateMetadata(const QTorrentHandle &h) {
QString hash = h.hash(); const QString &hash = h.hash();
int row = getRowFromHash(hash); const int row = getRowFromHash(hash);
if(row != -1) { if(row != -1) {
qDebug("Updating torrent metadata in download list"); qDebug("Updating torrent metadata in download list");
listModel->setData(listModel->index(row, TR_NAME), QVariant(h.name())); listModel->setData(listModel->index(row, TR_NAME), QVariant(h.name()));
@ -300,8 +300,8 @@ void TransferListWidget::previewFile(QString filePath) {
int TransferListWidget::updateTorrent(int row) { int TransferListWidget::updateTorrent(int row) {
TorrentState s = STATE_INVALID; TorrentState s = STATE_INVALID;
QString hash = getHashFromRow(row); const QString &hash = getHashFromRow(row);
QTorrentHandle h = BTSession->getTorrentHandle(hash); const QTorrentHandle &h = BTSession->getTorrentHandle(hash);
if(!h.is_valid()) { if(!h.is_valid()) {
// Torrent will be deleted from list by caller // Torrent will be deleted from list by caller
return s; return s;
@ -365,7 +365,7 @@ int TransferListWidget::updateTorrent(int row) {
listModel->setData(listModel->index(row, TR_SEEDS), QVariant(0.0)); listModel->setData(listModel->index(row, TR_SEEDS), QVariant(0.0));
if(!isColumnHidden(TR_PEERS)) if(!isColumnHidden(TR_PEERS))
listModel->setData(listModel->index(row, TR_PEERS), QVariant(0.0)); listModel->setData(listModel->index(row, TR_PEERS), QVariant(0.0));
setRowColor(row, QString::fromUtf8("grey")); setRowColor(row, "grey");
return s; return s;
} }
} }
@ -440,10 +440,9 @@ int TransferListWidget::updateTorrent(int row) {
return s; return s;
} }
void TransferListWidget::setFinished(QTorrentHandle &h) { void TransferListWidget::setFinished(const QTorrentHandle &h) {
int row = -1; const int row = getRowFromHash(h.hash());
try { try {
row = getRowFromHash(h.hash());
if(row >= 0) { if(row >= 0) {
if(h.is_paused()) { if(h.is_paused()) {
listModel->setData(listModel->index(row, TR_NAME), QIcon(":/Icons/skin/pausedUP.png"), Qt::DecorationRole); listModel->setData(listModel->index(row, TR_NAME), QIcon(":/Icons/skin/pausedUP.png"), Qt::DecorationRole);
@ -460,7 +459,7 @@ void TransferListWidget::setFinished(QTorrentHandle &h) {
listModel->setData(listModel->index(row, TR_PRIORITY), QVariant((int)-1)); listModel->setData(listModel->index(row, TR_PRIORITY), QVariant((int)-1));
listModel->setData(listModel->index(row, TR_SEED_DATE), QVariant(TorrentPersistentData::getSeedDate(h.hash()))); listModel->setData(listModel->index(row, TR_SEED_DATE), QVariant(TorrentPersistentData::getSeedDate(h.hash())));
} }
} catch(invalid_handle e) { } catch(invalid_handle) {
if(row >= 0) { if(row >= 0) {
deleteTorrent(row); deleteTorrent(row);
} }
@ -481,19 +480,18 @@ void TransferListWidget::refreshList() {
if(BTSession->getSession()->get_torrents().size() != (uint)listModel->rowCount()) { if(BTSession->getSession()->get_torrents().size() != (uint)listModel->rowCount()) {
// Oups, we have torrents that are not displayed, fix that // Oups, we have torrents that are not displayed, fix that
std::vector<torrent_handle> torrents = BTSession->getSession()->get_torrents(); std::vector<torrent_handle> torrents = BTSession->getSession()->get_torrents();
std::vector<torrent_handle>::iterator itr = torrents.begin(); std::vector<torrent_handle>::iterator itr;
while(itr != torrents.end()) { for(itr = torrents.begin(); itr != torrents.end(); itr++) {
QTorrentHandle h(*itr); const QTorrentHandle &h(*itr);
if(h.is_valid() && getRowFromHash(h.hash()) < 0) { if(h.is_valid() && getRowFromHash(h.hash()) < 0) {
addTorrent(h); addTorrent(h);
} }
itr++;
} }
} }
QStringList bad_hashes; QStringList bad_hashes;
for(int i=0; i<listModel->rowCount(); ++i) { for(int i=0; i<listModel->rowCount(); ++i) {
int s = updateTorrent(i); const int s = updateTorrent(i);
switch(s) { switch(s) {
case STATE_DOWNLOADING: case STATE_DOWNLOADING:
++nb_active; ++nb_active;
@ -525,8 +523,8 @@ void TransferListWidget::refreshList() {
} }
} }
// Remove bad torrents from list // Remove bad torrents from list
foreach(QString hash, bad_hashes) { foreach(const QString &hash, bad_hashes) {
int row = getRowFromHash(hash); const int row = getRowFromHash(hash);
if(row >= 0) if(row >= 0)
deleteTorrent(row, false); deleteTorrent(row, false);
} }
@ -537,34 +535,33 @@ void TransferListWidget::refreshList() {
} }
int TransferListWidget::getRowFromHash(QString hash) const{ int TransferListWidget::getRowFromHash(QString hash) const{
QList<QStandardItem *> items = listModel->findItems(hash, Qt::MatchExactly, TR_HASH); const QList<QStandardItem *> &items = listModel->findItems(hash, Qt::MatchExactly, TR_HASH);
if(items.empty()) return -1; if(items.empty()) return -1;
Q_ASSERT(items.size() == 1); Q_ASSERT(items.size() == 1);
return items.first()->row(); return items.first()->row();
} }
QString TransferListWidget::getHashFromRow(int row) const { inline QString TransferListWidget::getHashFromRow(int row) const {
return listModel->data(listModel->index(row, TR_HASH)).toString(); return listModel->data(listModel->index(row, TR_HASH)).toString();
} }
QModelIndex TransferListWidget::mapToSource(QModelIndex index) const { inline QModelIndex TransferListWidget::mapToSource(const QModelIndex &index) const {
return labelFilterModel->mapToSource(proxyModel->mapToSource(index)); return labelFilterModel->mapToSource(proxyModel->mapToSource(index));
} }
QModelIndex TransferListWidget::mapFromSource(QModelIndex index) const { inline QModelIndex TransferListWidget::mapFromSource(const QModelIndex &index) const {
return proxyModel->mapFromSource(labelFilterModel->mapFromSource(index)); return proxyModel->mapFromSource(labelFilterModel->mapFromSource(index));
} }
QStringList TransferListWidget::getCustomLabels() const { QStringList TransferListWidget::getCustomLabels() const {
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
settings.beginGroup(QString::fromUtf8("TransferListFilters")); return settings.value("TransferListFilters/customLabels", QStringList()).toStringList();
return settings.value("customLabels", QStringList()).toStringList();
} }
void TransferListWidget::torrentDoubleClicked(QModelIndex index) { void TransferListWidget::torrentDoubleClicked(const QModelIndex& index) {
int row = mapToSource(index).row(); const int row = mapToSource(index).row();
QString hash = getHashFromRow(row); const QString &hash = getHashFromRow(row);
QTorrentHandle h = BTSession->getTorrentHandle(hash); QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(!h.is_valid()) return; if(!h.is_valid()) return;
int action; int action;
@ -596,7 +593,7 @@ void TransferListWidget::torrentDoubleClicked(QModelIndex index) {
QStringList TransferListWidget::getSelectedTorrentsHashes() const { QStringList TransferListWidget::getSelectedTorrentsHashes() const {
QStringList hashes; QStringList hashes;
QModelIndexList selectedIndexes = selectionModel()->selectedRows(); const QModelIndexList &selectedIndexes = selectionModel()->selectedRows();
foreach(const QModelIndex &index, selectedIndexes) { foreach(const QModelIndex &index, selectedIndexes) {
hashes << getHashFromRow(mapToSource(index).row()); hashes << getHashFromRow(mapToSource(index).row());
} }
@ -604,7 +601,7 @@ QStringList TransferListWidget::getSelectedTorrentsHashes() const {
} }
void TransferListWidget::startSelectedTorrents() { void TransferListWidget::startSelectedTorrents() {
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && h.is_paused()) { if(h.is_valid() && h.is_paused()) {
@ -628,7 +625,7 @@ void TransferListWidget::startAllTorrents() {
} }
void TransferListWidget::pauseSelectedTorrents() { void TransferListWidget::pauseSelectedTorrents() {
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && !h.is_paused()) { if(h.is_valid() && !h.is_paused()) {
@ -653,12 +650,12 @@ void TransferListWidget::pauseAllTorrents() {
void TransferListWidget::deleteSelectedTorrents() { void TransferListWidget::deleteSelectedTorrents() {
if(main_window->getCurrentTabIndex() != TAB_TRANSFER) return; if(main_window->getCurrentTabIndex() != TAB_TRANSFER) return;
QStringList hashes = getSelectedTorrentsHashes(); const QStringList& hashes = getSelectedTorrentsHashes();
if(!hashes.empty()) { if(!hashes.empty()) {
bool delete_local_files = false; bool delete_local_files = false;
if(DeletionConfirmationDlg::askForDeletionConfirmation(&delete_local_files)) { if(DeletionConfirmationDlg::askForDeletionConfirmation(&delete_local_files)) {
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
int row = getRowFromHash(hash); const int row = getRowFromHash(hash);
deleteTorrent(row, false); deleteTorrent(row, false);
BTSession->deleteTorrent(hash, delete_local_files); BTSession->deleteTorrent(hash, delete_local_files);
} }
@ -669,7 +666,7 @@ void TransferListWidget::deleteSelectedTorrents() {
// FIXME: Should work only if the tab is displayed // FIXME: Should work only if the tab is displayed
void TransferListWidget::increasePrioSelectedTorrents() { void TransferListWidget::increasePrioSelectedTorrents() {
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && !h.is_seed()) { if(h.is_valid() && !h.is_seed()) {
@ -681,7 +678,7 @@ void TransferListWidget::increasePrioSelectedTorrents() {
// FIXME: Should work only if the tab is displayed // FIXME: Should work only if the tab is displayed
void TransferListWidget::decreasePrioSelectedTorrents() { void TransferListWidget::decreasePrioSelectedTorrents() {
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && !h.is_seed()) { if(h.is_valid() && !h.is_seed()) {
@ -692,9 +689,9 @@ void TransferListWidget::decreasePrioSelectedTorrents() {
} }
void TransferListWidget::buySelectedTorrents() const { void TransferListWidget::buySelectedTorrents() const {
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); const QTorrentHandle &h = BTSession->getTorrentHandle(hash);
if(h.is_valid()) if(h.is_valid())
QDesktopServices::openUrl("http://match.sharemonkey.com/?info_hash="+h.hash()+"&n="+h.name()+"&cid=33"); QDesktopServices::openUrl("http://match.sharemonkey.com/?info_hash="+h.hash()+"&n="+h.name()+"&cid=33");
} }
@ -702,9 +699,9 @@ void TransferListWidget::buySelectedTorrents() const {
void TransferListWidget::copySelectedMagnetURIs() const { void TransferListWidget::copySelectedMagnetURIs() const {
QStringList magnet_uris; QStringList magnet_uris;
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); const QTorrentHandle &h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && h.has_metadata()) if(h.is_valid() && h.has_metadata())
magnet_uris << misc::toQString(make_magnet_uri(h.get_torrent_info())); magnet_uris << misc::toQString(make_magnet_uri(h.get_torrent_info()));
} }
@ -717,11 +714,11 @@ void TransferListWidget::hidePriorityColumn(bool hide) {
void TransferListWidget::openSelectedTorrentsFolder() const { void TransferListWidget::openSelectedTorrentsFolder() const {
QStringList pathsList; QStringList pathsList;
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); const QTorrentHandle &h = BTSession->getTorrentHandle(hash);
if(h.is_valid()) { if(h.is_valid()) {
QString savePath = h.root_path(); const QString &savePath = h.root_path();
if(!pathsList.contains(savePath)) { if(!pathsList.contains(savePath)) {
pathsList.append(savePath); pathsList.append(savePath);
QDesktopServices::openUrl(QUrl(QString("file://")+savePath)); QDesktopServices::openUrl(QUrl(QString("file://")+savePath));
@ -732,9 +729,9 @@ void TransferListWidget::openSelectedTorrentsFolder() const {
void TransferListWidget::previewSelectedTorrents() { void TransferListWidget::previewSelectedTorrents() {
QStringList pathsList; QStringList pathsList;
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); const QTorrentHandle &h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && h.has_metadata()) { if(h.is_valid() && h.has_metadata()) {
new previewSelect(this, h); new previewSelect(this, h);
} }
@ -745,9 +742,9 @@ void TransferListWidget::setDlLimitSelectedTorrents() {
QList<QTorrentHandle> selected_torrents; QList<QTorrentHandle> selected_torrents;
bool first = true; bool first = true;
bool all_same_limit = true; bool all_same_limit = true;
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); const QTorrentHandle &h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && !h.is_seed()) { if(h.is_valid() && !h.is_seed()) {
selected_torrents << h; selected_torrents << h;
// Determine current limit for selected torrents // Determine current limit for selected torrents
@ -765,10 +762,10 @@ void TransferListWidget::setDlLimitSelectedTorrents() {
int default_limit = -1; int default_limit = -1;
if(all_same_limit) if(all_same_limit)
default_limit = selected_torrents.first().download_limit(); default_limit = selected_torrents.first().download_limit();
long new_limit = SpeedLimitDialog::askSpeedLimit(&ok, tr("Torrent Download Speed Limiting"), default_limit, Preferences::getGlobalDownloadLimit()*1024.); const long new_limit = SpeedLimitDialog::askSpeedLimit(&ok, tr("Torrent Download Speed Limiting"), default_limit, Preferences::getGlobalDownloadLimit()*1024.);
if(ok) { if(ok) {
foreach(QTorrentHandle h, selected_torrents) { foreach(const QTorrentHandle &h, selected_torrents) {
qDebug("Applying download speed limit of %ld Kb/s to torrent %s", (long)(new_limit/1024.), h.hash().toLocal8Bit().data()); qDebug("Applying download speed limit of %ld Kb/s to torrent %s", (long)(new_limit/1024.), h.hash().toLocal8Bit().constData());
BTSession->setDownloadLimit(h.hash(), new_limit); BTSession->setDownloadLimit(h.hash(), new_limit);
} }
} }
@ -778,9 +775,9 @@ void TransferListWidget::setUpLimitSelectedTorrents() {
QList<QTorrentHandle> selected_torrents; QList<QTorrentHandle> selected_torrents;
bool first = true; bool first = true;
bool all_same_limit = true; bool all_same_limit = true;
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); const QTorrentHandle &h = BTSession->getTorrentHandle(hash);
if(h.is_valid()) { if(h.is_valid()) {
selected_torrents << h; selected_torrents << h;
// Determine current limit for selected torrents // Determine current limit for selected torrents
@ -798,31 +795,31 @@ void TransferListWidget::setUpLimitSelectedTorrents() {
int default_limit = -1; int default_limit = -1;
if(all_same_limit) if(all_same_limit)
default_limit = selected_torrents.first().upload_limit(); default_limit = selected_torrents.first().upload_limit();
long new_limit = SpeedLimitDialog::askSpeedLimit(&ok, tr("Torrent Upload Speed Limiting"), default_limit, Preferences::getGlobalUploadLimit()*1024.); const long new_limit = SpeedLimitDialog::askSpeedLimit(&ok, tr("Torrent Upload Speed Limiting"), default_limit, Preferences::getGlobalUploadLimit()*1024.);
if(ok) { if(ok) {
foreach(QTorrentHandle h, selected_torrents) { foreach(const QTorrentHandle &h, selected_torrents) {
qDebug("Applying upload speed limit of %ld Kb/s to torrent %s", (long)(new_limit/1024.), h.hash().toLocal8Bit().data()); qDebug("Applying upload speed limit of %ld Kb/s to torrent %s", (long)(new_limit/1024.), h.hash().toLocal8Bit().constData());
BTSession->setUploadLimit(h.hash(), new_limit); BTSession->setUploadLimit(h.hash(), new_limit);
} }
} }
} }
void TransferListWidget::recheckSelectedTorrents() { void TransferListWidget::recheckSelectedTorrents() {
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
BTSession->recheckTorrent(hash); BTSession->recheckTorrent(hash);
} }
} }
// save the hidden columns in settings // save the hidden columns in settings
void TransferListWidget::saveHiddenColumns() { void TransferListWidget::saveHiddenColumns() const {
QSettings settings("qBittorrent", "qBittorrent"); QSettings settings("qBittorrent", "qBittorrent");
QStringList ishidden_list; QStringList ishidden_list;
short nbColumns = listModel->columnCount()-1;//hash column is hidden const short nbColumns = listModel->columnCount()-1;//hash column is hidden
for(short i=0; i<nbColumns; ++i){ for(short i=0; i<nbColumns; ++i){
if(isColumnHidden(i)) { if(isColumnHidden(i)) {
qDebug("Column named %s is hidden.", listModel->headerData(i, Qt::Horizontal).toString().toLocal8Bit().data()); qDebug("Column named %s is hidden.", qPrintable(listModel->headerData(i, Qt::Horizontal).toString()));
ishidden_list << "0"; ishidden_list << "0";
} else { } else {
ishidden_list << "1"; ishidden_list << "1";
@ -834,11 +831,10 @@ void TransferListWidget::saveHiddenColumns() {
// load the previous settings, and hide the columns // load the previous settings, and hide the columns
bool TransferListWidget::loadHiddenColumns() { bool TransferListWidget::loadHiddenColumns() {
QSettings settings("qBittorrent", "qBittorrent"); QSettings settings("qBittorrent", "qBittorrent");
QString line = settings.value("TransferListColsHoS", "").toString(); const QString &line = settings.value("TransferListColsHoS", "").toString();
bool loaded = false; bool loaded = false;
QStringList ishidden_list; const QStringList &ishidden_list = line.split(' ');
ishidden_list = line.split(' '); const unsigned int nbCol = ishidden_list.size();
unsigned int nbCol = ishidden_list.size();
if(nbCol == (unsigned int)listModel->columnCount()-1) { if(nbCol == (unsigned int)listModel->columnCount()-1) {
for(unsigned int i=0; i<nbCol; ++i){ for(unsigned int i=0; i<nbCol; ++i){
if(ishidden_list.at(i) == "0") { if(ishidden_list.at(i) == "0") {
@ -884,8 +880,8 @@ void TransferListWidget::displayDLHoSMenu(const QPoint&){
} }
#ifdef LIBTORRENT_0_15 #ifdef LIBTORRENT_0_15
void TransferListWidget::toggleSelectedTorrentsSuperSeeding() { void TransferListWidget::toggleSelectedTorrentsSuperSeeding() const {
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && h.has_metadata()) { if(h.is_valid() && h.has_metadata()) {
@ -895,8 +891,8 @@ void TransferListWidget::toggleSelectedTorrentsSuperSeeding() {
} }
#endif #endif
void TransferListWidget::toggleSelectedTorrentsSequentialDownload() { void TransferListWidget::toggleSelectedTorrentsSequentialDownload() const {
QStringList hashes = getSelectedTorrentsHashes(); const QStringList &hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && h.has_metadata()) { if(h.is_valid() && h.has_metadata()) {
@ -905,7 +901,7 @@ void TransferListWidget::toggleSelectedTorrentsSequentialDownload() {
} }
} }
void TransferListWidget::toggleSelectedFirstLastPiecePrio() { void TransferListWidget::toggleSelectedFirstLastPiecePrio() const {
QStringList hashes = getSelectedTorrentsHashes(); QStringList hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash); QTorrentHandle h = BTSession->getTorrentHandle(hash);
@ -918,11 +914,10 @@ void TransferListWidget::toggleSelectedFirstLastPiecePrio() {
void TransferListWidget::askNewLabelForSelection() { void TransferListWidget::askNewLabelForSelection() {
// Ask for label // Ask for label
bool ok; bool ok;
QString label = "";
bool invalid; bool invalid;
do { do {
invalid = false; invalid = false;
label = QInputDialog::getText(this, tr("New Label"), tr("Label:"), QLineEdit::Normal, label, &ok); const QString &label = QInputDialog::getText(this, tr("New Label"), tr("Label:"), QLineEdit::Normal, label, &ok);
if (ok && !label.isEmpty()) { if (ok && !label.isEmpty()) {
if(misc::isValidFileSystemName(label)) { if(misc::isValidFileSystemName(label)) {
setSelectionLabel(label); setSelectionLabel(label);
@ -935,15 +930,15 @@ void TransferListWidget::askNewLabelForSelection() {
} }
void TransferListWidget::renameSelectedTorrent() { void TransferListWidget::renameSelectedTorrent() {
QModelIndexList selectedIndexes = selectionModel()->selectedRows(); const QModelIndexList &selectedIndexes = selectionModel()->selectedRows();
if(selectedIndexes.size() != 1) return; if(selectedIndexes.size() != 1) return;
if(!selectedIndexes.first().isValid()) return; if(!selectedIndexes.first().isValid()) return;
QString hash = getHashFromRow(mapToSource(selectedIndexes.first()).row()); const QString &hash = getHashFromRow(mapToSource(selectedIndexes.first()).row());
QTorrentHandle h = BTSession->getTorrentHandle(hash); const QTorrentHandle &h = BTSession->getTorrentHandle(hash);
if(!h.is_valid()) return; if(!h.is_valid()) return;
// Ask for a new Name // Ask for a new Name
bool ok; bool ok;
QString name = QInputDialog::getText(this, tr("Rename"), tr("New name:"), QLineEdit::Normal, h.name(), &ok); const QString &name = QInputDialog::getText(this, tr("Rename"), tr("New name:"), QLineEdit::Normal, h.name(), &ok);
if (ok && !name.isEmpty()) { if (ok && !name.isEmpty()) {
// Remember the name // Remember the name
TorrentPersistentData::saveName(hash, name); TorrentPersistentData::saveName(hash, name);
@ -953,11 +948,11 @@ void TransferListWidget::renameSelectedTorrent() {
} }
void TransferListWidget::setSelectionLabel(QString label) { void TransferListWidget::setSelectionLabel(QString label) {
QStringList hashes = getSelectedTorrentsHashes(); const QStringList& hashes = getSelectedTorrentsHashes();
foreach(const QString &hash, hashes) { foreach(const QString &hash, hashes) {
Q_ASSERT(!hash.isEmpty()); Q_ASSERT(!hash.isEmpty());
int row = getRowFromHash(hash); const int row = getRowFromHash(hash);
QString old_label = listModel->data(listModel->index(row, TR_LABEL)).toString(); const QString &old_label = listModel->data(listModel->index(row, TR_LABEL)).toString();
listModel->setData(listModel->index(row, TR_LABEL), QVariant(label)); listModel->setData(listModel->index(row, TR_LABEL), QVariant(label));
TorrentPersistentData::saveLabel(hash, label); TorrentPersistentData::saveLabel(hash, label);
emit torrentChangedLabel(old_label, label); emit torrentChangedLabel(old_label, label);
@ -969,7 +964,7 @@ void TransferListWidget::setSelectionLabel(QString label) {
void TransferListWidget::removeLabelFromRows(QString label) { void TransferListWidget::removeLabelFromRows(QString label) {
for(int i=0; i<listModel->rowCount(); ++i) { for(int i=0; i<listModel->rowCount(); ++i) {
if(listModel->data(listModel->index(i, TR_LABEL)) == label) { if(listModel->data(listModel->index(i, TR_LABEL)) == label) {
QString hash = getHashFromRow(i); const QString &hash = getHashFromRow(i);
listModel->setData(listModel->index(i, TR_LABEL), "", Qt::DisplayRole); listModel->setData(listModel->index(i, TR_LABEL), "", Qt::DisplayRole);
TorrentPersistentData::saveLabel(hash, ""); TorrentPersistentData::saveLabel(hash, "");
emit torrentChangedLabel(label, ""); emit torrentChangedLabel(label, "");
@ -1090,7 +1085,7 @@ void TransferListWidget::displayListMenu(const QPoint&) {
if(selectedIndexes.size() == 1) if(selectedIndexes.size() == 1)
listMenu.addAction(&actionRename); listMenu.addAction(&actionRename);
// Label Menu // Label Menu
QStringList customLabels = getCustomLabels(); const QStringList &customLabels = getCustomLabels();
QList<QAction*> labelActions; QList<QAction*> labelActions;
QMenu *labelMenu = listMenu.addMenu(QIcon(":/Icons/oxygen/feed-subscribe.png"), tr("Label")); QMenu *labelMenu = listMenu.addMenu(QIcon(":/Icons/oxygen/feed-subscribe.png"), tr("Label"));
labelActions << labelMenu->addAction(QIcon(":/Icons/oxygen/list-add.png"), tr("New...", "New label...")); labelActions << labelMenu->addAction(QIcon(":/Icons/oxygen/list-add.png"), tr("New...", "New label..."));
@ -1189,8 +1184,8 @@ void TransferListWidget::saveColWidthList() {
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
QStringList width_list; QStringList width_list;
QStringList new_width_list; QStringList new_width_list;
short nbColumns = listModel->columnCount()-1; // HASH is hidden const short nbColumns = listModel->columnCount()-1; // HASH is hidden
QString line = settings.value("TransferListColsWidth", QString()).toString(); const QString &line = settings.value("TransferListColsWidth", QString()).toString();
if(!line.isEmpty()) { if(!line.isEmpty()) {
width_list = line.split(' '); width_list = line.split(' ');
} }
@ -1220,19 +1215,19 @@ void TransferListWidget::saveColWidthList() {
bool TransferListWidget::loadColWidthList() { bool TransferListWidget::loadColWidthList() {
qDebug("Loading columns width for download list"); qDebug("Loading columns width for download list");
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
QString line = settings.value(QString::fromUtf8("TransferListColsWidth"), QString()).toString(); const QString &line = settings.value(QString::fromUtf8("TransferListColsWidth"), QString()).toString();
if(line.isEmpty()) if(line.isEmpty())
return false; return false;
QStringList width_list = line.split(QString::fromUtf8(" ")); const QStringList &width_list = line.split(QString::fromUtf8(" "));
if(width_list.size() != listModel->columnCount()-1) { if(width_list.size() != listModel->columnCount()-1) {
qDebug("Corrupted values for transfer list columns sizes"); qDebug("Corrupted values for transfer list columns sizes");
return false; return false;
} }
unsigned int listSize = width_list.size(); const unsigned int listSize = width_list.size();
for(unsigned int i=0; i<listSize; ++i) { for(unsigned int i=0; i<listSize; ++i) {
header()->resizeSection(i, width_list.at(i).toInt()); header()->resizeSection(i, width_list.at(i).toInt());
} }
QVariantList visualIndexes = settings.value(QString::fromUtf8("TransferListVisualIndexes"), QVariantList()).toList(); const QVariantList& visualIndexes = settings.value(QString::fromUtf8("TransferListVisualIndexes"), QVariantList()).toList();
if(visualIndexes.size() != listModel->columnCount()-1) { if(visualIndexes.size() != listModel->columnCount()-1) {
qDebug("Corrupted values for transfer list columns indexes"); qDebug("Corrupted values for transfer list columns indexes");
return false; return false;
@ -1241,7 +1236,7 @@ bool TransferListWidget::loadColWidthList() {
do { do {
change = false; change = false;
for(int i=0;i<visualIndexes.size(); ++i) { for(int i=0;i<visualIndexes.size(); ++i) {
int new_visual_index = visualIndexes.at(header()->logicalIndex(i)).toInt(); const int new_visual_index = visualIndexes.at(header()->logicalIndex(i)).toInt();
if(i != new_visual_index) { if(i != new_visual_index) {
qDebug("Moving column from %d to %d", header()->logicalIndex(i), new_visual_index); qDebug("Moving column from %d to %d", header()->logicalIndex(i), new_visual_index);
header()->moveSection(i, new_visual_index); header()->moveSection(i, new_visual_index);
@ -1255,13 +1250,13 @@ bool TransferListWidget::loadColWidthList() {
void TransferListWidget::saveLastSortedColumn() { void TransferListWidget::saveLastSortedColumn() {
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
Qt::SortOrder sortOrder = header()->sortIndicatorOrder(); const Qt::SortOrder &sortOrder = header()->sortIndicatorOrder();
QString sortOrderLetter; QString sortOrderLetter;
if(sortOrder == Qt::AscendingOrder) if(sortOrder == Qt::AscendingOrder)
sortOrderLetter = QString::fromUtf8("a"); sortOrderLetter = QString::fromUtf8("a");
else else
sortOrderLetter = QString::fromUtf8("d"); sortOrderLetter = QString::fromUtf8("d");
int index = header()->sortIndicatorSection(); const int index = header()->sortIndicatorSection();
settings.setValue(QString::fromUtf8("TransferListSortedCol"), QString::number(index)+sortOrderLetter); settings.setValue(QString::fromUtf8("TransferListSortedCol"), QString::number(index)+sortOrderLetter);
} }
@ -1276,7 +1271,7 @@ void TransferListWidget::loadLastSortedColumn() {
else else
sortOrder = Qt::AscendingOrder; sortOrder = Qt::AscendingOrder;
sortedCol.chop(1); sortedCol.chop(1);
int index = sortedCol.toInt(); const int index = sortedCol.toInt();
sortByColumn(index, sortOrder); sortByColumn(index, sortOrder);
} }
} }
@ -1284,7 +1279,7 @@ void TransferListWidget::loadLastSortedColumn() {
void TransferListWidget::currentChanged(const QModelIndex& current, const QModelIndex&) { void TransferListWidget::currentChanged(const QModelIndex& current, const QModelIndex&) {
QTorrentHandle h; QTorrentHandle h;
if(current.isValid()) { if(current.isValid()) {
int row = mapToSource(current).row(); const int row = mapToSource(current).row();
h = BTSession->getTorrentHandle(getHashFromRow(row)); h = BTSession->getTorrentHandle(getHashFromRow(row));
// Scroll Fix // Scroll Fix
scrollTo(current); scrollTo(current);

86
src/transferlistwidget.h

@ -46,58 +46,17 @@ enum TorrentFilter {FILTER_ALL, FILTER_DOWNLOADING, FILTER_COMPLETED, FILTER_ACT
class TransferListWidget: public QTreeView { class TransferListWidget: public QTreeView {
Q_OBJECT Q_OBJECT
private:
TransferListDelegate *listDelegate;
QStandardItemModel *listModel;
QSortFilterProxyModel *proxyModel;
QSortFilterProxyModel *labelFilterModel;
Bittorrent* BTSession;
QTimer *refreshTimer;
GUI *main_window;
public: public:
TransferListWidget(QWidget *parent, GUI *main_window, Bittorrent* BTSession); TransferListWidget(QWidget *parent, GUI *main_window, Bittorrent* BTSession);
~TransferListWidget(); ~TransferListWidget();
int getNbTorrents() const; int getNbTorrents() const;
QStandardItemModel* getSourceModel() const; QStandardItemModel* getSourceModel() const;
protected:
int getRowFromHash(QString hash) const;
QString getHashFromRow(int row) const;
QModelIndex mapToSource(QModelIndex index) const;
QModelIndex mapFromSource(QModelIndex index) const;
QStringList getCustomLabels() const;
void saveColWidthList();
bool loadColWidthList();
void saveLastSortedColumn();
void loadLastSortedColumn();
QStringList getSelectedTorrentsHashes() const;
protected slots:
int updateTorrent(int row);
void deleteTorrent(int row, bool refresh_list=true);
void pauseTorrent(int row, bool refresh_list=true);
void resumeTorrent(int row, bool refresh_list=true);
void torrentDoubleClicked(QModelIndex index);
bool loadHiddenColumns();
void saveHiddenColumns();
void displayListMenu(const QPoint&);
void updateMetadata(QTorrentHandle &h);
void currentChanged(const QModelIndex& current, const QModelIndex&);
void resumeTorrent(QTorrentHandle &h);
#ifdef LIBTORRENT_0_15
void toggleSelectedTorrentsSuperSeeding();
#endif
void toggleSelectedTorrentsSequentialDownload();
void toggleSelectedFirstLastPiecePrio();
void askNewLabelForSelection();
void setRowColor(int row, QColor color);
public slots: public slots:
void refreshList(); void refreshList();
void addTorrent(const QTorrentHandle& h); void addTorrent(const QTorrentHandle& h);
void pauseTorrent(QTorrentHandle &h); void pauseTorrent(const QTorrentHandle &h);
void setFinished(QTorrentHandle &h); void setFinished(const QTorrentHandle &h);
void setSelectionLabel(QString label); void setSelectionLabel(QString label);
void setRefreshInterval(int t); void setRefreshInterval(int t);
void startSelectedTorrents(); void startSelectedTorrents();
@ -122,12 +81,53 @@ public slots:
void removeLabelFromRows(QString label); void removeLabelFromRows(QString label);
void renameSelectedTorrent(); void renameSelectedTorrent();
protected:
int getRowFromHash(QString hash) const;
QString getHashFromRow(int row) const;
QModelIndex mapToSource(const QModelIndex &index) const;
QModelIndex mapFromSource(const QModelIndex &index) const;
QStringList getCustomLabels() const;
void saveColWidthList();
bool loadColWidthList();
void saveLastSortedColumn();
void loadLastSortedColumn();
QStringList getSelectedTorrentsHashes() const;
protected slots:
int updateTorrent(int row);
void deleteTorrent(int row, bool refresh_list=true);
void pauseTorrent(int row, bool refresh_list=true);
void resumeTorrent(int row, bool refresh_list=true);
void torrentDoubleClicked(const QModelIndex& index);
bool loadHiddenColumns();
void saveHiddenColumns() const;
void displayListMenu(const QPoint&);
void updateMetadata(const QTorrentHandle &h);
void currentChanged(const QModelIndex& current, const QModelIndex&);
void resumeTorrent(const QTorrentHandle &h);
#ifdef LIBTORRENT_0_15
void toggleSelectedTorrentsSuperSeeding() const;
#endif
void toggleSelectedTorrentsSequentialDownload() const;
void toggleSelectedFirstLastPiecePrio() const;
void askNewLabelForSelection();
void setRowColor(int row, QColor color);
signals: signals:
void currentTorrentChanged(QTorrentHandle &h); void currentTorrentChanged(QTorrentHandle &h);
void torrentStatusUpdate(unsigned int nb_downloading, unsigned int nb_seeding, unsigned int nb_active, unsigned int nb_inactive); void torrentStatusUpdate(unsigned int nb_downloading, unsigned int nb_seeding, unsigned int nb_active, unsigned int nb_inactive);
void torrentAdded(QModelIndex index); void torrentAdded(QModelIndex index);
void torrentAboutToBeRemoved(QModelIndex index); void torrentAboutToBeRemoved(QModelIndex index);
void torrentChangedLabel(QString old_label, QString new_label); void torrentChangedLabel(QString old_label, QString new_label);
private:
TransferListDelegate *listDelegate;
QStandardItemModel *listModel;
QSortFilterProxyModel *proxyModel;
QSortFilterProxyModel *labelFilterModel;
Bittorrent* BTSession;
QTimer *refreshTimer;
GUI *main_window;
}; };
#endif // TRANSFERLISTWIDGET_H #endif // TRANSFERLISTWIDGET_H

Loading…
Cancel
Save