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 using QStringMultiMap = QMultiMap< QString, QString >;
46 
47 
49 
50 class NameValue;
51 class NameValues;
52 class NameValue
53 {
54  public:
55  QString m_sName;
56  QString m_sValue;
57  bool m_bRequired {false};
58 
60 
61  public:
62  NameValue() = default;
63  NameValue(QString name, QString value, bool required = false) :
64  m_sName(std::move(name)), m_sValue(std::move(value)), m_bRequired(required) { }
65  NameValue(QString name, const char *value, bool required = false) :
66  m_sName(std::move(name)), m_sValue(value), m_bRequired(required) { }
67  NameValue(QString name, int value, bool required = false) :
68  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
69  NameValue(QString name, long value, bool required = false) :
70  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
71  NameValue(QString name, qlonglong value, bool required = false) :
72  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
73  NameValue(QString name, uint value, bool required = false) :
74  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
75  NameValue(QString name, ulong value, bool required = false) :
76  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
77  NameValue(QString name, qulonglong value, bool required = false) :
78  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
79  NameValue(QString name, bool value, bool required = false) :
80  m_sName(std::move(name)), m_sValue((value) ? "1" : "0"), m_bRequired(required) { }
81  inline NameValue(const NameValue &nv);
82  inline NameValue& operator=(const NameValue &nv);
83 
84  inline ~NameValue();
85 
86  inline void AddAttribute(const QString &name, const QString &value, bool required);
87  inline QString toXML() const;
88 };
89 class NameValues : public QList<NameValue> {};
90 
91 inline NameValue::NameValue(const NameValue &nv) :
92  m_sName(nv.m_sName), m_sValue(nv.m_sValue), m_bRequired(nv.m_bRequired)
93 {
94  if (nv.m_pAttributes)
95  {
98  }
99 }
100 
102 {
103  if (this == &nv)
104  return *this;
105 
106  m_sName = nv.m_sName;
107  m_sValue = nv.m_sValue;
109 
110  if (nv.m_pAttributes)
111  {
114  }
115  else
116  {
117  m_pAttributes = nullptr;
118  }
119 
120  return *this;
121 }
122 
124 {
125  delete m_pAttributes;
126  m_pAttributes = nullptr;
127 }
128 
129 inline void NameValue::AddAttribute(const QString &name, const QString &value,
130  bool required)
131 {
132  if (!m_pAttributes)
133  m_pAttributes = new NameValues();
134 
135  m_pAttributes->push_back(NameValue(name, value, required));
136 }
137 
138 
139 inline QString NameValue::toXML() const
140 {
141  QString sAttributes;
142  QString attributeTemplate = " %1=\"%2\"";
143  QString xml = "<%1%2>%3</%1>";
144 
145  NameValues::const_iterator it;
146  for (it = m_pAttributes->constBegin(); it != m_pAttributes->constEnd(); ++it)
147  {
148  sAttributes += attributeTemplate.arg((*it).m_sName).arg((*it).m_sValue);
149  }
150 
151  return xml.arg(m_sName).arg(sAttributes).arg(m_sValue);
152 }
153 
155 // Global Function Prototypes
157 
158 QString LookupUDN ( const QString &sDeviceType );
159 
160 bool operator< ( TaskTime t1, TaskTime t2 );
161 bool operator== ( TaskTime t1, TaskTime t2 );
162 
164 void AddSecondsToTaskTime ( TaskTime &t, long nSecs );
165 
166 UPNP_PUBLIC QStringList GetSourceProtocolInfos ();
167 UPNP_PUBLIC QStringList GetSinkProtocolInfos ();
168 
169 #endif // UPNPUTIL_H
NameValue
Definition: upnputil.h:53
NameValue::NameValue
NameValue(QString name, const char *value, bool required=false)
Definition: upnputil.h:65
NameValue::NameValue
NameValue(QString name, bool value, bool required=false)
Definition: upnputil.h:79
NameValue::m_sName
QString m_sName
Definition: upnputil.h:55
NameValue::NameValue
NameValue(QString name, uint value, bool required=false)
Definition: upnputil.h:73
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:139
NameValue::NameValue
NameValue(QString name, QString value, bool required=false)
Definition: upnputil.h:63
NameValues
Definition: upnputil.h:89
NameValue::AddAttribute
void AddAttribute(const QString &name, const QString &value, bool required)
Definition: upnputil.h:129
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
QStringMultiMap
QMultiMap< QString, QString > QStringMultiMap
Definition: upnputil.h:45
NameValue::NameValue
NameValue(QString name, qulonglong value, bool required=false)
Definition: upnputil.h:77
NameValue::NameValue
NameValue(QString name, ulong value, bool required=false)
Definition: upnputil.h:75
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:59
Min
const T & Min(const T &x, const T &y)
Definition: upnputil.h:30
NameValue::operator=
NameValue & operator=(const NameValue &nv)
Definition: upnputil.h:101
NameValue::~NameValue
~NameValue()
Definition: upnputil.h:123
NameValue::NameValue
NameValue(QString name, int value, bool required=false)
Definition: upnputil.h:67
NameValue::m_bRequired
bool m_bRequired
Definition: upnputil.h:57
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
suseconds_t
long suseconds_t
Definition: compat.h:332
operator==
bool operator==(TaskTime t1, TaskTime t2)
Definition: upnputil.cpp:98
NameValue::NameValue
NameValue(QString name, long value, bool required=false)
Definition: upnputil.h:69
NameValue::m_sValue
QString m_sValue
Definition: upnputil.h:56
upnpexp.h
NameValue::NameValue
NameValue(QString name, qlonglong value, bool required=false)
Definition: upnputil.h:71