2007-08-20 10:29:18 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
2009-04-05 21:00:55 +04:00
|
|
|
* 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.
|
|
|
|
*
|
2007-08-20 10:29:18 +04:00
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QString>
|
2007-08-23 18:04:53 +04:00
|
|
|
#include <QStringList>
|
2007-08-20 10:29:18 +04:00
|
|
|
#include <QFile>
|
2007-08-23 18:04:53 +04:00
|
|
|
#include <QDir>
|
2007-08-20 10:29:18 +04:00
|
|
|
#include <QByteArray>
|
2009-11-21 15:35:26 +03:00
|
|
|
#include <math.h>
|
2012-05-16 22:19:05 +04:00
|
|
|
#include "fs_utils.h"
|
2007-08-20 10:29:18 +04:00
|
|
|
#include "misc.h"
|
2010-07-25 18:55:30 +04:00
|
|
|
#include "preferences.h"
|
2007-08-20 10:29:18 +04:00
|
|
|
#include "qtorrenthandle.h"
|
2010-01-01 16:25:59 +03:00
|
|
|
#include "torrentpersistentdata.h"
|
2010-06-07 12:32:41 +04:00
|
|
|
#include <libtorrent/version.hpp>
|
2009-08-17 09:14:03 +04:00
|
|
|
#include <libtorrent/magnet_uri.hpp>
|
2009-11-21 15:35:26 +03:00
|
|
|
#include <libtorrent/torrent_info.hpp>
|
2010-01-20 00:47:53 +03:00
|
|
|
#include <libtorrent/bencode.hpp>
|
|
|
|
#include <libtorrent/entry.hpp>
|
2012-02-18 18:44:20 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR < 15
|
|
|
|
#include <boost/date_time/posix_time/posix_time_types.hpp>
|
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
|
2011-01-06 15:26:14 +03:00
|
|
|
#ifdef Q_WS_WIN
|
2011-01-06 16:57:11 +03:00
|
|
|
#include <Windows.h>
|
2011-01-06 15:26:14 +03:00
|
|
|
#endif
|
|
|
|
|
2010-11-23 00:55:32 +03:00
|
|
|
using namespace libtorrent;
|
2011-01-06 15:12:07 +03:00
|
|
|
using namespace std;
|
2010-11-23 00:55:32 +03:00
|
|
|
|
2012-02-21 21:49:43 +04:00
|
|
|
static QPair<int, int> get_file_extremity_pieces(const torrent_info& t, int file_index)
|
|
|
|
{
|
|
|
|
const int num_pieces = t.num_pieces();
|
|
|
|
const int piece_size = t.piece_length();
|
|
|
|
const file_entry& file = t.file_at(file_index);
|
|
|
|
|
|
|
|
// Determine the first and last piece of the file
|
|
|
|
int first_piece = floor((file.offset + 1) / (float) piece_size);
|
|
|
|
Q_ASSERT(first_piece >= 0 && first_piece < num_pieces);
|
|
|
|
qDebug("First piece of the file is %d/%d", first_piece, num_pieces - 1);
|
|
|
|
|
|
|
|
int num_pieces_in_file = ceil(file.size / (float) piece_size);
|
|
|
|
int last_piece = first_piece + num_pieces_in_file - 1;
|
|
|
|
Q_ASSERT(last_piece >= 0 && last_piece < num_pieces);
|
|
|
|
qDebug("last piece of the file is %d/%d", last_piece, num_pieces - 1);
|
|
|
|
|
|
|
|
return qMakePair(first_piece, last_piece);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTorrentHandle::QTorrentHandle(const torrent_handle& h): torrent_handle(h) {}
|
2007-08-20 10:29:18 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Getters
|
|
|
|
//
|
|
|
|
|
|
|
|
QString QTorrentHandle::hash() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQString(torrent_handle::info_hash());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::name() const {
|
2010-01-01 16:25:59 +03:00
|
|
|
QString name = TorrentPersistentData::getName(hash());
|
2012-02-20 21:30:53 +04:00
|
|
|
if (name.isEmpty()) {
|
2010-10-20 00:00:50 +04:00
|
|
|
name = misc::toQStringU(torrent_handle::name());
|
2010-01-01 16:25:59 +03:00
|
|
|
}
|
|
|
|
return name;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
QString QTorrentHandle::creation_date() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
2010-11-28 13:29:59 +03:00
|
|
|
boost::optional<time_t> t = torrent_handle::get_torrent_info().creation_date();
|
2012-05-15 20:57:31 +04:00
|
|
|
return t ? misc::toQString(*t) : "";
|
2010-11-28 13:29:59 +03:00
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
boost::optional<boost::posix_time::ptime> boostDate = torrent_handle::get_torrent_info().creation_date();
|
2012-05-20 17:37:22 +04:00
|
|
|
return boostDate ? misc::toQString(*boostDate) : "";
|
2010-11-28 13:29:59 +03:00
|
|
|
#endif
|
2009-11-12 10:42:21 +03:00
|
|
|
}
|
|
|
|
|
2010-05-18 13:35:55 +04:00
|
|
|
QString QTorrentHandle::next_announce() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return misc::userFriendlyDuration(torrent_handle::status(0x0).next_announce.total_seconds());
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::userFriendlyDuration(torrent_handle::status().next_announce.total_seconds());
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2010-05-18 13:35:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
qlonglong QTorrentHandle::next_announce_s() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).next_announce.total_seconds();
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().next_announce.total_seconds();
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2010-05-18 13:35:55 +04:00
|
|
|
}
|
|
|
|
|
2012-02-21 21:49:43 +04:00
|
|
|
float QTorrentHandle::progress() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
torrent_status st = torrent_handle::status(query_accurate_download_counters);
|
|
|
|
#else
|
|
|
|
torrent_status st = torrent_handle::status();
|
|
|
|
#endif
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!st.total_wanted)
|
2008-07-17 01:54:01 +04:00
|
|
|
return 0.;
|
2011-04-17 14:29:44 +04:00
|
|
|
if (st.total_wanted_done == st.total_wanted)
|
2008-08-17 07:47:52 +04:00
|
|
|
return 1.;
|
2012-02-21 21:49:43 +04:00
|
|
|
float progress = (float) st.total_wanted_done / (float) st.total_wanted;
|
2007-08-28 23:22:05 +04:00
|
|
|
Q_ASSERT(progress >= 0. && progress <= 1.);
|
|
|
|
return progress;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2008-11-02 00:42:56 +03:00
|
|
|
bitfield QTorrentHandle::pieces() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).pieces;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().pieces;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-11-18 21:06:44 +03:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QString QTorrentHandle::current_tracker() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return misc::toQString(torrent_handle::status(0x0).current_tracker);
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQString(torrent_handle::status().current_tracker);
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QTorrentHandle::is_paused() const {
|
2011-04-17 00:08:32 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
2011-04-17 14:29:44 +04:00
|
|
|
torrent_status st = torrent_handle::status(0x0);
|
|
|
|
return st.paused && !st.auto_managed;
|
2011-04-17 00:08:32 +04:00
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::is_paused() && !torrent_handle::is_auto_managed();
|
2011-04-17 00:08:32 +04:00
|
|
|
#endif
|
2008-11-02 14:43:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QTorrentHandle::is_queued() const {
|
2011-04-17 00:08:32 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
2011-04-17 14:29:44 +04:00
|
|
|
torrent_status st = torrent_handle::status(0x0);
|
|
|
|
return st.paused && st.auto_managed;
|
2011-04-17 00:08:32 +04:00
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::is_paused() && torrent_handle::is_auto_managed();
|
2011-04-17 00:08:32 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-11-18 21:06:44 +03:00
|
|
|
size_type QTorrentHandle::total_size() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::get_torrent_info().total_size();
|
2007-11-18 21:06:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_type QTorrentHandle::piece_length() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::get_torrent_info().piece_length();
|
2007-11-18 21:06:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_pieces() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::get_torrent_info().num_pieces();
|
2009-11-14 13:37:45 +03:00
|
|
|
}
|
|
|
|
|
2009-11-21 13:17:57 +03:00
|
|
|
bool QTorrentHandle::first_last_piece_first() const {
|
2012-02-21 21:49:43 +04:00
|
|
|
const torrent_info& t = get_torrent_info();
|
|
|
|
|
|
|
|
// Get int first media file
|
2010-12-04 23:47:20 +03:00
|
|
|
int index = 0;
|
2012-02-21 21:49:43 +04:00
|
|
|
for (index = 0; index < t.num_files(); ++index) {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
2012-02-21 21:49:43 +04:00
|
|
|
QString path = misc::toQStringU(t.file_at(index).path);
|
2011-04-17 14:29:44 +04:00
|
|
|
#else
|
2012-02-21 21:49:43 +04:00
|
|
|
QString path = misc::toQStringU(t.file_at(index).path.string());
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2012-05-16 22:19:05 +04:00
|
|
|
const QString ext = fsutils::fileExtension(path);
|
2012-02-21 21:49:43 +04:00
|
|
|
if (misc::isPreviewable(ext) && torrent_handle::file_priority(index) > 0)
|
2010-12-04 23:47:20 +03:00
|
|
|
break;
|
2009-11-21 15:35:26 +03:00
|
|
|
}
|
2012-02-21 21:49:43 +04:00
|
|
|
|
|
|
|
if (index >= t.num_files()) // No media file
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
QPair<int, int> extremities = get_file_extremity_pieces (t, index);
|
|
|
|
|
|
|
|
return (torrent_handle::piece_priority(extremities.first) == 7)
|
|
|
|
&& (torrent_handle::piece_priority(extremities.second) == 7);
|
2009-11-21 13:17:57 +03:00
|
|
|
}
|
|
|
|
|
2007-10-10 23:10:34 +04:00
|
|
|
size_type QTorrentHandle::total_wanted_done() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(query_accurate_download_counters).total_wanted_done;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_wanted_done;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-11-16 20:43:54 +03:00
|
|
|
size_type QTorrentHandle::total_wanted() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).total_wanted;
|
|
|
|
#else
|
2010-11-16 20:43:54 +03:00
|
|
|
return torrent_handle::status().total_wanted;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2010-11-16 20:43:54 +03:00
|
|
|
}
|
|
|
|
|
2011-01-25 20:01:09 +03:00
|
|
|
qreal QTorrentHandle::download_payload_rate() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).download_payload_rate;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().download_payload_rate;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2011-01-25 20:01:09 +03:00
|
|
|
qreal QTorrentHandle::upload_payload_rate() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).upload_payload_rate;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().upload_payload_rate;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_peers() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).num_peers;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_peers;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_seeds() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).num_seeds;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_seeds;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-07-12 09:37:09 +04:00
|
|
|
int QTorrentHandle::num_complete() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).num_complete;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_complete;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2009-07-12 09:37:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_incomplete() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).num_incomplete;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_incomplete;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2009-07-12 09:37:09 +04:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QString QTorrentHandle::save_path() const {
|
2010-11-28 13:29:59 +03:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return misc::toQStringU(torrent_handle::save_path()).replace("\\", "/");
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQStringU(torrent_handle::save_path().string()).replace("\\", "/");
|
2010-11-28 13:29:59 +03:00
|
|
|
#endif
|
2009-10-22 00:47:46 +04:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QStringList QTorrentHandle::url_seeds() const {
|
|
|
|
QStringList res;
|
2009-08-17 09:14:03 +04:00
|
|
|
try {
|
2010-10-20 00:00:50 +04:00
|
|
|
const std::set<std::string> existing_seeds = torrent_handle::url_seeds();
|
2010-10-10 19:39:08 +04:00
|
|
|
std::set<std::string>::const_iterator it;
|
2012-02-20 21:30:53 +04:00
|
|
|
for (it = existing_seeds.begin(); it != existing_seeds.end(); it++) {
|
2010-10-10 19:39:08 +04:00
|
|
|
qDebug("URL Seed: %s", it->c_str());
|
|
|
|
res << misc::toQString(*it);
|
2009-08-17 09:14:03 +04:00
|
|
|
}
|
2010-10-10 19:39:08 +04:00
|
|
|
} catch(std::exception e) {
|
|
|
|
std::cout << "ERROR: Failed to convert the URL seed" << std::endl;
|
|
|
|
}
|
2007-08-20 10:29:18 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the size of the torrent without the filtered files
|
2011-04-17 14:29:44 +04:00
|
|
|
size_type QTorrentHandle::actual_size() const {
|
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(query_accurate_download_counters).total_wanted;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_wanted;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-08-29 18:30:30 +04:00
|
|
|
bool QTorrentHandle::has_filtered_pieces() const {
|
2012-02-21 21:49:43 +04:00
|
|
|
const std::vector<int> piece_priorities = torrent_handle::piece_priorities();
|
|
|
|
foreach (const int priority, piece_priorities) {
|
|
|
|
if (priority == 0)
|
|
|
|
return true;
|
2007-08-29 18:30:30 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
int QTorrentHandle::num_files() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::get_torrent_info().num_files();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-10-23 20:21:56 +04:00
|
|
|
QString QTorrentHandle::filename_at(unsigned int index) const {
|
2010-10-20 00:00:50 +04:00
|
|
|
Q_ASSERT(index < (unsigned int)torrent_handle::get_torrent_info().num_files());
|
2010-11-28 13:29:59 +03:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
2012-05-16 22:19:05 +04:00
|
|
|
return fsutils::fileName(filepath_at(index));
|
2010-11-28 13:29:59 +03:00
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().file_at(index).path.leaf());
|
2010-11-28 13:29:59 +03:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-08-29 20:07:37 +04:00
|
|
|
size_type QTorrentHandle::filesize_at(unsigned int index) const {
|
2010-10-20 00:00:50 +04:00
|
|
|
Q_ASSERT(index < (unsigned int)torrent_handle::get_torrent_info().num_files());
|
|
|
|
return torrent_handle::get_torrent_info().file_at(index).size;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-11-28 13:29:59 +03:00
|
|
|
QString QTorrentHandle::filepath_at(unsigned int index) const {
|
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
2011-04-17 14:29:44 +04:00
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().file_at(index).path);
|
2010-11-28 13:29:59 +03:00
|
|
|
#else
|
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().file_at(index).path.string());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::orig_filepath_at(unsigned int index) const {
|
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
2011-04-17 14:29:44 +04:00
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().orig_files().at(index).path);
|
2010-11-28 13:29:59 +03:00
|
|
|
#else
|
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().orig_files().at(index).path.string());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
torrent_status::state_t QTorrentHandle::state() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).state;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().state;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2009-08-16 07:09:20 +04:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QString QTorrentHandle::creator() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().creator());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::comment() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().comment());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_type QTorrentHandle::total_failed_bytes() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).total_failed_bytes;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_failed_bytes;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
size_type QTorrentHandle::total_redundant_bytes() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).total_redundant_bytes;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_redundant_bytes;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-11-17 14:46:43 +03:00
|
|
|
bool QTorrentHandle::is_checking() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
torrent_status st = torrent_handle::status(0x0);
|
|
|
|
#else
|
|
|
|
torrent_status st = torrent_handle::status();
|
|
|
|
#endif
|
|
|
|
return st.state == torrent_status::checking_files || st.state == torrent_status::checking_resume_data;
|
2009-11-17 14:46:43 +03:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
size_type QTorrentHandle::total_done() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).total_done;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_done;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2010-02-17 11:10:57 +03:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
size_type QTorrentHandle::all_time_download() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).all_time_download;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().all_time_download;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2008-11-02 01:07:14 +03:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
size_type QTorrentHandle::all_time_upload() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).all_time_upload;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().all_time_upload;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2008-11-02 01:07:14 +03:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
size_type QTorrentHandle::total_payload_download() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).total_payload_download;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_payload_download;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
size_type QTorrentHandle::total_payload_upload() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).total_payload_upload;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_payload_upload;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-08-23 18:04:53 +04:00
|
|
|
// Return a list of absolute paths corresponding
|
|
|
|
// to all files in a torrent
|
2011-04-17 14:29:44 +04:00
|
|
|
QStringList QTorrentHandle::absolute_files_path() const {
|
2010-10-18 21:44:37 +04:00
|
|
|
QDir saveDir(save_path());
|
2007-08-23 18:04:53 +04:00
|
|
|
QStringList res;
|
2012-02-20 21:30:53 +04:00
|
|
|
for (int i = 0; i<num_files(); ++i) {
|
2011-04-17 14:29:44 +04:00
|
|
|
res << QDir::cleanPath(saveDir.absoluteFilePath(filepath_at(i)));
|
2007-08-23 18:04:53 +04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-04-17 14:29:44 +04:00
|
|
|
QStringList QTorrentHandle::absolute_files_path_uneeded() const {
|
2010-10-18 21:44:37 +04:00
|
|
|
QDir saveDir(save_path());
|
|
|
|
QStringList res;
|
2010-10-20 00:00:50 +04:00
|
|
|
std::vector<int> fp = torrent_handle::file_priorities();
|
2012-02-20 21:30:53 +04:00
|
|
|
for (uint i = 0; i < fp.size(); ++i) {
|
|
|
|
if (fp[i] == 0) {
|
2011-04-17 14:29:44 +04:00
|
|
|
const QString file_path = QDir::cleanPath(saveDir.absoluteFilePath(filepath_at(i)));
|
2012-02-20 21:30:53 +04:00
|
|
|
if (file_path.contains(".unwanted"))
|
2011-04-10 19:00:27 +04:00
|
|
|
res << file_path;
|
|
|
|
}
|
2010-10-18 21:44:37 +04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-08-22 00:22:59 +04:00
|
|
|
bool QTorrentHandle::has_missing_files() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
const QStringList paths = absolute_files_path();
|
2012-02-20 21:30:53 +04:00
|
|
|
foreach (const QString &path, paths) {
|
|
|
|
if (!QFile::exists(path)) return true;
|
2010-08-22 00:22:59 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-02 13:47:59 +03:00
|
|
|
int QTorrentHandle::queue_position() const {
|
2012-02-20 21:30:53 +04:00
|
|
|
if (torrent_handle::queue_position() < 0)
|
2010-05-21 15:58:14 +04:00
|
|
|
return -1;
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::queue_position()+1;
|
2008-11-02 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
2007-08-24 16:25:52 +04:00
|
|
|
int QTorrentHandle::num_uploads() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).num_uploads;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_uploads;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2007-08-24 16:25:52 +04:00
|
|
|
}
|
|
|
|
|
2008-09-03 22:19:17 +04:00
|
|
|
bool QTorrentHandle::is_seed() const {
|
2008-12-26 22:14:19 +03:00
|
|
|
// Affected by bug http://code.rasterbar.com/libtorrent/ticket/402
|
2010-10-20 00:00:50 +04:00
|
|
|
//return torrent_handle::is_seed();
|
2008-12-27 12:10:07 +03:00
|
|
|
// May suffer from approximation problems
|
|
|
|
//return (progress() == 1.);
|
|
|
|
// This looks safe
|
2012-02-21 21:49:43 +04:00
|
|
|
torrent_status::state_t st = state();
|
|
|
|
return (st == torrent_status::finished || st == torrent_status::seeding);
|
2008-09-03 22:19:17 +04:00
|
|
|
}
|
|
|
|
|
2008-11-02 14:43:20 +03:00
|
|
|
bool QTorrentHandle::is_auto_managed() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
torrent_status status = torrent_handle::status(0x0);
|
|
|
|
return status.auto_managed;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::is_auto_managed();
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2008-11-02 14:43:20 +03:00
|
|
|
}
|
|
|
|
|
2011-04-17 14:36:50 +04:00
|
|
|
bool QTorrentHandle::is_sequential_download() const {
|
2011-04-17 14:36:28 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
torrent_status status = torrent_handle::status(0x0);
|
|
|
|
return status.sequential_download;
|
|
|
|
#else
|
|
|
|
return torrent_handle::is_sequential_download();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
qlonglong QTorrentHandle::active_time() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).active_time;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().active_time;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2008-11-02 16:07:38 +03:00
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
qlonglong QTorrentHandle::seeding_time() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).seeding_time;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().seeding_time;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2009-11-12 10:42:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_connections() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).num_connections;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_connections;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2009-11-12 10:42:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::connections_limit() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).connections_limit;
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().connections_limit;
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2009-11-15 13:59:11 +03:00
|
|
|
}
|
|
|
|
|
2009-11-19 18:43:00 +03:00
|
|
|
bool QTorrentHandle::priv() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::get_torrent_info().priv();
|
2009-11-19 18:43:00 +03:00
|
|
|
}
|
|
|
|
|
2010-06-27 21:58:08 +04:00
|
|
|
QString QTorrentHandle::firstFileSavePath() const {
|
|
|
|
Q_ASSERT(has_metadata());
|
|
|
|
QString fsave_path = TorrentPersistentData::getSavePath(hash());
|
2012-02-20 21:30:53 +04:00
|
|
|
if (fsave_path.isEmpty())
|
2010-06-27 21:58:08 +04:00
|
|
|
fsave_path = save_path();
|
2011-09-25 16:04:51 +04:00
|
|
|
fsave_path.replace("\\", "/");
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!fsave_path.endsWith("/"))
|
2010-06-27 21:58:08 +04:00
|
|
|
fsave_path += "/";
|
2010-11-28 13:29:59 +03:00
|
|
|
fsave_path += filepath_at(0);
|
2010-07-25 18:00:35 +04:00
|
|
|
// Remove .!qB extension
|
2012-02-20 21:30:53 +04:00
|
|
|
if (fsave_path.endsWith(".!qB", Qt::CaseInsensitive))
|
2010-07-25 18:00:35 +04:00
|
|
|
fsave_path.chop(4);
|
2010-06-27 21:58:08 +04:00
|
|
|
return fsave_path;
|
|
|
|
}
|
|
|
|
|
2010-01-31 22:03:34 +03:00
|
|
|
bool QTorrentHandle::has_error() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
torrent_status st = torrent_handle::status(0x0);
|
|
|
|
return st.paused && !st.error.empty();
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::is_paused() && !torrent_handle::status().error.empty();
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2010-05-24 18:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::error() const {
|
2011-04-17 14:29:44 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return misc::toQString(torrent_handle::status(0x0).error);
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQString(torrent_handle::status().error);
|
2011-04-17 14:29:44 +04:00
|
|
|
#endif
|
2010-01-31 22:03:34 +03:00
|
|
|
}
|
|
|
|
|
2010-03-19 01:58:32 +03:00
|
|
|
void QTorrentHandle::downloading_pieces(bitfield &bf) const {
|
|
|
|
std::vector<partial_piece_info> queue;
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::get_download_queue(queue);
|
2012-02-21 21:49:43 +04:00
|
|
|
for (std::vector<partial_piece_info>::const_iterator it=queue.begin(); it!= queue.end(); it++) {
|
2010-03-19 01:58:32 +03:00
|
|
|
bf.set_bit(it->piece_index);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-17 00:08:32 +04:00
|
|
|
bool QTorrentHandle::has_metadata() const {
|
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
2011-04-17 19:00:48 +04:00
|
|
|
return torrent_handle::status(query_distributed_copies).has_metadata;
|
2011-04-17 00:08:32 +04:00
|
|
|
#else
|
|
|
|
return torrent_handle::has_metadata();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-17 19:00:48 +04:00
|
|
|
float QTorrentHandle::distributed_copies() const {
|
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return torrent_handle::status(0x0).distributed_copies;
|
|
|
|
#else
|
|
|
|
return torrent_handle::status().distributed_copies;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-06-05 20:08:30 +04:00
|
|
|
void QTorrentHandle::file_progress(std::vector<size_type>& fp) const {
|
2012-02-18 18:44:20 +04:00
|
|
|
torrent_handle::file_progress(fp, torrent_handle::piece_granularity);
|
2011-06-05 20:08:30 +04:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
//
|
|
|
|
// Setters
|
|
|
|
//
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
void QTorrentHandle::pause() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::auto_managed(false);
|
|
|
|
torrent_handle::pause();
|
|
|
|
torrent_handle::save_resume_data();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
void QTorrentHandle::resume() const {
|
2012-02-21 21:49:43 +04:00
|
|
|
if (has_error())
|
|
|
|
torrent_handle::clear_error();
|
|
|
|
|
2010-07-25 18:55:30 +04:00
|
|
|
const QString torrent_hash = hash();
|
|
|
|
bool has_persistant_error = TorrentPersistentData::hasError(torrent_hash);
|
|
|
|
TorrentPersistentData::setErrorState(torrent_hash, false);
|
2010-11-16 23:34:31 +03:00
|
|
|
bool temp_path_enabled = Preferences().isTempPathEnabled();
|
2012-02-20 21:30:53 +04:00
|
|
|
if (has_persistant_error && temp_path_enabled) {
|
2010-07-25 18:55:30 +04:00
|
|
|
// 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));
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!final_save_path.isEmpty())
|
2010-07-25 18:55:30 +04:00
|
|
|
move_storage(final_save_path);
|
|
|
|
}
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::auto_managed(true);
|
|
|
|
torrent_handle::resume();
|
2012-02-20 21:30:53 +04:00
|
|
|
if (has_persistant_error && temp_path_enabled) {
|
2010-07-25 18:55:30 +04:00
|
|
|
// Force recheck
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::force_recheck();
|
2010-07-25 18:55:30 +04:00
|
|
|
}
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2012-02-21 21:49:43 +04:00
|
|
|
void QTorrentHandle::remove_url_seed(const QString& seed) const {
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::remove_url_seed(seed.toStdString());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2012-02-21 21:49:43 +04:00
|
|
|
void QTorrentHandle::add_url_seed(const QString& seed) const {
|
2010-10-10 19:39:08 +04:00
|
|
|
const std::string str_seed = seed.toStdString();
|
2010-10-20 00:00:50 +04:00
|
|
|
qDebug("calling torrent_handle::add_url_seed(%s)", str_seed.c_str());
|
|
|
|
torrent_handle::add_url_seed(str_seed);
|
2007-09-09 11:44:22 +04:00
|
|
|
}
|
|
|
|
|
2012-02-21 21:49:43 +04:00
|
|
|
void QTorrentHandle::set_tracker_login(const QString& username, const QString& password) const {
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::set_tracker_login(std::string(username.toLocal8Bit().constData()), std::string(password.toLocal8Bit().constData()));
|
2008-11-02 16:53:45 +03:00
|
|
|
}
|
|
|
|
|
2012-02-21 21:49:43 +04:00
|
|
|
void QTorrentHandle::move_storage(const QString& new_path) const {
|
|
|
|
if (QDir(save_path()) == QDir(new_path))
|
|
|
|
return;
|
|
|
|
|
2010-07-18 01:35:00 +04:00
|
|
|
TorrentPersistentData::setPreviousSavePath(hash(), save_path());
|
2010-10-17 19:17:39 +04:00
|
|
|
// Create destination directory if necessary
|
|
|
|
// or move_storage() will fail...
|
|
|
|
QDir().mkpath(new_path);
|
|
|
|
// Actually move the storage
|
2011-01-28 20:05:44 +03:00
|
|
|
torrent_handle::move_storage(new_path.toUtf8().constData());
|
2008-11-02 22:55:55 +03:00
|
|
|
}
|
|
|
|
|
2012-02-21 21:49:43 +04:00
|
|
|
bool QTorrentHandle::save_torrent_file(const QString& path) const {
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!has_metadata()) return false;
|
2011-10-25 21:56:54 +04:00
|
|
|
|
2012-02-21 21:49:43 +04:00
|
|
|
const torrent_info& t = torrent_handle::get_torrent_info();
|
|
|
|
|
|
|
|
entry meta = bdecode(t.metadata().get(),
|
|
|
|
t.metadata().get() + t.metadata_size());
|
2011-10-25 21:56:54 +04:00
|
|
|
entry torrent_entry(entry::dictionary_t);
|
|
|
|
torrent_entry["info"] = meta;
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!torrent_handle::trackers().empty())
|
2011-10-25 21:56:54 +04:00
|
|
|
torrent_entry["announce"] = torrent_handle::trackers().front().url;
|
|
|
|
|
|
|
|
vector<char> out;
|
|
|
|
bencode(back_inserter(out), torrent_entry);
|
|
|
|
QFile torrent_file(path);
|
|
|
|
if (!out.empty() && torrent_file.open(QIODevice::WriteOnly)) {
|
|
|
|
torrent_file.write(&out[0], out.size());
|
|
|
|
torrent_file.close();
|
2010-01-20 00:47:53 +03:00
|
|
|
return true;
|
|
|
|
}
|
2011-10-25 21:56:54 +04:00
|
|
|
|
2010-01-20 00:47:53 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-06 15:12:07 +03:00
|
|
|
void QTorrentHandle::file_priority(int index, int priority) const {
|
|
|
|
vector<int> priorities = torrent_handle::file_priorities();
|
2012-02-20 21:30:53 +04:00
|
|
|
if (priorities[index] != priority) {
|
2011-01-06 15:12:07 +03:00
|
|
|
priorities[index] = priority;
|
|
|
|
prioritize_files(priorities);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::prioritize_files(const vector<int> &files) const {
|
2012-02-20 21:30:53 +04:00
|
|
|
if ((int)files.size() != torrent_handle::get_torrent_info().num_files()) return;
|
2011-01-08 16:31:57 +03:00
|
|
|
qDebug() << Q_FUNC_INFO;
|
2011-01-06 15:12:07 +03:00
|
|
|
bool was_seed = is_seed();
|
2011-01-08 16:48:29 +03:00
|
|
|
vector<size_type> progress;
|
2011-06-05 20:08:30 +04:00
|
|
|
file_progress(progress);
|
2012-02-15 23:13:42 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << "Changing files priorities...";
|
2011-01-06 15:12:07 +03:00
|
|
|
torrent_handle::prioritize_files(files);
|
2012-02-15 23:13:42 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << "Moving unwanted files to .unwanted folder...";
|
2012-02-21 21:49:43 +04:00
|
|
|
for (uint i = 0; i < files.size(); ++i) {
|
2011-01-06 15:12:07 +03:00
|
|
|
// Move unwanted files to a .unwanted subfolder
|
2012-02-20 21:30:53 +04:00
|
|
|
if (files[i] == 0 && progress[i] < filesize_at(i)) {
|
2011-01-06 15:12:07 +03:00
|
|
|
QString old_path = filepath_at(i);
|
2011-04-10 19:00:27 +04:00
|
|
|
// Make sure the file does not already exists
|
2012-02-20 21:30:53 +04:00
|
|
|
if (QFile::exists(QDir(save_path()).absoluteFilePath(old_path))) {
|
2011-04-10 19:00:27 +04:00
|
|
|
qWarning() << "File" << old_path << "already exists at destination.";
|
|
|
|
qWarning() << "We do not move it to .unwanted folder";
|
|
|
|
continue;
|
|
|
|
}
|
2011-01-06 15:12:07 +03:00
|
|
|
QString old_name = filename_at(i);
|
2012-05-16 22:19:05 +04:00
|
|
|
QString parent_path = fsutils::branchPath(old_path);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (parent_path.isEmpty() || QDir(parent_path).dirName() != ".unwanted") {
|
2011-01-06 16:57:11 +03:00
|
|
|
QString unwanted_abspath = QDir::cleanPath(save_path()+"/"+parent_path+"/.unwanted");
|
|
|
|
qDebug() << "Unwanted path is" << unwanted_abspath;
|
|
|
|
bool created = QDir().mkpath(unwanted_abspath);
|
2011-01-06 15:26:14 +03:00
|
|
|
#ifdef Q_WS_WIN
|
2011-01-06 16:57:11 +03:00
|
|
|
qDebug() << "unwanted folder was created:" << created;
|
2012-02-20 21:30:53 +04:00
|
|
|
if (created) {
|
2011-01-06 16:57:11 +03:00
|
|
|
// Hide the folder on Windows
|
|
|
|
qDebug() << "Hiding folder (Windows)";
|
|
|
|
wstring win_path = unwanted_abspath.replace("/","\\").toStdWString();
|
|
|
|
DWORD dwAttrs = GetFileAttributesW(win_path.c_str());
|
|
|
|
bool ret = SetFileAttributesW(win_path.c_str(), dwAttrs|FILE_ATTRIBUTE_HIDDEN);
|
|
|
|
Q_ASSERT(ret != 0); Q_UNUSED(ret);
|
|
|
|
}
|
2011-01-06 15:26:14 +03:00
|
|
|
#else
|
|
|
|
Q_UNUSED(created);
|
|
|
|
#endif
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!parent_path.isEmpty() && !parent_path.endsWith("/"))
|
2011-01-06 16:57:11 +03:00
|
|
|
parent_path += "/";
|
|
|
|
rename_file(i, parent_path+".unwanted/"+old_name);
|
2011-01-06 15:12:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Move wanted files back to their original folder
|
2012-02-15 23:13:42 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << "Moving wanted files back from .unwanted folder";
|
2012-02-20 21:30:53 +04:00
|
|
|
if (files[i] > 0) {
|
2012-05-16 22:19:05 +04:00
|
|
|
QString parent_relpath = fsutils::branchPath(filepath_at(i));
|
2012-02-20 21:30:53 +04:00
|
|
|
if (QDir(parent_relpath).dirName() == ".unwanted") {
|
2012-02-15 23:13:42 +04:00
|
|
|
QString old_name = filename_at(i);
|
2012-05-16 22:19:05 +04:00
|
|
|
QString new_relpath = fsutils::branchPath(parent_relpath);
|
2012-02-15 23:13:42 +04:00
|
|
|
if (new_relpath.isEmpty())
|
|
|
|
rename_file(i, old_name);
|
|
|
|
else
|
|
|
|
rename_file(i, QDir(new_relpath).filePath(old_name));
|
2011-01-06 15:12:07 +03:00
|
|
|
// Remove .unwanted directory if empty
|
2012-02-15 23:13:42 +04:00
|
|
|
qDebug() << "Attempting to remove .unwanted folder at " << QDir(save_path() + QDir::separator() + new_relpath).absoluteFilePath(".unwanted");
|
|
|
|
QDir(save_path() + QDir::separator() + new_relpath).rmdir(".unwanted");
|
2011-01-06 15:12:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-15 23:13:42 +04:00
|
|
|
|
2012-02-20 21:30:53 +04:00
|
|
|
if (was_seed && !is_seed()) {
|
2012-02-15 23:13:42 +04:00
|
|
|
qDebug() << "Torrent is no longer SEEDING";
|
2011-01-06 15:12:07 +03:00
|
|
|
// Save seed status
|
|
|
|
TorrentPersistentData::saveSeedStatus(*this);
|
|
|
|
// Move to temp folder if necessary
|
|
|
|
const Preferences pref;
|
2012-02-20 21:30:53 +04:00
|
|
|
if (pref.isTempPathEnabled()) {
|
2011-01-06 15:12:07 +03:00
|
|
|
QString tmp_path = pref.getTempPath();
|
2012-02-15 23:13:42 +04:00
|
|
|
qDebug() << "tmp folder is enabled, move torrent to " << tmp_path << " from " << save_path();
|
2011-01-06 15:12:07 +03:00
|
|
|
move_storage(tmp_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:47:20 +03:00
|
|
|
void QTorrentHandle::prioritize_first_last_piece(int file_index, bool b) const {
|
2009-11-21 15:35:26 +03:00
|
|
|
// Determine the priority to set
|
2012-02-21 21:49:43 +04:00
|
|
|
int prio = b ? 7 : torrent_handle::file_priority(file_index);
|
|
|
|
|
|
|
|
QPair<int, int> extremities = get_file_extremity_pieces (get_torrent_info(), file_index);
|
|
|
|
piece_priority(extremities.first, prio);
|
|
|
|
piece_priority(extremities.second, prio);
|
2009-11-21 13:17:57 +03:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:47:20 +03:00
|
|
|
void QTorrentHandle::prioritize_first_last_piece(bool b) const {
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!has_metadata()) return;
|
2010-12-04 23:47:20 +03:00
|
|
|
// Download first and last pieces first for all media files in the torrent
|
2012-02-21 21:49:43 +04:00
|
|
|
const uint nbfiles = num_files();
|
|
|
|
for (uint index = 0; index < nbfiles; ++index) {
|
2011-04-17 14:29:44 +04:00
|
|
|
const QString path = filepath_at(index);
|
2012-05-16 22:19:05 +04:00
|
|
|
const QString ext = fsutils::fileExtension(path);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (misc::isPreviewable(ext) && torrent_handle::file_priority(index) > 0) {
|
2011-04-17 14:29:44 +04:00
|
|
|
qDebug() << "File" << path << "is previewable, toggle downloading of first/last pieces first";
|
2010-12-04 23:47:20 +03:00
|
|
|
prioritize_first_last_piece(index, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-21 21:49:43 +04:00
|
|
|
void QTorrentHandle::rename_file(int index, const QString& name) const {
|
2011-01-06 16:57:11 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << index << name;
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::rename_file(index, std::string(name.toUtf8().constData()));
|
2009-12-18 16:36:47 +03:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
//
|
|
|
|
// Operators
|
|
|
|
//
|
|
|
|
|
2012-02-20 23:32:58 +04:00
|
|
|
bool QTorrentHandle::operator ==(const QTorrentHandle& new_h) const {
|
2012-02-21 21:49:43 +04:00
|
|
|
return info_hash() == new_h.info_hash();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|