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