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 #include "mythfexml.h"
9 
10 #include "mythcorecontext.h"
11 #include "mythdate.h"
12 #include "mythdbcon.h"
13 
14 #include "mythmainwindow.h"
15 
16 #include <QTextStream>
17 #include <QCoreApplication>
18 #include <QTextStream>
19 #include <QDir>
20 #include <QFile>
21 #include <QBuffer>
22 #include <QKeyEvent>
23 
24 #include "../../config.h"
25 
26 #include "keybindings.h"
27 
28 #include "services/frontend.h"
29 
31 //
33 
34 MythFEXML::MythFEXML( UPnpDevice *pDevice , const QString &sSharePath)
35  : Eventing( "MythFEXML", "MYTHTV_Event", sSharePath)
36 {
37 
38  QString sUPnpDescPath =
39  UPnp::GetConfiguration()->GetValue( "UPnP/DescXmlPath", m_sSharePath );
40 
41  m_sServiceDescFileName = sUPnpDescPath + "MFEXML_scpd.xml";
42  m_sControlUrl = "/MythFE";
43 
44  // Add our Service Definition to the device.
45 
46  RegisterService( pDevice );
47 }
48 
50 //
52 
54 {
55  if (sURI == "GetServDesc") return MFEXML_GetServiceDescription;
56  if (sURI == "GetScreenShot") return MFEXML_GetScreenShot;
57  if (sURI == "GetActionTest") return MFEXML_ActionListTest;
58  if (sURI == "GetRemote") return MFEXML_GetRemote;
59 
60  return( MFEXML_Unknown );
61 }
62 
64 //
66 
68 {
70 }
71 
73 //
75 
77 {
78  if (!pRequest)
79  return false;
80 
81  if (pRequest->m_sBaseUrl != m_sControlUrl)
82  return false;
83 
84  LOG(VB_UPNP, LOG_INFO, QString("MythFEXML::ProcessRequest: %1 : %2")
85  .arg(pRequest->m_sMethod, pRequest->m_sRawRequest));
86 
87  switch(GetMethod(pRequest->m_sMethod))
88  {
91  break;
93  GetScreenShot(pRequest);
94  break;
96  GetActionListTest(pRequest);
97  break;
98  case MFEXML_GetRemote:
99  pRequest->FormatFileResponse(m_sSharePath + "html/frontend_index.qsp");
100  break;
101  default:
103  }
104  return true;
105 }
106 
107 // ==========================================================================
108 // Request handler Methods
109 // ==========================================================================
110 
112 //
114 
116 {
117  pRequest->m_eResponseType = ResponseTypeFile;
118 
119  // Optional Parameters
120  int nWidth = pRequest->m_mapParams[ "width" ].toInt();
121  int nHeight = pRequest->m_mapParams[ "height" ].toInt();
122  QString sFormat = pRequest->m_mapParams[ "format" ];
123 
124  if (sFormat.isEmpty())
125  sFormat = "png";
126 
127  if (sFormat != "jpg" && sFormat != "png")
128  {
129  LOG(VB_GENERAL, LOG_ERR, "Invalid screen shot format: " + sFormat);
130  return;
131  }
132 
133  LOG(VB_GENERAL, LOG_INFO,
134  QString("Screen shot requested (%1x%2), format %3")
135  .arg(nWidth).arg(nHeight).arg(sFormat));
136 
137  QString sFileName = QString("/%1/myth-screenshot-XML.%2")
138  .arg(gCoreContext->GetSetting("ScreenShotPath","/tmp"), sFormat);
139 
140  MythMainWindow *window = GetMythMainWindow();
141  window->RemoteScreenShot(sFileName, nWidth, nHeight);
142 
143  pRequest->m_sFileName = sFileName;
144 }
145 
146 static const QString PROCESS_ACTION =
147  " <script type =\"text/javascript\">\n"
148  " function postaction(action) {\n"
149  " var myForm = document.createElement(\"form\");\n"
150  " myForm.method =\"Post\";\n"
151  " myForm.action =\"../Frontend/SendAction?\";\n"
152  " myForm.target =\"post_target\";\n"
153  " var myInput = document.createElement(\"input\");\n"
154  " myInput.setAttribute(\"name\", \"Action\");\n"
155  " myInput.setAttribute(\"value\", action);\n"
156  " myForm.appendChild(myInput);\n"
157  " document.body.appendChild(myForm);\n"
158  " myForm.submit();\n"
159  " document.body.removeChild(myForm);\n"
160  " }\n"
161  " </script>\n";
162 
163 static const QString HIDDEN_IFRAME =
164  " <iframe id=\"hidden_target\" name=\"post_target\" src=\"\""
165  " style=\"width:0;height:0;border:0px solid #fff;\"></iframe>\n";
166 
168 {
170 
171  pRequest->m_eResponseType = ResponseTypeHTML;
172  pRequest->m_mapRespHeaders[ "Cache-Control" ] = "no-cache=\"Ext\", max-age = 5000";
173 
174  QTextStream stream( &pRequest->m_response );
175 
176  stream <<
177  "<html>\n" << PROCESS_ACTION <<
178  " <body>\n" << HIDDEN_IFRAME;
179 
180  QHashIterator<QString,QStringList> contexts(Frontend::gActionDescriptions);
181  while (contexts.hasNext())
182  {
183  contexts.next();
184  QStringList actions = contexts.value();
185  for (const QString & action : qAsConst(actions))
186  {
187  QStringList split = action.split(",");
188  if (split.size() == 2)
189  {
190  stream <<
191  QString(" <div>%1&nbsp;<input type=\"button\" value=\"%2\" onClick=\"postaction('%2');\"></input>&nbsp;%3</div>\n")
192  .arg(contexts.key(), split[0], split[1]);
193  }
194  }
195  }
196 
197  stream <<
198  " </body>\n"
199  "</html>\n";
200 
201 }
202 
203 #define BUTTON(action,desc) \
204  QString(" <input class=\"bigb\" type=\"button\" value=\"%1\" onClick=\"postaction('%2');\"></input>\r\n").arg(action, desc)
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:76
HTTPRequest::m_sBaseUrl
QString m_sBaseUrl
Definition: httprequest.h:126
HTTPRequest
Definition: httprequest.h:108
Eventing::GetBasePaths
QStringList GetBasePaths() override
Definition: eventing.cpp:141
HTTPRequest::m_sMethod
QString m_sMethod
Definition: httprequest.h:128
MythFEXML::GetScreenShot
static void GetScreenShot(HTTPRequest *pRequest)
Definition: mythfexml.cpp:115
PROCESS_ACTION
static const QString PROCESS_ACTION
Definition: mythfexml.cpp:146
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:23
UPnp::FormatErrorResponse
static void FormatErrorResponse(HTTPRequest *pRequest, UPnPResultCode eCode, const QString &sMsg="")
Definition: upnp.cpp:280
ResponseTypeFile
@ ResponseTypeFile
Definition: httprequest.h:83
HTTPRequest::m_mapRespHeaders
QStringMap m_mapRespHeaders
Definition: httprequest.h:152
MythFEXML::GetMethod
static MythFEXMLMethod GetMethod(const QString &sURI)
Definition: mythfexml.cpp:53
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:67
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:130
MythMainWindow::RemoteScreenShot
void RemoteScreenShot(QString Filename, int Width, int Height)
Definition: mythmainwindow.cpp:529
BUTTON
#define BUTTON(action, desc)
Definition: mythfexml.cpp:203
UPnpDevice
Definition: upnpdevice.h:101
frontend.h
HTTPRequest::m_sRawRequest
QString m_sRawRequest
Definition: httprequest.h:122
MFEXML_ActionListTest
@ MFEXML_ActionListTest
Definition: mythfexml.h:20
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
HTTPRequest::m_sFileName
QString m_sFileName
Definition: httprequest.h:154
ResponseTypeHTML
@ ResponseTypeHTML
Definition: httprequest.h:78
UPnPResult_InvalidAction
@ UPnPResult_InvalidAction
Definition: upnp.h:40
MythFEXML::GetActionListTest
static void GetActionListTest(HTTPRequest *pRequest)
Definition: mythfexml.cpp:167
HIDDEN_IFRAME
static const QString HIDDEN_IFRAME
Definition: mythfexml.cpp:163
mythfexml.h
mythcorecontext.h
HTTPRequest::FormatFileResponse
void FormatFileResponse(const QString &sFileName)
Definition: httprequest.cpp:858
HTTPRequest::m_eResponseType
HttpResponseType m_eResponseType
Definition: httprequest.h:148
Frontend::gActionDescriptions
static QHash< QString, QStringList > gActionDescriptions
Definition: programs/mythfrontend/services/frontend.h:50
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
Configuration::GetValue
virtual int GetValue(const QString &sSetting, int Default)=0
build_compdb.action
action
Definition: build_compdb.py:9
UPnp::GetConfiguration
static Configuration * GetConfiguration()
Definition: upnp.cpp:84
MythFEXML::MythFEXML
MythFEXML(UPnpDevice *pDevice, const QString &sSharePath)
Definition: mythfexml.cpp:34
mythmainwindow.h
Frontend::InitialiseActions
static void InitialiseActions(void)
Definition: frontend.cpp:303
HTTPRequest::m_response
QBuffer m_response
Definition: httprequest.h:156
MythMainWindow
Definition: mythmainwindow.h:35
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:76
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:922
MFEXML_GetScreenShot
@ MFEXML_GetScreenShot
Definition: mythfexml.h:19