/src/libtomahawk/sip/SipInfo.cpp
C++ | 282 lines | 200 code | 58 blank | 24 comment | 39 complexity | 5958e38185ba132d478f8a2d0febe3f6 MD5 | raw file
1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === 2 * 3 * Copyright 2011, Dominik Schmidt <dev@dominik-schmidt.de> 4 * Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org> 5 * 6 * Tomahawk is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * Tomahawk is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20#include "SipInfo.h" 21 22#include "utils/Logger.h" 23#include "utils/Json.h" 24 25#include <QVariant> 26 27 28class SipInfoPrivate : public QSharedData 29{ 30public: 31 SipInfoPrivate() 32 : port( -1 ) 33 { 34 } 35 36 SipInfoPrivate( const SipInfoPrivate& other ) 37 : QSharedData( other ) 38 , visible( other.visible ) 39 , host( other.host ) 40 , port( other.port ) 41 , nodeId( other.nodeId ) 42 , key( other.key ) 43 { 44 } 45 ~SipInfoPrivate() { } 46 47 QVariant visible; 48 QString host; 49 int port; 50 QString nodeId; 51 QString key; 52}; 53 54 55SipInfo::SipInfo() 56{ 57 d = new SipInfoPrivate; 58} 59 60 61SipInfo::SipInfo( const SipInfo& other ) 62 : QObject() 63 , d( other.d ) 64{ 65} 66 67 68SipInfo::~SipInfo() 69{ 70} 71 72 73SipInfo& 74SipInfo::operator=( const SipInfo& other ) 75{ 76 d = other.d; 77 return *this; 78} 79 80 81void 82SipInfo::clear() 83{ 84 d->visible.clear(); 85 d->host = QString(); 86 d->port = -1; 87 d->nodeId = QString(); 88 d->key = QString(); 89} 90 91 92bool 93SipInfo::isValid() const 94{ 95 tDebug( LOGVERBOSE ) << Q_FUNC_INFO << d->visible << d->host << d->port << d->nodeId << d->key; 96 if ( !d->visible.isNull() ) 97 { 98 // visible and all data available 99 if ( d->visible.toBool() && !d->host.isEmpty() && ( d->port > 0 ) && !d->nodeId.isNull() && !d->key.isNull() ) 100 return true; 101 // invisible and no data available 102 if ( !d->visible.toBool() && d->host.isEmpty() && ( d->port < 0 ) && d->nodeId.isNull() && d->key.isNull() ) 103 return true; 104 // invisible and but nodeId and key available 105 if ( !d->visible.toBool() && d->host.isEmpty() && ( d->port < 0 ) && !d->nodeId.isNull() && !d->key.isNull() ) 106 return true; 107 } 108 109 return false; 110} 111 112 113void 114SipInfo::setVisible( bool visible ) 115{ 116 d->visible.setValue( visible ); 117} 118 119 120bool 121SipInfo::isVisible() const 122{ 123 Q_ASSERT( isValid() ); 124 return d->visible.toBool(); 125} 126 127 128void 129SipInfo::setHost( const QString& host ) 130{ 131 d->host = host; 132} 133 134 135const QString 136SipInfo::host() const 137{ 138 Q_ASSERT( isValid() ); 139 return d->host; 140} 141 142 143void 144SipInfo::setPort( int port ) 145{ 146 d->port = port; 147} 148 149 150int 151SipInfo::port() const 152{ 153 Q_ASSERT( isValid() ); 154 return d->port; 155} 156 157 158void 159SipInfo::setNodeId( const QString& nodeId ) 160{ 161 d->nodeId = nodeId; 162} 163 164 165const QString 166SipInfo::nodeId() const 167{ 168 Q_ASSERT( isValid() ); 169 return d->nodeId; 170} 171 172 173void 174SipInfo::setKey( const QString& key ) 175{ 176 d->key = key; 177} 178 179 180const QString 181SipInfo::key() const 182{ 183 Q_ASSERT( isValid() ); 184 return d->key; 185} 186 187 188const QString 189SipInfo::toJson() const 190{ 191 // build variant map 192 QVariantMap m; 193 m["visible"] = isVisible(); 194 if ( isVisible() ) 195 { 196 m["ip"] = host(); 197 m["port"] = port(); 198 m["key"] = key(); 199 m["uniqname"] = nodeId(); 200 } 201 202 // serialize 203 QByteArray ba = TomahawkUtils::toJson( m ); 204 205 return QString::fromLatin1( ba ); 206} 207 208 209const SipInfo 210SipInfo::fromJson( QString json ) 211{ 212 SipInfo info; 213 214 bool ok; 215 QVariant v = TomahawkUtils::parseJson( json.toLatin1(), &ok ); 216 if ( !ok || v.type() != QVariant::Map ) 217 { 218 qDebug() << Q_FUNC_INFO << "Invalid JSON: " << json; 219 return info; 220 } 221 QVariantMap m = v.toMap(); 222 223 info.setVisible( m["visible"].toBool() ); 224 if ( m["visible"].toBool() ) 225 { 226 info.setHost( m["host"].toString() ); 227 info.setPort( m["port"].toInt() ); 228 info.setNodeId( m["uniqname"].toString() ); 229 info.setKey( m["key"].toString() ); 230 } 231 232 return info; 233} 234 235 236QDebug 237operator<< ( QDebug dbg, const SipInfo& info ) 238{ 239 if ( !info.isValid() ) 240 dbg.nospace() << "info is invalid"; 241 else 242 dbg.nospace() << info.toJson(); 243 244 return dbg.maybeSpace(); 245} 246 247 248bool 249operator==( const SipInfo& one, const SipInfo& two ) 250{ 251 // check valid/invalid combinations first, so we don't try to access any invalid sipInfos (->assert) 252 if ( ( one.isValid() && !two.isValid() ) || ( !one.isValid() && two.isValid() ) ) 253 { 254 return false; 255 } 256 else if ( one.isValid() && two.isValid() ) 257 { 258 if ( one.isVisible() == two.isVisible() 259 && one.host() == two.host() 260 && one.port() == two.port() 261 && one.nodeId() == two.nodeId() 262 && one.key() == two.key() ) 263 { 264 return true; 265 } 266 } 267 268 return false; 269} 270 271 272const QString 273SipInfo::debugString() const 274{ 275 QString debugString( "SIP INFO: visible: %1 host: host %2 port: %3 nodeid: %4 key: %5" ); 276 return debugString.arg( d->visible.toBool() ) 277 .arg( d->host ) 278 .arg( d->port ) 279 .arg( d->nodeId ) 280 .arg( d->key ); 281} 282