kabc
sound.cpp
00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "sound.h" 00022 00023 #include <QtCore/QDataStream> 00024 #include <QtCore/QSharedData> 00025 00026 using namespace KABC; 00027 00028 class Sound::Private : public QSharedData 00029 { 00030 public: 00031 Private() 00032 : mIntern( false ) 00033 { 00034 } 00035 00036 Private( const Private &other ) 00037 : QSharedData( other ) 00038 { 00039 mUrl = other.mUrl; 00040 mData = other.mData; 00041 mIntern = other.mIntern; 00042 } 00043 00044 QString mUrl; 00045 QByteArray mData; 00046 00047 bool mIntern; 00048 }; 00049 00050 Sound::Sound() 00051 : d( new Private ) 00052 { 00053 } 00054 00055 Sound::Sound( const QString &url ) 00056 : d( new Private ) 00057 { 00058 d->mUrl = url; 00059 } 00060 00061 Sound::Sound( const QByteArray &data ) 00062 : d( new Private ) 00063 { 00064 d->mIntern = true; 00065 d->mData = data; 00066 } 00067 00068 Sound::Sound( const Sound &other ) 00069 : d( other.d ) 00070 { 00071 } 00072 00073 Sound::~Sound() 00074 { 00075 } 00076 00077 Sound &Sound::operator=( const Sound &other ) 00078 { 00079 if ( this != &other ) { 00080 d = other.d; 00081 } 00082 00083 return *this; 00084 } 00085 00086 bool Sound::operator==( const Sound &other ) const 00087 { 00088 if ( d->mIntern != other.d->mIntern ) { 00089 return false; 00090 } 00091 00092 if ( d->mIntern ) { 00093 if ( d->mData != other.d->mData ) { 00094 return false; 00095 } 00096 } else { 00097 if ( d->mUrl != other.d->mUrl ) { 00098 return false; 00099 } 00100 } 00101 00102 return true; 00103 } 00104 00105 bool Sound::operator!=( const Sound &other ) const 00106 { 00107 return !( other == *this ); 00108 } 00109 00110 void Sound::setUrl( const QString &url ) 00111 { 00112 d->mIntern = false; 00113 d->mUrl = url; 00114 } 00115 00116 void Sound::setData( const QByteArray &data ) 00117 { 00118 d->mIntern = true; 00119 d->mData = data; 00120 } 00121 00122 bool Sound::isIntern() const 00123 { 00124 return d->mIntern; 00125 } 00126 00127 bool Sound::isEmpty() const 00128 { 00129 return 00130 ( ( d->mIntern && d->mData.isEmpty() ) || ( !d->mIntern && d->mUrl.isEmpty() ) ); 00131 } 00132 00133 QString Sound::url() const 00134 { 00135 return d->mUrl; 00136 } 00137 00138 QByteArray Sound::data() const 00139 { 00140 return d->mData; 00141 } 00142 00143 QString Sound::toString() const 00144 { 00145 QString str; 00146 00147 str += QLatin1String( "Sound {\n" ); 00148 str += QString::fromLatin1( " IsIntern: %1\n" ). 00149 arg( d->mIntern ? QLatin1String( "true" ) : QLatin1String( "false" ) ); 00150 if ( d->mIntern ) { 00151 str += QString::fromLatin1( " Data: %1\n" ). 00152 arg( QString::fromLatin1( d->mData.toBase64() ) ); 00153 } else { 00154 str += QString::fromLatin1( " Url: %1\n" ).arg( d->mUrl ); 00155 } 00156 str += QLatin1String( "}\n" ); 00157 00158 return str; 00159 } 00160 00161 QDataStream &KABC::operator<<( QDataStream &s, const Sound &sound ) 00162 { 00163 return s << sound.d->mIntern << sound.d->mUrl << sound.d->mData; 00164 } 00165 00166 QDataStream &KABC::operator>>( QDataStream &s, Sound &sound ) 00167 { 00168 s >> sound.d->mIntern >> sound.d->mUrl >> sound.d->mData; 00169 00170 return s; 00171 }