KContacts

picture.cpp
1 /*
2  This file is part of the KContacts framework.
3  SPDX-FileCopyrightText: 2002 Tobias Koenig <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "picture.h"
9 
10 #include <QBuffer>
11 #include <QSharedData>
12 
13 #include <QImage>
14 
15 namespace KContacts {
16 class PicturePrivate : public QSharedData
17 {
18 public:
19  PicturePrivate()
20  : mIntern(false)
21  {
22  }
23 
24  PicturePrivate(const PicturePrivate &other)
25  : QSharedData(other)
26  {
27  mUrl = other.mUrl;
28  mType = other.mType;
29  mData = other.mData;
30  mIntern = other.mIntern;
31  }
32 
33  QString mUrl;
34  QString mType;
35  mutable QImage mData;
36  mutable QByteArray mRawData;
37 
38  bool mIntern;
39 };
40 }
41 
42 Q_GLOBAL_STATIC_WITH_ARGS(QSharedDataPointer<KContacts::PicturePrivate>, s_sharedEmpty, (new KContacts::PicturePrivate))
43 
44 using namespace KContacts;
45 
47  : d(*s_sharedEmpty())
48 {
49 }
50 
52  : d(new PicturePrivate)
53 {
54  d->mUrl = url;
55 }
56 
58  : d(new PicturePrivate)
59 {
60  setData(data);
61 }
62 
64  : d(other.d)
65 {
66 }
67 
69 {
70 }
71 
73 {
74  if (this != &other) {
75  d = other.d;
76  }
77 
78  return *this;
79 }
80 
81 bool Picture::operator==(const Picture &p) const
82 {
83  if (d->mIntern != p.d->mIntern) {
84  return false;
85  }
86 
87  if (d->mType != p.d->mType) {
88  return false;
89  }
90 
91  if (d->mIntern) {
92  if (!d->mData.isNull() && !p.d->mData.isNull()) {
93  if (d->mData != p.d->mData) {
94  return false;
95  }
96  } else if (!d->mRawData.isEmpty() && !p.d->mRawData.isEmpty()) {
97  if (d->mRawData != p.d->mRawData) {
98  return false;
99  }
100  } else if ((!d->mData.isNull() || !d->mRawData.isEmpty())
101  && (!p.d->mData.isNull() || !p.d->mRawData.isEmpty())) {
102  if (data() != p.data()) {
103  return false;
104  }
105  } else {
106  // if one picture is empty and the other is not
107  return false;
108  }
109  } else {
110  if (d->mUrl != p.d->mUrl) {
111  return false;
112  }
113  }
114 
115  return true;
116 }
117 
118 bool Picture::operator!=(const Picture &p) const
119 {
120  return !(p == *this);
121 }
122 
123 bool Picture::isEmpty() const
124 {
125  return
126  (!d->mIntern && d->mUrl.isEmpty()) ||
127  (d->mIntern && d->mData.isNull() && d->mRawData.isEmpty());
128 }
129 
131 {
132  d->mUrl = url;
133  d->mType.clear();
134  d->mIntern = false;
135 }
136 
137 void Picture::setUrl(const QString &url, const QString &type)
138 {
139  d->mUrl = url;
140  d->mType = type;
141  d->mIntern = false;
142 }
143 
145 {
146  d->mRawData.clear();
147  d->mData = data;
148  d->mIntern = true;
149 
150 // set the type, the raw data will have when accessed through Picture::rawData()
151  if (!d->mData.hasAlphaChannel()) {
152  d->mType = QStringLiteral("jpeg");
153  } else {
154  d->mType = QStringLiteral("png");
155  }
156 }
157 
159 {
160  d->mRawData = rawData;
161  d->mType = type;
162  d->mData = QImage();
163  d->mIntern = true;
164 }
165 
166 bool Picture::isIntern() const
167 {
168  return d->mIntern;
169 }
170 
172 {
173  return d->mUrl;
174 }
175 
177 {
178  if (d->mData.isNull() && !d->mRawData.isEmpty()) {
179  d->mData.loadFromData(d->mRawData);
180  }
181 
182  return d->mData;
183 }
184 
186 {
187  if (d->mRawData.isEmpty() && !d->mData.isNull()) {
188  QBuffer buffer(&d->mRawData);
189  buffer.open(QIODevice::WriteOnly);
190 
191  // d->mType was already set accordingly by Picture::setData()
192  d->mData.save(&buffer, d->mType.toUpper().toLatin1().data());
193  }
194 
195  return d->mRawData;
196 }
197 
199 {
200  return d->mType;
201 }
202 
204 {
205  QString str = QLatin1String("Picture {\n");
206  str += QStringLiteral(" Type: %1\n").arg(d->mType);
207  str += QStringLiteral(" IsIntern: %1\n").
208  arg(d->mIntern ? QStringLiteral("true") : QStringLiteral("false"));
209  if (d->mIntern) {
210  str += QStringLiteral(" Data: %1\n").arg(QString::fromLatin1(rawData().toBase64()));
211  } else {
212  str += QStringLiteral(" Url: %1\n").arg(d->mUrl);
213  }
214  str += QLatin1String("}\n");
215 
216  return str;
217 }
218 
219 QDataStream &KContacts::operator<<(QDataStream &s, const Picture &picture)
220 {
221  return s << picture.d->mIntern << picture.d->mUrl << picture.d->mType << picture.data();
222 }
223 
224 QDataStream &KContacts::operator>>(QDataStream &s, Picture &picture)
225 {
226  s >> picture.d->mIntern >> picture.d->mUrl >> picture.d->mType >> picture.d->mData;
227 
228  return s;
229 }
QString type() const
Returns the type of this picture.
Definition: picture.cpp:198
bool operator!=(const Picture &other) const
Not-Equal operator.
Definition: picture.cpp:118
bool operator==(const Picture &other) const
Equality operator.
Definition: picture.cpp:81
bool isEmpty() const
Returns true, if the picture is empty.
Definition: picture.cpp:123
Picture()
Creates an empty picture.
Definition: picture.cpp:46
void setData(const QImage &data)
Sets the image data of the picture.
Definition: picture.cpp:144
QString url() const
Returns the location URL of this picture.
Definition: picture.cpp:171
QImage data() const
Returns the image data of this picture.
Definition: picture.cpp:176
virtual bool open(QIODevice::OpenMode flags) override
A class to store a picture of an addressee.
Definition: picture.h:26
QByteArray rawData() const
Returns the raw data of this picture.
Definition: picture.cpp:185
void setRawData(const QByteArray &rawData, const QString &type)
Sets the raw data of the picture.
Definition: picture.cpp:158
void setUrl(const QString &url)
Sets a URL for the location of the picture file.
Definition: picture.cpp:130
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
~Picture()
Destructor.
Definition: picture.cpp:68
QString fromLatin1(const char *str, int size)
QString toString() const
Returns string representation of the picture.
Definition: picture.cpp:203
bool isIntern() const
Returns whether the picture is described by a URL (extern) or by the raw data (intern).
Definition: picture.cpp:166
Picture & operator=(const Picture &other)
Assignment operator.
Definition: picture.cpp:72
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Aug 6 2020 22:54:05 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.