00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012
00013 #ifndef __UPNPDEVICE_H__
00014 #define __UPNPDEVICE_H__
00015
00016 #include <QDomDocument>
00017 #include <QHash>
00018 #include <QUrl>
00019
00020 #include "compat.h"
00021 #include "upnpexp.h"
00022 #include "upnputil.h"
00023 #include "refcounted.h"
00024
00025 class UPnpDeviceDesc;
00026 class UPnpDevice;
00027 class UPnpService;
00028 class UPnpIcon;
00029 class QTextStream;
00030
00032
00034
00035 typedef QList< UPnpDevice* > UPnpDeviceList;
00036 typedef QList< UPnpService* > UPnpServiceList;
00037 typedef QList< UPnpIcon* > UPnpIconList;
00038
00040
00042
00043 class UPNP_PUBLIC UPnpIcon
00044 {
00045 public:
00046 QString m_sURL;
00047 QString m_sMimeType;
00048 int m_nWidth;
00049 int m_nHeight;
00050 int m_nDepth;
00051
00052 UPnpIcon() : m_nWidth(0), m_nHeight(0), m_nDepth(0) {}
00053
00054 QString toString(uint padding) const
00055 {
00056 QString pad;
00057 for (uint i = 0; i < padding; i++)
00058 pad += " ";
00059 return QString("%0Icon %1 %2x%3^%4 %5")
00060 .arg(pad).arg(m_sURL).arg(m_nWidth).arg(m_nHeight)
00061 .arg(m_nDepth).arg(m_sMimeType);
00062 }
00063 };
00064
00066
00067 class UPNP_PUBLIC UPnpService
00068 {
00069 public:
00070 QString m_sServiceType;
00071 QString m_sServiceId;
00072 QString m_sSCPDURL;
00073 QString m_sControlURL;
00074 QString m_sEventSubURL;
00075
00076 UPnpService() {}
00077
00078 QString toString(uint padding) const
00079 {
00080 QString pad;
00081 for (uint i = 0; i < padding; i++)
00082 pad += " ";
00083 return
00084 QString("%0Service %1\n").arg(pad).arg(m_sServiceType) +
00085 QString("%0 id: %1\n").arg(pad).arg(m_sServiceId) +
00086 QString("%0 SCPD URL: %1\n").arg(pad).arg(m_sSCPDURL) +
00087 QString("%0 Control URL: %1\n").arg(pad).arg(m_sControlURL) +
00088 QString("%0 Event Sub URL: %1").arg(pad).arg(m_sEventSubURL);
00089 }
00090 };
00091
00093
00094 class UPNP_PUBLIC UPnpDevice
00095 {
00096 public:
00097
00098 QString m_sDeviceType;
00099 QString m_sFriendlyName;
00100 QString m_sManufacturer;
00101 QString m_sManufacturerURL;
00102 QString m_sModelDescription;
00103 QString m_sModelName;
00104 QString m_sModelNumber;
00105 QString m_sModelURL;
00106 QString m_sSerialNumber;
00107 QString m_sUPC;
00108 QString m_sPresentationURL;
00109 mutable QString m_sUDN;
00110
00111 NameValues m_lstExtra;
00112
00114 bool m_securityPin;
00115 QString m_protocolVersion;
00116
00117 UPnpIconList m_listIcons;
00118 UPnpServiceList m_listServices;
00119 UPnpDeviceList m_listDevices;
00120
00121 public:
00122 UPnpDevice();
00123 ~UPnpDevice();
00124
00125 QString GetUDN(void) const;
00126
00127 void toMap(QHash<QString, QString> &map);
00128
00129 UPnpService GetService(const QString &urn, bool *found = NULL) const;
00130
00131 QString toString(uint padding = 0) const;
00132 };
00133
00134
00137
00138
00139
00142
00143 class UPNP_PUBLIC UPnpDeviceDesc
00144 {
00145 public:
00146
00147 UPnpDevice m_rootDevice;
00148 QString m_sHostName;
00149 QUrl m_HostUrl;
00150
00151 protected:
00152
00153 void _InternalLoad( QDomNode oNode, UPnpDevice *pCurDevice );
00154
00155 void ProcessIconList ( QDomNode oListNode, UPnpDevice *pDevice );
00156 void ProcessServiceList( QDomNode oListNode, UPnpDevice *pDevice );
00157 void ProcessDeviceList ( QDomNode oListNode, UPnpDevice *pDevice );
00158
00159 void OutputDevice( QTextStream &os,
00160 UPnpDevice *pDevice,
00161 const QString &sUserAgent = "" );
00162
00163 void SetStrValue ( const QDomNode &n, QString &sValue );
00164 void SetNumValue ( const QDomNode &n, int &nValue );
00165 void SetBoolValue( const QDomNode &n, bool &nValue );
00166
00167 QString FormatValue ( const QString &sName, const QString &sValue );
00168 QString FormatValue ( const QString &sName, int nValue );
00169
00170 QString GetHostName ();
00171
00172 public:
00173
00174 UPnpDeviceDesc();
00175 virtual ~UPnpDeviceDesc();
00176
00177 bool Load ( const QString &sFileName );
00178 bool Load ( const QDomDocument &xmlDevDesc );
00179
00180 void GetValidXML( const QString &sBaseAddress, int nPort, QTextStream &os, const QString &sUserAgent = "" );
00181 QString GetValidXML( const QString &sBaseAddress, int nPort );
00182
00183 QString FindDeviceUDN( UPnpDevice *pDevice, QString sST );
00184
00185 UPnpDevice *FindDevice( const QString &sURI );
00186
00187 static UPnpDevice *FindDevice( UPnpDevice *pDevice, const QString &sURI );
00188 static UPnpDeviceDesc *Retrieve ( QString &sURL, bool bInQtThread = true );
00189
00190 void toMap(QHash<QString, QString> &map)
00191 {
00192 map["hostname"] = m_sHostName;
00193 m_rootDevice.toMap(map);
00194 }
00195 };
00196
00198
00200
00201 class UPNP_PUBLIC DeviceLocation : public RefCounted
00202 {
00203 public:
00204
00205 static int g_nAllocated;
00206
00207 protected:
00208
00209
00210
00211
00212
00213 virtual ~DeviceLocation()
00214 {
00215
00216 g_nAllocated--;
00217
00218 if (m_pDeviceDesc != NULL)
00219 delete m_pDeviceDesc;
00220 }
00221
00222 UPnpDeviceDesc *m_pDeviceDesc;
00223
00224 public:
00225
00226 QString m_sURI;
00227 QString m_sUSN;
00228 QString m_sLocation;
00229 TaskTime m_ttExpires;
00230 QString m_sSecurityPin;
00231
00232 public:
00233
00234
00235
00236 DeviceLocation( const QString &sURI,
00237 const QString &sUSN,
00238 const QString &sLocation,
00239 TaskTime ttExpires ) : m_pDeviceDesc( NULL ),
00240 m_sURI ( sURI ),
00241 m_sUSN ( sUSN ),
00242 m_sLocation ( sLocation ),
00243 m_ttExpires ( ttExpires )
00244 {
00245
00246 g_nAllocated++;
00247 }
00248
00249
00250
00251 int ExpiresInSecs(void) const
00252 {
00253 TaskTime ttNow;
00254 gettimeofday( (&ttNow), NULL );
00255
00256 return m_ttExpires.tv_sec - ttNow.tv_sec;
00257 }
00258
00259
00260
00261 UPnpDeviceDesc *GetDeviceDesc( bool bInQtThread = true )
00262 {
00263 if (m_pDeviceDesc == NULL)
00264 m_pDeviceDesc = UPnpDeviceDesc::Retrieve( m_sLocation, bInQtThread );
00265
00266 return m_pDeviceDesc;
00267 }
00268
00269
00270
00271 QString GetFriendlyName( bool bInQtThread = true )
00272 {
00273 UPnpDeviceDesc *pDevice = GetDeviceDesc( bInQtThread );
00274
00275 if ( pDevice == NULL)
00276 return "<Unknown>";
00277
00278 QString sName = pDevice->m_rootDevice.m_sFriendlyName;
00279
00280 if (sName == "mythtv: MythTV AV Media Server")
00281 return sName + " (" + pDevice->m_sHostName + ")";
00282
00283 return sName;
00284 }
00285
00286 QString GetNameAndDetails( bool bInQtThread = true )
00287 {
00288 UPnpDeviceDesc *pDevice = GetDeviceDesc( bInQtThread );
00289
00290 if ( pDevice == NULL)
00291 return "<Unknown> (" + m_sLocation + ")";
00292
00293 return pDevice->m_rootDevice.m_sFriendlyName
00294 + " (" + pDevice->m_sHostName + "), "
00295 + pDevice->m_rootDevice.m_sUDN;
00296 }
00297
00298 void GetDeviceDetail(QHash<QString, QString> &map,
00299 bool bInQtThread = true)
00300 {
00301 map["location"] = m_sLocation;
00302
00303 UPnpDeviceDesc *pDevice = GetDeviceDesc(bInQtThread);
00304 if (!pDevice)
00305 return;
00306
00307 pDevice->toMap(map);
00308 }
00309
00310 bool NeedSecurityPin( bool bInQtThread = true )
00311 {
00312 UPnpDeviceDesc *pDevice = GetDeviceDesc(bInQtThread);
00313 if (!pDevice)
00314 return false;
00315
00316 return pDevice->m_rootDevice.m_securityPin;
00317 }
00318
00319 QString toString() const
00320 {
00321 return QString("\nURI:%1\nUSN:%2\nDeviceXML:%3\n"
00322 "Expires:%4\nMythTV PIN:%5")
00323 .arg(m_sURI).arg(m_sUSN).arg(m_sLocation)
00324 .arg(ExpiresInSecs()).arg(m_sSecurityPin);
00325 }
00326 };
00327
00328 #endif