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 LICENSE 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 "libmythbase/mythchrono.h"
24 
25 #include "upnpexp.h"
26 
28 // Typedefs
30 
31 using TaskTime = std::chrono::microseconds;
32 using QStringMap = QMap< QString, QString >;
33 using QStringMultiMap = QMultiMap< QString, QString >;
34 
35 
37 
38 class NameValue;
39 class NameValues;
40 class NameValue
41 {
42  public:
43  QString m_sName;
44  QString m_sValue;
45  bool m_bRequired {false};
46 
48 
49  public:
50  NameValue() = default;
51  NameValue(QString name, QString value, bool required = false) :
52  m_sName(std::move(name)), m_sValue(std::move(value)), m_bRequired(required) { }
53  NameValue(QString name, const char *value, bool required = false) :
54  m_sName(std::move(name)), m_sValue(value), m_bRequired(required) { }
55  NameValue(QString name, int value, bool required = false) :
56  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
57  NameValue(QString name, long value, bool required = false) :
58  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
59  NameValue(QString name, qlonglong value, bool required = false) :
60  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
61  NameValue(QString name, uint value, bool required = false) :
62  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
63  NameValue(QString name, ulong value, bool required = false) :
64  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
65  NameValue(QString name, qulonglong value, bool required = false) :
66  m_sName(std::move(name)), m_sValue(QString::number(value)), m_bRequired(required) { }
67  NameValue(QString name, bool value, bool required = false) :
68  m_sName(std::move(name)), m_sValue((value) ? "1" : "0"), m_bRequired(required) { }
69  inline NameValue(const NameValue &nv);
70  inline NameValue& operator=(const NameValue &nv);
71 
72  inline ~NameValue();
73 
74  inline void AddAttribute(const QString &name, const QString &value, bool required);
75  inline QString toXML() const;
76 };
77 class NameValues : public QList<NameValue> {};
78 
79 inline NameValue::NameValue(const NameValue &nv) :
80  m_sName(nv.m_sName), m_sValue(nv.m_sValue), m_bRequired(nv.m_bRequired)
81 {
82  if (nv.m_pAttributes)
83  {
86  }
87 }
88 
90 {
91  if (this == &nv)
92  return *this;
93 
94  m_sName = nv.m_sName;
95  m_sValue = nv.m_sValue;
97 
98  if (nv.m_pAttributes)
99  {
102  }
103  else
104  {
105  m_pAttributes = nullptr;
106  }
107 
108  return *this;
109 }
110 
112 {
113  delete m_pAttributes;
114  m_pAttributes = nullptr;
115 }
116 
117 inline void NameValue::AddAttribute(const QString &name, const QString &value,
118  bool required)
119 {
120  if (!m_pAttributes)
121  m_pAttributes = new NameValues();
122 
123  m_pAttributes->push_back(NameValue(name, value, required));
124 }
125 
126 
127 inline QString NameValue::toXML() const
128 {
129  QString sAttributes;
130  QString attributeTemplate = " %1=\"%2\"";
131  QString xml = "<%1%2>%3</%1>";
132 
133  NameValues::const_iterator it;
134  for (it = m_pAttributes->constBegin(); it != m_pAttributes->constEnd(); ++it)
135  {
136  sAttributes += attributeTemplate.arg((*it).m_sName, (*it).m_sValue);
137  }
138 
139  return xml.arg(m_sName, sAttributes, m_sValue);
140 }
141 
143 // Global Function Prototypes
145 
146 QString LookupUDN ( const QString &sDeviceType );
147 
148 UPNP_PUBLIC QStringList GetSourceProtocolInfos ();
149 UPNP_PUBLIC QStringList GetSinkProtocolInfos ();
150 
151 #endif // UPNPUTIL_H
NameValue
Definition: upnputil.h:40
NameValue::NameValue
NameValue(QString name, const char *value, bool required=false)
Definition: upnputil.h:53
NameValue::NameValue
NameValue(QString name, bool value, bool required=false)
Definition: upnputil.h:67
TaskTime
std::chrono::microseconds TaskTime
Definition: upnputil.h:31
NameValue::m_sName
QString m_sName
Definition: upnputil.h:43
NameValue::NameValue
NameValue(QString name, uint value, bool required=false)
Definition: upnputil.h:61
LookupUDN
QString LookupUDN(const QString &sDeviceType)
Definition: upnputil.cpp:46
NameValue::toXML
QString toXML() const
Definition: upnputil.h:127
NameValue::NameValue
NameValue(QString name, QString value, bool required=false)
Definition: upnputil.h:51
NameValues
Definition: upnputil.h:77
NameValue::AddAttribute
void AddAttribute(const QString &name, const QString &value, bool required)
Definition: upnputil.h:117
GetSinkProtocolInfos
UPNP_PUBLIC QStringList GetSinkProtocolInfos()
Return a QStringList containing the supported Sink Protocols.
Definition: upnputil.cpp:142
GetSourceProtocolInfos
UPNP_PUBLIC QStringList GetSourceProtocolInfos()
Return a QStringList containing the supported Source Protocols.
Definition: upnputil.cpp:90
QStringMap
QMap< QString, QString > QStringMap
Definition: upnputil.h:32
NameValue::NameValue
NameValue()=default
uint
unsigned int uint
Definition: compat.h:79
QStringMultiMap
QMultiMap< QString, QString > QStringMultiMap
Definition: upnputil.h:33
NameValue::NameValue
NameValue(QString name, qulonglong value, bool required=false)
Definition: upnputil.h:65
NameValue::NameValue
NameValue(QString name, ulong value, bool required=false)
Definition: upnputil.h:63
NameValue::m_pAttributes
NameValues * m_pAttributes
Definition: upnputil.h:47
NameValue::operator=
NameValue & operator=(const NameValue &nv)
Definition: upnputil.h:89
std
Definition: mythchrono.h:23
NameValue::~NameValue
~NameValue()
Definition: upnputil.h:111
NameValue::NameValue
NameValue(QString name, int value, bool required=false)
Definition: upnputil.h:55
NameValue::m_bRequired
bool m_bRequired
Definition: upnputil.h:45
mythchrono.h
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
NameValue::NameValue
NameValue(QString name, long value, bool required=false)
Definition: upnputil.h:57
NameValue::m_sValue
QString m_sValue
Definition: upnputil.h:44
upnpexp.h
NameValue::NameValue
NameValue(QString name, qlonglong value, bool required=false)
Definition: upnputil.h:59