Started code reorganizing (Moved libtorrent specific files and webui files to subfolders)

This commit is contained in:
Christophe Dumez
2010-10-09 14:06:35 +00:00
parent 593fdad426
commit 6e7309316c
49 changed files with 478 additions and 465 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,283 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
*
* 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.
*
* Contact : chris@qbittorrent.org
*/
#ifndef __BITTORRENT_H__
#define __BITTORRENT_H__
#include <QHash>
#include <QMap>
#include <QUrl>
#include <QStringList>
#ifdef DISABLE_GUI
#include <QCoreApplication>
#else
#include <QApplication>
#include <QPalette>
#endif
#include <QPointer>
#include <QTimer>
#include <libtorrent/version.hpp>
#include <libtorrent/session.hpp>
#include <libtorrent/ip_filter.hpp>
#include "qtorrenthandle.h"
using namespace libtorrent;
#define MAX_SAMPLES 20
class downloadThread;
class QTimer;
class FilterParserThread;
class HttpServer;
class BandwidthScheduler;
class ScanFoldersModel;
class TrackerInfos {
public:
QString name_or_url;
QString last_message;
unsigned long num_peers;
#if LIBTORRENT_VERSION_MINOR < 15
bool verified;
uint fail_count;
#endif
//TrackerInfos() {}
TrackerInfos(const TrackerInfos &b) {
name_or_url = b.name_or_url;
Q_ASSERT(!name_or_url.isEmpty());
last_message = b.last_message;
num_peers = b.num_peers;
#if LIBTORRENT_VERSION_MINOR < 15
verified = b.verified;
fail_count = b.fail_count;
#endif
}
TrackerInfos(QString name_or_url): name_or_url(name_or_url), last_message(""), num_peers(0) {
#if LIBTORRENT_VERSION_MINOR < 15
fail_count = 0;
verified = false;
#endif
}
};
class Bittorrent : public QObject {
Q_OBJECT
public:
// Constructor / Destructor
Bittorrent();
~Bittorrent();
QTorrentHandle getTorrentHandle(QString hash) const;
std::vector<torrent_handle> getTorrents() const;
bool isFilePreviewPossible(QString fileHash) const;
bool isDHTEnabled() const;
bool isLSDEnabled() const;
float getPayloadDownloadRate() const;
float getPayloadUploadRate() const;
session_status getSessionStatus() const;
int getListenPort() const;
float getRealRatio(QString hash) const;
session* getSession() const;
QHash<QString, TrackerInfos> getTrackersInfo(QString hash) const;
bool hasActiveTorrents() const;
bool hasDownloadingTorrents() const;
bool isQueueingEnabled() const;
int getMaximumActiveDownloads() const;
int getMaximumActiveTorrents() const;
int loadTorrentPriority(QString hash);
QStringList getConsoleMessages() const;
QStringList getPeerBanMessages() const;
qlonglong getETA(QString hash);
bool useTemporaryFolder() const;
QString getDefaultSavePath() const;
ScanFoldersModel* getScanFoldersModel() const;
bool isPexEnabled() const;
#if LIBTORRENT_VERSION_MINOR < 15
void saveDHTEntry();
#endif
public slots:
QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false);
QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false);
void loadSessionState();
void saveSessionState();
void downloadFromUrl(QString url);
void deleteTorrent(QString hash, bool delete_local_files = false);
void startUpTorrents();
session_proxy asyncDeletion();
void recheckTorrent(QString hash);
void useAlternativeSpeedsLimit(bool alternative);
/* Needed by Web UI */
void pauseAllTorrents();
void pauseTorrent(QString hash);
void resumeTorrent(QString hash);
void resumeAllTorrents();
/* End Web UI */
void preAllocateAllFiles(bool b);
void saveFastResumeData();
void enableIPFilter(QString filter);
void disableIPFilter();
void setQueueingEnabled(bool enable);
void handleDownloadFailure(QString url, QString reason);
void downloadUrlAndSkipDialog(QString url, QString save_path=QString::null);
// Session configuration - Setters
void setListeningPort(int port);
void setMaxConnections(int maxConnec);
void setMaxConnectionsPerTorrent(int max);
void setMaxUploadsPerTorrent(int max);
void setDownloadRateLimit(long rate);
void setUploadRateLimit(long rate);
void setMaxRatio(float ratio);
void setDHTPort(int dht_port);
void setPeerProxySettings(const proxy_settings &proxySettings);
void setHTTPProxySettings(const proxy_settings &proxySettings);
void setSessionSettings(const session_settings &sessionSettings);
void startTorrentsInPause(bool b);
void setDefaultTempPath(QString temppath);
void setAppendLabelToSavePath(bool append);
void appendLabelToTorrentSavePath(QTorrentHandle &h);
void changeLabelInTorrentSavePath(QTorrentHandle &h, QString old_label, QString new_label);
#if LIBTORRENT_VERSION_MINOR > 14
void appendqBextensionToTorrent(QTorrentHandle &h, bool append);
void setAppendqBExtension(bool append);
#endif
void applyEncryptionSettings(pe_settings se);
void setDownloadLimit(QString hash, long val);
void setUploadLimit(QString hash, long val);
void enableUPnP(bool b);
void enableNATPMP(bool b);
void enableLSD(bool b);
bool enableDHT(bool b);
#ifdef DISABLE_GUI
void addConsoleMessage(QString msg, QString color=QString::null);
#else
void addConsoleMessage(QString msg, QColor color=QApplication::palette().color(QPalette::WindowText));
#endif
void addPeerBanMessage(QString msg, bool from_ipfilter);
void processDownloadedFile(QString, QString);
void addMagnetSkipAddDlg(QString uri);
void downloadFromURLList(const QStringList& urls);
void configureSession();
void banIP(QString ip);
void recursiveTorrentDownload(const QTorrentHandle &h);
protected:
QString getSavePath(QString hash, bool fromScanDir = false, QString filePath = QString::null, QString root_folder=QString::null);
bool initWebUi(QString username, QString password, int port);
protected slots:
void addTorrentsFromScanFolder(QStringList&);
void readAlerts();
void processBigRatios();
void takeETASamples();
void exportTorrentFiles(QString path);
void saveTempFastResumeData();
void sendNotificationEmail(QTorrentHandle h);
void autoRunExternalProgram(QTorrentHandle h, bool async=true);
void cleanUpAutoRunProcess(int);
signals:
void addedTorrent(QTorrentHandle& h);
void deletedTorrent(QString hash);
void pausedTorrent(QTorrentHandle& h);
void resumedTorrent(QTorrentHandle& h);
void finishedTorrent(QTorrentHandle& h);
void fullDiskError(QTorrentHandle& h, QString msg);
void trackerError(QString hash, QString time, QString msg);
void trackerAuthenticationRequired(QTorrentHandle& h);
void newDownloadedTorrent(QString path, QString url);
void updateFileSize(QString hash);
void downloadFromUrlFailure(QString url, QString reason);
void torrentFinishedChecking(QTorrentHandle& h);
void metadataReceived(QTorrentHandle &h);
void savePathChanged(QTorrentHandle &h);
void newConsoleMessage(QString msg);
void alternativeSpeedsModeChanged(bool alternative);
void recursiveTorrentDownloadPossible(QTorrentHandle &h);
private:
// Bittorrent
session *s;
QPointer<QTimer> timerAlerts;
QPointer<BandwidthScheduler> bd_scheduler;
QMap<QUrl, QString> savepath_fromurl;
QHash<QString, QHash<QString, TrackerInfos> > trackersInfos;
QHash<QString, QString> savePathsToRemove;
QStringList torrentsToPausedAfterChecking;
QTimer resumeDataTimer;
// Ratio
QPointer<QTimer> BigRatioTimer;
// HTTP
QPointer<downloadThread> downloader;
// File System
ScanFoldersModel *m_scanFolders;
// Console / Log
QStringList consoleMessages;
QStringList peerBanMessages;
// Settings
bool preAllocateAll;
bool addInPause;
float ratio_limit;
int high_ratio_action;
bool UPnPEnabled;
bool NATPMPEnabled;
bool LSDEnabled;
bool DHTEnabled;
int current_dht_port;
bool PeXEnabled;
bool queueingEnabled;
bool appendLabelToSavePath;
bool torrentExport;
#if LIBTORRENT_VERSION_MINOR > 14
bool appendqBExtension;
#endif
QString defaultSavePath;
QString defaultTempPath;
// ETA Computation
QPointer<QTimer> timerETA;
QHash<QString, QList<int> > ETA_samples;
// IP filtering
QPointer<FilterParserThread> filterParser;
QString filterPath;
// Web UI
QPointer<HttpServer> httpServer;
QList<QUrl> url_skippingDlg;
// Fast exit (async)
bool exiting;
// GeoIP
#ifndef DISABLE_GUI
bool geoipDBLoaded;
bool resolve_countries;
#endif
};
#endif

View File

@@ -0,0 +1,7 @@
INCLUDEPATH += $$PWD
HEADERS += $$PWD/qbtsession.h \
$$PWD/qtorrenthandle.h
SOURCES += $$PWD/qbtsession.cpp \
$$PWD/qtorrenthandle.cpp

View File

@@ -0,0 +1,765 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
*
* 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.
*
* Contact : chris@qbittorrent.org
*/
#include <QString>
#include <QStringList>
#include <QFile>
#include <QDir>
#include <QByteArray>
#include <math.h>
#include "misc.h"
#include "preferences.h"
#include "qtorrenthandle.h"
#include "torrentpersistentdata.h"
#include <libtorrent/version.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/bencode.hpp>
#include <libtorrent/entry.hpp>
#include <boost/filesystem/fstream.hpp>
QTorrentHandle::QTorrentHandle(torrent_handle h): h(h) {}
//
// Getters
//
const torrent_handle& QTorrentHandle::get_torrent_handle() const {
Q_ASSERT(h.is_valid());
return h;
}
const torrent_info& QTorrentHandle::get_torrent_info() const {
Q_ASSERT(h.is_valid());
return h.get_torrent_info();
}
QString QTorrentHandle::hash() const {
Q_ASSERT(h.is_valid());
return misc::toQString(h.info_hash());
}
QString QTorrentHandle::name() const {
Q_ASSERT(h.is_valid());
QString name = TorrentPersistentData::getName(hash());
if(name.isEmpty()) {
name = misc::toQStringU(h.name());
}
return name;
}
QString QTorrentHandle::creation_date() const {
Q_ASSERT(h.is_valid());
boost::optional<boost::posix_time::ptime> boostDate = h.get_torrent_info().creation_date();
return misc::boostTimeToQString(boostDate);
}
QString QTorrentHandle::next_announce() const {
Q_ASSERT(h.is_valid());
return misc::userFriendlyDuration(h.status().next_announce.total_seconds());
}
qlonglong QTorrentHandle::next_announce_s() const {
Q_ASSERT(h.is_valid());
return h.status().next_announce.total_seconds();
}
float QTorrentHandle::progress() const {
Q_ASSERT(h.is_valid());
if(!h.status().total_wanted)
return 0.;
if (h.status().total_wanted_done == h.status().total_wanted)
return 1.;
float progress = (float)h.status().total_wanted_done/(float)h.status().total_wanted;
Q_ASSERT(progress >= 0. && progress <= 1.);
return progress;
}
bitfield QTorrentHandle::pieces() const {
Q_ASSERT(h.is_valid());
return h.status().pieces;
}
void QTorrentHandle::piece_availability(std::vector<int>& avail) const {
Q_ASSERT(h.is_valid());
h.piece_availability(avail);
}
void QTorrentHandle::get_download_queue(std::vector<partial_piece_info>& queue) const {
Q_ASSERT(h.is_valid());
h.get_download_queue(queue);
}
QString QTorrentHandle::current_tracker() const {
Q_ASSERT(h.is_valid());
return misc::toQString(h.status().current_tracker);
}
bool QTorrentHandle::is_valid() const {
return h.is_valid();
}
bool QTorrentHandle::is_paused() const {
Q_ASSERT(h.is_valid());
return h.is_paused() && !h.is_auto_managed();
}
bool QTorrentHandle::is_queued() const {
Q_ASSERT(h.is_valid());
return h.is_paused() && h.is_auto_managed();
}
size_type QTorrentHandle::total_size() const {
Q_ASSERT(h.is_valid());
return h.get_torrent_info().total_size();
}
size_type QTorrentHandle::piece_length() const {
Q_ASSERT(h.is_valid());
return h.get_torrent_info().piece_length();
}
int QTorrentHandle::num_pieces() const {
Q_ASSERT(h.is_valid());
return h.get_torrent_info().num_pieces();
}
void QTorrentHandle::get_peer_info(std::vector<peer_info>& v) const {
Q_ASSERT(h.is_valid());
h.get_peer_info(v);
}
bool QTorrentHandle::first_last_piece_first() const {
Q_ASSERT(h.is_valid());
// Detect main file
int rank=0;
int main_file_index = 0;
file_entry main_file = h.get_torrent_info().file_at(0);
torrent_info::file_iterator it = h.get_torrent_info().begin_files();
it++; ++rank;
while(it != h.get_torrent_info().end_files()) {
if(it->size > main_file.size) {
main_file = *it;
main_file_index = rank;
}
it++;
++rank;
}
qDebug("Main file in the torrent is %s", main_file.path.string().c_str());
int piece_size = h.get_torrent_info().piece_length();
Q_ASSERT(piece_size>0);
int first_piece = floor((main_file.offset+1)/(double)piece_size);
Q_ASSERT(first_piece >= 0 && first_piece < h.get_torrent_info().num_pieces());
qDebug("First piece of the file is %d/%d", first_piece, h.get_torrent_info().num_pieces()-1);
int num_pieces_in_file = ceil(main_file.size/(double)piece_size);
int last_piece = first_piece+num_pieces_in_file-1;
Q_ASSERT(last_piece >= 0 && last_piece < h.get_torrent_info().num_pieces());
qDebug("last piece of the file is %d/%d", last_piece, h.get_torrent_info().num_pieces()-1);
return (h.piece_priority(first_piece) == 7) && (h.piece_priority(last_piece) == 7);
}
size_type QTorrentHandle::total_wanted_done() const {
Q_ASSERT(h.is_valid());
return h.status().total_wanted_done;
}
float QTorrentHandle::download_payload_rate() const {
Q_ASSERT(h.is_valid());
return h.status().download_payload_rate;
}
float QTorrentHandle::upload_payload_rate() const {
Q_ASSERT(h.is_valid());
return h.status().upload_payload_rate;
}
int QTorrentHandle::num_peers() const {
Q_ASSERT(h.is_valid());
return h.status().num_peers;
}
int QTorrentHandle::num_seeds() const {
Q_ASSERT(h.is_valid());
return h.status().num_seeds;
}
int QTorrentHandle::num_complete() const {
Q_ASSERT(h.is_valid());
return h.status().num_complete;
}
void QTorrentHandle::scrape_tracker() const {
Q_ASSERT(h.is_valid());
h.scrape_tracker();
}
int QTorrentHandle::num_incomplete() const {
Q_ASSERT(h.is_valid());
return h.status().num_incomplete;
}
QString QTorrentHandle::save_path() const {
Q_ASSERT(h.is_valid());
return misc::toQStringU(h.save_path().string()).replace("\\", "/");
}
#if LIBTORRENT_VERSION_MINOR > 14
bool QTorrentHandle::super_seeding() const {
Q_ASSERT(h.is_valid());
return h.super_seeding();
}
#endif
QStringList QTorrentHandle::url_seeds() const {
Q_ASSERT(h.is_valid());
QStringList res;
try {
std::vector<std::string> existing_seeds = h.get_torrent_info().url_seeds();
unsigned int nbSeeds = existing_seeds.size();
QString existing_seed;
for(unsigned int i=0; i<nbSeeds; ++i) {
res << misc::toQString(existing_seeds[i]);
}
} catch(std::exception e) {}
return res;
}
// get the size of the torrent without the filtered files
size_type QTorrentHandle::actual_size() const{
Q_ASSERT(h.is_valid());
return h.status().total_wanted;
}
bool QTorrentHandle::has_filtered_pieces() const {
Q_ASSERT(h.is_valid());
std::vector<int> piece_priorities = h.piece_priorities();
for(unsigned int i = 0; i<piece_priorities.size(); ++i) {
if(!piece_priorities[i]) return true;
}
return false;
}
int QTorrentHandle::download_limit() const {
Q_ASSERT(h.is_valid());
return h.download_limit();
}
int QTorrentHandle::upload_limit() const {
Q_ASSERT(h.is_valid());
return h.upload_limit();
}
int QTorrentHandle::num_files() const {
Q_ASSERT(h.is_valid());
return h.get_torrent_info().num_files();
}
bool QTorrentHandle::has_metadata() const {
Q_ASSERT(h.is_valid());
return h.has_metadata();
}
void QTorrentHandle::save_resume_data() const {
Q_ASSERT(h.is_valid());
return h.save_resume_data();
}
QString QTorrentHandle::file_at(unsigned int index) const {
Q_ASSERT(h.is_valid());
Q_ASSERT(index < (unsigned int)h.get_torrent_info().num_files());
return misc::toQStringU(h.get_torrent_info().file_at(index).path.leaf());
}
size_type QTorrentHandle::filesize_at(unsigned int index) const {
Q_ASSERT(h.is_valid());
Q_ASSERT(index < (unsigned int)h.get_torrent_info().num_files());
return h.get_torrent_info().file_at(index).size;
}
const std::vector<announce_entry> QTorrentHandle::trackers() const {
Q_ASSERT(h.is_valid());
return h.trackers();
}
torrent_status::state_t QTorrentHandle::state() const {
Q_ASSERT(h.is_valid());
return h.status().state;
}
std::vector<int> QTorrentHandle::file_priorities() const {
Q_ASSERT(h.is_valid());
return h.file_priorities();
}
QString QTorrentHandle::creator() const {
Q_ASSERT(h.is_valid());
return misc::toQStringU(h.get_torrent_info().creator());
}
QString QTorrentHandle::comment() const {
Q_ASSERT(h.is_valid());
return misc::toQStringU(h.get_torrent_info().comment());
}
size_type QTorrentHandle::total_failed_bytes() const {
Q_ASSERT(h.is_valid());
return h.status().total_failed_bytes;
}
size_type QTorrentHandle::total_redundant_bytes() const {
Q_ASSERT(h.is_valid());
return h.status().total_redundant_bytes;
}
void QTorrentHandle::file_progress(std::vector<size_type>& fp) const {
Q_ASSERT(h.is_valid());
return h.file_progress(fp);
}
bool QTorrentHandle::is_checking() const {
Q_ASSERT(h.is_valid());
return h.status().state == torrent_status::checking_files || h.status().state == torrent_status::checking_resume_data;
}
size_type QTorrentHandle::total_done() const {
Q_ASSERT(h.is_valid());
return h.status().total_done;
}
size_type QTorrentHandle::all_time_download() const {
Q_ASSERT(h.is_valid());
return h.status().all_time_download;
}
size_type QTorrentHandle::all_time_upload() const {
Q_ASSERT(h.is_valid());
return h.status().all_time_upload;
}
size_type QTorrentHandle::total_payload_download() const {
Q_ASSERT(h.is_valid());
return h.status().total_payload_download;
}
size_type QTorrentHandle::total_payload_upload() const {
Q_ASSERT(h.is_valid());
return h.status().total_payload_upload;
}
// Return a list of absolute paths corresponding
// to all files in a torrent
QStringList QTorrentHandle::files_path() const {
Q_ASSERT(h.is_valid());
QDir saveDir(misc::toQString(h.save_path().string()));
QStringList res;
torrent_info::file_iterator fi = h.get_torrent_info().begin_files();
while(fi != h.get_torrent_info().end_files()) {
res << QDir::cleanPath(saveDir.absoluteFilePath(misc::toQStringU(fi->path.string())));
fi++;
}
return res;
}
bool QTorrentHandle::has_missing_files() const {
const QStringList paths = files_path();
foreach(const QString &path, paths) {
if(!QFile::exists(path)) return true;
}
return false;
}
int QTorrentHandle::queue_position() const {
Q_ASSERT(h.is_valid());
if(h.queue_position() < 0)
return -1;
return h.queue_position()+1;
}
int QTorrentHandle::num_uploads() const {
Q_ASSERT(h.is_valid());
return h.status().num_uploads;
}
bool QTorrentHandle::is_seed() const {
Q_ASSERT(h.is_valid());
// Affected by bug http://code.rasterbar.com/libtorrent/ticket/402
//return h.is_seed();
// May suffer from approximation problems
//return (progress() == 1.);
// This looks safe
return (state() == torrent_status::finished || state() == torrent_status::seeding);
}
bool QTorrentHandle::is_auto_managed() const {
Q_ASSERT(h.is_valid());
return h.is_auto_managed();
}
qlonglong QTorrentHandle::active_time() const {
Q_ASSERT(h.is_valid());
return h.status().active_time;
}
qlonglong QTorrentHandle::seeding_time() const {
Q_ASSERT(h.is_valid());
return h.status().seeding_time;
}
int QTorrentHandle::num_connections() const {
Q_ASSERT(h.is_valid());
return h.status().num_connections;
}
int QTorrentHandle::connections_limit() const {
Q_ASSERT(h.is_valid());
return h.status().connections_limit;
}
bool QTorrentHandle::is_sequential_download() const {
Q_ASSERT(h.is_valid());
return h.is_sequential_download();
}
#ifndef DISABLE_GUI
bool QTorrentHandle::resolve_countries() const {
Q_ASSERT(h.is_valid());
return h.resolve_countries();
}
#endif
bool QTorrentHandle::priv() const {
Q_ASSERT(h.is_valid());
return h.get_torrent_info().priv();
}
QString QTorrentHandle::firstFileSavePath() const {
Q_ASSERT(h.is_valid());
Q_ASSERT(has_metadata());
QString fsave_path = TorrentPersistentData::getSavePath(hash());
if(fsave_path.isEmpty())
fsave_path = save_path();
fsave_path = fsave_path.replace("\\", "/");
if(!fsave_path.endsWith("/"))
fsave_path += "/";
fsave_path += misc::toQStringU(h.get_torrent_info().file_at(0).path.string());
// Remove .!qB extension
if(fsave_path.endsWith(".!qB", Qt::CaseInsensitive))
fsave_path.chop(4);
return fsave_path;
}
bool QTorrentHandle::has_error() const {
Q_ASSERT(h.is_valid());
return h.is_paused() && !h.status().error.empty();
}
QString QTorrentHandle::error() const {
Q_ASSERT(h.is_valid());
return misc::toQString(h.status().error);
}
void QTorrentHandle::downloading_pieces(bitfield &bf) const {
Q_ASSERT(h.is_valid());
std::vector<partial_piece_info> queue;
h.get_download_queue(queue);
for(std::vector<partial_piece_info>::iterator it=queue.begin(); it!= queue.end(); it++) {
bf.set_bit(it->piece_index);
}
return;
}
//
// Setters
//
void QTorrentHandle::set_download_limit(int limit) {
Q_ASSERT(h.is_valid());
h.set_download_limit(limit);
}
void QTorrentHandle::set_upload_limit(int limit) {
Q_ASSERT(h.is_valid());
h.set_upload_limit(limit);
}
void QTorrentHandle::pause() {
Q_ASSERT(h.is_valid());
h.auto_managed(false);
h.pause();
h.save_resume_data();
}
void QTorrentHandle::resume() {
Q_ASSERT(h.is_valid());
if(has_error()) h.clear_error();
const QString torrent_hash = hash();
bool has_persistant_error = TorrentPersistentData::hasError(torrent_hash);
TorrentPersistentData::setErrorState(torrent_hash, false);
bool temp_path_enabled = Preferences::isTempPathEnabled();
if(has_persistant_error && temp_path_enabled) {
// Torrent was supposed to be seeding, checking again in final destination
qDebug("Resuming a torrent with error...");
const QString final_save_path = TorrentPersistentData::getSavePath(torrent_hash);
qDebug("Torrent final path is: %s", qPrintable(final_save_path));
if(!final_save_path.isEmpty())
move_storage(final_save_path);
}
h.auto_managed(true);
h.resume();
if(has_persistant_error && temp_path_enabled) {
// Force recheck
h.force_recheck();
}
}
void QTorrentHandle::remove_url_seed(QString seed) {
Q_ASSERT(h.is_valid());
h.remove_url_seed(seed.toStdString());
}
void QTorrentHandle::add_url_seed(QString seed) {
Q_ASSERT(h.is_valid());
h.add_url_seed(seed.toStdString());
}
void QTorrentHandle::set_max_uploads(int val) {
Q_ASSERT(h.is_valid());
h.set_max_uploads(val);
}
void QTorrentHandle::set_max_connections(int val) {
Q_ASSERT(h.is_valid());
h.set_max_connections(val);
}
void QTorrentHandle::prioritize_files(const std::vector<int> &v) {
// Does not do anything for seeding torrents
Q_ASSERT(h.is_valid());
if(v.size() != (unsigned int)h.get_torrent_info().num_files())
return;
h.prioritize_files(v);
// Save seed status
TorrentPersistentData::saveSeedStatus(*this);
}
void QTorrentHandle::set_ratio(float ratio) const {
Q_ASSERT(h.is_valid());
h.set_ratio(ratio);
}
void QTorrentHandle::replace_trackers(const std::vector<announce_entry> & v) const {
Q_ASSERT(h.is_valid());
h.replace_trackers(v);
}
void QTorrentHandle::auto_managed(bool b) const {
Q_ASSERT(h.is_valid());
h.auto_managed(b);
}
void QTorrentHandle::queue_position_down() const {
Q_ASSERT(h.is_valid());
h.queue_position_down();
}
void QTorrentHandle::queue_position_up() const {
Q_ASSERT(h.is_valid());
if(h.queue_position() > 0)
h.queue_position_up();
}
void QTorrentHandle::queue_position_top() const {
Q_ASSERT(h.is_valid());
h.queue_position_top();
}
void QTorrentHandle::queue_position_bottom() const {
Q_ASSERT(h.is_valid());
h.queue_position_bottom();
}
void QTorrentHandle::force_reannounce() {
Q_ASSERT(h.is_valid());
h.force_reannounce();
}
void QTorrentHandle::set_sequential_download(bool b) {
Q_ASSERT(h.is_valid());
h.set_sequential_download(b);
}
void QTorrentHandle::set_tracker_login(QString username, QString password) {
Q_ASSERT(h.is_valid());
h.set_tracker_login(std::string(username.toLocal8Bit().constData()), std::string(password.toLocal8Bit().constData()));
}
void QTorrentHandle::force_recheck() const {
Q_ASSERT(h.is_valid());
h.force_recheck();
}
void QTorrentHandle::move_storage(QString new_path) const {
Q_ASSERT(h.is_valid());
if(QDir(save_path()) == QDir(new_path)) return;
TorrentPersistentData::setPreviousSavePath(hash(), save_path());
h.move_storage(new_path.toLocal8Bit().constData());
}
void QTorrentHandle::file_priority(int index, int priority) const {
Q_ASSERT(h.is_valid());
h.file_priority(index, priority);
// Save seed status
TorrentPersistentData::saveSeedStatus(*this);
}
#if LIBTORRENT_VERSION_MINOR > 14
void QTorrentHandle::super_seeding(bool on) const {
Q_ASSERT(h.is_valid());
h.super_seeding(on);
}
void QTorrentHandle::flush_cache() const {
Q_ASSERT(h.is_valid());
h.flush_cache();
}
#endif
#ifndef DISABLE_GUI
void QTorrentHandle::resolve_countries(bool r) {
Q_ASSERT(h.is_valid());
h.resolve_countries(r);
}
#endif
void QTorrentHandle::connect_peer(libtorrent::asio::ip::tcp::endpoint const& adr, int source) const {
Q_ASSERT(h.is_valid());
h.connect_peer(adr, source);
}
void QTorrentHandle::set_peer_upload_limit(libtorrent::asio::ip::tcp::endpoint ip, int limit) const {
Q_ASSERT(h.is_valid());
h.set_peer_upload_limit(ip, limit);
}
bool QTorrentHandle::save_torrent_file(QString path) {
if(!h.has_metadata()) return false;
QFile met_file(path);
if(met_file.open(QIODevice::WriteOnly)) {
entry meta = bdecode(h.get_torrent_info().metadata().get(), h.get_torrent_info().metadata().get()+h.get_torrent_info().metadata_size());
entry torrent_file(entry::dictionary_t);
torrent_file["info"] = meta;
if(!h.trackers().empty())
torrent_file["announce"] = h.trackers().front().url;
boost::filesystem::ofstream out(path.toLocal8Bit().constData(), std::ios_base::binary);
out.unsetf(std::ios_base::skipws);
bencode(std::ostream_iterator<char>(out), torrent_file);
return true;
}
return false;
}
void QTorrentHandle::set_peer_download_limit(libtorrent::asio::ip::tcp::endpoint ip, int limit) const {
Q_ASSERT(h.is_valid());
h.set_peer_download_limit(ip, limit);
}
void QTorrentHandle::add_tracker(const announce_entry& url) {
Q_ASSERT(h.is_valid());
#if LIBTORRENT_VERSION_MINOR > 14
h.add_tracker(url);
#else
std::vector<announce_entry> trackers = h.trackers();
bool exists = false;
std::vector<announce_entry>::iterator it = trackers.begin();
while(it != trackers.end()) {
if(it->url == url.url) {
exists = true;
break;
}
it++;
}
if(!exists) {
trackers.push_back(url);
h.replace_trackers(trackers);
}
#endif
}
void QTorrentHandle::prioritize_first_last_piece(bool b) {
Q_ASSERT(h.is_valid());
// Detect main file
int rank=0;
int main_file_index = 0;
file_entry main_file = h.get_torrent_info().file_at(0);
torrent_info::file_iterator it = h.get_torrent_info().begin_files();
it++; ++rank;
while(it != h.get_torrent_info().end_files()) {
if(it->size > main_file.size) {
main_file = *it;
main_file_index = rank;
}
it++;
++rank;
}
qDebug("Main file in the torrent is %s", main_file.path.string().c_str());
// Determine the priority to set
int prio = 7; // MAX
if(!b) prio = h.file_priority(main_file_index);
// Determine the first and last piece of the main file
int piece_size = h.get_torrent_info().piece_length();
Q_ASSERT(piece_size>0);
int first_piece = floor((main_file.offset+1)/(double)piece_size);
Q_ASSERT(first_piece >= 0 && first_piece < h.get_torrent_info().num_pieces());
qDebug("First piece of the file is %d/%d", first_piece, h.get_torrent_info().num_pieces()-1);
int num_pieces_in_file = ceil(main_file.size/(double)piece_size);
int last_piece = first_piece+num_pieces_in_file-1;
Q_ASSERT(last_piece >= 0 && last_piece < h.get_torrent_info().num_pieces());
qDebug("last piece of the file is %d/%d", last_piece, h.get_torrent_info().num_pieces()-1);
h.piece_priority(first_piece, prio);
h.piece_priority(last_piece, prio);
}
void QTorrentHandle::rename_file(int index, QString name) {
h.rename_file(index, std::string(name.toUtf8().constData()));
}
//
// Operators
//
QTorrentHandle& QTorrentHandle::operator =(const torrent_handle& new_h) {
h = new_h;
return *this;
}
bool QTorrentHandle::operator ==(const QTorrentHandle& new_h) const{
QString hash = misc::toQString(h.info_hash());
return (hash == new_h.hash());
}

View File

@@ -0,0 +1,188 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
*
* 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.
*
* Contact : chris@qbittorrent.org
*/
#ifndef QTORRENTHANDLE_H
#define QTORRENTHANDLE_H
#include <libtorrent/version.hpp>
#include <libtorrent/torrent_handle.hpp>
#include <libtorrent/torrent_info.hpp>
using namespace libtorrent;
#include <QString>
class QStringList;
// A wrapper for torrent_handle in libtorrent
// to interact well with Qt types
class QTorrentHandle {
private:
torrent_handle h;
public:
//
// Constructors
//
QTorrentHandle() {}
explicit QTorrentHandle(torrent_handle h);
//
// Getters
//
const torrent_handle& get_torrent_handle() const;
const torrent_info& get_torrent_info() const;
QString hash() const;
QString name() const;
float progress() const;
bitfield pieces() const;
void piece_availability(std::vector<int>& avail) const;
void get_download_queue(std::vector<partial_piece_info>& queue) const;
QString current_tracker() const;
bool is_valid() const;
bool is_paused() const;
bool has_filtered_pieces() const;
size_type total_size() const;
size_type piece_length() const;
int num_pieces() const;
size_type total_wanted_done() const;
float download_payload_rate() const;
float upload_payload_rate() const;
int num_connections() const;
int connections_limit() const;
int num_peers() const;
int num_seeds() const;
int num_complete() const;
int num_incomplete() const;
void scrape_tracker() const;
QString save_path() const;
QStringList url_seeds() const;
size_type actual_size() const;
int download_limit() const;
int upload_limit() const;
int num_files() const;
bool has_metadata() const;
void save_resume_data() const;
int queue_position() const;
bool is_queued() const;
QString file_at(unsigned int index) const;
size_type filesize_at(unsigned int index) const;
const std::vector<announce_entry> trackers() const;
torrent_status::state_t state() const;
QString creator() const;
QString comment() const;
size_type total_failed_bytes() const;
size_type total_redundant_bytes() const;
void file_progress(std::vector<size_type>& fp) const;
size_type total_payload_download() const;
size_type total_payload_upload() const;
size_type all_time_upload() const;
size_type all_time_download() const;
size_type total_done() const;
QStringList files_path() const;
bool has_missing_files() const;
int num_uploads() const;
bool is_seed() const;
bool is_checking() const;
bool is_auto_managed() const;
qlonglong active_time() const;
qlonglong seeding_time() const;
std::vector<int> file_priorities() const;
bool is_sequential_download() const;
#if LIBTORRENT_VERSION_MINOR > 14
bool super_seeding() const;
#endif
QString creation_date() const;
QString next_announce() const;
qlonglong next_announce_s() const;
void get_peer_info(std::vector<peer_info>&) const;
#ifndef DISABLE_GUI
bool resolve_countries() const;
#endif
bool priv() const;
bool first_last_piece_first() const;
QString root_path() const;
QString firstFileSavePath() const;
bool has_error() const;
QString error() const;
void downloading_pieces(bitfield &bf) const;
//
// Setters
//
void set_download_limit(int limit);
void set_upload_limit(int limit);
void pause();
void resume();
void remove_url_seed(QString seed);
void add_url_seed(QString seed);
void set_max_uploads(int val);
void set_max_connections(int val);
void prioritize_files(const std::vector<int> &v);
void file_priority(int index, int priority) const;
void set_ratio(float ratio) const;
void replace_trackers(const std::vector<announce_entry>& trackers) const;
void force_reannounce();
void set_sequential_download(bool);
void set_tracker_login(QString username, QString password);
void queue_position_down() const;
void queue_position_up() const;
void queue_position_top() const;
void queue_position_bottom() const;
void auto_managed(bool) const;
void force_recheck() const;
void move_storage(QString path) const;
#if LIBTORRENT_VERSION_MINOR > 14
void super_seeding(bool on) const;
void flush_cache() const;
#endif
#ifndef DISABLE_GUI
void resolve_countries(bool r);
#endif
void connect_peer(libtorrent::asio::ip::tcp::endpoint const& adr, int source = 0) const;
void set_peer_upload_limit(libtorrent::asio::ip::tcp::endpoint ip, int limit) const;
void set_peer_download_limit(libtorrent::asio::ip::tcp::endpoint ip, int limit) const;
void add_tracker(const announce_entry& url);
void prioritize_first_last_piece(bool b);
void rename_file(int index, QString name);
bool save_torrent_file(QString path);
//
// Operators
//
QTorrentHandle& operator =(const torrent_handle& new_h);
bool operator ==(const QTorrentHandle& new_h) const;
};
#endif