• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kopete/libkopete

  • sources
  • kde-4.14
  • kdenetwork
  • kopete
  • libkopete
kopetepicture.cpp
Go to the documentation of this file.
1 /*
2  kopetepicture.cpp - Kopete Picture
3 
4  Copyright (c) 2005 by MichaĆ«l Larouche <larouche@kde.org>
5 
6  Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>
7 
8  *************************************************************************
9  * *
10  * This library is free software; you can redistribute it and/or *
11  * modify it under the terms of the GNU Lesser General Public *
12  * License as published by the Free Software Foundation; either *
13  * version 2 of the License, or (at your option) any later version. *
14  * *
15  *************************************************************************
16 */
17 #include "kopetepicture.h"
18 
19 #include <qbuffer.h>
20 
21 #include <kabc/picture.h>
22 
23 #include <kcodecs.h>
24 #include <kstandarddirs.h>
25 #include <kdebug.h>
26 
27 
28 namespace Kopete
29 {
30 
31 class Picture::Private : public KShared
32 {
33 public:
34  Private()
35  {}
36 
37  QString pictureBase64;
38  QImage pictureImage;
39  QString picturePath;
40 };
41 
42 Picture::Picture()
43  : d(new Private)
44 {
45 }
46 
47 Picture::Picture(const QString &path)
48  : d(new Private)
49 {
50  setPicture(path);
51 }
52 
53 Picture::Picture(const QImage &image)
54  : d(new Private)
55 {
56  setPicture(image);
57 }
58 
59 Picture::Picture(const KABC::Picture &picture)
60  : d(new Private)
61 {
62  setPicture(picture);
63 }
64 
65 Picture::Picture(const Picture &other)
66  : d(other.d)
67 {}
68 
69 Picture::~Picture()
70 {}
71 
72 Picture &Picture::operator=(const Picture &other)
73 {
74  d = other.d;
75  return *this;
76 }
77 
78 QImage Picture::image()
79 {
80  // Do the conversion if only needed.
81  // If the image is null, the path is not empty then.
82  if( d->pictureImage.isNull() )
83  {
84  d->pictureImage = QImage(d->picturePath);
85  }
86 
87  return d->pictureImage;
88 }
89 
90 QString Picture::base64()
91 {
92  if( d->pictureBase64.isEmpty() )
93  {
94  // Generate base64 cache for the picture.
95  QByteArray tempArray;
96  QBuffer tempBuffer( &tempArray );
97  tempBuffer.open( QIODevice::WriteOnly );
98  // Make sure it create a image cache.
99  if( image().save( &tempBuffer, "PNG" ) )
100  {
101  d->pictureBase64 = tempArray.toBase64();
102  }
103  }
104 
105  return d->pictureBase64;
106 }
107 
108 QString Picture::path()
109 {
110  if( d->picturePath.isEmpty() )
111  {
112  // For a image source, finding a filename is tricky.
113  // I decided to use MD5 Hash as the filename.
114  QString localPhotoPath;
115 
116  // Generate MD5 Hash for the image.
117  QByteArray tempArray;
118  QBuffer tempBuffer(&tempArray);
119  tempBuffer.open( QIODevice::WriteOnly );
120  image().save(&tempBuffer, "PNG");
121  KMD5 context(tempArray);
122  // Save the image to a file.
123  localPhotoPath = context.hexDigest() + ".png";
124  localPhotoPath = KStandardDirs::locateLocal( "appdata", QString::fromUtf8("metacontactpicturecache/%1").arg( localPhotoPath) );
125  if( image().save(localPhotoPath, "PNG") )
126  {
127  d->picturePath = localPhotoPath;
128  }
129  }
130 
131  return d->picturePath;
132 }
133 
134 bool Picture::isNull()
135 {
136  if( d->pictureBase64.isEmpty() && d->picturePath.isEmpty() && d->pictureImage.isNull() )
137  {
138  return true;
139  }
140  else
141  {
142  return false;
143  }
144 }
145 
146 void Picture::clear()
147 {
148  detach();
149  d->pictureBase64.clear();
150  d->picturePath.clear();
151  d->pictureImage = QImage();
152 }
153 
154 void Picture::setPicture(const QImage &image)
155 {
156  detach();
157 
158  d->pictureImage = image;
159 
160  // Clear the path and base64, it will call the update of then when "getted"
161  d->picturePath.clear();
162  d->pictureBase64.clear();
163 }
164 
165 void Picture::setPicture(const QString &path)
166 {
167  detach();
168  d->picturePath = path;
169 
170  // Clear the image and base64, it will call the update of then when "getted"
171  d->pictureImage = QImage();
172  d->pictureBase64.clear();
173 }
174 
175 void Picture::setPicture(const KABC::Picture &picture)
176 {
177  // No need to call detach() here because setPicture will do it.
178  if ( picture.isIntern())
179  {
180  setPicture( picture.data() );
181  }
182  else
183  {
184  setPicture( picture.url() );
185  }
186 }
187 
188 void Picture::detach()
189 {
190  // there is no detach in KSharedPtr.
191  if( d.count() == 1 )
192  return;
193 
194  // Warning: this only works as long as the private object doesn't contain pointers to allocated objects.
195  d = new Private(*d);
196 }
197 
198 } // END namespace Kopete
Kopete::Picture
Represent a picture in Kopete context.
Definition: kopetepicture.h:61
QByteArray
QImage::save
bool save(const QString &fileName, const char *format, int quality) const
Kopete::Picture::Picture
Picture()
Create a empty Kopete::Picture.
Definition: kopetepicture.cpp:42
QBuffer
Kopete::Picture::image
QImage image()
Return the current picture as QImage.
Definition: kopetepicture.cpp:78
QString::fromUtf8
QString fromUtf8(const char *str, int size)
Kopete::Picture::clear
void clear()
Reset the picture.
Definition: kopetepicture.cpp:146
kopetepicture.h
QBuffer::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > flags)
QString
Kopete::Picture::path
QString path()
Return the local path of the current picture.
Definition: kopetepicture.cpp:108
Kopete::Picture::base64
QString base64()
Return the current picture as a base64 string.
Definition: kopetepicture.cpp:90
QImage
Kopete::Picture::isNull
bool isNull()
Check if the picture is null.
Definition: kopetepicture.cpp:134
Kopete::Picture::setPicture
void setPicture(const QImage &image)
Set the picture content.
Definition: kopetepicture.cpp:154
QByteArray::toBase64
QByteArray toBase64() const
Kopete::Picture::~Picture
~Picture()
Delete the Kopete::Picture.
Definition: kopetepicture.cpp:69
Kopete::Picture::operator=
Picture & operator=(const Picture &other)
Assignment operator.
Definition: kopetepicture.cpp:72
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/libkopete

Skip menu "kopete/libkopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



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