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