20 #ifndef REMOTECONTROLSERVICEINTERFACE_HPP_ 21 #define REMOTECONTROLSERVICEINTERFACE_HPP_ 26 #include <QJsonDocument> 28 #include <QMimeDatabase> 46 void setHeader(
const QByteArray &name,
const QByteArray &val)
51 void setHeader(
const QByteArray& name,
const int val)
53 headers[name]=QByteArray::number(val);
59 setHeader(
"Cache-Control",
"max-age="+QByteArray::number(seconds));
65 this->status = status;
66 this->statusText = text;
72 this->responseData = data;
77 this->responseData.append(data);
92 QByteArray data = doc.toJson(QJsonDocument::Compact);
95 QByteArray data = doc.toJson(QJsonDocument::Indented);
98 setHeader(
"Content-Type",
"application/json; charset=utf-8");
111 QString wrapped = QStringLiteral(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html><head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n<title>")
112 + title + QStringLiteral(
"</title>\n<link type=\"text/css\" rel=\"stylesheet\" href=\"/iframestyle.css\">\n<base target=\"_blank\">\n</head><body>\n")
113 + html + QStringLiteral(
"</body></html>");
115 setHeader(
"Content-Type",
"text/html; charset=UTF-8");
122 void writeFile(
const QString& path,
bool allowCaching =
false)
125 if (path.isEmpty() || !file.exists())
128 setData(
"requested file resource not found");
132 QMimeType mime = QMimeDatabase().mimeTypeForFile(path);
134 if(file.open(QIODevice::ReadOnly))
137 setHeader(
"Cache-Control",
"max-age="+QByteArray::number(60*60));
140 if(!mime.isDefault())
142 setHeader(
"Content-Type", mime.name().toLatin1());
150 qWarning()<<
"Could not open requested file resource"<<path<<file.errorString();
152 setData(
"could not open file resource");
159 QByteArray statusText;
160 QMap<QByteArray,QByteArray> headers;
161 QByteArray responseData;
163 static int metaTypeId;
164 static int parametersMetaTypeId;
182 virtual ~RemoteControlServiceInterface() {}
186 virtual QLatin1String getPath()
const = 0;
191 virtual bool isThreadSafe()
const = 0;
211 virtual void update(
double deltaTime) = 0;
215 #define RemoteControlServiceInterface_iid "org.stellarium.plugin.RemoteSync.RemoteControlServiceInterface/1.0" void writeRequestError(const QByteArray &msg)
Sets the HTTP status to 400, and sets the response data to the message.
void setCacheTime(int seconds)
Sets the time in seconds for which the browser is allowed to cache the reply.
void writeJSON(const QJsonDocument &doc)
Sets the Content-Type to "application/json" and serializes the given document into JSON text format...
Thread-safe version of HttpResponse that can be passed around through QMetaObject::invokeMethod.
APIServiceResponse()
Constructs an invalid response.
void appendData(const QByteArray &data)
Appends to the current return data.
void writeWrappedHTML(const QString &html, const QString &title)
Because the HTML descriptions in Stellarium are often not compatible with "clean" HTML5 which is used...
void setHeader(const QByteArray &name, const QByteArray &val)
Sets a specific HTTP header to the specified value.
QMultiMap< QByteArray, QByteArray > APIParameters
Defines the HTTP request parameters for the service.
void update(double deltaTime)
Should be called each frame from the main thread, like from StelModule::update.
Interface for all Remote Control Plug-in services.
void setData(const QByteArray &data)
Replaces the current return data.
This class handles the API-specific requests and dispatches them to the correct RemoteControlServiceI...
void setStatus(int status, const QByteArray &text)
Sets the HTTP status type and status text.
void setHeader(const QByteArray &name, const int val)
Shortcut for int header values.
void writeFile(const QString &path, bool allowCaching=false)
Writes the specified file contents into the response.