2008-05-17 17:23:37 +04:00
|
|
|
/*
|
2009-04-05 22:48:45 +04:00
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2006 Ishan Arora and Christophe Dumez
|
2008-05-17 17:23:37 +04:00
|
|
|
*
|
2009-04-05 22:48:45 +04:00
|
|
|
* 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.
|
2008-05-17 17:23:37 +04:00
|
|
|
*
|
2009-04-05 22:48:45 +04:00
|
|
|
* 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.
|
2008-05-17 17:23:37 +04:00
|
|
|
*
|
2009-04-05 22:48:45 +04:00
|
|
|
* 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
|
2008-05-17 17:23:37 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "eventmanager.h"
|
2008-08-08 17:17:26 +04:00
|
|
|
#include "bittorrent.h"
|
2009-11-23 13:37:55 +03:00
|
|
|
#include "misc.h"
|
2009-11-24 11:53:14 +03:00
|
|
|
#include "torrentpersistentdata.h"
|
2008-05-17 17:23:37 +04:00
|
|
|
#include <QDebug>
|
|
|
|
|
2009-11-20 11:01:59 +03:00
|
|
|
EventManager::EventManager(QObject *parent, Bittorrent *BTSession)
|
2009-11-19 21:28:30 +03:00
|
|
|
: QObject(parent), BTSession(BTSession)
|
2008-05-17 17:23:37 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-12-25 17:33:37 +03:00
|
|
|
QList<QVariantMap> EventManager::getEventList() const {
|
2009-11-19 21:28:30 +03:00
|
|
|
return event_list.values();
|
2008-05-17 17:23:37 +04:00
|
|
|
}
|
|
|
|
|
2009-11-24 14:14:02 +03:00
|
|
|
QList<QVariantMap> EventManager::getPropTrackersInfo(QString hash) const {
|
|
|
|
QList<QVariantMap> trackersInfo;
|
|
|
|
QTorrentHandle h = BTSession->getTorrentHandle(hash);
|
|
|
|
if(h.is_valid()) {
|
|
|
|
QHash<QString, TrackerInfos> trackers_data = BTSession->getTrackersInfo(hash);
|
|
|
|
std::vector<announce_entry> vect_trackers = h.trackers();
|
|
|
|
std::vector<announce_entry>::iterator it;
|
|
|
|
for(it = vect_trackers.begin(); it != vect_trackers.end(); it++) {
|
|
|
|
QVariantMap tracker;
|
|
|
|
QString tracker_url = misc::toQString(it->url);
|
|
|
|
tracker["url"] = tracker_url;
|
|
|
|
TrackerInfos data = trackers_data.value(tracker_url, TrackerInfos(tracker_url));
|
|
|
|
QString error_message = data.last_message.trimmed();
|
|
|
|
#ifdef LIBTORRENT_0_15
|
|
|
|
if(it->verified) {
|
|
|
|
tracker["status"] = tr("Working");
|
|
|
|
} else {
|
|
|
|
if(it->updating && it->fails == 0) {
|
|
|
|
tracker["status"] = tr("Updating...");
|
|
|
|
} else {
|
|
|
|
if(it->fails > 0) {
|
|
|
|
tracker["status"] = tr("Not working");
|
|
|
|
} else {
|
|
|
|
tracker["status"] = tr("Not contacted yet");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if(data.verified) {
|
|
|
|
tracker["status"] = tr("Working");
|
|
|
|
} else {
|
|
|
|
if(data.fail_count > 0)
|
|
|
|
tracker["status"] = tr("Not working");
|
|
|
|
else
|
|
|
|
tracker["status"] = tr("Not contacted yet");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
tracker["num_peers"] = QString::number(trackers_data.value(tracker_url, TrackerInfos(tracker_url)).num_peers);
|
|
|
|
tracker["msg"] = error_message;
|
|
|
|
trackersInfo << tracker;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return trackersInfo;
|
|
|
|
}
|
|
|
|
|
2009-11-24 11:53:14 +03:00
|
|
|
QVariantMap EventManager::getPropGeneralInfo(QString hash) const {
|
|
|
|
QVariantMap data;
|
|
|
|
QTorrentHandle h = BTSession->getTorrentHandle(hash);
|
|
|
|
if(h.is_valid()) {
|
|
|
|
// Save path
|
|
|
|
data["save_path"] = TorrentPersistentData::getSavePath(hash);
|
|
|
|
// Creation date
|
|
|
|
data["creation_date"] = h.creation_date();
|
|
|
|
// Comment
|
|
|
|
data["comment"] = h.comment();
|
|
|
|
data["total_wasted"] = misc::friendlyUnit(h.total_failed_bytes()+h.total_redundant_bytes());
|
|
|
|
data["total_uploaded"] = misc::friendlyUnit(h.all_time_upload()) + " ("+misc::friendlyUnit(h.total_payload_upload())+" "+tr("this session")+")";
|
|
|
|
data["total_downloaded"] = misc::friendlyUnit(h.all_time_download()) + " ("+misc::friendlyUnit(h.total_payload_download())+" "+tr("this session")+")";
|
|
|
|
if(h.upload_limit() <= 0)
|
|
|
|
data["up_limit"] = QString::fromUtf8("∞");
|
|
|
|
else
|
|
|
|
data["up_limit"] = misc::friendlyUnit(h.upload_limit())+tr("/s", "/second (i.e. per second)");
|
|
|
|
if(h.download_limit() <= 0)
|
|
|
|
data["dl_limit"] = QString::fromUtf8("∞");
|
|
|
|
else
|
|
|
|
data["dl_limit"] = misc::friendlyUnit(h.download_limit())+tr("/s", "/second (i.e. per second)");
|
|
|
|
QString elapsed_txt = misc::userFriendlyDuration(h.active_time());
|
|
|
|
if(h.is_seed()) {
|
|
|
|
elapsed_txt += " ("+tr("Seeded for %1", "e.g. Seeded for 3m10s").arg(misc::userFriendlyDuration(h.seeding_time()))+")";
|
|
|
|
}
|
|
|
|
data["time_elapsed"] = elapsed_txt;
|
|
|
|
data["nb_connections"] = QString::number(h.num_connections())+" ("+tr("%1 max", "e.g. 10 max").arg(QString::number(h.connections_limit()))+")";
|
|
|
|
// Update ratio info
|
|
|
|
float ratio;
|
|
|
|
if(h.total_payload_download() == 0){
|
|
|
|
if(h.total_payload_upload() == 0)
|
|
|
|
ratio = 1.;
|
|
|
|
else
|
|
|
|
ratio = 10.; // Max ratio
|
|
|
|
}else{
|
|
|
|
ratio = (double)h.total_payload_upload()/(double)h.total_payload_download();
|
|
|
|
if(ratio > 10.){
|
|
|
|
ratio = 10.;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data["share_ratio"] = QString(QByteArray::number(ratio, 'f', 1));
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2008-09-07 15:31:29 +04:00
|
|
|
void EventManager::addedTorrent(QTorrentHandle& h)
|
2008-05-17 17:23:37 +04:00
|
|
|
{
|
2009-11-19 21:28:30 +03:00
|
|
|
modifiedTorrent(h);
|
2008-12-25 16:48:23 +03:00
|
|
|
}
|
2008-09-27 23:55:56 +04:00
|
|
|
|
2008-09-28 15:30:24 +04:00
|
|
|
void EventManager::deletedTorrent(QString hash)
|
|
|
|
{
|
2009-11-19 21:28:30 +03:00
|
|
|
event_list.remove(hash);
|
2008-09-27 23:55:56 +04:00
|
|
|
}
|
|
|
|
|
2008-09-28 15:30:24 +04:00
|
|
|
void EventManager::modifiedTorrent(QTorrentHandle h)
|
|
|
|
{
|
2009-11-19 21:28:30 +03:00
|
|
|
QString hash = h.hash();
|
|
|
|
QVariantMap event;
|
2009-11-23 13:37:55 +03:00
|
|
|
event["eta"] = QVariant(QString::fromUtf8("∞"));
|
2009-11-19 21:28:30 +03:00
|
|
|
if(h.is_paused()) {
|
2009-11-23 12:16:32 +03:00
|
|
|
if(h.is_seed())
|
|
|
|
event["state"] = QVariant("pausedUP");
|
|
|
|
else
|
|
|
|
event["state"] = QVariant("pausedDL");
|
2009-11-19 21:28:30 +03:00
|
|
|
} else {
|
|
|
|
if(BTSession->isQueueingEnabled() && h.is_queued()) {
|
2009-11-23 12:16:32 +03:00
|
|
|
if(h.is_seed())
|
|
|
|
event["state"] = QVariant("queuedUP");
|
|
|
|
else
|
|
|
|
event["state"] = QVariant("queuedDL");
|
2008-11-02 14:43:20 +03:00
|
|
|
} else {
|
2009-11-19 21:28:30 +03:00
|
|
|
switch(h.state())
|
|
|
|
{
|
2009-11-23 12:16:32 +03:00
|
|
|
case torrent_status::finished:
|
|
|
|
case torrent_status::seeding:
|
2009-11-23 13:37:55 +03:00
|
|
|
if(h.upload_payload_rate() > 0) {
|
2009-11-23 12:16:32 +03:00
|
|
|
event["state"] = QVariant("seeding");
|
2009-11-23 13:37:55 +03:00
|
|
|
} else {
|
2009-11-23 12:16:32 +03:00
|
|
|
event["state"] = QVariant("stalledUP");
|
2009-11-23 13:37:55 +03:00
|
|
|
}
|
2009-11-19 21:28:30 +03:00
|
|
|
break;
|
2009-11-23 12:16:32 +03:00
|
|
|
case torrent_status::allocating:
|
2009-11-19 21:28:30 +03:00
|
|
|
case torrent_status::checking_files:
|
|
|
|
case torrent_status::queued_for_checking:
|
|
|
|
case torrent_status::checking_resume_data:
|
2009-11-23 13:37:55 +03:00
|
|
|
if(h.is_seed()) {
|
2009-11-23 12:16:32 +03:00
|
|
|
event["state"] = QVariant("checkingUP");
|
2009-11-23 13:37:55 +03:00
|
|
|
} else {
|
2009-11-23 12:16:32 +03:00
|
|
|
event["state"] = QVariant("checkingDL");
|
2009-11-23 13:37:55 +03:00
|
|
|
}
|
2009-11-19 21:28:30 +03:00
|
|
|
break;
|
2009-11-23 12:16:32 +03:00
|
|
|
case torrent_status::downloading:
|
|
|
|
case torrent_status::downloading_metadata:
|
2009-11-19 21:28:30 +03:00
|
|
|
if(h.download_payload_rate() > 0)
|
|
|
|
event["state"] = QVariant("downloading");
|
|
|
|
else
|
2009-11-23 12:16:32 +03:00
|
|
|
event["state"] = QVariant("stalledDL");
|
2009-11-23 13:37:55 +03:00
|
|
|
event["eta"] = misc::userFriendlyDuration(BTSession->getETA(hash));
|
2009-11-19 21:28:30 +03:00
|
|
|
break;
|
2008-11-02 14:43:20 +03:00
|
|
|
default:
|
2009-11-19 21:28:30 +03:00
|
|
|
qDebug("No status, should not happen!!! status is %d", h.state());
|
|
|
|
event["state"] = QVariant();
|
|
|
|
}
|
2008-11-02 14:43:20 +03:00
|
|
|
}
|
2009-11-19 21:28:30 +03:00
|
|
|
}
|
|
|
|
event["name"] = QVariant(h.name());
|
2009-11-23 13:37:55 +03:00
|
|
|
event["size"] = QVariant(misc::friendlyUnit(h.actual_size()));
|
2009-11-23 12:16:32 +03:00
|
|
|
event["progress"] = QVariant(h.progress());
|
2009-11-23 13:37:55 +03:00
|
|
|
event["dlspeed"] = QVariant(tr("%1/s", "e.g. 120 KiB/s").arg(misc::friendlyUnit(h.download_payload_rate())));
|
2009-11-23 12:16:32 +03:00
|
|
|
if(BTSession->isQueueingEnabled()) {
|
2009-11-23 16:35:46 +03:00
|
|
|
if(h.queue_position() >= 0)
|
|
|
|
event["priority"] = QVariant(QString::number(h.queue_position()));
|
|
|
|
else
|
|
|
|
event["priority"] = "*";
|
2009-11-23 12:16:32 +03:00
|
|
|
} else {
|
2009-11-23 16:35:46 +03:00
|
|
|
event["priority"] = "*";
|
2009-11-19 21:28:30 +03:00
|
|
|
}
|
2009-11-23 13:37:55 +03:00
|
|
|
event["upspeed"] = QVariant(tr("%1/s", "e.g. 120 KiB/s").arg(misc::friendlyUnit(h.upload_payload_rate())));
|
|
|
|
QString seeds = QString::number(h.num_seeds());
|
|
|
|
if(h.num_complete() > 0)
|
|
|
|
seeds += " ("+QString::number(h.num_complete())+")";
|
|
|
|
event["num_seeds"] = QVariant(seeds);
|
|
|
|
QString leechs = QString::number(h.num_peers()-h.num_seeds());
|
|
|
|
if(h.num_incomplete() > 0)
|
|
|
|
leechs += " ("+QString::number(h.num_incomplete())+")";
|
|
|
|
event["num_leechs"] = QVariant(leechs);
|
2009-11-19 21:28:30 +03:00
|
|
|
event["seed"] = QVariant(h.is_seed());
|
2009-11-23 13:40:16 +03:00
|
|
|
event["ratio"] = QVariant(QString::number(BTSession->getRealRatio(hash), 'f', 1));
|
2009-11-19 21:28:30 +03:00
|
|
|
event["hash"] = QVariant(hash);
|
|
|
|
event_list[hash] = event;
|
2008-05-17 17:23:37 +04:00
|
|
|
}
|