1 | /* |
2 | This file is part of the KContacts framework. |
3 | SPDX-FileCopyrightText: 2002 Tobias Koenig <tokoe@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #include "sound.h" |
9 | |
10 | #include <QDataStream> |
11 | #include <QSharedData> |
12 | |
13 | using namespace KContacts; |
14 | |
15 | class Q_DECL_HIDDEN Sound::Private : public QSharedData |
16 | { |
17 | public: |
18 | Private() |
19 | : mIntern(false) |
20 | { |
21 | } |
22 | |
23 | Private(const Private &other) |
24 | : QSharedData(other) |
25 | { |
26 | mUrl = other.mUrl; |
27 | mData = other.mData; |
28 | mIntern = other.mIntern; |
29 | } |
30 | |
31 | QString mUrl; |
32 | QByteArray mData; |
33 | |
34 | bool mIntern; |
35 | }; |
36 | |
37 | Sound::Sound() |
38 | : d(new Private) |
39 | { |
40 | } |
41 | |
42 | Sound::Sound(const QString &url) |
43 | : d(new Private) |
44 | { |
45 | d->mUrl = url; |
46 | } |
47 | |
48 | Sound::Sound(const QByteArray &data) |
49 | : d(new Private) |
50 | { |
51 | d->mIntern = true; |
52 | d->mData = data; |
53 | } |
54 | |
55 | Sound::Sound(const Sound &other) |
56 | : d(other.d) |
57 | { |
58 | } |
59 | |
60 | Sound::~Sound() |
61 | { |
62 | } |
63 | |
64 | Sound &Sound::operator=(const Sound &other) |
65 | { |
66 | if (this != &other) { |
67 | d = other.d; |
68 | } |
69 | |
70 | return *this; |
71 | } |
72 | |
73 | bool Sound::operator==(const Sound &other) const |
74 | { |
75 | if (d->mIntern != other.d->mIntern) { |
76 | return false; |
77 | } |
78 | |
79 | if (d->mIntern) { |
80 | if (d->mData != other.d->mData) { |
81 | return false; |
82 | } |
83 | } else { |
84 | if (d->mUrl != other.d->mUrl) { |
85 | return false; |
86 | } |
87 | } |
88 | |
89 | return true; |
90 | } |
91 | |
92 | bool Sound::operator!=(const Sound &other) const |
93 | { |
94 | return !(other == *this); |
95 | } |
96 | |
97 | void Sound::setUrl(const QString &url) |
98 | { |
99 | d->mIntern = false; |
100 | d->mUrl = url; |
101 | } |
102 | |
103 | void Sound::setData(const QByteArray &data) |
104 | { |
105 | d->mIntern = true; |
106 | d->mData = data; |
107 | } |
108 | |
109 | bool Sound::isIntern() const |
110 | { |
111 | return d->mIntern; |
112 | } |
113 | |
114 | bool Sound::isEmpty() const |
115 | { |
116 | return (d->mIntern && d->mData.isEmpty()) || (!d->mIntern && d->mUrl.isEmpty()); |
117 | } |
118 | |
119 | QString Sound::url() const |
120 | { |
121 | return d->mUrl; |
122 | } |
123 | |
124 | QByteArray Sound::data() const |
125 | { |
126 | return d->mData; |
127 | } |
128 | |
129 | QString Sound::toString() const |
130 | { |
131 | QString str = QLatin1String("Sound {\n" ); |
132 | str += QStringLiteral(" IsIntern: %1\n" ).arg(a: d->mIntern ? QStringLiteral("true" ) : QStringLiteral("false" )); |
133 | if (d->mIntern) { |
134 | str += QStringLiteral(" Data: %1\n" ).arg(a: QString::fromLatin1(ba: d->mData.toBase64())); |
135 | } else { |
136 | str += QStringLiteral(" Url: %1\n" ).arg(a: d->mUrl); |
137 | } |
138 | str += QLatin1String("}\n" ); |
139 | |
140 | return str; |
141 | } |
142 | |
143 | QDataStream &KContacts::operator<<(QDataStream &s, const Sound &sound) |
144 | { |
145 | return s << sound.d->mIntern << sound.d->mUrl << sound.d->mData; |
146 | } |
147 | |
148 | QDataStream &KContacts::operator>>(QDataStream &s, Sound &sound) |
149 | { |
150 | s >> sound.d->mIntern >> sound.d->mUrl >> sound.d->mData; |
151 | |
152 | return s; |
153 | } |
154 | |