2008-05-17 17:23:37 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 by Ishan Arora
|
|
|
|
* ishanarora@gmail.com
|
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "eventmanager.h"
|
2008-08-08 17:17:26 +04:00
|
|
|
#include "bittorrent.h"
|
2008-05-17 17:23:37 +04:00
|
|
|
#include "json.h"
|
|
|
|
#include <QDebug>
|
|
|
|
|
2008-08-08 17:17:26 +04:00
|
|
|
EventManager::EventManager(QObject *parent, bittorrent *BTSession)
|
|
|
|
: QObject(parent), BTSession(BTSession)
|
2008-05-17 17:23:37 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-09-28 15:30:24 +04:00
|
|
|
QVariant EventManager::getEventList() const {
|
2008-05-17 17:23:37 +04:00
|
|
|
QVariantList list;
|
2008-09-28 15:30:24 +04:00
|
|
|
foreach(QVariantMap event, event_list.values()) {
|
|
|
|
list << QVariant(event);
|
2008-05-17 17:23:37 +04:00
|
|
|
}
|
2008-09-28 15:30:24 +04:00
|
|
|
return QVariant(list);
|
2008-05-17 17:23:37 +04:00
|
|
|
}
|
|
|
|
|
2008-09-07 15:31:29 +04:00
|
|
|
void EventManager::addedTorrent(QTorrentHandle& h)
|
2008-05-17 17:23:37 +04:00
|
|
|
{
|
|
|
|
QVariantMap event;
|
2008-09-27 23:55:56 +04:00
|
|
|
QString hash = h.hash();
|
|
|
|
event["hash"] = QVariant(hash);
|
|
|
|
event["name"] = QVariant(h.name());
|
|
|
|
event["seed"] = QVariant(h.is_seed());
|
2008-09-28 14:07:00 +04:00
|
|
|
event["size"] = QVariant((qlonglong)h.actual_size());
|
|
|
|
if(!h.is_seed()) {
|
|
|
|
event["progress"] = QVariant(h.progress());
|
|
|
|
event["dlspeed"] = QVariant(h.download_payload_rate());
|
|
|
|
}
|
|
|
|
event["upspeed"] = QVariant(h.upload_payload_rate());
|
2008-09-27 23:55:56 +04:00
|
|
|
if(h.is_paused()) {
|
|
|
|
if(BTSession->isQueueingEnabled() && (BTSession->isDownloadQueued(hash) || BTSession->isUploadQueued(hash)))
|
|
|
|
event["state"] = QVariant("queued");
|
|
|
|
else
|
|
|
|
event["state"] = QVariant("paused");
|
|
|
|
} else {
|
|
|
|
switch(h.state())
|
|
|
|
{
|
|
|
|
case torrent_status::finished:
|
|
|
|
case torrent_status::seeding:
|
|
|
|
event["state"] = QVariant("seeding");
|
|
|
|
break;
|
|
|
|
case torrent_status::checking_files:
|
|
|
|
case torrent_status::queued_for_checking:
|
|
|
|
event["state"] = QVariant("checking");
|
|
|
|
break;
|
|
|
|
case torrent_status::connecting_to_tracker:
|
|
|
|
if(h.download_payload_rate() > 0)
|
|
|
|
event["state"] = QVariant("downloading");
|
|
|
|
else
|
|
|
|
event["state"] = QVariant("connecting");
|
|
|
|
break;
|
|
|
|
case torrent_status::downloading:
|
|
|
|
case torrent_status::downloading_metadata:
|
|
|
|
if(h.download_payload_rate() > 0)
|
|
|
|
event["state"] = QVariant("downloading");
|
|
|
|
else
|
|
|
|
event["state"] = QVariant("stalled");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qDebug("No status, should not happen!!! status is %d", h.state());
|
|
|
|
event["state"] = QVariant();
|
|
|
|
}
|
|
|
|
}
|
2008-09-28 15:30:24 +04:00
|
|
|
event_list[hash] = event;
|
2008-09-27 23:55:56 +04:00
|
|
|
}
|
|
|
|
|
2008-09-28 15:30:24 +04:00
|
|
|
void EventManager::deletedTorrent(QString hash)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
QString hash = h.hash();
|
2008-09-27 23:55:56 +04:00
|
|
|
QVariantMap event;
|
2008-09-28 15:30:24 +04:00
|
|
|
|
2008-09-28 00:31:50 +04:00
|
|
|
if(h.is_paused()) {
|
|
|
|
if(BTSession->isQueueingEnabled() && (BTSession->isDownloadQueued(hash) || BTSession->isUploadQueued(hash)))
|
|
|
|
event["state"] = QVariant("queued");
|
|
|
|
else
|
|
|
|
event["state"] = QVariant("paused");
|
|
|
|
} else {
|
|
|
|
switch(h.state())
|
|
|
|
{
|
|
|
|
case torrent_status::finished:
|
|
|
|
case torrent_status::seeding:
|
|
|
|
event["state"] = QVariant("seeding");
|
|
|
|
break;
|
|
|
|
case torrent_status::checking_files:
|
|
|
|
case torrent_status::queued_for_checking:
|
|
|
|
event["state"] = QVariant("checking");
|
|
|
|
break;
|
|
|
|
case torrent_status::connecting_to_tracker:
|
|
|
|
if(h.download_payload_rate() > 0)
|
|
|
|
event["state"] = QVariant("downloading");
|
|
|
|
else
|
|
|
|
event["state"] = QVariant("connecting");
|
|
|
|
break;
|
|
|
|
case torrent_status::downloading:
|
|
|
|
case torrent_status::downloading_metadata:
|
|
|
|
if(h.download_payload_rate() > 0)
|
|
|
|
event["state"] = QVariant("downloading");
|
|
|
|
else
|
|
|
|
event["state"] = QVariant("stalled");
|
|
|
|
break;
|
|
|
|
default:
|
2008-09-28 15:30:24 +04:00
|
|
|
qDebug("No status, should not happen!!! status is %d", h.state());
|
2008-09-28 00:31:50 +04:00
|
|
|
event["state"] = QVariant();
|
|
|
|
}
|
|
|
|
}
|
2008-09-28 15:30:24 +04:00
|
|
|
event["name"] = QVariant(h.name());
|
2008-09-28 00:31:50 +04:00
|
|
|
event["size"] = QVariant((qlonglong)h.actual_size());
|
|
|
|
if(!h.is_seed()) {
|
2008-09-28 15:30:24 +04:00
|
|
|
event["progress"] = QVariant(h.progress());
|
|
|
|
event["dlspeed"] = QVariant(h.download_payload_rate());
|
2008-05-17 17:23:37 +04:00
|
|
|
}
|
2008-09-28 15:30:24 +04:00
|
|
|
event["upspeed"] = QVariant(h.upload_payload_rate());
|
|
|
|
event["seed"] = QVariant(h.is_seed());
|
|
|
|
event["hash"] = QVariant(hash);
|
|
|
|
event_list[hash] = event;
|
2008-05-17 17:23:37 +04:00
|
|
|
}
|