1
/***************************************************************************
2
* Copyright (C) 2006 by Diego R. Brogna and Joris Guisson *
4
* 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
***************************************************************************/
23
#include <kio/global.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"
38
using bt::FIRST_PRIORITY;
39
using bt::NORMAL_PRIORITY;
40
using bt::LAST_PRIORITY;
42
using bt::ONLY_SEED_PRIORITY;
44
QString BytesToString2(Uint64 bytes,int precision = 2)
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));
54
return QString("%1 B").arg(bytes);
57
QString KBytesPerSecToString2(double speed,int precision = 2)
59
KLocale* loc = KGlobal::locale();
60
return QString("%1 KB/s").arg(loc->formatNumber(speed,precision));
63
PhpCodeGenerator::PhpCodeGenerator(CoreInterface *c)
68
PhpCodeGenerator::~PhpCodeGenerator()
71
void PhpCodeGenerator::downloadStatus(QTextStream & out)
74
out << "function downloadStatus()\n{\nreturn ";
77
QList<bt::TorrentInterface*>::iterator i= core->getQueueManager()->begin();
78
for(int k=0; i != core->getQueueManager()->end(); i++, k++)
82
const TorrentStats & stats = (*i)->getStats();
83
out << QString("%1 => array(").arg(k);
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);
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)
126
//for loop to add each file+status to "files" array
127
for (Uint32 j = 0;j < (*i)->getNumFiles();j++)
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());
148
void PhpCodeGenerator::globalInfo(QTextStream & out)
150
out << "function globalInfo()\n{\nreturn ";
152
CurrentStats stats=core->getStats();
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());
165
out << QString("\"dht_support\" => \"%1\",").arg(false);
167
out << QString("\"use_encryption\" => \"%1\"").arg(Settings::useEncryption());