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 <QTextStream>
10
11// MythTV
20
21// MythFrontend
22#include "mythfexml.h"
23#include "services/frontend.h"
24
26//
28
29MythFEXML::MythFEXML( UPnpDevice *pDevice , const QString &sSharePath)
30 : Eventing( "MythFEXML", "MYTHTV_Event", sSharePath),
31 m_sControlUrl("/MythFE")
32{
33
34 QString sUPnpDescPath = XmlConfiguration().GetValue("UPnP/DescXmlPath", m_sSharePath);
35
36 m_sServiceDescFileName = sUPnpDescPath + "MFEXML_scpd.xml";
37
38 // Add our Service Definition to the device.
39
40 RegisterService( pDevice );
41}
42
44//
46
48{
49 if (sURI == "GetServDesc") return MFEXML_GetServiceDescription;
50 if (sURI == "GetScreenShot") return MFEXML_GetScreenShot;
51 if (sURI == "GetActionTest") return MFEXML_ActionListTest;
52
53 return MFEXML_Unknown;
54}
55
57//
59
61{
63}
64
66//
68
70{
71 if (!pRequest)
72 return false;
73
74 if (pRequest->m_sBaseUrl != m_sControlUrl)
75 return false;
76
77 LOG(VB_UPNP, LOG_INFO, QString("MythFEXML::ProcessRequest: %1 : %2")
78 .arg(pRequest->m_sMethod, pRequest->m_sRawRequest));
79
80 switch(GetMethod(pRequest->m_sMethod))
81 {
84 break;
86 GetScreenShot(pRequest);
87 break;
89 GetActionListTest(pRequest);
90 break;
91 default:
93 }
94 return true;
95}
96
97// ==========================================================================
98// Request handler Methods
99// ==========================================================================
100
102//
104
106{
108
109 // Optional Parameters
110 int nWidth = pRequest->m_mapParams[ "width" ].toInt();
111 int nHeight = pRequest->m_mapParams[ "height" ].toInt();
112 QString sFormat = pRequest->m_mapParams[ "format" ];
113
114 if (sFormat.isEmpty())
115 sFormat = "png";
116
117 if (sFormat != "jpg" && sFormat != "png")
118 {
119 LOG(VB_GENERAL, LOG_ERR, "Invalid screen shot format: " + sFormat);
120 return;
121 }
122
123 LOG(VB_GENERAL, LOG_INFO,
124 QString("Screen shot requested (%1x%2), format %3")
125 .arg(nWidth).arg(nHeight).arg(sFormat));
126
127 QString sFileName = QString("/%1/myth-screenshot-XML.%2")
128 .arg(gCoreContext->GetSetting("ScreenShotPath","/tmp"), sFormat);
129
131 window->RemoteScreenShot(sFileName, nWidth, nHeight);
132
133 pRequest->m_sFileName = sFileName;
134}
135
136static const QString PROCESS_ACTION =
137 " <script type =\"text/javascript\">\n"
138 " function postaction(action) {\n"
139 " var myForm = document.createElement(\"form\");\n"
140 " myForm.method =\"Post\";\n"
141 " myForm.action =\"../Frontend/SendAction?\";\n"
142 " myForm.target =\"post_target\";\n"
143 " var myInput = document.createElement(\"input\");\n"
144 " myInput.setAttribute(\"name\", \"Action\");\n"
145 " myInput.setAttribute(\"value\", action);\n"
146 " myForm.appendChild(myInput);\n"
147 " document.body.appendChild(myForm);\n"
148 " myForm.submit();\n"
149 " document.body.removeChild(myForm);\n"
150 " }\n"
151 " </script>\n";
152
153static const QString HIDDEN_IFRAME =
154 " <iframe id=\"hidden_target\" name=\"post_target\" src=\"\""
155 " style=\"width:0;height:0;border:0px solid #fff;\"></iframe>\n";
156
158{
160
162 pRequest->m_mapRespHeaders[ "Cache-Control" ] = "no-cache=\"Ext\", max-age = 5000";
163
164 QTextStream stream( &pRequest->m_response );
165
166 stream <<
167 "<html>\n" << PROCESS_ACTION <<
168 " <body>\n" << HIDDEN_IFRAME;
169
170 for (auto contexts = Frontend::gActionDescriptions.cbegin();
171 contexts != Frontend::gActionDescriptions.cend(); ++contexts)
172 {
173 QStringList actions = contexts.value();
174 for (const QString & action : std::as_const(actions))
175 {
176 QStringList split = action.split(",");
177 if (split.size() == 2)
178 {
179 stream <<
180 QString(" <div>%1&nbsp;<input type=\"button\" value=\"%2\" onClick=\"postaction('%2');\"></input>&nbsp;%3</div>\n")
181 .arg(contexts.key(), split[0], split[1]);
182 }
183 }
184 }
185
186 stream <<
187 " </body>\n"
188 "</html>\n";
189
190}
QStringList GetBasePaths() override
Definition: eventing.cpp:136
static void InitialiseActions(void)
Definition: frontend.cpp:305
static QHash< QString, QStringList > gActionDescriptions
Definition: frontend.h:50
HttpResponseType m_eResponseType
Definition: httprequest.h:150
QString m_sFileName
Definition: httprequest.h:156
void FormatErrorResponse(bool bServerError, const QString &sFaultString, const QString &sDetails)
QString m_sMethod
Definition: httprequest.h:131
void FormatFileResponse(const QString &sFileName)
QStringMap m_mapRespHeaders
Definition: httprequest.h:154
QString m_sBaseUrl
Definition: httprequest.h:129
QString m_sRawRequest
Definition: httprequest.h:125
QStringMap m_mapParams
Definition: httprequest.h:133
QBuffer m_response
Definition: httprequest.h:158
QString m_sSharePath
Definition: httpserver.h:78
QString GetSetting(const QString &key, const QString &defaultval="")
static void GetActionListTest(HTTPRequest *pRequest)
Definition: mythfexml.cpp:157
QString m_sServiceDescFileName
Definition: mythfexml.h:32
MythFEXML(UPnpDevice *pDevice, const QString &sSharePath)
Definition: mythfexml.cpp:29
QStringList GetBasePaths() override
Definition: mythfexml.cpp:60
static void GetScreenShot(HTTPRequest *pRequest)
Definition: mythfexml.cpp:105
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: mythfexml.cpp:69
static MythFEXMLMethod GetMethod(const QString &sURI)
Definition: mythfexml.cpp:47
QString m_sControlUrl
Definition: mythfexml.h:31
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.
QString GetValue(const QString &setting)
@ ResponseTypeFile
Definition: httprequest.h:86
@ ResponseTypeHTML
Definition: httprequest.h:81
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
static const QString HIDDEN_IFRAME
Definition: mythfexml.cpp:153
static const QString PROCESS_ACTION
Definition: mythfexml.cpp:136
MythFEXMLMethod
Definition: mythfexml.h:20
@ MFEXML_ActionListTest
Definition: mythfexml.h:24
@ MFEXML_GetScreenShot
Definition: mythfexml.h:23
@ MFEXML_GetServiceDescription
Definition: mythfexml.h:22
@ MFEXML_Unknown
Definition: mythfexml.h:21
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythMainWindow * GetMythMainWindow(void)
@ UPnPResult_InvalidAction