| 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 | , mUrl(other.mUrl) |
| 26 | , mType(other.mType) |
| 27 | , mData(other.mData) |
| 28 | , mIntern(other.mIntern) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | QString mUrl; |
| 33 | QString mType; |
| 34 | QByteArray mData; |
| 35 | |
| 36 | bool mIntern; |
| 37 | }; |
| 38 | |
| 39 | Sound::Sound() |
| 40 | : d(new Private) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | Sound::Sound(const QString &url) |
| 45 | : d(new Private) |
| 46 | { |
| 47 | d->mUrl = url; |
| 48 | } |
| 49 | |
| 50 | Sound::Sound(const QByteArray &data) |
| 51 | : d(new Private) |
| 52 | { |
| 53 | d->mIntern = true; |
| 54 | d->mData = data; |
| 55 | } |
| 56 | |
| 57 | Sound::Sound(const Sound &other) |
| 58 | : d(other.d) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | Sound::~Sound() = default; |
| 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->mType != other.d->mType) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | if (d->mIntern) { |
| 84 | if (d->mData != other.d->mData) { |
| 85 | return false; |
| 86 | } |
| 87 | } else { |
| 88 | if (d->mUrl != other.d->mUrl) { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool Sound::operator!=(const Sound &other) const |
| 97 | { |
| 98 | return !(other == *this); |
| 99 | } |
| 100 | |
| 101 | void Sound::setUrl(const QString &url) |
| 102 | { |
| 103 | d->mIntern = false; |
| 104 | d->mUrl = url; |
| 105 | } |
| 106 | |
| 107 | void Sound::setData(const QByteArray &data) |
| 108 | { |
| 109 | d->mIntern = true; |
| 110 | d->mData = data; |
| 111 | } |
| 112 | |
| 113 | bool Sound::isIntern() const |
| 114 | { |
| 115 | return d->mIntern; |
| 116 | } |
| 117 | |
| 118 | bool Sound::isEmpty() const |
| 119 | { |
| 120 | return (d->mIntern && d->mData.isEmpty()) || (!d->mIntern && d->mUrl.isEmpty()); |
| 121 | } |
| 122 | |
| 123 | QString Sound::url() const |
| 124 | { |
| 125 | return d->mUrl; |
| 126 | } |
| 127 | |
| 128 | QByteArray Sound::data() const |
| 129 | { |
| 130 | return d->mData; |
| 131 | } |
| 132 | |
| 133 | QString Sound::type() const |
| 134 | { |
| 135 | return d->mType; |
| 136 | } |
| 137 | |
| 138 | QString Sound::toString() const |
| 139 | { |
| 140 | QString str = QLatin1String("Sound {\n" ); |
| 141 | str += QStringLiteral(" Type: %1\n" ).arg(a: d->mType); |
| 142 | str += QStringLiteral(" IsIntern: %1\n" ).arg(a: d->mIntern ? QStringLiteral("true" ) : QStringLiteral("false" )); |
| 143 | if (d->mIntern) { |
| 144 | str += QStringLiteral(" Data: %1\n" ).arg(a: QString::fromLatin1(ba: d->mData.toBase64())); |
| 145 | } else { |
| 146 | str += QStringLiteral(" Url: %1\n" ).arg(a: d->mUrl); |
| 147 | } |
| 148 | str += QLatin1String("}\n" ); |
| 149 | |
| 150 | return str; |
| 151 | } |
| 152 | |
| 153 | QDataStream &KContacts::operator<<(QDataStream &s, const Sound &sound) |
| 154 | { |
| 155 | return s << sound.d->mIntern << sound.d->mUrl << sound.d->mData << sound.d->mType; |
| 156 | } |
| 157 | |
| 158 | QDataStream &KContacts::operator>>(QDataStream &s, Sound &sound) |
| 159 | { |
| 160 | s >> sound.d->mIntern >> sound.d->mUrl >> sound.d->mData >> sound.d->mType; |
| 161 | |
| 162 | return s; |
| 163 | } |
| 164 | |