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 "upnpexp.h"
17 #include "upnputil.h"
18 #include "httprequest.h"
19 
20 #include <QString>
21 #include <QMap>
22 #include <QDateTime>
23 #include <QMutex>
24 #include <QTextStream>
25 #include <QScriptEngine>
26 #include <QScriptable>
27 
28 #ifdef _WIN32
29 #include <QScriptEngineDebugger>
30 #endif
31 
32 class ScriptInfo
33 {
34  public:
35  QScriptValue m_oFunc;
36  QDateTime m_dtTimeStamp;
37 
38  ScriptInfo() = default;
39 
40  ScriptInfo( QScriptValue func, QDateTime dt )
41  : m_oFunc( func ), m_dtTimeStamp( dt )
42  {}
43 };
44 
46 {
47  public:
48 
49  QScriptEngine m_engine;
50 
51 #ifdef _WIN32
52  QScriptEngineDebugger m_debugger;
53 #endif
54 
57 
58  QString SetResourceRootPath( const QString &path );
59 
60  void RegisterMetaObjectType( const QString &sName,
61  const QMetaObject *pMetaObject,
62  QScriptEngine::FunctionSignature pFunction);
63 
64  bool EvaluatePage( QTextStream *pOutStream, const QString &sFileName,
65  HTTPRequest *pRequest, const QByteArray &cspToken);
66 
67  protected:
68 
69  QMutex m_mutex;
70  QMap< QString, ScriptInfo* > m_mapScripts;
71  QString m_sResRootPath;
72 
73  void Lock () { m_mutex.lock(); }
74  void Unlock () { m_mutex.unlock(); }
75 
76  ScriptInfo *GetLoadedScript ( const QString &sFileName );
77 
78  QString ReadFileContents ( const QString &sFileName ) const;
79 
80  QString CreateMethodFromFile( const QString &sFileName ) const;
81 
82  bool ProcessLine ( QTextStream &sCode,
83  QString &sLine,
84  bool bInCode,
85  QString &sTransBuffer ) const;
86 };
87 
88 
89 class OutputStream : public QObject, public QScriptable
90 {
91  Q_OBJECT
92 
93  public:
94 
95  OutputStream( QTextStream *pStream, QObject *parent = nullptr )
96  : QObject( parent ), m_pTextStream( pStream ) {}
97 
98  ~OutputStream() = default;
99 
100  public slots:
101 
102  void write( const QString &sValue )
103  {
104  *m_pTextStream << sValue;
105  }
106 
107  void writeln( const QString &sValue )
108  {
109  *m_pTextStream << sValue << "\n";
110  }
111 
112  private:
113  QTextStream *m_pTextStream {nullptr};
114 };
115 
116 #endif
void writeln(const QString &sValue)
unsigned int slots[4]
Definition: element.c:38
~OutputStream()=default
QDateTime m_dtTimeStamp
OutputStream(QTextStream *pStream, QObject *parent=nullptr)
QTextStream * m_pTextStream
#define UPNP_PUBLIC
Definition: upnpexp.h:9
ScriptInfo(QScriptValue func, QDateTime dt)
QScriptValue m_oFunc
void write(const QString &sValue)
QMap< QString, ScriptInfo * > m_mapScripts
ScriptInfo()=default
QScriptEngineDebugger m_debugger