Browse Source

- Rewrote a lot of code concerning finishedTorrents code. It was optimized and should be safer too

adaptive-webui-19844
Christophe Dumez 17 years ago
parent
commit
5231f5a22b
  1. 21
      src/FinishedTorrents.cpp
  2. 4
      src/FinishedTorrents.h
  3. 51
      src/GUI.cpp
  4. 59
      src/bittorrent.cpp
  5. 11
      src/bittorrent.h

21
src/FinishedTorrents.cpp

@ -74,9 +74,7 @@ FinishedTorrents::~FinishedTorrents(){
delete finishedListModel; delete finishedListModel;
} }
void FinishedTorrents::addFinishedSHA(QString hash){ void FinishedTorrents::addFinishedTorrent(QString hash){
if(finishedSHAs.indexOf(hash) == -1) {
finishedSHAs << hash;
int row = finishedListModel->rowCount(); int row = finishedListModel->rowCount();
torrent_handle h = BTSession->getTorrentHandle(hash); torrent_handle h = BTSession->getTorrentHandle(hash);
// Adding torrent to download list // Adding torrent to download list
@ -102,7 +100,6 @@ void FinishedTorrents::addFinishedSHA(QString hash){
// Update the number of finished torrents // Update the number of finished torrents
++nbFinished; ++nbFinished;
((GUI*)parent)->setTabText(1, tr("Finished") +" ("+QString(misc::toString(nbFinished).c_str())+")"); ((GUI*)parent)->setTabText(1, tr("Finished") +" ("+QString(misc::toString(nbFinished).c_str())+")");
}
} }
// Set the color of a row in data model // Set the color of a row in data model
@ -161,6 +158,7 @@ void FinishedTorrents::on_actionSet_upload_limit_triggered(){
void FinishedTorrents::updateFinishedList(){ void FinishedTorrents::updateFinishedList(){
Q_ASSERT(((GUI*)parent)->getCurrentTabIndex() == 1); Q_ASSERT(((GUI*)parent)->getCurrentTabIndex() == 1);
QString hash; QString hash;
QStringList finishedSHAs = BTSession->getFinishedTorrents();
foreach(hash, finishedSHAs){ foreach(hash, finishedSHAs){
torrent_handle h = BTSession->getTorrentHandle(hash); torrent_handle h = BTSession->getTorrentHandle(hash);
if(!h.is_valid()){ if(!h.is_valid()){
@ -170,7 +168,8 @@ void FinishedTorrents::updateFinishedList(){
torrent_status torrentStatus = h.status(); torrent_status torrentStatus = h.status();
int row = getRowFromHash(hash); int row = getRowFromHash(hash);
if(row == -1){ if(row == -1){
std::cerr << "ERROR: Can't find torrent in finished list\n"; qDebug("Cannot find torrent in finished list, adding it");
addFinishedTorrent(hash);
continue; continue;
} }
if(h.is_paused()) continue; if(h.is_paused()) continue;
@ -178,6 +177,7 @@ void FinishedTorrents::updateFinishedList(){
// What are you doing here? go back to download tab! // What are you doing here? go back to download tab!
qDebug("Info: a torrent was moved from finished to download tab"); qDebug("Info: a torrent was moved from finished to download tab");
deleteFromFinishedList(hash); deleteFromFinishedList(hash);
BTSession->setFinishedTorrent(hash);
emit torrentMovedFromFinishedList(h); emit torrentMovedFromFinishedList(h);
continue; continue;
} }
@ -187,10 +187,6 @@ void FinishedTorrents::updateFinishedList(){
} }
} }
QStringList FinishedTorrents::getFinishedSHAs(){
return finishedSHAs;
}
int FinishedTorrents::getRowFromHash(QString hash) const{ int FinishedTorrents::getRowFromHash(QString hash) const{
unsigned int nbRows = finishedListModel->rowCount(); unsigned int nbRows = finishedListModel->rowCount();
for(unsigned int i=0; i<nbRows; ++i){ for(unsigned int i=0; i<nbRows; ++i){
@ -204,15 +200,12 @@ int FinishedTorrents::getRowFromHash(QString hash) const{
// Will move it to download tab // Will move it to download tab
void FinishedTorrents::deleteFromFinishedList(QString hash){ void FinishedTorrents::deleteFromFinishedList(QString hash){
int row = getRowFromHash(hash); int row = getRowFromHash(hash);
if(row == -1){ Q_ASSERT(row != -1);
std::cerr << "Error: couldn't find hash in finished list\n";
return;
}
finishedListModel->removeRow(row); finishedListModel->removeRow(row);
QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished"); QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished");
--nbFinished; --nbFinished;
((GUI*)parent)->setTabText(1, tr("Finished") +" ("+QString(misc::toString(nbFinished).c_str())+")"); ((GUI*)parent)->setTabText(1, tr("Finished") +" ("+QString(misc::toString(nbFinished).c_str())+")");
finishedSHAs.removeAll(hash); BTSession->setUnfinishedTorrent(hash);
} }
QTreeView* FinishedTorrents::getFinishedList(){ QTreeView* FinishedTorrents::getFinishedList(){

4
src/FinishedTorrents.h

@ -38,7 +38,6 @@ class FinishedTorrents : public QWidget, public Ui::seeding{
QObject *parent; QObject *parent;
bittorrent *BTSession; bittorrent *BTSession;
FinishedListDelegate *finishedListDelegate; FinishedListDelegate *finishedListDelegate;
QStringList finishedSHAs;
QStandardItemModel *finishedListModel; QStandardItemModel *finishedListModel;
unsigned int nbFinished; unsigned int nbFinished;
@ -46,14 +45,13 @@ class FinishedTorrents : public QWidget, public Ui::seeding{
FinishedTorrents(QObject *parent, bittorrent *BTSession); FinishedTorrents(QObject *parent, bittorrent *BTSession);
~FinishedTorrents(); ~FinishedTorrents();
// Methods // Methods
QStringList getFinishedSHAs();
QTreeView* getFinishedList(); QTreeView* getFinishedList();
QStandardItemModel* getFinishedListModel(); QStandardItemModel* getFinishedListModel();
bool loadColWidthFinishedList(); bool loadColWidthFinishedList();
int getRowFromHash(QString hash) const; int getRowFromHash(QString hash) const;
public slots: public slots:
void addFinishedSHA(QString sha); void addFinishedTorrent(QString hash);
void updateFinishedList(); void updateFinishedList();
void deleteFromFinishedList(QString hash); void deleteFromFinishedList(QString hash);
void showProperties(const QModelIndex &index); void showProperties(const QModelIndex &index);

51
src/GUI.cpp

@ -314,7 +314,6 @@ void GUI::addLogPeerBlocked(QString ip){
// Update Info Bar information // Update Info Bar information
void GUI::setInfoBar(QString info, QString color){ void GUI::setInfoBar(QString info, QString color){
qDebug("setInfoBar called");
static unsigned short nbLines = 0; static unsigned short nbLines = 0;
++nbLines; ++nbLines;
// Check log size, clear it if too big // Check log size, clear it if too big
@ -548,16 +547,13 @@ void GUI::updateDlList(bool force){
LCD_UpSpeed->display(tmp); // UP LCD LCD_UpSpeed->display(tmp); // UP LCD
LCD_DownSpeed->display(tmp2); // DL LCD LCD_DownSpeed->display(tmp2); // DL LCD
// browse handles // browse handles
std::vector<torrent_handle> handles = BTSession->getTorrentHandles(); QStringList unfinishedTorrents = BTSession->getUnfinishedTorrents();
QStringList finishedSHAs = finishedTorrentTab->getFinishedSHAs(); QString hash;
unsigned int handlesSize = handles.size(); foreach(hash, unfinishedTorrents){
for(unsigned int i=0; i<handlesSize; ++i){ torrent_handle h = BTSession->getTorrentHandle(hash);
torrent_handle h = handles[i];
try{ try{
torrent_status torrentStatus = h.status(); torrent_status torrentStatus = h.status();
QString fileHash = QString(misc::toString(h.info_hash()).c_str()); QString fileHash = QString(misc::toString(h.info_hash()).c_str());
// If this is a finished torrent, no need to refresh it here
if(finishedSHAs.indexOf(fileHash) != -1) continue;
int row = getRowFromHash(fileHash); int row = getRowFromHash(fileHash);
if(row == -1){ if(row == -1){
qDebug("Info: Could not find filename in download list, adding it..."); qDebug("Info: Could not find filename in download list, adding it...");
@ -574,6 +570,7 @@ void GUI::updateDlList(bool force){
case torrent_status::finished: case torrent_status::finished:
case torrent_status::seeding: case torrent_status::seeding:
qDebug("A torrent that was in download tab just finished, moving it to finished tab"); qDebug("A torrent that was in download tab just finished, moving it to finished tab");
BTSession->setFinishedTorrent(fileHash);
finishedTorrent(h); finishedTorrent(h);
continue; continue;
case torrent_status::checking_files: case torrent_status::checking_files:
@ -1112,8 +1109,8 @@ void GUI::on_actionDelete_triggered(){
// Called when a torrent is added // Called when a torrent is added
void GUI::torrentAdded(QString path, torrent_handle& h, bool fastResume){ void GUI::torrentAdded(QString path, torrent_handle& h, bool fastResume){
QString hash = QString(misc::toString(h.info_hash()).c_str()); QString hash = QString(misc::toString(h.info_hash()).c_str());
if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished")){ if(BTSession->isFinished(hash)) {
finishedTorrentTab->addFinishedSHA(hash); finishedTorrentTab->addFinishedTorrent(hash);
return; return;
} }
int row = DLListModel->rowCount(); int row = DLListModel->rowCount();
@ -1585,7 +1582,6 @@ void GUI::finishedTorrent(torrent_handle& h){
} }
if(show_msg) if(show_msg)
setInfoBar(tr("%1 has finished downloading.", "e.g: xxx.avi has finished downloading.").arg(fileName)); setInfoBar(tr("%1 has finished downloading.", "e.g: xxx.avi has finished downloading.").arg(fileName));
finishedTorrentTab->addFinishedSHA(hash);
QList<QStandardItem *> items = DLListModel->findItems(hash, Qt::MatchExactly, HASH); QList<QStandardItem *> items = DLListModel->findItems(hash, Qt::MatchExactly, HASH);
Q_ASSERT(items.size() <= 1); Q_ASSERT(items.size() <= 1);
if(items.size() != 0){ if(items.size() != 0){
@ -1605,32 +1601,39 @@ void GUI::torrentChecked(QString hash){
// Check if the torrent was paused after checking // Check if the torrent was paused after checking
if(BTSession->isPaused(hash)){ if(BTSession->isPaused(hash)){
// Was paused, change its icon/color // Was paused, change its icon/color
if(finishedTorrentTab->getFinishedSHAs().indexOf(hash) != -1){ if(BTSession->isFinished(hash)){
// In finished list // In finished list
qDebug("Automatically paused torrent was in finished list"); qDebug("Automatically paused torrent was in finished list");
int row = finishedTorrentTab->getRowFromHash(hash); int row = finishedTorrentTab->getRowFromHash(hash);
if(row == -1){
finishedTorrentTab->addFinishedTorrent(hash);
row = finishedTorrentTab->getRowFromHash(hash);
}
Q_ASSERT(row != -1);
finishedTorrentTab->getFinishedListModel()->setData(finishedTorrentTab->getFinishedListModel()->index(row, F_UPSPEED), QVariant((double)0.0)); finishedTorrentTab->getFinishedListModel()->setData(finishedTorrentTab->getFinishedListModel()->index(row, F_UPSPEED), QVariant((double)0.0));
finishedTorrentTab->getFinishedListModel()->setData(finishedTorrentTab->getFinishedListModel()->index(row, F_NAME), QIcon(":/Icons/skin/paused.png"), Qt::DecorationRole); finishedTorrentTab->getFinishedListModel()->setData(finishedTorrentTab->getFinishedListModel()->index(row, F_NAME), QIcon(":/Icons/skin/paused.png"), Qt::DecorationRole);
finishedTorrentTab->setRowColor(row, "red"); finishedTorrentTab->setRowColor(row, "red");
}else{ }else{
// In download list // In download list
int row = getRowFromHash(hash); int row = getRowFromHash(hash);
torrent_handle h = BTSession->getTorrentHandle(hash);
torrent_status torrentStatus = h.status();
if(row ==-1){
restoreInDownloadList(h);
row = getRowFromHash(hash);
}
Q_ASSERT(row != -1);
DLListModel->setData(DLListModel->index(row, DLSPEED), QVariant((double)0.0)); DLListModel->setData(DLListModel->index(row, DLSPEED), QVariant((double)0.0));
DLListModel->setData(DLListModel->index(row, UPSPEED), QVariant((double)0.0)); DLListModel->setData(DLListModel->index(row, UPSPEED), QVariant((double)0.0));
DLListModel->setData(DLListModel->index(row, ETA), QVariant((qlonglong)-1)); DLListModel->setData(DLListModel->index(row, ETA), QVariant((qlonglong)-1));
DLListModel->setData(DLListModel->index(row, NAME), QIcon(":/Icons/skin/paused.png"), Qt::DecorationRole); DLListModel->setData(DLListModel->index(row, NAME), QIcon(":/Icons/skin/paused.png"), Qt::DecorationRole);
setRowColor(row, "red"); setRowColor(row, "red");
}
}
if(finishedTorrentTab->getFinishedSHAs().indexOf(hash) == -1) {
// Update progress in download list // Update progress in download list
torrent_handle h = BTSession->getTorrentHandle(hash);
torrent_status torrentStatus = h.status();
int row = getRowFromHash(hash);
DLListModel->setData(DLListModel->index(row, PROGRESS), QVariant((double)torrentStatus.progress)); DLListModel->setData(DLListModel->index(row, PROGRESS), QVariant((double)torrentStatus.progress));
// Delayed Sorting // Delayed Sorting
sortProgressColumnDelayed(); sortProgressColumnDelayed();
} }
}
} }
// Notification when disk is full // Notification when disk is full
@ -1643,16 +1646,26 @@ void GUI::fullDiskError(torrent_handle& h){
// Download will be paused by libtorrent. Updating GUI information accordingly // Download will be paused by libtorrent. Updating GUI information accordingly
QString hash = QString(misc::toString(h.info_hash()).c_str()); QString hash = QString(misc::toString(h.info_hash()).c_str());
qDebug("Full disk error, pausing torrent %s", (const char*)hash.toUtf8()); qDebug("Full disk error, pausing torrent %s", (const char*)hash.toUtf8());
if(finishedTorrentTab->getFinishedSHAs().indexOf(hash) != -1){ if(BTSession->isFinished(hash)){
// In finished list // In finished list
qDebug("Automatically paused torrent was in finished list"); qDebug("Automatically paused torrent was in finished list");
int row = finishedTorrentTab->getRowFromHash(hash); int row = finishedTorrentTab->getRowFromHash(hash);
if(row == -1){
finishedTorrentTab->addFinishedTorrent(hash);
row = finishedTorrentTab->getRowFromHash(hash);
}
Q_ASSERT(row != -1);
finishedTorrentTab->getFinishedListModel()->setData(finishedTorrentTab->getFinishedListModel()->index(row, F_UPSPEED), QVariant((double)0.0)); finishedTorrentTab->getFinishedListModel()->setData(finishedTorrentTab->getFinishedListModel()->index(row, F_UPSPEED), QVariant((double)0.0));
finishedTorrentTab->getFinishedListModel()->setData(finishedTorrentTab->getFinishedListModel()->index(row, F_NAME), QIcon(":/Icons/skin/paused.png"), Qt::DecorationRole); finishedTorrentTab->getFinishedListModel()->setData(finishedTorrentTab->getFinishedListModel()->index(row, F_NAME), QIcon(":/Icons/skin/paused.png"), Qt::DecorationRole);
finishedTorrentTab->setRowColor(row, "red"); finishedTorrentTab->setRowColor(row, "red");
}else{ }else{
// In download list // In download list
int row = getRowFromHash(hash); int row = getRowFromHash(hash);
if(row == -1){
restoreInDownloadList(BTSession->getTorrentHandle(hash));
row = getRowFromHash(hash);
}
Q_ASSERT(row != -1);
DLListModel->setData(DLListModel->index(row, DLSPEED), QVariant((double)0.0)); DLListModel->setData(DLListModel->index(row, DLSPEED), QVariant((double)0.0));
DLListModel->setData(DLListModel->index(row, UPSPEED), QVariant((double)0.0)); DLListModel->setData(DLListModel->index(row, UPSPEED), QVariant((double)0.0));
DLListModel->setData(DLListModel->index(row, ETA), QVariant((qlonglong)-1)); DLListModel->setData(DLListModel->index(row, ETA), QVariant((qlonglong)-1));

59
src/bittorrent.cpp

@ -187,12 +187,25 @@ void bittorrent::deleteTorrent(QString hash, bool permanent){
// Remove it from ratio table // Remove it from ratio table
ratioData.remove(hash); ratioData.remove(hash);
int index = fullAllocationModeList.indexOf(hash); int index = fullAllocationModeList.indexOf(hash);
if(index != -1) if(index != -1){
fullAllocationModeList.removeAt(index); fullAllocationModeList.removeAt(index);
}
// Remove it from pausedTorrents list // Remove it from pausedTorrents list
index = pausedTorrents.indexOf(hash); index = pausedTorrents.indexOf(hash);
if(index != -1) if(index != -1){
pausedTorrents.removeAt(index); pausedTorrents.removeAt(index);
}
index = finishedTorrents.indexOf(hash);
if(index != -1){
finishedTorrents.removeAt(index);
}else{
index = unfinishedTorrents.indexOf(hash);
if(index != -1){
unfinishedTorrents.removeAt(index);
}else{
std::cerr << "Error: Torrent " << hash.toStdString() << " is neither in finished or unfinished list\n";
}
}
if(permanent){ if(permanent){
// Remove from Hard drive // Remove from Hard drive
qDebug("Removing this on hard drive: %s", qPrintable(savePath+QDir::separator()+fileName)); qDebug("Removing this on hard drive: %s", qPrintable(savePath+QDir::separator()+fileName));
@ -201,6 +214,41 @@ void bittorrent::deleteTorrent(QString hash, bool permanent){
} }
} }
// Return a list of hashes for the finished torrents
QStringList bittorrent::getFinishedTorrents() const {
return finishedTorrents;
}
QStringList bittorrent::getUnfinishedTorrents() const {
return unfinishedTorrents;
}
bool bittorrent::isFinished(QString hash) const {
return finishedTorrents.contains(hash);
}
// Remove the given hash from the list of finished torrents
void bittorrent::setUnfinishedTorrent(QString hash) {
int index = finishedTorrents.indexOf(hash);
if(index != -1){
finishedTorrents.removeAt(index);
}
if(!unfinishedTorrents.contains(hash)){
unfinishedTorrents << hash;
}
}
// Add the given hash to the list of finished torrents
void bittorrent::setFinishedTorrent(QString hash){
if(!finishedTorrents.contains(hash)){
finishedTorrents << hash;
}
int index = unfinishedTorrents.indexOf(hash);
if(index != -1){
unfinishedTorrents.removeAt(index);
}
}
// Pause a running torrent // Pause a running torrent
bool bittorrent::pauseTorrent(QString hash){ bool bittorrent::pauseTorrent(QString hash){
bool change = false; bool change = false;
@ -406,6 +454,11 @@ void bittorrent::addTorrent(QString path, bool fromScanDir, QString from_url){
qDebug("Incremental download enabled for %s", t.name().c_str()); qDebug("Incremental download enabled for %s", t.name().c_str());
h.set_sequenced_download_threshold(1); h.set_sequenced_download_threshold(1);
} }
if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished")){
finishedTorrents << hash;
}else{
unfinishedTorrents << hash;
}
// If download from url, remove temp file // If download from url, remove temp file
if(!from_url.isNull()) QFile::remove(file); if(!from_url.isNull()) QFile::remove(file);
// Delete from scan dir to avoid trying to download it again // Delete from scan dir to avoid trying to download it again
@ -936,6 +989,8 @@ void bittorrent::readAlerts(){
std::auto_ptr<alert> a = s->pop_alert(); std::auto_ptr<alert> a = s->pop_alert();
while (a.get()){ while (a.get()){
if (torrent_finished_alert* p = dynamic_cast<torrent_finished_alert*>(a.get())){ if (torrent_finished_alert* p = dynamic_cast<torrent_finished_alert*>(a.get())){
QString hash = QString(misc::toString(p->handle.info_hash()).c_str());
setFinishedTorrent(hash);
emit finishedTorrent(p->handle); emit finishedTorrent(p->handle);
} }
else if (file_error_alert* p = dynamic_cast<file_error_alert*>(a.get())){ else if (file_error_alert* p = dynamic_cast<file_error_alert*>(a.get())){

11
src/bittorrent.h

@ -53,10 +53,12 @@ class bittorrent : public QObject{
QHash<QString, long> ETAs; QHash<QString, long> ETAs;
QHash<QString, QPair<size_type,size_type> > ratioData; QHash<QString, QPair<size_type,size_type> > ratioData;
QTimer *ETARefresher; QTimer *ETARefresher;
QList<QString> fullAllocationModeList; QStringList fullAllocationModeList;
QHash<QString, QList<QPair<QString, QString> > > trackersErrors; QHash<QString, QList<QPair<QString, QString> > > trackersErrors;
deleteThread *deleter; deleteThread *deleter;
QList<QString> pausedTorrents; QStringList pausedTorrents;
QStringList finishedTorrents;
QStringList unfinishedTorrents;
protected: protected:
QString getSavePath(QString hash); QString getSavePath(QString hash);
@ -84,6 +86,9 @@ class bittorrent : public QObject{
QList<QPair<QString, QString> > getTrackersErrors(QString hash) const; QList<QPair<QString, QString> > getTrackersErrors(QString hash) const;
bool receivedPausedAlert(QString hash) const; bool receivedPausedAlert(QString hash) const;
void printPausedTorrents(); void printPausedTorrents();
QStringList getFinishedTorrents() const;
QStringList getUnfinishedTorrents() const;
bool isFinished(QString hash) const;
public slots: public slots:
void addTorrent(QString path, bool fromScanDir = false, QString from_url = QString()); void addTorrent(QString path, bool fromScanDir = false, QString from_url = QString());
@ -124,6 +129,8 @@ class bittorrent : public QObject{
void loadFilesPriorities(torrent_handle& h); void loadFilesPriorities(torrent_handle& h);
void setDownloadLimit(QString hash, long val); void setDownloadLimit(QString hash, long val);
void setUploadLimit(QString hash, long val); void setUploadLimit(QString hash, long val);
void setUnfinishedTorrent(QString hash);
void setFinishedTorrent(QString hash);
protected slots: protected slots:
void scanDirectory(); void scanDirectory();

Loading…
Cancel
Save