MythTV  master
serverSideScripting.h
Go to the documentation of this file.
1 // Program Name: serverSideScripting.h
3 // Created : Mar. 22, 2011
4 //
5 // Purpose : Server Side Scripting support for Html Server
6 //
7 // Copyright (c) 2011 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see COPYING for details
10 //
12 
13 #ifndef SERVERSIDESCRIPTING_H_
14 #define SERVERSIDESCRIPTING_H_
15 
16 #include <utility>
17 
18 // Qt headers
19 #include <QDateTime>
20 #include <QMap>
21 #include <QMutex>
22 #include <QScriptEngine>
23 #include <QScriptable>
24 #include <QString>
25 #include <QTextStream>
26 
27 // MythTV headers
28 #include "upnpexp.h"
29 #include "upnputil.h"
30 #include "httprequest.h"
31 
32 #ifdef _WIN32
33 #include <QScriptEngineDebugger>
34 #endif
35 
36 class ScriptInfo
37 {
38  public:
39  QScriptValue m_oFunc;
40  QDateTime m_dtTimeStamp;
41 
42  ScriptInfo() = default;
43 
44  ScriptInfo( const QScriptValue& func, QDateTime dt )
45  : m_oFunc( func ), m_dtTimeStamp(std::move( dt ))
46  {}
47 };
48 
50 {
51  public:
52 
53  QScriptEngine m_engine;
54 
55 #ifdef _WIN32
56  QScriptEngineDebugger m_debugger;
57 #endif
58 
61 
62  QString SetResourceRootPath( const QString &path );
63 
64  void RegisterMetaObjectType( const QString &sName,
65  const QMetaObject *pMetaObject,
66  QScriptEngine::FunctionSignature pFunction);
67 
68  bool EvaluatePage( QTextStream *pOutStream, const QString &sFileName,
69  HTTPRequest *pRequest, const QByteArray &cspToken);
70 
71  protected:
72 
73  QMutex m_mutex;
74  QMap< QString, ScriptInfo* > m_mapScripts;
75  QString m_sResRootPath;
76 
77  void Lock () { m_mutex.lock(); }
78  void Unlock () { m_mutex.unlock(); }
79 
80  ScriptInfo *GetLoadedScript ( const QString &sFileName );
81 
82  static QString ReadFileContents ( const QString &sFileName ) ;
83 
84  QString CreateMethodFromFile( const QString &sFileName ) const;
85 
86  bool ProcessLine ( QTextStream &sCode,
87  QString &sLine,
88  bool bInCode,
89  QString &sTransBuffer ) const;
90 };
91 
92 
93 class OutputStream : public QObject, public QScriptable
94 {
95  Q_OBJECT
96 
97  public:
98 
99  explicit OutputStream( QTextStream *pStream, QObject *parent = nullptr )
100  : QObject( parent ), m_pTextStream( pStream ) {}
101 
102  ~OutputStream() override = default;
103 
104  public slots:
105 
106  void write( const QString &sValue )
107  {
108  *m_pTextStream << sValue;
109  }
110 
111  void writeln( const QString &sValue )
112  {
113  *m_pTextStream << sValue << "\n";
114  }
115 
116  private:
117  QTextStream *m_pTextStream {nullptr};
118 };
119 
120 #endif
void writeln(const QString &sValue)
unsigned int slots[4]
Definition: element.c:38
~OutputStream() override=default
QDateTime m_dtTimeStamp
OutputStream(QTextStream *pStream, QObject *parent=nullptr)
QTextStream * m_pTextStream
#define UPNP_PUBLIC
Definition: upnpexp.h:9
ScriptInfo(const QScriptValue &func, QDateTime dt)
QScriptValue m_oFunc
void write(const QString &sValue)
QMap< QString, ScriptInfo * > m_mapScripts
ScriptInfo()=default
QScriptEngineDebugger m_debugger