1 | /* |
2 | This file is part of libkabc. |
3 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "sound.h" |
22 | |
23 | #include <QtCore/QDataStream> |
24 | #include <QtCore/QSharedData> |
25 | |
26 | using namespace KABC; |
27 | |
28 | class Sound::Private : public QSharedData |
29 | { |
30 | public: |
31 | Private() |
32 | : mIntern( false ) |
33 | { |
34 | } |
35 | |
36 | Private( const Private &other ) |
37 | : QSharedData( other ) |
38 | { |
39 | mUrl = other.mUrl; |
40 | mData = other.mData; |
41 | mIntern = other.mIntern; |
42 | } |
43 | |
44 | QString mUrl; |
45 | QByteArray mData; |
46 | |
47 | bool mIntern; |
48 | }; |
49 | |
50 | Sound::Sound() |
51 | : d( new Private ) |
52 | { |
53 | } |
54 | |
55 | Sound::Sound( const QString &url ) |
56 | : d( new Private ) |
57 | { |
58 | d->mUrl = url; |
59 | } |
60 | |
61 | Sound::Sound( const QByteArray &data ) |
62 | : d( new Private ) |
63 | { |
64 | d->mIntern = true; |
65 | d->mData = data; |
66 | } |
67 | |
68 | Sound::Sound( const Sound &other ) |
69 | : d( other.d ) |
70 | { |
71 | } |
72 | |
73 | Sound::~Sound() |
74 | { |
75 | } |
76 | |
77 | Sound &Sound::operator=( const Sound &other ) |
78 | { |
79 | if ( this != &other ) { |
80 | d = other.d; |
81 | } |
82 | |
83 | return *this; |
84 | } |
85 | |
86 | bool Sound::operator==( const Sound &other ) const |
87 | { |
88 | if ( d->mIntern != other.d->mIntern ) { |
89 | return false; |
90 | } |
91 | |
92 | if ( d->mIntern ) { |
93 | if ( d->mData != other.d->mData ) { |
94 | return false; |
95 | } |
96 | } else { |
97 | if ( d->mUrl != other.d->mUrl ) { |
98 | return false; |
99 | } |
100 | } |
101 | |
102 | return true; |
103 | } |
104 | |
105 | bool Sound::operator!=( const Sound &other ) const |
106 | { |
107 | return !( other == *this ); |
108 | } |
109 | |
110 | void Sound::setUrl( const QString &url ) |
111 | { |
112 | d->mIntern = false; |
113 | d->mUrl = url; |
114 | } |
115 | |
116 | void Sound::setData( const QByteArray &data ) |
117 | { |
118 | d->mIntern = true; |
119 | d->mData = data; |
120 | } |
121 | |
122 | bool Sound::isIntern() const |
123 | { |
124 | return d->mIntern; |
125 | } |
126 | |
127 | bool Sound::isEmpty() const |
128 | { |
129 | return |
130 | ( ( d->mIntern && d->mData.isEmpty() ) || ( !d->mIntern && d->mUrl.isEmpty() ) ); |
131 | } |
132 | |
133 | QString Sound::url() const |
134 | { |
135 | return d->mUrl; |
136 | } |
137 | |
138 | QByteArray Sound::data() const |
139 | { |
140 | return d->mData; |
141 | } |
142 | |
143 | QString Sound::toString() const |
144 | { |
145 | QString str; |
146 | |
147 | str += QLatin1String( "Sound {\n" ); |
148 | str += QString::fromLatin1( " IsIntern: %1\n" ). |
149 | arg( d->mIntern ? QLatin1String( "true" ) : QLatin1String( "false" ) ); |
150 | if ( d->mIntern ) { |
151 | str += QString::fromLatin1( " Data: %1\n" ). |
152 | arg( QString::fromLatin1( d->mData.toBase64() ) ); |
153 | } else { |
154 | str += QString::fromLatin1( " Url: %1\n" ).arg( d->mUrl ); |
155 | } |
156 | str += QLatin1String( "}\n" ); |
157 | |
158 | return str; |
159 | } |
160 | |
161 | QDataStream &KABC::operator<<( QDataStream &s, const Sound &sound ) |
162 | { |
163 | return s << sound.d->mIntern << sound.d->mUrl << sound.d->mData; |
164 | } |
165 | |
166 | QDataStream &KABC::operator>>( QDataStream &s, Sound &sound ) |
167 | { |
168 | s >> sound.d->mIntern >> sound.d->mUrl >> sound.d->mData; |
169 | |
170 | return s; |
171 | } |
172 | |