~ubuntu-branches/ubuntu/lucid/ktorrent/lucid

« back to all changes in this revision

Viewing changes to plugins/webinterface/torrentlistgenerator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-02-16 18:37:14 UTC
  • mfrom: (1.1.25 upstream) (0.4.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090216183714-52tf47jrnmk4xkmp
Tags: 3.2+dfsg.1-2ubuntu1
* Merge with Debian, remaining changes: (LP: #296433)
  - Use Kubuntu's kde4.mk
  - Build-depend on libboost-serialization1.35-dev since unversioned -dev is
    in universe
  - Change plasma-applet-ktorrent to plasma-widget-ktorrent since we're
    supposed to call them widgets for the users

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 by Joris Guisson and Ivan Vasic                    *
 
3
 *   joris.guisson@gmail.com                                               *
 
4
 *   ivasic@gmail.com                                                      *
 
5
 *                                                                         *
 
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.                                   *
 
10
 *                                                                         *
 
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.                          *
 
15
 *                                                                         *
 
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"
 
31
 
 
32
using namespace bt;
 
33
 
 
34
namespace kt
 
35
{
 
36
 
 
37
        TorrentListGenerator::TorrentListGenerator(CoreInterface *core,HttpServer* server) 
 
38
                : WebContentGenerator(server,"/data/torrents.xml",LOGIN_REQUIRED),core(core)
 
39
        {
 
40
        }
 
41
 
 
42
 
 
43
        TorrentListGenerator::~TorrentListGenerator()
 
44
        {
 
45
        }
 
46
 
 
47
 
 
48
        void TorrentListGenerator::get(HttpClientHandler* hdlr, const QHttpRequestHeader& hdr)
 
49
        {
 
50
                Q_UNUSED(hdr);
 
51
                HttpResponseHeader rhdr(200);
 
52
                server->setDefaultResponseHeaders(rhdr,"text/xml",true);
 
53
                
 
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())
 
62
                {
 
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();
 
84
                        i++;
 
85
                }
 
86
                out.writeEndElement();
 
87
                out.writeEndDocument();
 
88
                hdlr->send(rhdr,output_data);
 
89
        }
 
90
 
 
91
        void TorrentListGenerator::writeElement(QXmlStreamWriter & out,const QString & name,const QString & value)
 
92
        {
 
93
                out.writeStartElement(name);
 
94
                out.writeCharacters(value);
 
95
                out.writeEndElement();
 
96
        }
 
97
        
 
98
        void TorrentListGenerator::post(HttpClientHandler* hdlr, const QHttpRequestHeader& hdr, const QByteArray& data)
 
99
        {
 
100
                Q_UNUSED(data);
 
101
                get(hdlr,hdr);
 
102
        }
 
103
}