From ae692ba9b894664028996e11cd163ddaeee69b72 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Thu, 29 Sep 2011 20:47:51 +0300 Subject: [PATCH] Web UI code optimization --- src/webui/eventmanager.cpp | 7 ++++--- src/webui/eventmanager.h | 5 ++++- src/webui/httpconnection.cpp | 4 +--- src/webui/httpconnection.h | 1 - src/webui/httpserver.cpp | 10 ++++++++++ src/webui/httpserver.h | 3 +++ 6 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/webui/eventmanager.cpp b/src/webui/eventmanager.cpp index 271885662..04936a02a 100644 --- a/src/webui/eventmanager.cpp +++ b/src/webui/eventmanager.cpp @@ -124,7 +124,7 @@ QList EventManager::getPropFilesInfo(QString hash) const { return files; } -void EventManager::setGlobalPreferences(QVariantMap m) const { +void EventManager::setGlobalPreferences(QVariantMap m) { // UI Preferences pref; if(m.contains("locale")) { @@ -137,9 +137,10 @@ void EventManager::setGlobalPreferences(QVariantMap m) const { qDebug("%s locale unrecognized, using default (en_GB).", qPrintable(locale)); } qApp->installTranslator(translator); - } - pref.setLocale(locale); + pref.setLocale(locale); + emit localeChanged(locale); + } } // Downloads if(m.contains("save_path")) diff --git a/src/webui/eventmanager.h b/src/webui/eventmanager.h index 01104c1df..1124f31dc 100644 --- a/src/webui/eventmanager.h +++ b/src/webui/eventmanager.h @@ -54,7 +54,10 @@ public: QList getPropTrackersInfo(QString hash) const; QList getPropFilesInfo(QString hash) const; QVariantMap getGlobalPreferences() const; - void setGlobalPreferences(QVariantMap m) const; + void setGlobalPreferences(QVariantMap m); + +signals: + void localeChanged(const QString &locale); public slots: void addedTorrent(const QTorrentHandle& h); diff --git a/src/webui/httpconnection.cpp b/src/webui/httpconnection.cpp index ab3a09d82..70b647749 100644 --- a/src/webui/httpconnection.cpp +++ b/src/webui/httpconnection.cpp @@ -56,7 +56,6 @@ using namespace libtorrent; HttpConnection::HttpConnection(QTcpSocket *socket, HttpServer *parent) : QObject(parent), m_socket(socket), m_httpserver(parent) { - m_needsTranslation = !Preferences().getLocale().startsWith("en"); m_socket->setParent(this); connect(m_socket, SIGNAL(readyRead()), SLOT(read())); connect(m_socket, SIGNAL(disconnected()), SLOT(deleteLater())); @@ -121,7 +120,7 @@ void HttpConnection::translateDocument(QString& data) { QByteArray word = regex.cap(1).toLocal8Bit(); QString translation = word; - if (m_needsTranslation) { + if (m_httpserver->isTranslationNeeded()) { int context_index = 0; do { translation = qApp->translate(contexts[context_index].c_str(), word.constData(), 0, QCoreApplication::UnicodeUTF8, 1); @@ -433,7 +432,6 @@ void HttpConnection::respondCommand(const QString& command) { QString json_str = m_parser.post("json"); EventManager* manager = m_httpserver->eventManager(); manager->setGlobalPreferences(json::fromJson(json_str)); - m_needsTranslation = !Preferences().getLocale().startsWith("en"); return; } if(command == "setFilePrio") { diff --git a/src/webui/httpconnection.h b/src/webui/httpconnection.h index e7a09ab4d..62d6992cd 100644 --- a/src/webui/httpconnection.h +++ b/src/webui/httpconnection.h @@ -88,7 +88,6 @@ private: HttpServer *m_httpserver; HttpRequestParser m_parser; HttpResponseGenerator m_generator; - bool m_needsTranslation; }; #endif diff --git a/src/webui/httpserver.cpp b/src/webui/httpserver.cpp index 63402e0b1..1e2526ad6 100644 --- a/src/webui/httpserver.cpp +++ b/src/webui/httpserver.cpp @@ -97,6 +97,8 @@ HttpServer::HttpServer(int msec, QObject* parent) : QTcpServer(parent), m_username = pref.getWebUiUsername().toLocal8Bit(); m_passwordSha1 = pref.getWebUiPassword().toLocal8Bit(); m_localAuthEnabled = pref.isWebUiLocalAuthEnabled(); + m_needsTranslation = !Preferences().getLocale().startsWith("en"); + connect(m_eventManager, SIGNAL(localeChanged(QString)), SLOT(onLocaleChanged(QString))); // HTTPS-related #ifndef QT_NO_OPENSSL @@ -337,3 +339,11 @@ void HttpServer::setlocalAuthEnabled(bool enabled) { bool HttpServer::isLocalAuthEnabled() const { return m_localAuthEnabled; } + +bool HttpServer::isTranslationNeeded() { + return m_needsTranslation; +} + +void HttpServer::onLocaleChanged(const QString &locale) { + m_needsTranslation = !locale.startsWith("en"); +} diff --git a/src/webui/httpserver.h b/src/webui/httpserver.h index cbb314376..96323949f 100644 --- a/src/webui/httpserver.h +++ b/src/webui/httpserver.h @@ -69,6 +69,7 @@ public: int NbFailedAttemptsForIp(const QString& ip) const; void increaseNbFailedAttemptsForIp(const QString& ip); void resetNbFailedAttemptsForIp(const QString& ip); + bool isTranslationNeeded(); #ifndef QT_NO_OPENSSL void enableHttps(const QSslCertificate &certificate, const QSslKey &key); @@ -81,6 +82,7 @@ private: private slots: void onTimer(); void UnbanTimerEvent(); + void onLocaleChanged(const QString &locale); private: void handleNewConnection(QTcpSocket *socket); @@ -92,6 +94,7 @@ private: QTimer m_timer; QHash m_clientFailedAttempts; bool m_localAuthEnabled; + bool m_needsTranslation; #ifndef QT_NO_OPENSSL bool m_https; QSslCertificate m_certificate;