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 <utility>
17 
18 // Qt headers
19 #include <QMap>
20 #include <QStringList>
21 
22 // MythTV headers
23 #include "upnpexp.h"
24 #include "compat.h" // for suseconds_t
25 
27 //
29 
30 template <class T> inline const T& Min( const T &x, const T &y )
31 {
32  return( ( x < y ) ? x : y );
33 }
34 
35 template <class T> inline const T& Max( const T &x, const T &y )
36 {
37  return( ( x > y ) ? x : y );
38 }
40 // Typedefs
42 
43 using TaskTime = struct timeval;
44 using QStringMap = QMap< QString, QString >;
45 
46 
48 
49 class NameValue;
50 class NameValues;
51 class NameValue
52 {
53  public:
54  QString m_sName;
55  QString m_sValue;
56  bool m_bRequired {false};
57 
59 
60  public:
61  NameValue() = default;
62  NameValue(QString name, QString value, bool required = false) :
63  m_sName(std::move(name)), m_sValue(std::move(value)), m_bRequired(required) { }
64  NameValue(QString name, const char *value, bool required = false) :
65  m_sName(std::move(name)), m_sValue(value), m_bRequired(required) { }
66  NameValue(QString name, int value, bool required = false) :
67  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
68  NameValue(QString name, long value, bool required = false) :
69  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
70  NameValue(QString name, qlonglong value, bool required = false) :
71  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
72  NameValue(QString name, uint value, bool required = false) :
73  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
74  NameValue(QString name, ulong value, bool required = false) :
75  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
76  NameValue(QString name, qulonglong value, bool required = false) :
77  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
78  NameValue(QString name, bool value, bool required = false) :
79  m_sName(std::move(name)), m_sValue((value) ? "1" : "0"), m_bRequired(required) { }
80  inline NameValue(const NameValue &nv);
81  inline NameValue& operator=(const NameValue &nv);
82 
83  inline ~NameValue();
84 
85  inline void AddAttribute(const QString &name, const QString &value, bool required);
86  inline QString toXML() const;
87 };
88 class NameValues : public QList<NameValue> {};
89 
90 inline NameValue::NameValue(const NameValue &nv) :
91  m_sName(nv.m_sName), m_sValue(nv.m_sValue), m_bRequired(nv.m_bRequired)
92 {
93  if (nv.m_pAttributes)
94  {
97  }
98 }
99 
101 {
102  if (this == &nv)
103  return *this;
104 
105  m_sName = nv.m_sName;
106  m_sValue = nv.m_sValue;
108 
109  if (nv.m_pAttributes)
110  {
113  }
114  else
115  {
116  m_pAttributes = nullptr;
117  }
118 
119  return *this;
120 }
121 
123 {
124  delete m_pAttributes;
125  m_pAttributes = nullptr;
126 }
127 
128 inline void NameValue::AddAttribute(const QString &name, const QString &value,
129  bool required)
130 {
131  if (!m_pAttributes)
132  m_pAttributes = new NameValues();
133 
134  m_pAttributes->push_back(NameValue(name, value, required));
135 }
136 
137 
138 inline QString NameValue::toXML() const
139 {
140  QString sAttributes;
141  QString attributeTemplate = " %1=\"%2\"";
142  QString xml = "<%1%2>%3</%1>";
143 
144  NameValues::const_iterator it;
145  for (it = m_pAttributes->constBegin(); it != m_pAttributes->constEnd(); ++it)
146  {
147  sAttributes += attributeTemplate.arg((*it).m_sName).arg((*it).m_sValue);
148  }
149 
150  return xml.arg(m_sName).arg(sAttributes).arg(m_sValue);
151 }
152 
154 // Global Function Prototypes
156 
157 QString LookupUDN ( const QString &sDeviceType );
158 
159 bool operator< ( TaskTime t1, TaskTime t2 );
160 bool operator== ( TaskTime t1, TaskTime t2 );
161 
163 void AddSecondsToTaskTime ( TaskTime &t, long nSecs );
164 
165 UPNP_PUBLIC QStringList GetSourceProtocolInfos ();
166 UPNP_PUBLIC QStringList GetSinkProtocolInfos ();
167 
168 #endif // UPNPUTIL_H
NameValue
Definition: upnputil.h:52
NameValue::NameValue
NameValue(QString name, const char *value, bool required=false)
Definition: upnputil.h:64
NameValue::NameValue
NameValue(QString name, bool value, bool required=false)
Definition: upnputil.h:78
NameValue::m_sName
QString m_sName
Definition: upnputil.h:54
NameValue::NameValue
NameValue(QString name, uint value, bool required=false)
Definition: upnputil.h:72
operator<
bool operator<(TaskTime t1, TaskTime t2)
Definition: upnputil.cpp:88
TaskTime
struct timeval TaskTime
Definition: httpserver.h:45
LookupUDN
QString LookupUDN(const QString &sDeviceType)
Definition: upnputil.cpp:45
NameValue::toXML
QString toXML() const
Definition: upnputil.h:138
NameValue::NameValue
NameValue(QString name, QString value, bool required=false)
Definition: upnputil.h:62
NameValues
Definition: upnputil.h:88
NameValue::AddAttribute
void AddAttribute(const QString &name, const QString &value, bool required)
Definition: upnputil.h:128
GetSinkProtocolInfos
UPNP_PUBLIC QStringList GetSinkProtocolInfos()
Return a QStringList containing the supported Sink Protocols.
Definition: upnputil.cpp:182
GetSourceProtocolInfos
UPNP_PUBLIC QStringList GetSourceProtocolInfos()
Return a QStringList containing the supported Source Protocols.
Definition: upnputil.cpp:130
hardwareprofile.i18n.t
t
Definition: i18n.py:36
compat.h
QStringMap
QMap< QString, QString > QStringMap
Definition: upnputil.h:44
AddSecondsToTaskTime
void AddSecondsToTaskTime(TaskTime &t, long nSecs)
Definition: upnputil.cpp:119
NameValue::NameValue
NameValue()=default
uint
unsigned int uint
Definition: compat.h:140
NameValue::NameValue
NameValue(QString name, qulonglong value, bool required=false)
Definition: upnputil.h:76
NameValue::NameValue
NameValue(QString name, ulong value, bool required=false)
Definition: upnputil.h:74
Max
const T & Max(const T &x, const T &y)
Definition: upnputil.h:35
AddMicroSecToTaskTime
void AddMicroSecToTaskTime(TaskTime &t, suseconds_t uSecs)
Definition: upnputil.cpp:107
NameValue::m_pAttributes
NameValues * m_pAttributes
Definition: upnputil.h:58
Min
const T & Min(const T &x, const T &y)
Definition: upnputil.h:30
NameValue::operator=
NameValue & operator=(const NameValue &nv)
Definition: upnputil.h:100
NameValue::~NameValue
~NameValue()
Definition: upnputil.h:122
NameValue::NameValue
NameValue(QString name, int value, bool required=false)
Definition: upnputil.h:66
NameValue::m_bRequired
bool m_bRequired
Definition: upnputil.h:56
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
suseconds_t
long suseconds_t
Definition: compat.h:331
operator==
bool operator==(TaskTime t1, TaskTime t2)
Definition: upnputil.cpp:98
NameValue::NameValue
NameValue(QString name, long value, bool required=false)
Definition: upnputil.h:68
NameValue::m_sValue
QString m_sValue
Definition: upnputil.h:55
upnpexp.h
NameValue::NameValue
NameValue(QString name, qlonglong value, bool required=false)
Definition: upnputil.h:70