MythTV master
mythfexml.cpp
Go to the documentation of this file.
1
2// Program Name: MythFE.cpp
3//
4// Purpose - Frontend Html & XML status HttpServerExtension
5//
7
8// Qt
9#include <QBuffer>
10#include <QCoreApplication>
11#include <QDir>
12#include <QFile>
13#include <QKeyEvent>
14#include <QTextStream>
15
16// MythTV
17#include "libmythbase/mythconfig.h"
24
25// MythFrontend
26#include "keybindings.h"
27#include "mythfexml.h"
28#include "services/frontend.h"
29
31//
33
34MythFEXML::MythFEXML( UPnpDevice *pDevice , const QString &sSharePath)
35 : Eventing( "MythFEXML", "MYTHTV_Event", sSharePath),
36 m_sControlUrl("/MythFE")
37{
38
39 QString sUPnpDescPath = XmlConfiguration().GetValue("UPnP/DescXmlPath", m_sSharePath);
40
41 m_sServiceDescFileName = sUPnpDescPath + "MFEXML_scpd.xml";
42
43 // Add our Service Definition to the device.
44
45 RegisterService( pDevice );
46}
47
49//
51
53{
54 if (sURI == "GetServDesc") return MFEXML_GetServiceDescription;
55 if (sURI == "GetScreenShot") return MFEXML_GetScreenShot;
56 if (sURI == "GetActionTest") return MFEXML_ActionListTest;
57 if (sURI == "GetRemote") return MFEXML_GetRemote;
58
59 return( MFEXML_Unknown );
60}
61
63//
65
67{
69}
70
72//
74
76{
77 if (!pRequest)
78 return false;
79
80 if (pRequest->m_sBaseUrl != m_sControlUrl)
81 return false;
82
83 LOG(VB_UPNP, LOG_INFO, QString("MythFEXML::ProcessRequest: %1 : %2")
84 .arg(pRequest->m_sMethod, pRequest->m_sRawRequest));
85
86 switch(GetMethod(pRequest->m_sMethod))
87 {
90 break;
92 GetScreenShot(pRequest);
93 break;
95 GetActionListTest(pRequest);
96 break;
98 pRequest->FormatFileResponse(m_sSharePath + "html/frontend_index.qsp");
99 break;
100 default:
102 }
103 return true;
104}
105
106// ==========================================================================
107// Request handler Methods
108// ==========================================================================
109
111//
113
115{
117
118 // Optional Parameters
119 int nWidth = pRequest->m_mapParams[ "width" ].toInt();
120 int nHeight = pRequest->m_mapParams[ "height" ].toInt();
121 QString sFormat = pRequest->m_mapParams[ "format" ];
122
123 if (sFormat.isEmpty())
124 sFormat = "png";
125
126 if (sFormat != "jpg" && sFormat != "png")
127 {
128 LOG(VB_GENERAL, LOG_ERR, "Invalid screen shot format: " + sFormat);
129 return;
130 }
131
132 LOG(VB_GENERAL, LOG_INFO,
133 QString("Screen shot requested (%1x%2), format %3")
134 .arg(nWidth).arg(nHeight).arg(sFormat));
135
136 QString sFileName = QString("/%1/myth-screenshot-XML.%2")
137 .arg(gCoreContext->GetSetting("ScreenShotPath","/tmp"), sFormat);
138
140 window->RemoteScreenShot(sFileName, nWidth, nHeight);
141
142 pRequest->m_sFileName = sFileName;
143}
144
145static const QString PROCESS_ACTION =
146 " <script type =\"text/javascript\">\n"
147 " function postaction(action) {\n"
148 " var myForm = document.createElement(\"form\");\n"
149 " myForm.method =\"Post\";\n"
150 " myForm.action =\"../Frontend/SendAction?\";\n"
151 " myForm.target =\"post_target\";\n"
152 " var myInput = document.createElement(\"input\");\n"
153 " myInput.setAttribute(\"name\", \"Action\");\n"
154 " myInput.setAttribute(\"value\", action);\n"
155 " myForm.appendChild(myInput);\n"
156 " document.body.appendChild(myForm);\n"
157 " myForm.submit();\n"
158 " document.body.removeChild(myForm);\n"
159 " }\n"
160 " </script>\n";
161
162static const QString HIDDEN_IFRAME =
163 " <iframe id=\"hidden_target\" name=\"post_target\" src=\"\""
164 " style=\"width:0;height:0;border:0px solid #fff;\"></iframe>\n";
165
167{
169
171 pRequest->m_mapRespHeaders[ "Cache-Control" ] = "no-cache=\"Ext\", max-age = 5000";
172
173 QTextStream stream( &pRequest->m_response );
174
175 stream <<
176 "<html>\n" << PROCESS_ACTION <<
177 " <body>\n" << HIDDEN_IFRAME;
178
179 QHashIterator<QString,QStringList> contexts(Frontend::gActionDescriptions);
180 while (contexts.hasNext())
181 {
182 contexts.next();
183 QStringList actions = contexts.value();
184 for (const QString & action : std::as_const(actions))
185 {
186 QStringList split = action.split(",");
187 if (split.size() == 2)
188 {
189 stream <<
190 QString(" <div>%1&nbsp;<input type=\"button\" value=\"%2\" onClick=\"postaction('%2');\"></input>&nbsp;%3</div>\n")
191 .arg(contexts.key(), split[0], split[1]);
192 }
193 }
194 }
195
196 stream <<
197 " </body>\n"
198 "</html>\n";
199
200}
201
202static inline QString BUTTON(const char *action, const char *desc)
203{
204 return QString(" <input class=\"bigb\" type=\"button\" value=\"%1\" onClick=\"postaction('%2');\"></input>\r\n").arg(action, desc);
205};
206
208{
210 pRequest->m_mapRespHeaders[ "Cache-Control" ] = "no-cache=\"Ext\", max-age = 5000";
211
212 QTextStream stream( &pRequest->m_response );
213
214 stream <<
215 "<html>\n" << PROCESS_ACTION <<
216 " <style type=\"text/css\" title=\"Default\" media=\"all\">\r\n"
217 " <!--\r\n"
218 " body {\r\n"
219 " margin: 0px;\r\n"
220 " width : 310px;\r\n"
221 " }\r\n"
222 " -->\r\n"
223 " .bigb {\r\n"
224 " width : 100px;\r\n"
225 " height: 50px;\r\n"
226 " margin: 0px;\r\n"
227 " text-align: center;\r\n"
228 " }\r\n"
229 " </style>\r\n"
230 " <title>MythFrontend Control</title>\r\n" <<
231 " <body>\n" << HIDDEN_IFRAME;
232
233 stream <<
234 " <div>\r\n" <<
235 BUTTON("1","1") << BUTTON("2","2") << BUTTON("3","3") <<
236 " </div>\r\n" <<
237 " <div>\r\n" <<
238 BUTTON("4","4") << BUTTON("5","5") << BUTTON("6","6") <<
239 " </div>\r\n" <<
240 " <div>\r\n" <<
241 BUTTON("7","7") << BUTTON("8","8") << BUTTON("9","9") <<
242 " </div>\r\n" <<
243 " <div>\r\n" <<
244 BUTTON("MENU","MENU") << BUTTON("0","0") << BUTTON("INFO","INFO") <<
245 " </div>\r\n" <<
246 " <div>\r\n" <<
247 BUTTON("Back","ESCAPE") << BUTTON("^","UP") << BUTTON("MUTE","MUTE") <<
248 " </div>\r\n" <<
249 " <div>\r\n" <<
250 BUTTON("<","LEFT") << BUTTON("Enter","SELECT") << BUTTON(">","RIGHT") <<
251 " </div>\r\n" <<
252 " <div>\r\n" <<
253 BUTTON("<<","JUMPRWND") << BUTTON("v","DOWN") << BUTTON(">>","JUMPFFWD") <<
254 " </div>\r\n";
255
256 stream <<
257 " </body>\n"
258 "</html>\n";
259}
QStringList GetBasePaths() override
Definition: eventing.cpp:135
static void InitialiseActions(void)
Definition: frontend.cpp:306
static QHash< QString, QStringList > gActionDescriptions
Definition: frontend.h:53
HttpResponseType m_eResponseType
Definition: httprequest.h:150
QString m_sFileName
Definition: httprequest.h:156
QString m_sMethod
Definition: httprequest.h:130
void FormatFileResponse(const QString &sFileName)
QStringMap m_mapRespHeaders
Definition: httprequest.h:154
QString m_sBaseUrl
Definition: httprequest.h:128
QString m_sRawRequest
Definition: httprequest.h:124
QStringMap m_mapParams
Definition: httprequest.h:132
QBuffer m_response
Definition: httprequest.h:158
QString m_sSharePath
Definition: httpserver.h:74
QString GetSetting(const QString &key, const QString &defaultval="")
static void GetActionListTest(HTTPRequest *pRequest)
Definition: mythfexml.cpp:166
QString m_sServiceDescFileName
Definition: mythfexml.h:28
MythFEXML(UPnpDevice *pDevice, const QString &sSharePath)
Definition: mythfexml.cpp:34
QStringList GetBasePaths() override
Definition: mythfexml.cpp:66
static void GetRemote(HTTPRequest *pRequest)
Definition: mythfexml.cpp:207
static void GetScreenShot(HTTPRequest *pRequest)
Definition: mythfexml.cpp:114
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: mythfexml.cpp:75
static MythFEXMLMethod GetMethod(const QString &sURI)
Definition: mythfexml.cpp:52
QString m_sControlUrl
Definition: mythfexml.h:27
void RemoteScreenShot(QString Filename, int Width, int Height)
void RegisterService(UPnpDevice *device)
Creates a UPnpService and adds it to the UPnpDevice's list of services.
static void FormatErrorResponse(HTTPRequest *pRequest, UPnPResultCode eCode, const QString &sMsg="")
Definition: upnp.cpp:242
QString GetValue(const QString &setting)
@ ResponseTypeFile
Definition: httprequest.h:85
@ ResponseTypeHTML
Definition: httprequest.h:80
Main header for keybinding classes.
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
static const QString HIDDEN_IFRAME
Definition: mythfexml.cpp:162
static const QString PROCESS_ACTION
Definition: mythfexml.cpp:145
static QString BUTTON(const char *action, const char *desc)
Definition: mythfexml.cpp:202
MythFEXMLMethod
Definition: mythfexml.h:15
@ MFEXML_ActionListTest
Definition: mythfexml.h:19
@ MFEXML_GetScreenShot
Definition: mythfexml.h:18
@ MFEXML_GetRemote
Definition: mythfexml.h:20
@ MFEXML_GetServiceDescription
Definition: mythfexml.h:17
@ MFEXML_Unknown
Definition: mythfexml.h:16
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythMainWindow * GetMythMainWindow(void)
@ UPnPResult_InvalidAction
Definition: upnp.h:37