Browse Source

Web UI code optimization

adaptive-webui-19844
Christophe Dumez 13 years ago
parent
commit
ae692ba9b8
  1. 5
      src/webui/eventmanager.cpp
  2. 5
      src/webui/eventmanager.h
  3. 4
      src/webui/httpconnection.cpp
  4. 1
      src/webui/httpconnection.h
  5. 10
      src/webui/httpserver.cpp
  6. 3
      src/webui/httpserver.h

5
src/webui/eventmanager.cpp

@ -124,7 +124,7 @@ QList<QVariantMap> EventManager::getPropFilesInfo(QString hash) const {
return files; return files;
} }
void EventManager::setGlobalPreferences(QVariantMap m) const { void EventManager::setGlobalPreferences(QVariantMap m) {
// UI // UI
Preferences pref; Preferences pref;
if(m.contains("locale")) { if(m.contains("locale")) {
@ -137,9 +137,10 @@ void EventManager::setGlobalPreferences(QVariantMap m) const {
qDebug("%s locale unrecognized, using default (en_GB).", qPrintable(locale)); qDebug("%s locale unrecognized, using default (en_GB).", qPrintable(locale));
} }
qApp->installTranslator(translator); qApp->installTranslator(translator);
}
pref.setLocale(locale); pref.setLocale(locale);
emit localeChanged(locale);
}
} }
// Downloads // Downloads
if(m.contains("save_path")) if(m.contains("save_path"))

5
src/webui/eventmanager.h

@ -54,7 +54,10 @@ public:
QList<QVariantMap> getPropTrackersInfo(QString hash) const; QList<QVariantMap> getPropTrackersInfo(QString hash) const;
QList<QVariantMap> getPropFilesInfo(QString hash) const; QList<QVariantMap> getPropFilesInfo(QString hash) const;
QVariantMap getGlobalPreferences() const; QVariantMap getGlobalPreferences() const;
void setGlobalPreferences(QVariantMap m) const; void setGlobalPreferences(QVariantMap m);
signals:
void localeChanged(const QString &locale);
public slots: public slots:
void addedTorrent(const QTorrentHandle& h); void addedTorrent(const QTorrentHandle& h);

4
src/webui/httpconnection.cpp

@ -56,7 +56,6 @@ using namespace libtorrent;
HttpConnection::HttpConnection(QTcpSocket *socket, HttpServer *parent) HttpConnection::HttpConnection(QTcpSocket *socket, HttpServer *parent)
: QObject(parent), m_socket(socket), m_httpserver(parent) : QObject(parent), m_socket(socket), m_httpserver(parent)
{ {
m_needsTranslation = !Preferences().getLocale().startsWith("en");
m_socket->setParent(this); m_socket->setParent(this);
connect(m_socket, SIGNAL(readyRead()), SLOT(read())); connect(m_socket, SIGNAL(readyRead()), SLOT(read()));
connect(m_socket, SIGNAL(disconnected()), SLOT(deleteLater())); connect(m_socket, SIGNAL(disconnected()), SLOT(deleteLater()));
@ -121,7 +120,7 @@ void HttpConnection::translateDocument(QString& data) {
QByteArray word = regex.cap(1).toLocal8Bit(); QByteArray word = regex.cap(1).toLocal8Bit();
QString translation = word; QString translation = word;
if (m_needsTranslation) { if (m_httpserver->isTranslationNeeded()) {
int context_index = 0; int context_index = 0;
do { do {
translation = qApp->translate(contexts[context_index].c_str(), word.constData(), 0, QCoreApplication::UnicodeUTF8, 1); 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"); QString json_str = m_parser.post("json");
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");
return; return;
} }
if(command == "setFilePrio") { if(command == "setFilePrio") {

1
src/webui/httpconnection.h

@ -88,7 +88,6 @@ private:
HttpServer *m_httpserver; HttpServer *m_httpserver;
HttpRequestParser m_parser; HttpRequestParser m_parser;
HttpResponseGenerator m_generator; HttpResponseGenerator m_generator;
bool m_needsTranslation;
}; };
#endif #endif

10
src/webui/httpserver.cpp

@ -97,6 +97,8 @@ HttpServer::HttpServer(int msec, QObject* parent) : QTcpServer(parent),
m_username = pref.getWebUiUsername().toLocal8Bit(); m_username = pref.getWebUiUsername().toLocal8Bit();
m_passwordSha1 = pref.getWebUiPassword().toLocal8Bit(); m_passwordSha1 = pref.getWebUiPassword().toLocal8Bit();
m_localAuthEnabled = pref.isWebUiLocalAuthEnabled(); m_localAuthEnabled = pref.isWebUiLocalAuthEnabled();
m_needsTranslation = !Preferences().getLocale().startsWith("en");
connect(m_eventManager, SIGNAL(localeChanged(QString)), SLOT(onLocaleChanged(QString)));
// HTTPS-related // HTTPS-related
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
@ -337,3 +339,11 @@ void HttpServer::setlocalAuthEnabled(bool enabled) {
bool HttpServer::isLocalAuthEnabled() const { bool HttpServer::isLocalAuthEnabled() const {
return m_localAuthEnabled; return m_localAuthEnabled;
} }
bool HttpServer::isTranslationNeeded() {
return m_needsTranslation;
}
void HttpServer::onLocaleChanged(const QString &locale) {
m_needsTranslation = !locale.startsWith("en");
}

3
src/webui/httpserver.h

@ -69,6 +69,7 @@ public:
int NbFailedAttemptsForIp(const QString& ip) const; int NbFailedAttemptsForIp(const QString& ip) const;
void increaseNbFailedAttemptsForIp(const QString& ip); void increaseNbFailedAttemptsForIp(const QString& ip);
void resetNbFailedAttemptsForIp(const QString& ip); void resetNbFailedAttemptsForIp(const QString& ip);
bool isTranslationNeeded();
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void enableHttps(const QSslCertificate &certificate, const QSslKey &key); void enableHttps(const QSslCertificate &certificate, const QSslKey &key);
@ -81,6 +82,7 @@ private:
private slots: private slots:
void onTimer(); void onTimer();
void UnbanTimerEvent(); void UnbanTimerEvent();
void onLocaleChanged(const QString &locale);
private: private:
void handleNewConnection(QTcpSocket *socket); void handleNewConnection(QTcpSocket *socket);
@ -92,6 +94,7 @@ private:
QTimer m_timer; QTimer m_timer;
QHash<QString, int> m_clientFailedAttempts; QHash<QString, int> m_clientFailedAttempts;
bool m_localAuthEnabled; bool m_localAuthEnabled;
bool m_needsTranslation;
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
bool m_https; bool m_https;
QSslCertificate m_certificate; QSslCertificate m_certificate;

Loading…
Cancel
Save