1
/***************************************************************************
2
* Copyright (C) 2008 by Joris Guisson and Ivan Vasic *
3
* joris.guisson@gmail.com *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20
***************************************************************************/
21
#include <QXmlStreamWriter>
22
#include <util/sha1hash.h>
23
#include <util/functions.h>
24
#include <torrent/queuemanager.h>
25
#include <interfaces/coreinterface.h>
26
#include <interfaces/torrentinterface.h>
27
#include "httpserver.h"
28
#include "torrentlistgenerator.h"
29
#include "httpresponseheader.h"
30
#include "httpclienthandler.h"
37
TorrentListGenerator::TorrentListGenerator(CoreInterface *core,HttpServer* server)
38
: WebContentGenerator(server,"/data/torrents.xml",LOGIN_REQUIRED),core(core)
43
TorrentListGenerator::~TorrentListGenerator()
48
void TorrentListGenerator::get(HttpClientHandler* hdlr, const QHttpRequestHeader& hdr)
51
HttpResponseHeader rhdr(200);
52
server->setDefaultResponseHeaders(rhdr,"text/xml",true);
54
QByteArray output_data;
55
QXmlStreamWriter out(&output_data);
56
out.setAutoFormatting(true);
57
out.writeStartDocument();
58
out.writeStartElement("torrents");
59
kt::QueueManager* qman = core->getQueueManager();
60
kt::QueueManager::iterator i = qman->begin();
61
while (i != qman->end())
63
bt::TorrentInterface* ti = *i;
64
const bt::TorrentStats & s = ti->getStats();
65
out.writeStartElement("torrent");
66
writeElement(out,"name",ti->getDisplayName());
67
writeElement(out,"info_hash",ti->getInfoHash().toString());
68
writeElement(out,"status",ti->statusToString());
69
writeElement(out,"bytes_downloaded",BytesToString(s.bytes_downloaded));
70
writeElement(out,"bytes_uploaded",BytesToString(s.bytes_uploaded));
71
writeElement(out,"total_bytes",BytesToString(s.total_bytes));
72
writeElement(out,"total_bytes_to_download",BytesToString(s.total_bytes_to_download));
73
writeElement(out,"download_rate",BytesPerSecToString(s.download_rate));
74
writeElement(out,"upload_rate",BytesPerSecToString(s.upload_rate));
75
writeElement(out,"num_peers",QString::number(s.num_peers));
76
writeElement(out,"seeders",QString::number(s.seeders_connected_to));
77
writeElement(out,"seeders_total",QString::number(s.seeders_total));
78
writeElement(out,"leechers",QString::number(s.leechers_connected_to));
79
writeElement(out,"leechers_total",QString::number(s.leechers_total));
80
writeElement(out,"running",s.running ? "1" : "0");
81
writeElement(out,"percentage",QString::number(Percentage(s),'f',2));
82
writeElement(out,"num_files",QString::number(ti->getNumFiles()));
83
out.writeEndElement();
86
out.writeEndElement();
87
out.writeEndDocument();
88
hdlr->send(rhdr,output_data);
91
void TorrentListGenerator::writeElement(QXmlStreamWriter & out,const QString & name,const QString & value)
93
out.writeStartElement(name);
94
out.writeCharacters(value);
95
out.writeEndElement();
98
void TorrentListGenerator::post(HttpClientHandler* hdlr, const QHttpRequestHeader& hdr, const QByteArray& data)