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

« back to all changes in this revision

Viewing changes to plugins/webinterface/phpcodegenerator.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) 2006 by Diego R. Brogna and Joris Guisson               *
3
 
 *   dierbro@gmail.com                                                     *
4
 
 *   joris.guisson@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 <klocale.h>
22
 
#include <kglobal.h>
23
 
#include <kio/global.h>
24
 
#include <settings.h>
25
 
#include <peer/peermanager.h>
26
 
#include <torrent/queuemanager.h>
27
 
#include <interfaces/coreinterface.h>
28
 
#include <interfaces/functions.h>
29
 
#include <util/functions.h>
30
 
#include <interfaces/torrentinterface.h>
31
 
#include <interfaces/torrentfileinterface.h>
32
 
#include "phpcodegenerator.h"
33
 
 
34
 
using namespace bt;
35
 
 
36
 
namespace kt
37
 
{
38
 
        using bt::FIRST_PRIORITY;
39
 
        using bt::NORMAL_PRIORITY;
40
 
        using bt::LAST_PRIORITY;
41
 
        using bt::EXCLUDED;
42
 
        using bt::ONLY_SEED_PRIORITY;
43
 
        
44
 
        QString BytesToString2(Uint64 bytes,int precision = 2)
45
 
        {
46
 
                KLocale* loc = KGlobal::locale();
47
 
                if (bytes >= 1024 * 1024 * 1024)
48
 
                        return QString("%1 GB").arg(loc->formatNumber(bytes / TO_GIG,precision < 0 ? 2 : precision));
49
 
                else if (bytes >= 1024*1024)
50
 
                        return QString("%1 MB").arg(loc->formatNumber(bytes / TO_MEG,precision < 0 ? 1 : precision));
51
 
                else if (bytes >= 1024)
52
 
                        return QString("%1 KB").arg(loc->formatNumber(bytes / TO_KB,precision < 0 ? 1 : precision));
53
 
                else
54
 
                        return QString("%1 B").arg(bytes);
55
 
        }
56
 
 
57
 
        QString KBytesPerSecToString2(double speed,int precision = 2)
58
 
        {
59
 
                KLocale* loc = KGlobal::locale();
60
 
                return QString("%1 KB/s").arg(loc->formatNumber(speed,precision));
61
 
        }
62
 
 
63
 
        PhpCodeGenerator::PhpCodeGenerator(CoreInterface *c)
64
 
        {
65
 
                core=c;
66
 
        }
67
 
        
68
 
        PhpCodeGenerator::~PhpCodeGenerator()
69
 
        {}
70
 
        
71
 
        void PhpCodeGenerator::downloadStatus(QTextStream & out)
72
 
        {
73
 
                QString ret;
74
 
                out << "function downloadStatus()\n{\nreturn ";
75
 
                out << "array(";
76
 
        
77
 
                QList<bt::TorrentInterface*>::iterator i= core->getQueueManager()->begin();
78
 
                for(int k=0; i != core->getQueueManager()->end(); i++, k++)
79
 
                {
80
 
                        if (k > 0)
81
 
                                out << ",\n";
82
 
                        const TorrentStats & stats = (*i)->getStats();
83
 
                        out << QString("%1 => array(").arg(k);
84
 
                        
85
 
                        out << QString("\"imported_bytes\" => %1,").arg(stats.imported_bytes);
86
 
                        out << QString("\"bytes_downloaded\" => \"%1\",").arg(BytesToString2(stats.bytes_downloaded));
87
 
                        out << QString("\"bytes_uploaded\" => \"%1\",").arg(BytesToString2(stats.bytes_uploaded));
88
 
                        out << QString("\"bytes_left\" => %1,").arg(stats.bytes_left);
89
 
                        out << QString("\"bytes_left_to_download\" => %1,").arg(stats.bytes_left_to_download);
90
 
                        out << QString("\"total_bytes\" => \"%1\",").arg(BytesToString2(stats.total_bytes));
91
 
                        out << QString("\"total_bytes_to_download\" => %1,").arg(stats.total_bytes_to_download);
92
 
                        out << QString("\"download_rate\" => \"%1\",").arg(KBytesPerSecToString2(stats.download_rate / 1024.0));
93
 
                        out << QString("\"upload_rate\" => \"%1\",").arg(KBytesPerSecToString2(stats.upload_rate / 1024.0));
94
 
                        out << QString("\"num_peers\" => %1,").arg(stats.num_peers);
95
 
                        out << QString("\"num_chunks_downloading\" => %1,").arg(stats.num_chunks_downloading);
96
 
                        out << QString("\"total_chunks\" => %1,").arg(stats.total_chunks);
97
 
                        out << QString("\"num_chunks_downloaded\" => %1,").arg(stats.num_chunks_downloaded);
98
 
                        out << QString("\"num_chunks_excluded\" => %1,").arg(stats.num_chunks_excluded);
99
 
                        out << QString("\"chunk_size\" => %1,").arg(stats.chunk_size);
100
 
                        out << QString("\"seeders_total\" => %1,").arg(stats.seeders_total);
101
 
                        out << QString("\"seeders_connected_to\" => %1,").arg(stats.seeders_connected_to);
102
 
                        out << QString("\"leechers_total\" => %1,").arg(stats.leechers_total);
103
 
                        out << QString("\"leechers_connected_to\" => %1,").arg(stats.leechers_connected_to);
104
 
                        out << QString("\"status\" => %1,").arg(stats.status);
105
 
                        out << QString("\"running\" => %1,").arg(stats.running);
106
 
                        QString tmp = stats.trackerstatus;
107
 
                        out << QString("\"trackerstatus\" => \"%1\",").arg(tmp.replace("\\", "\\\\").replace("\"", "\\\"").replace("$", "\\$"));
108
 
                        out << QString("\"session_bytes_downloaded\" => %1,").arg(stats.session_bytes_downloaded);
109
 
                        out << QString("\"session_bytes_uploaded\" => %1,").arg(stats.session_bytes_uploaded);
110
 
                        out << QString("\"trk_bytes_downloaded\" => %1,").arg(stats.trk_bytes_downloaded);
111
 
                        out << QString("\"trk_bytes_uploaded\" => %1,").arg(stats.trk_bytes_uploaded);
112
 
                        
113
 
                        tmp = stats.torrent_name;
114
 
                        out << QString("\"torrent_name\" => \"%1\",").arg(tmp.replace("\\", "\\\\").replace("\"", "\\\"").replace("$", "\\$"));
115
 
                        tmp = stats.output_path;
116
 
                        out << QString("\"output_path\" => \"%1\",").arg(tmp.replace("\\", "\\\\").replace("\"", "\\\"").replace("$", "\\$"));
117
 
                        out << QString("\"stopped_by_error\" => \"%1\",").arg(stats.stopped_by_error);
118
 
                        out << QString("\"completed\" => \"%1\",").arg(stats.completed);
119
 
                        out << QString("\"user_controlled\" => \"%1\",").arg(stats.user_controlled);
120
 
                        out << QString("\"max_share_ratio\" => %1,").arg(stats.max_share_ratio);
121
 
                        out << QString("\"priv_torrent\" => \"%1\",").arg(stats.priv_torrent);
122
 
                        out << QString("\"num_files\" => \"%1\",").arg((*i)->getNumFiles());                    
123
 
                        out << QString("\"files\" => array(");
124
 
                        if (stats.multi_file_torrent)
125
 
                        {
126
 
                                //for loop to add each file+status to "files" array                     
127
 
                                for (Uint32 j = 0;j < (*i)->getNumFiles();j++)
128
 
                                {
129
 
                                        if (j > 0)
130
 
                                                out << ",\n";
131
 
                                        TorrentFileInterface & file = (*i)->getTorrentFile(j);
132
 
                                        out << QString("\"%1\" => array(").arg(j);
133
 
                                        out << QString("\"name\" => \"%1\",").arg(file.getPath());
134
 
                                        out << QString("\"size\" => \"%1\",").arg(KIO::convertSize(file.getSize()));
135
 
                                        out << QString("\"perc_done\" => \"%1\",").arg(file.getDownloadPercentage());
136
 
                                        out << QString("\"status\" => \"%1\"").arg(file.getPriority());
137
 
                                        out << QString(")");    
138
 
                                }
139
 
                        }
140
 
                        
141
 
                        out << ")";
142
 
                        out << ")";
143
 
                }
144
 
                
145
 
                out << ");\n}\n";               
146
 
        }
147
 
        
148
 
        void PhpCodeGenerator::globalInfo(QTextStream & out)
149
 
        {
150
 
                out << "function globalInfo()\n{\nreturn ";
151
 
                out << "array(";
152
 
                CurrentStats stats=core->getStats();
153
 
        
154
 
                out << QString("\"download_speed\" => \"%1\",").arg(KBytesPerSecToString2(stats.download_speed / 1024.0));
155
 
                out << QString("\"upload_speed\" => \"%1\",").arg(KBytesPerSecToString2(stats.upload_speed / 1024.0));
156
 
                out << QString("\"bytes_downloaded\" => \"%1\",").arg(stats.bytes_downloaded);
157
 
                out << QString("\"bytes_uploaded\" => \"%1\",").arg(stats.bytes_uploaded);
158
 
                out << QString("\"max_download_speed\" => \"%1\",").arg(Settings::maxDownloadRate());
159
 
                out << QString("\"max_upload_speed\" => \"%1\",").arg(Settings::maxUploadRate());
160
 
                out << QString("\"max_downloads\" => \"%1\",").arg(Settings::maxDownloads());
161
 
                out << QString("\"max_seeds\"=> \"%1\",").arg(Settings::maxSeeds());
162
 
#ifdef ENABLE_DHT_SUPPORT
163
 
                out << QString("\"dht_support\" => \"%1\",").arg(Settings::dhtSupport());
164
 
#else
165
 
                out << QString("\"dht_support\" => \"%1\",").arg(false);
166
 
#endif
167
 
                out << QString("\"use_encryption\" => \"%1\"").arg(Settings::useEncryption());
168
 
                out << ");\n}\n";
169
 
        }
170
 
        
171
 
}