MythTV  master
upnputil.h
Go to the documentation of this file.
1 // Program Name: upnputil.h
3 // Created : Jan. 15, 2007
4 //
5 // Purpose : Global Helper Methods...
6 //
7 // Copyright (c) 2007 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see COPYING for details
10 //
12 
13 #ifndef __UPNPUTIL_H__
14 #define __UPNPUTIL_H__
15 
16 #include <QStringList>
17 #include <QMap>
18 
19 #include "upnpexp.h"
20 #include "compat.h" // for suseconds_t
21 
23 //
25 
26 template <class T> inline const T& Min( const T &x, const T &y )
27 {
28  return( ( x < y ) ? x : y );
29 }
30 
31 template <class T> inline const T& Max( const T &x, const T &y )
32 {
33  return( ( x > y ) ? x : y );
34 }
36 // Typedefs
38 
39 using TaskTime = struct timeval;
40 using QStringMap = QMap< QString, QString >;
41 
42 
44 
45 class NameValue;
46 class NameValues;
47 class NameValue
48 {
49  public:
50  QString m_sName;
51  QString m_sValue;
52  bool m_bRequired {false};
53 
55 
56  public:
57  NameValue() = default;
58  NameValue(const QString &name, const QString &value, bool required = false) :
59  m_sName(name), m_sValue(value), m_bRequired(required) { }
60  NameValue(const QString &name, const char *value, bool required = false) :
61  m_sName(name), m_sValue(value), m_bRequired(required) { }
62  NameValue(const QString &name, int value, bool required = false) :
63  m_sName(name), m_sValue(QString::number(value)), m_bRequired(required) { }
64  NameValue(const QString &name, long value, bool required = false) :
65  m_sName(name), m_sValue(QString::number(value)), m_bRequired(required) { }
66  NameValue(const QString &name, qlonglong value, bool required = false) :
67  m_sName(name), m_sValue(QString::number(value)), m_bRequired(required) { }
68  NameValue(const QString &name, uint value, bool required = false) :
69  m_sName(name), m_sValue(QString::number(value)), m_bRequired(required) { }
70  NameValue(const QString &name, ulong value, bool required = false) :
71  m_sName(name), m_sValue(QString::number(value)), m_bRequired(required) { }
72  NameValue(const QString &name, qulonglong value, bool required = false) :
73  m_sName(name), m_sValue(QString::number(value)), m_bRequired(required) { }
74  NameValue(const QString &name, bool value, bool required = false) :
75  m_sName(name), m_sValue((value) ? "1" : "0"), m_bRequired(required) { }
76  inline NameValue(const NameValue &nv);
77  inline NameValue& operator=(const NameValue &nv);
78 
79  inline ~NameValue();
80 
81  inline void AddAttribute(const QString &name, const QString &value, bool required);
82  inline QString toXML();
83 };
84 class NameValues : public QList<NameValue> {};
85 
86 inline NameValue::NameValue(const NameValue &nv) :
87  m_sName(nv.m_sName), m_sValue(nv.m_sValue), m_bRequired(nv.m_bRequired)
88 {
89  if (nv.m_pAttributes)
90  {
93  }
94 }
95 
97 {
98  if (this == &nv)
99  return *this;
100 
101  m_sName = nv.m_sName;
102  m_sValue = nv.m_sValue;
104 
105  if (nv.m_pAttributes)
106  {
109  }
110  else
111  {
112  m_pAttributes = nullptr;
113  }
114 
115  return *this;
116 }
117 
119 {
120  delete m_pAttributes;
121  m_pAttributes = nullptr;
122 }
123 
124 inline void NameValue::AddAttribute(const QString &name, const QString &value,
125  bool required)
126 {
127  if (!m_pAttributes)
128  m_pAttributes = new NameValues();
129 
130  m_pAttributes->push_back(NameValue(name, value, required));
131 }
132 
133 
134 inline QString NameValue::toXML()
135 {
136  QString sAttributes;
137  QString attributeTemplate = " %1=\"%2\"";
138  QString xml = "<%1%2>%3</%1>";
139 
140  NameValues::const_iterator it;
141  for (it = m_pAttributes->constBegin(); it != m_pAttributes->constEnd(); ++it)
142  {
143  sAttributes += attributeTemplate.arg((*it).m_sName).arg((*it).m_sValue);
144  }
145 
146  return xml.arg(m_sName).arg(sAttributes).arg(m_sValue);
147 }
148 
150 // Global Function Prototypes
152 
153 QString LookupUDN ( const QString &sDeviceType );
154 
155 bool operator< ( TaskTime t1, TaskTime t2 );
156 bool operator== ( TaskTime t1, TaskTime t2 );
157 
159 void AddSecondsToTaskTime ( TaskTime &t, long nSecs );
160 
161 UPNP_PUBLIC QStringList GetSourceProtocolInfos ();
162 UPNP_PUBLIC QStringList GetSinkProtocolInfos ();
163 
164 #endif
NameValue(const QString &name, const QString &value, bool required=false)
Definition: upnputil.h:58
QMap< QString, QString > QStringMap
Definition: upnputil.h:40
void AddSecondsToTaskTime(TaskTime &t, long nSecs)
Definition: upnputil.cpp:115
NameValues * m_pAttributes
Definition: upnputil.h:54
NameValue(const QString &name, const char *value, bool required=false)
Definition: upnputil.h:60
struct timeval TaskTime
Definition: httpserver.h:45
NameValue(const QString &name, int value, bool required=false)
Definition: upnputil.h:62
NameValue(const QString &name, long value, bool required=false)
Definition: upnputil.h:64
NameValue()=default
QString LookupUDN(const QString &sDeviceType)
Definition: upnputil.cpp:45
void AddMicroSecToTaskTime(TaskTime &t, suseconds_t uSecs)
Definition: upnputil.cpp:103
const T & Max(const T &x, const T &y)
Definition: upnputil.h:31
bool operator==(TaskTime t1, TaskTime t2)
Definition: upnputil.cpp:94
const T & Min(const T &x, const T &y)
Definition: upnputil.h:26
NameValue & operator=(const NameValue &nv)
Definition: upnputil.h:96
QString m_sName
Definition: upnputil.h:50
unsigned int uint
Definition: compat.h:140
UPNP_PUBLIC QStringList GetSinkProtocolInfos()
Return a QStringList containing the supported Sink Protocols.
Definition: upnputil.cpp:178
UPNP_PUBLIC QStringList GetSourceProtocolInfos()
Return a QStringList containing the supported Source Protocols.
Definition: upnputil.cpp:126
NameValue(const QString &name, uint value, bool required=false)
Definition: upnputil.h:68
#define UPNP_PUBLIC
Definition: upnpexp.h:9
QString toXML()
Definition: upnputil.h:134
long suseconds_t
Definition: compat.h:331
QString m_sValue
Definition: upnputil.h:51
NameValue(const QString &name, qlonglong value, bool required=false)
Definition: upnputil.h:66
NameValue(const QString &name, qulonglong value, bool required=false)
Definition: upnputil.h:72
bool m_bRequired
Definition: upnputil.h:52
bool operator<(TaskTime t1, TaskTime t2)
Definition: upnputil.cpp:84
~NameValue()
Definition: upnputil.h:118
void AddAttribute(const QString &name, const QString &value, bool required)
Definition: upnputil.h:124
NameValue(const QString &name, bool value, bool required=false)
Definition: upnputil.h:74
NameValue(const QString &name, ulong value, bool required=false)
Definition: upnputil.h:70