MythTV  0.27pre
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
upnpdevice.h
Go to the documentation of this file.
1 
2 // Program Name: upnpdevice.h
3 // Created : Oct. 24, 2005
4 //
5 // Purpose : UPnp Device Description parser/generator
6 //
7 // Copyright (c) 2005 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see COPYING for details
10 //
12 
13 #ifndef __UPNPDEVICE_H__
14 #define __UPNPDEVICE_H__
15 
16 #include <QDomDocument>
17 #include <QHash>
18 #include <QUrl>
19 
20 #include "compat.h"
21 #include "upnpexp.h"
22 #include "upnputil.h"
23 #include "referencecounter.h"
24 
25 class UPnpDeviceDesc;
26 class UPnpDevice;
27 class UPnpService;
28 class UPnpIcon;
29 class QTextStream;
30 
32 // Typedefs
34 
35 typedef QList< UPnpDevice* > UPnpDeviceList;
36 typedef QList< UPnpService* > UPnpServiceList;
37 typedef QList< UPnpIcon* > UPnpIconList;
38 
40 //
42 
43 class UPNP_PUBLIC UPnpIcon
44 {
45  public:
46  QString m_sURL;
47  QString m_sMimeType;
48  int m_nWidth;
49  int m_nHeight;
50  int m_nDepth;
51 
52  UPnpIcon() : m_nWidth(0), m_nHeight(0), m_nDepth(0) {}
53 
54  QString toString(uint padding) const
55  {
56  QString pad;
57  for (uint i = 0; i < padding; i++)
58  pad += " ";
59  return QString("%0Icon %1 %2x%3^%4 %5")
60  .arg(pad).arg(m_sURL).arg(m_nWidth).arg(m_nHeight)
61  .arg(m_nDepth).arg(m_sMimeType);
62  }
63 };
64 
66 
67 class UPNP_PUBLIC UPnpService
68 {
69  public:
70  QString m_sServiceType;
71  QString m_sServiceId;
72  QString m_sSCPDURL;
73  QString m_sControlURL;
74  QString m_sEventSubURL;
75 
77 
78  QString toString(uint padding) const
79  {
80  QString pad;
81  for (uint i = 0; i < padding; i++)
82  pad += " ";
83  return
84  QString("%0Service %1\n").arg(pad).arg(m_sServiceType) +
85  QString("%0 id: %1\n").arg(pad).arg(m_sServiceId) +
86  QString("%0 SCPD URL: %1\n").arg(pad).arg(m_sSCPDURL) +
87  QString("%0 Control URL: %1\n").arg(pad).arg(m_sControlURL) +
88  QString("%0 Event Sub URL: %1").arg(pad).arg(m_sEventSubURL);
89  }
90 };
91 
93 
94 class UPNP_PUBLIC UPnpDevice
95 {
96  public:
97 
98  QString m_sDeviceType;
99  QString m_sFriendlyName;
103  QString m_sModelName;
104  QString m_sModelNumber;
105  QString m_sModelURL;
107  QString m_sUPC;
109  mutable QString m_sUDN;
110 
112 
116 
120 
121  public:
122  UPnpDevice();
123  ~UPnpDevice();
124 
125  QString GetUDN(void) const;
126 
127  void toMap(QHash<QString, QString> &map);
128 
129  UPnpService GetService(const QString &urn, bool *found = NULL) const;
130 
131  QString toString(uint padding = 0) const;
132 };
133 
134 
137 //
138 //
139 //
142 
143 class UPNP_PUBLIC UPnpDeviceDesc
144 {
145  public:
146 
148  QString m_sHostName;
149  QUrl m_HostUrl;
150 
151  protected:
152 
153  void _InternalLoad( QDomNode oNode, UPnpDevice *pCurDevice );
154 
155  void ProcessIconList ( QDomNode oListNode, UPnpDevice *pDevice );
156  void ProcessServiceList( QDomNode oListNode, UPnpDevice *pDevice );
157  void ProcessDeviceList ( QDomNode oListNode, UPnpDevice *pDevice );
158 
159  void OutputDevice( QTextStream &os,
160  UPnpDevice *pDevice,
161  const QString &sUserAgent = "" );
162 
163  void SetStrValue ( const QDomNode &n, QString &sValue );
164  void SetNumValue ( const QDomNode &n, int &nValue );
165  void SetBoolValue( const QDomNode &n, bool &nValue );
166 
167  QString FormatValue ( const QString &sName, const QString &sValue );
168  QString FormatValue ( const QString &sName, int nValue );
169 
170  QString GetHostName ();
171 
172  public:
173 
174  UPnpDeviceDesc();
175  virtual ~UPnpDeviceDesc();
176 
177  bool Load ( const QString &sFileName );
178  bool Load ( const QDomDocument &xmlDevDesc );
179 
180  void GetValidXML( const QString &sBaseAddress, int nPort, QTextStream &os, const QString &sUserAgent = "" );
181  QString GetValidXML( const QString &sBaseAddress, int nPort );
182 
183  QString FindDeviceUDN( UPnpDevice *pDevice, QString sST );
184 
185  UPnpDevice *FindDevice( const QString &sURI );
186 
187  static UPnpDevice *FindDevice( UPnpDevice *pDevice, const QString &sURI );
188  static UPnpDeviceDesc *Retrieve ( QString &sURL );
189 
190  void toMap(QHash<QString, QString> &map)
191  {
192  map["hostname"] = m_sHostName;
193  m_rootDevice.toMap(map);
194  }
195 };
196 
198 // DeviceLocation Class Definition/Implementation
200 
201 class UPNP_PUBLIC DeviceLocation : public ReferenceCounter
202 {
203  public:
204 
205  static int g_nAllocated; // Debugging only
206 
207  protected:
208 
209  // ==================================================================
210  // Destructor protected to force use of Release Method
211  // ==================================================================
212 
213  virtual ~DeviceLocation()
214  {
215  // Should be atomic decrement
216  g_nAllocated--;
217 
218  if (m_pDeviceDesc != NULL)
219  delete m_pDeviceDesc;
220  }
221 
222  UPnpDeviceDesc *m_pDeviceDesc; // We take ownership of this pointer.
223 
224  public:
225 
226  QString m_sURI; // Service Type URI
227  QString m_sUSN; // Unique Service Name
228  QString m_sLocation; // URL to Device Description
230  QString m_sSecurityPin; // Use for MythXML methods needed pin
231 
232  public:
233 
234  // ==================================================================
235 
236  DeviceLocation( const QString &sURI,
237  const QString &sUSN,
238  const QString &sLocation,
239  TaskTime ttExpires ) : ReferenceCounter(
240  "DeviceLocation" ),
241  m_pDeviceDesc( NULL ),
242  m_sURI ( sURI ),
243  m_sUSN ( sUSN ),
244  m_sLocation ( sLocation ),
245  m_ttExpires ( ttExpires )
246  {
247  // Should be atomic increment
248  g_nAllocated++;
249  }
250 
251  // ==================================================================
252 
253  int ExpiresInSecs(void) const
254  {
255  TaskTime ttNow;
256  gettimeofday( (&ttNow), NULL );
257 
258  return m_ttExpires.tv_sec - ttNow.tv_sec;
259  }
260 
261  // ==================================================================
262 
263  UPnpDeviceDesc *GetDeviceDesc(void)
264  {
265  if (m_pDeviceDesc == NULL)
266  m_pDeviceDesc = UPnpDeviceDesc::Retrieve( m_sLocation );
267 
268  return m_pDeviceDesc;
269  }
270 
271  // ==================================================================
272 
273  QString GetFriendlyName(void)
274  {
275  UPnpDeviceDesc *pDevice = GetDeviceDesc();
276 
277  if ( pDevice == NULL)
278  return "<Unknown>";
279 
280  QString sName = pDevice->m_rootDevice.m_sFriendlyName;
281 
282  if (sName == "mythtv: MythTV AV Media Server")
283  return sName + " (" + pDevice->m_sHostName + ")";
284 
285  return sName;
286  }
287 
288  QString GetNameAndDetails(void)
289  {
290  UPnpDeviceDesc *pDevice = GetDeviceDesc();
291 
292  if ( pDevice == NULL)
293  return "<Unknown> (" + m_sLocation + ")";
294 
295  return pDevice->m_rootDevice.m_sFriendlyName
296  + " (" + pDevice->m_sHostName + "), "
297  + pDevice->m_rootDevice.m_sUDN;
298  }
299 
300  void GetDeviceDetail(QHash<QString, QString> &map)
301  {
302  map["location"] = m_sLocation;
303 
304  UPnpDeviceDesc *pDevice = GetDeviceDesc();
305  if (!pDevice)
306  return;
307 
308  pDevice->toMap(map);
309  }
310 
311  bool NeedSecurityPin(void)
312  {
313  UPnpDeviceDesc *pDevice = GetDeviceDesc();
314  if (!pDevice)
315  return false;
316 
317  return pDevice->m_rootDevice.m_securityPin;
318  }
319 
320  QString toString() const
321  {
322  return QString("\nURI:%1\nUSN:%2\nDeviceXML:%3\n"
323  "Expires:%4\nMythTV PIN:%5")
324  .arg(m_sURI).arg(m_sUSN).arg(m_sLocation)
325  .arg(ExpiresInSecs()).arg(m_sSecurityPin);
326  }
327 };
328 
329 #endif