• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

kabc

  • kabc
picture.cpp
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 "picture.h"
22 
23 #include <QtCore/QBuffer>
24 #include <QtCore/QSharedData>
25 
26 using namespace KABC;
27 
28 class Picture::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  mType = other.mType;
41  mData = other.mData;
42  mIntern = other.mIntern;
43  }
44 
45  QString mUrl;
46  QString mType;
47  mutable QImage mData;
48  mutable QByteArray mRawData;
49 
50  bool mIntern;
51 };
52 
53 Picture::Picture()
54  : d( new Private )
55 {
56 }
57 
58 Picture::Picture( const QString &url )
59  : d( new Private )
60 {
61  d->mUrl = url;
62 }
63 
64 Picture::Picture( const QImage &data )
65  : d( new Private )
66 {
67  setData( data );
68 }
69 
70 Picture::Picture( const Picture &other )
71  : d( other.d )
72 {
73 }
74 
75 Picture::~Picture()
76 {
77 }
78 
79 Picture &Picture::operator=( const Picture &other )
80 {
81  if ( this != &other ) {
82  d = other.d;
83  }
84 
85  return *this;
86 }
87 
88 bool Picture::operator==( const Picture &p ) const
89 {
90  if ( d->mIntern != p.d->mIntern ) {
91  return false;
92  }
93 
94  if ( d->mType != p.d->mType ) {
95  return false;
96  }
97 
98  if ( d->mIntern ) {
99  if ( !d->mData.isNull() && !p.d->mData.isNull() ) {
100  if ( d->mData != p.d->mData ) {
101  return false;
102  }
103  } else if ( !d->mRawData.isEmpty() && !p.d->mRawData.isEmpty() ) {
104  if ( d->mRawData != p.d->mRawData ) {
105  return false;
106  }
107  } else if ( ( !d->mData.isNull() || !d->mRawData.isEmpty() ) &&
108  ( !p.d->mData.isNull() || !p.d->mRawData.isEmpty() ) ) {
109  if ( data() != p.data() ) {
110  return false;
111  }
112  } else {
113  // if one picture is empty and the other is not
114  return false;
115  }
116  } else {
117  if ( d->mUrl != p.d->mUrl ) {
118  return false;
119  }
120  }
121 
122  return true;
123 }
124 
125 bool Picture::operator!=( const Picture &p ) const
126 {
127  return !( p == *this );
128 }
129 
130 bool Picture::isEmpty() const
131 {
132  return
133  ( ( d->mIntern == false && d->mUrl.isEmpty() ) ||
134  ( d->mIntern == true && d->mData.isNull() && d->mRawData.isEmpty() ) );
135 }
136 
137 void Picture::setUrl( const QString &url )
138 {
139  d->mUrl = url;
140  d->mType.clear();
141  d->mIntern = false;
142 }
143 
144 void Picture::setUrl( const QString &url, const QString &type )
145 {
146  d->mUrl = url;
147  d->mType = type;
148  d->mIntern = false;
149 }
150 
151 void Picture::setData( const QImage &data )
152 {
153  d->mRawData.clear();
154  d->mData = data;
155  d->mIntern = true;
156 
157  // set the type, the raw data will have when accessed through Picture::rawData()
158  if ( !d->mData.hasAlphaChannel() ) {
159  d->mType = QLatin1String( "jpeg" );
160  } else {
161  d->mType = QLatin1String( "png" );
162  }
163 }
164 
165 void Picture::setRawData( const QByteArray &rawData, const QString &type )
166 {
167  d->mRawData = rawData;
168  d->mType = type;
169  d->mData = QImage();
170  d->mIntern = true;
171 }
172 
173 void Picture::setType( const QString &type )
174 {
175  d->mType = type;
176 }
177 
178 bool Picture::isIntern() const
179 {
180  return d->mIntern;
181 }
182 
183 QString Picture::url() const
184 {
185  return d->mUrl;
186 }
187 
188 QImage Picture::data() const
189 {
190  if ( d->mData.isNull() && !d->mRawData.isEmpty() ) {
191  d->mData.loadFromData( d->mRawData );
192  }
193 
194  return d->mData;
195 }
196 
197 QByteArray Picture::rawData() const
198 {
199  if ( d->mRawData.isEmpty() && !d->mData.isNull() ) {
200  QBuffer buffer( &d->mRawData );
201  buffer.open( QIODevice::WriteOnly );
202 
203  // d->mType was already set accordingly by Picture::setData()
204  d->mData.save( &buffer, d->mType.toUpper().toLatin1().data() );
205  }
206 
207  return d->mRawData;
208 }
209 
210 QString Picture::type() const
211 {
212  return d->mType;
213 }
214 
215 QString Picture::toString() const
216 {
217  QString str;
218 
219  str += QLatin1String( "Picture {\n" );
220  str += QString::fromLatin1( " Type: %1\n" ).arg( d->mType );
221  str += QString::fromLatin1( " IsIntern: %1\n" ).
222  arg( d->mIntern ? QLatin1String( "true" ) : QLatin1String( "false" ) );
223  if ( d->mIntern ) {
224  str += QString::fromLatin1( " Data: %1\n" ).arg( QString::fromLatin1( rawData().toBase64() ) );
225  } else {
226  str += QString::fromLatin1( " Url: %1\n" ).arg( d->mUrl );
227  }
228  str += QLatin1String( "}\n" );
229 
230  return str;
231 }
232 
233 QDataStream &KABC::operator<<( QDataStream &s, const Picture &picture )
234 {
235  return s << picture.d->mIntern << picture.d->mUrl << picture.d->mType << picture.data();
236 }
237 
238 QDataStream &KABC::operator>>( QDataStream &s, Picture &picture )
239 {
240  s >> picture.d->mIntern >> picture.d->mUrl >> picture.d->mType >> picture.d->mData;
241 
242  return s;
243 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:29:41 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal