2 kopetepicture.cpp - Kopete Picture
4 Copyright (c) 2005 by Michaƫl Larouche <larouche@kde.org>
6 Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>
8 *************************************************************************
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. *
15 *************************************************************************
17 #include "kopetepicture.h"
21 #include <kabc/picture.h>
24 #include <kstandarddirs.h>
31 class Picture::Private
: public KShared
37 QString pictureBase64
;
47 Picture::Picture(const QString
&path
)
53 Picture::Picture(const QImage
&image
)
59 Picture::Picture(const KABC::Picture
&picture
)
65 Picture::Picture(const Picture
&other
)
72 Picture
&Picture::operator=(const Picture
&other
)
78 QImage
Picture::image()
80 // Do the conversion if only needed.
81 // If the image is null, the path is not empty then.
82 if( d
->pictureImage
.isNull() )
84 d
->pictureImage
= QImage(d
->picturePath
);
87 return d
->pictureImage
;
90 QString
Picture::base64()
92 if( d
->pictureBase64
.isEmpty() )
94 // Generate base64 cache for the picture.
96 QBuffer
tempBuffer( &tempArray
);
97 tempBuffer
.open( QIODevice::WriteOnly
);
98 // Make sure it create a image cache.
99 if( image().save( &tempBuffer
, "PNG" ) )
101 d
->pictureBase64
= tempArray
.toBase64();
105 return d
->pictureBase64
;
108 QString
Picture::path()
110 if( d
->picturePath
.isEmpty() )
112 // For a image source, finding a filename is tricky.
113 // I decided to use MD5 Hash as the filename.
114 QString localPhotoPath
;
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") )
127 d
->picturePath
= localPhotoPath
;
131 return d
->picturePath
;
134 bool Picture::isNull()
136 if( d
->pictureBase64
.isEmpty() && d
->picturePath
.isEmpty() && d
->pictureImage
.isNull() )
146 void Picture::clear()
149 d
->pictureBase64
.clear();
150 d
->picturePath
.clear();
151 d
->pictureImage
= QImage();
154 void Picture::setPicture(const QImage
&image
)
158 d
->pictureImage
= image
;
160 // Clear the path and base64, it will call the update of then when "getted"
161 d
->picturePath
.clear();
162 d
->pictureBase64
.clear();
165 void Picture::setPicture(const QString
&path
)
168 d
->picturePath
= path
;
170 // Clear the image and base64, it will call the update of then when "getted"
171 d
->pictureImage
= QImage();
172 d
->pictureBase64
.clear();
175 void Picture::setPicture(const KABC::Picture
&picture
)
177 // No need to call detach() here because setPicture will do it.
178 if ( picture
.isIntern())
180 setPicture( picture
.data() );
184 setPicture( picture
.url() );
188 void Picture::detach()
190 // there is no detach in KSharedPtr.
194 // Warning: this only works as long as the private object doesn't contain pointers to allocated objects.
198 } // END namespace Kopete