Browse Source

Optimize Web UI code

adaptive-webui-19844
Christophe Dumez 13 years ago
parent
commit
8f439589ea
  1. 1
      Changelog
  2. 32
      src/webui/httpconnection.cpp
  3. 11
      src/webui/json.h

1
Changelog

@ -6,6 +6,7 @@
- FEATURE: Add quick "set as default save path" checkbox to torrent addition dialog (sledgehammer999) - FEATURE: Add quick "set as default save path" checkbox to torrent addition dialog (sledgehammer999)
- BUGFIX: Add tray menu entry for toggling window visibility - BUGFIX: Add tray menu entry for toggling window visibility
- BUGFIX: Fix execution log lines selection and copying - BUGFIX: Fix execution log lines selection and copying
- BUGFIX: Reduce CPU usage when running Web UI
- COSMETIC: Display speed at the beginning of the Window title - COSMETIC: Display speed at the beginning of the Window title
- COSMETIC: Several cosmetic fixes to the Web UI - COSMETIC: Several cosmetic fixes to the Web UI
- OTHER: Display libraries versions in about dialog (sledgehammer999) - OTHER: Display libraries versions in about dialog (sledgehammer999)

32
src/webui/httpconnection.cpp

@ -163,10 +163,11 @@ void HttpConnection::respond() {
return; return;
} }
//qDebug("Auth: %s", qPrintable(auth.split(" ").first())); //qDebug("Auth: %s", qPrintable(auth.split(" ").first()));
if (QString::compare(auth.split(" ").first(), "Digest", Qt::CaseInsensitive) != 0 || !m_httpserver->isAuthorized(auth.toLocal8Bit(), m_parser.method())) { if (QString::compare(auth.split(" ").first(), "Digest", Qt::CaseInsensitive) != 0
|| !m_httpserver->isAuthorized(auth.toLocal8Bit(), m_parser.method())) {
// Update failed attempt counter // Update failed attempt counter
m_httpserver->increaseNbFailedAttemptsForIp(peer_ip); m_httpserver->increaseNbFailedAttemptsForIp(peer_ip);
qDebug("client IP: %s (%d failed attempts)", qPrintable(peer_ip), nb_fail+1); qDebug("client IP: %s (%d failed attempts)", qPrintable(peer_ip), nb_fail);
// Return unauthorized header // Return unauthorized header
m_generator.setStatusLine(401, "Unauthorized"); m_generator.setStatusLine(401, "Unauthorized");
m_generator.setValue("WWW-Authenticate", "Digest realm=\""+QString(QBT_REALM)+"\", nonce=\""+m_httpserver->generateNonce()+"\", opaque=\""+m_httpserver->generateNonce()+"\", stale=\"false\", algorithm=\"MD5\", qop=\"auth\""); m_generator.setValue("WWW-Authenticate", "Digest realm=\""+QString(QBT_REALM)+"\", nonce=\""+m_httpserver->generateNonce()+"\", opaque=\""+m_httpserver->generateNonce()+"\", stale=\"false\", algorithm=\"MD5\", qop=\"auth\"");
@ -183,6 +184,7 @@ void HttpConnection::respond() {
QFile favicon(":/Icons/skin/qbittorrent16.png"); QFile favicon(":/Icons/skin/qbittorrent16.png");
if(favicon.open(QIODevice::ReadOnly)) { if(favicon.open(QIODevice::ReadOnly)) {
QByteArray data = favicon.readAll(); QByteArray data = favicon.readAll();
favicon.close();
m_generator.setStatusLine(200, "OK"); m_generator.setStatusLine(200, "OK");
m_generator.setContentTypeByExt("png"); m_generator.setContentTypeByExt("png");
m_generator.setMessage(data); m_generator.setMessage(data);
@ -265,8 +267,7 @@ void HttpConnection::respond() {
url = ":/" + list.join("/"); url = ":/" + list.join("/");
} }
QFile file(url); QFile file(url);
if(!file.open(QIODevice::ReadOnly)) if(!file.open(QIODevice::ReadOnly)) {
{
qDebug("File %s was not found!", qPrintable(url)); qDebug("File %s was not found!", qPrintable(url));
respondNotFound(); respondNotFound();
return; return;
@ -278,6 +279,7 @@ void HttpConnection::respond() {
else else
ext.clear(); ext.clear();
QByteArray data = file.readAll(); QByteArray data = file.readAll();
file.close();
// Translate the page // Translate the page
if(ext == "html" || (ext == "js" && !list.last().startsWith("excanvas"))) { if(ext == "html" || (ext == "js" && !list.last().startsWith("excanvas"))) {
@ -294,14 +296,12 @@ void HttpConnection::respond() {
write(); write();
} }
void HttpConnection::respondNotFound() void HttpConnection::respondNotFound() {
{
m_generator.setStatusLine(404, "File not found"); m_generator.setStatusLine(404, "File not found");
write(); write();
} }
void HttpConnection::respondJson() void HttpConnection::respondJson() {
{
EventManager* manager = m_httpserver->eventManager(); EventManager* manager = m_httpserver->eventManager();
QString string = json::toJson(manager->getEventList()); QString string = json::toJson(manager->getEventList());
m_generator.setStatusLine(200, "OK"); m_generator.setStatusLine(200, "OK");
@ -358,8 +358,7 @@ void HttpConnection::respondGlobalTransferInfoJson() {
write(); write();
} }
void HttpConnection::respondCommand(const QString& command) void HttpConnection::respondCommand(const QString& command) {
{
if(command == "download") { if(command == "download") {
QString urls = m_parser.post("urls"); QString urls = m_parser.post("urls");
QStringList list = urls.split('\n'); QStringList list = urls.split('\n');
@ -388,12 +387,13 @@ void HttpConnection::respondCommand(const QString& command)
if(h.is_valid() && h.has_metadata()) { if(h.is_valid() && h.has_metadata()) {
QString urls = m_parser.post("urls"); QString urls = m_parser.post("urls");
QStringList list = urls.split('\n'); QStringList list = urls.split('\n');
foreach(QString url, list) { foreach(const QString& url, list) {
announce_entry e(url.toStdString()); announce_entry e(url.toStdString());
h.add_tracker(e); h.add_tracker(e);
} }
} }
} }
return;
} }
if(command == "upload") { if(command == "upload") {
// Get a unique filename // Get a unique filename
@ -436,6 +436,7 @@ void HttpConnection::respondCommand(const QString& command)
EventManager* manager = m_httpserver->eventManager(); EventManager* manager = m_httpserver->eventManager();
manager->setGlobalPreferences(json::fromJson(json_str)); manager->setGlobalPreferences(json::fromJson(json_str));
m_needsTranslation = !Preferences().getLocale().startsWith("en"); m_needsTranslation = !Preferences().getLocale().startsWith("en");
return;
} }
if(command == "setFilePrio") { if(command == "setFilePrio") {
QString hash = m_parser.post("hash"); QString hash = m_parser.post("hash");
@ -445,6 +446,7 @@ void HttpConnection::respondCommand(const QString& command)
if(h.is_valid() && h.has_metadata()) { if(h.is_valid() && h.has_metadata()) {
h.file_priority(file_id, priority); h.file_priority(file_id, priority);
} }
return;
} }
if(command == "getGlobalUpLimit") { if(command == "getGlobalUpLimit") {
m_generator.setStatusLine(200, "OK"); m_generator.setStatusLine(200, "OK");
@ -455,6 +457,7 @@ void HttpConnection::respondCommand(const QString& command)
m_generator.setMessage(QString::number(QBtSession::instance()->getSession()->upload_rate_limit())); m_generator.setMessage(QString::number(QBtSession::instance()->getSession()->upload_rate_limit()));
#endif #endif
write(); write();
return;
} }
if(command == "getGlobalDlLimit") { if(command == "getGlobalDlLimit") {
m_generator.setStatusLine(200, "OK"); m_generator.setStatusLine(200, "OK");
@ -465,6 +468,7 @@ void HttpConnection::respondCommand(const QString& command)
m_generator.setMessage(QString::number(QBtSession::instance()->getSession()->download_rate_limit())); m_generator.setMessage(QString::number(QBtSession::instance()->getSession()->download_rate_limit()));
#endif #endif
write(); write();
return;
} }
if(command == "getTorrentUpLimit") { if(command == "getTorrentUpLimit") {
QString hash = m_parser.post("hash"); QString hash = m_parser.post("hash");
@ -475,6 +479,7 @@ void HttpConnection::respondCommand(const QString& command)
m_generator.setMessage(QString::number(h.upload_limit())); m_generator.setMessage(QString::number(h.upload_limit()));
write(); write();
} }
return;
} }
if(command == "getTorrentDlLimit") { if(command == "getTorrentDlLimit") {
QString hash = m_parser.post("hash"); QString hash = m_parser.post("hash");
@ -485,6 +490,7 @@ void HttpConnection::respondCommand(const QString& command)
m_generator.setMessage(QString::number(h.download_limit())); m_generator.setMessage(QString::number(h.download_limit()));
write(); write();
} }
return;
} }
if(command == "setTorrentUpLimit") { if(command == "setTorrentUpLimit") {
QString hash = m_parser.post("hash"); QString hash = m_parser.post("hash");
@ -494,6 +500,7 @@ void HttpConnection::respondCommand(const QString& command)
if(h.is_valid()) { if(h.is_valid()) {
h.set_upload_limit(limit); h.set_upload_limit(limit);
} }
return;
} }
if(command == "setTorrentDlLimit") { if(command == "setTorrentDlLimit") {
QString hash = m_parser.post("hash"); QString hash = m_parser.post("hash");
@ -503,18 +510,21 @@ void HttpConnection::respondCommand(const QString& command)
if(h.is_valid()) { if(h.is_valid()) {
h.set_download_limit(limit); h.set_download_limit(limit);
} }
return;
} }
if(command == "setGlobalUpLimit") { if(command == "setGlobalUpLimit") {
qlonglong limit = m_parser.post("limit").toLongLong(); qlonglong limit = m_parser.post("limit").toLongLong();
if(limit == 0) limit = -1; if(limit == 0) limit = -1;
QBtSession::instance()->setUploadRateLimit(limit); QBtSession::instance()->setUploadRateLimit(limit);
Preferences().setGlobalUploadLimit(limit/1024.); Preferences().setGlobalUploadLimit(limit/1024.);
return;
} }
if(command == "setGlobalDlLimit") { if(command == "setGlobalDlLimit") {
qlonglong limit = m_parser.post("limit").toLongLong(); qlonglong limit = m_parser.post("limit").toLongLong();
if(limit == 0) limit = -1; if(limit == 0) limit = -1;
QBtSession::instance()->setDownloadRateLimit(limit); QBtSession::instance()->setDownloadRateLimit(limit);
Preferences().setGlobalDownloadLimit(limit/1024.); Preferences().setGlobalDownloadLimit(limit/1024.);
return;
} }
if(command == "pause") { if(command == "pause") {
emit pauseTorrent(m_parser.post("hash")); emit pauseTorrent(m_parser.post("hash"));

11
src/webui/json.h

@ -36,7 +36,7 @@
namespace json { namespace json {
QString toJson(QVariant v) { QString toJson(const QVariant& v) {
if (v.isNull()) if (v.isNull())
return "null"; return "null";
switch(v.type()) switch(v.type())
@ -97,7 +97,7 @@ namespace json {
} }
} }
QString toJson(QVariantMap m) { QString toJson(const QVariantMap& m) {
QStringList vlist; QStringList vlist;
QVariantMap::ConstIterator it; QVariantMap::ConstIterator it;
for (it = m.constBegin(); it != m.constEnd(); it++) { for (it = m.constBegin(); it != m.constEnd(); it++) {
@ -106,15 +106,14 @@ namespace json {
return "{"+vlist.join(",")+"}"; return "{"+vlist.join(",")+"}";
} }
QVariantMap fromJson(QString json) { QVariantMap fromJson(const QString& json) {
qDebug("JSON is %s", qPrintable(json)); qDebug("JSON is %s", qPrintable(json));
QVariantMap m; QVariantMap m;
if(json.startsWith("{") && json.endsWith("}")) { if(json.startsWith("{") && json.endsWith("}")) {
json = json.mid(1, json.length()-2);
QStringList couples; QStringList couples;
QString tmp = ""; QString tmp = "";
bool in_list = false; bool in_list = false;
foreach(const QChar &c, json) { foreach(const QChar &c, json.mid(1, json.length()-2)) {
if(c == ',' && !in_list) { if(c == ',' && !in_list) {
couples << tmp; couples << tmp;
tmp = ""; tmp = "";
@ -169,7 +168,7 @@ namespace json {
return m; return m;
} }
QString toJson(QList<QVariantMap> v) { QString toJson(const QList<QVariantMap>& v) {
QStringList res; QStringList res;
foreach(QVariantMap m, v) { foreach(QVariantMap m, v) {
QStringList vlist; QStringList vlist;

Loading…
Cancel
Save