MythTV  master
mythfexml.cpp
Go to the documentation of this file.
1 // 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"
20 #include "libmythbase/mythdate.h"
21 #include "libmythbase/mythdbcon.h"
24 
25 // MythFrontend
26 #include "keybindings.h"
27 #include "mythfexml.h"
28 #include "services/frontend.h"
29 
31 //
33 
34 MythFEXML::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;
97  case MFEXML_GetRemote:
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 {
116  pRequest->m_eResponseType = ResponseTypeFile;
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 
139  MythMainWindow *window = GetMythMainWindow();
140  window->RemoteScreenShot(sFileName, nWidth, nHeight);
141 
142  pRequest->m_sFileName = sFileName;
143 }
144 
145 static 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 
162 static 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 
170  pRequest->m_eResponseType = ResponseTypeHTML;
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 
202 static 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 {
209  pRequest->m_eResponseType = ResponseTypeHTML;
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 }
MythFEXML::ProcessRequest
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: mythfexml.cpp:75
HTTPRequest::m_sBaseUrl
QString m_sBaseUrl
Definition: httprequest.h:127
HTTPRequest
Definition: httprequest.h:109
MFEXML_GetScreenShot
@ MFEXML_GetScreenShot
Definition: mythfexml.h:18
Eventing::GetBasePaths
QStringList GetBasePaths() override
Definition: eventing.cpp:134
HTTPRequest::m_sMethod
QString m_sMethod
Definition: httprequest.h:129
MythFEXML::GetScreenShot
static void GetScreenShot(HTTPRequest *pRequest)
Definition: mythfexml.cpp:114
PROCESS_ACTION
static const QString PROCESS_ACTION
Definition: mythfexml.cpp:145
mythdbcon.h
Eventing
Definition: eventing.h:245
MythFEXML::m_sControlUrl
QString m_sControlUrl
Definition: mythfexml.h:27
keybindings.h
Main header for keybinding classes.
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MFEXML_GetRemote
@ MFEXML_GetRemote
Definition: mythfexml.h:20
ResponseTypeFile
@ ResponseTypeFile
Definition: httprequest.h:84
UPnp::FormatErrorResponse
static void FormatErrorResponse(HTTPRequest *pRequest, UPnPResultCode eCode, const QString &sMsg="")
Definition: upnp.cpp:241
XmlConfiguration
Definition: configuration.h:38
HTTPRequest::m_mapRespHeaders
QStringMap m_mapRespHeaders
Definition: httprequest.h:153
MythFEXML::GetMethod
static MythFEXMLMethod GetMethod(const QString &sURI)
Definition: mythfexml.cpp:52
MFEXML_ActionListTest
@ MFEXML_ActionListTest
Definition: mythfexml.h:19
UPnpServiceImpl::RegisterService
void RegisterService(UPnpDevice *device)
Creates a UPnpService and adds it to the UPnpDevice's list of services.
Definition: upnpserviceimpl.cpp:5
mythdate.h
MythFEXML::GetBasePaths
QStringList GetBasePaths() override
Definition: mythfexml.cpp:66
mythlogging.h
MythFEXML::m_sServiceDescFileName
QString m_sServiceDescFileName
Definition: mythfexml.h:28
HTTPRequest::m_mapParams
QStringMap m_mapParams
Definition: httprequest.h:131
MythMainWindow::RemoteScreenShot
void RemoteScreenShot(QString Filename, int Width, int Height)
Definition: mythmainwindow.cpp:529
MythFEXMLMethod
MythFEXMLMethod
Definition: mythfexml.h:14
UPnPResult_InvalidAction
@ UPnPResult_InvalidAction
Definition: upnp.h:39
UPnpDevice
Definition: upnpdevice.h:102
HTTPRequest::m_sRawRequest
QString m_sRawRequest
Definition: httprequest.h:123
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
ResponseTypeHTML
@ ResponseTypeHTML
Definition: httprequest.h:79
HTTPRequest::m_sFileName
QString m_sFileName
Definition: httprequest.h:155
MythFEXML::GetActionListTest
static void GetActionListTest(HTTPRequest *pRequest)
Definition: mythfexml.cpp:166
HIDDEN_IFRAME
static const QString HIDDEN_IFRAME
Definition: mythfexml.cpp:162
mythfexml.h
XmlConfiguration::GetValue
QString GetValue(const QString &setting)
Definition: configuration.cpp:202
mythcorecontext.h
MFEXML_Unknown
@ MFEXML_Unknown
Definition: mythfexml.h:16
HTTPRequest::FormatFileResponse
void FormatFileResponse(const QString &sFileName)
Definition: httprequest.cpp:792
HTTPRequest::m_eResponseType
HttpResponseType m_eResponseType
Definition: httprequest.h:149
frontend.h
Frontend::gActionDescriptions
static QHash< QString, QStringList > gActionDescriptions
Definition: frontend.h:53
configuration.h
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
build_compdb.action
action
Definition: build_compdb.py:9
MythFEXML::MythFEXML
MythFEXML(UPnpDevice *pDevice, const QString &sSharePath)
Definition: mythfexml.cpp:34
MFEXML_GetServiceDescription
@ MFEXML_GetServiceDescription
Definition: mythfexml.h:17
BUTTON
static QString BUTTON(const char *action, const char *desc)
Definition: mythfexml.cpp:202
mythmainwindow.h
Frontend::InitialiseActions
static void InitialiseActions(void)
Definition: frontend.cpp:306
HTTPRequest::m_response
QBuffer m_response
Definition: httprequest.h:157
MythMainWindow
Definition: mythmainwindow.h:28
MythFEXML::GetRemote
static void GetRemote(HTTPRequest *pRequest)
Definition: mythfexml.cpp:207
HttpServerExtension::m_sSharePath
QString m_sSharePath
Definition: httpserver.h:78
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:904