RequestHandler.cpp   RequestHandler.cpp 
skipping to change at line 45 skipping to change at line 45
#include "StelApp.hpp" #include "StelApp.hpp"
#include "StelUtils.hpp" #include "StelUtils.hpp"
#include "StelTranslator.hpp" #include "StelTranslator.hpp"
#include "StelFileMgr.hpp" #include "StelFileMgr.hpp"
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
const QByteArray RequestHandler::AUTH_REALM = "Basic realm=\"Stellarium rem ote control\""; const QByteArray RequestHandler::AUTH_REALM = "Basic realm=\"Stellarium rem ote control\"";
class StelTemplateTranslationProvider : public ITemplateTranslationProvider class HtmlTranslationProvider : public ITemplateTranslationProvider
{ {
public: public:
StelTemplateTranslationProvider() HtmlTranslationProvider(StelTranslator* localInstance)
{ {
//create a translator for remote control specific stuff rcTranslator = localInstance;
rcTranslator = new StelTranslator("stellarium-remotecontrol"
,StelTranslator::globalTranslator->getTrueLocaleName());
} }
QString getTranslation(const QString &key) Q_DECL_OVERRIDE
{
//try to get a RemoteControl specific translation first
QString trans = rcTranslator->tryQtranslate(key);
if(trans.isNull())
trans = StelTranslator::globalTranslator->qtranslate
(key);
//HTML escape + single quote escape
return trans.toHtmlEscaped().replace('\'',"&#39");
}
private:
StelTranslator* rcTranslator;
};
~StelTemplateTranslationProvider() class JsTranslationProvider : public ITemplateTranslationProvider
{
public:
JsTranslationProvider(StelTranslator* localInstance)
{ {
delete rcTranslator; rcTranslator = localInstance;
} }
QString getTranslation(const QString &key) Q_DECL_OVERRIDE QString getTranslation(const QString &key) Q_DECL_OVERRIDE
{ {
//try to get a RemoteControl specific translation first //try to get a RemoteControl specific translation first
QString trans = rcTranslator->tryQtranslate(key); QString trans = rcTranslator->tryQtranslate(key);
if(trans.isNull()) if(trans.isNull())
return StelTranslator::globalTranslator->qtranslate( trans = StelTranslator::globalTranslator->qtranslate
key); (key);
return trans; //JS escape single/double quotes
return trans.replace('\'',"\\'").replace('"',"\\\"");
} }
private: private:
StelTranslator* rcTranslator; StelTranslator* rcTranslator;
}; };
RequestHandler::RequestHandler(const StaticFileControllerSettings& settings , QObject* parent) : HttpRequestHandler(parent), usePassword(false) RequestHandler::RequestHandler(const StaticFileControllerSettings& settings , QObject* parent) : HttpRequestHandler(parent), usePassword(false)
{ {
apiController = new APIController(QByteArray("/api/").size(),this); apiController = new APIController(QByteArray("/api/").size(),this);
//register the services //register the services
skipping to change at line 89 skipping to change at line 104
apiController->registerService(new ObjectService("objects",apiContro ller)); apiController->registerService(new ObjectService("objects",apiContro ller));
apiController->registerService(new ScriptService("scripts",apiContro ller)); apiController->registerService(new ScriptService("scripts",apiContro ller));
apiController->registerService(new SimbadService("simbad",apiControl ler)); apiController->registerService(new SimbadService("simbad",apiControl ler));
apiController->registerService(new StelActionService("stelaction",ap iController)); apiController->registerService(new StelActionService("stelaction",ap iController));
apiController->registerService(new StelPropertyService("stelproperty ",apiController)); apiController->registerService(new StelPropertyService("stelproperty ",apiController));
apiController->registerService(new LocationService("location",apiCon troller)); apiController->registerService(new LocationService("location",apiCon troller));
apiController->registerService(new LocationSearchService("locationse arch",apiController)); apiController->registerService(new LocationSearchService("locationse arch",apiController));
apiController->registerService(new ViewService("view",apiController) ); apiController->registerService(new ViewService("view",apiController) );
staticFiles = new StaticFileController(settings,this); staticFiles = new StaticFileController(settings,this);
connect(&StelApp::getInstance(),SIGNAL(languageChanged()),this,SLOT( connect(&StelApp::getInstance(),SIGNAL(languageChanged()),this,SLOT(
refreshHtmlTemplate())); refreshTemplates()));
refreshHtmlTemplate(); refreshTemplates();
} }
RequestHandler::~RequestHandler() RequestHandler::~RequestHandler()
{ {
} }
void RequestHandler::update(double deltaTime) void RequestHandler::update(double deltaTime)
{ {
apiController->update(deltaTime); apiController->update(deltaTime);
} }
void RequestHandler::service(HttpRequest &request, HttpResponse &response) void RequestHandler::service(HttpRequest &request, HttpResponse &response)
{ {
skipping to change at line 149 skipping to change at line 163
{ {
//transparently redirect to index.html //transparently redirect to index.html
path = "/index.html"; path = "/index.html";
} }
if(templateMap.contains(path)) if(templateMap.contains(path))
{ {
#ifndef QT_NO_DEBUG #ifndef QT_NO_DEBUG
//force fresh loading for each request in debug mode //force fresh loading for each request in debug mode
//to allow for immediate display of changes //to allow for immediate display of changes
refreshHtmlTemplate(); refreshTemplates();
#endif #endif
//get a mime type //get a mime type
QByteArray mime = StaticFileController::getContentTy pe(path,"utf-8"); QByteArray mime = StaticFileController::getContentTy pe(path,"utf-8");
if(!mime.isEmpty()) if(!mime.isEmpty())
response.setHeader("Content-Type",mime); response.setHeader("Content-Type",mime);
//serve the stored template //serve the stored template
response.write(templateMap[path].toUtf8(),true); response.write(templateMap[path].toUtf8(),true);
} }
else else
skipping to change at line 182 skipping to change at line 196
void RequestHandler::setPassword(const QString &pw) void RequestHandler::setPassword(const QString &pw)
{ {
password = pw; password = pw;
//pre-create the expected response string //pre-create the expected response string
QByteArray arr = password.toUtf8(); QByteArray arr = password.toUtf8();
arr.prepend(':'); arr.prepend(':');
passwordReply = "Basic " + arr.toBase64(); passwordReply = "Basic " + arr.toBase64();
} }
void RequestHandler::refreshHtmlTemplate() void RequestHandler::refreshTemplates()
{ {
//remove old tranlations //multiple threads can potentially enter here,
//so this requires locking
QMutexLocker locker(&templateMutex);
//remove old translations
templateMap.clear(); templateMap.clear();
//create a translator for remote control specific stuff, with the cu
rrent language
StelTranslator rcTrans("stellarium-remotecontrol",StelTranslator::gl
obalTranslator->getTrueLocaleName());
JsTranslationProvider jsTranslator(&rcTrans);
HtmlTranslationProvider htmlTranslator(&rcTrans);
StelTemplateTranslationProvider transProv;
QDir docRoot = QDir(staticFiles->getDocRoot()); QDir docRoot = QDir(staticFiles->getDocRoot());
//load the translate_files list //load the translate_files list
QFile transFileList(docRoot.absoluteFilePath("translate_files")); QFile transFileList(docRoot.absoluteFilePath("translate_files"));
if(transFileList.open(QFile::ReadOnly)) if(transFileList.open(QFile::ReadOnly))
{ {
QTextStream text(&transFileList); QTextStream text(&transFileList);
//read line by line, ignoring whitespace and comments //read line by line, ignoring whitespace and comments
while(!text.atEnd()) while(!text.atEnd())
{ {
QString line = text.readLine().trimmed(); QString line = text.readLine().trimmed();
if(line.isEmpty() || line.startsWith('#')) if(line.isEmpty() || line.startsWith('#'))
continue; continue;
//load file and translate //load file and translate
QFile f(docRoot.absoluteFilePath(line)); QFile f(docRoot.absoluteFilePath(line));
if(f.exists()) if(f.exists())
{ {
//use the HTML escapes by default,
//but use JS escapes for js files
ITemplateTranslationProvider* transProv = &h
tmlTranslator;
if(line.endsWith(".js"))
transProv = &jsTranslator;
Template tmp(f); Template tmp(f);
tmp.translate(transProv); tmp.translate(*transProv);
//check if the file was correctly loaded //check if the file was correctly loaded
if(tmp.size()>0) if(tmp.size()>0)
{ {
templateMap.insert('/'+line.toUtf8() ,tmp); templateMap.insert('/'+line.toUtf8() ,tmp);
} }
} }
else else
qWarning()<<"[RemoteControl] Translatable fi le"<<f.fileName()<<"does not exist!"; qWarning()<<"[RemoteControl] Translatable fi le"<<f.fileName()<<"does not exist!";
} }
transFileList.close(); transFileList.close();
 End of changes. 16 change blocks. 
19 lines changed or deleted 48 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/