remove actions which are not supported, this way they are also not visible in shortcu...
[kdenetwork.git] / kopete / libkopete / kopetepicture.cpp
blobc84ab34d23ca6a9bbfa7d009d104b3ba69ea9b91
1 /*
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 *************************************************************************
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 *************************************************************************
17 #include "kopetepicture.h"
19 #include <qbuffer.h>
21 #include <kabc/picture.h>
23 #include <kcodecs.h>
24 #include <kstandarddirs.h>
25 #include <kdebug.h>
28 namespace Kopete
31 class Picture::Private : public KShared
33 public:
34 Private()
37 QString pictureBase64;
38 QImage pictureImage;
39 QString picturePath;
42 Picture::Picture()
43 : d(new Private)
47 Picture::Picture(const QString &path)
48 : d(new Private)
50 setPicture(path);
53 Picture::Picture(const QImage &image)
54 : d(new Private)
56 setPicture(image);
59 Picture::Picture(const KABC::Picture &picture)
60 : d(new Private)
62 setPicture(picture);
65 Picture::Picture(const Picture &other)
66 : d(other.d)
69 Picture::~Picture()
72 Picture &Picture::operator=(const Picture &other)
74 d = other.d;
75 return *this;
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.
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" ) )
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() )
138 return true;
140 else
142 return false;
146 void Picture::clear()
148 detach();
149 d->pictureBase64.clear();
150 d->picturePath.clear();
151 d->pictureImage = QImage();
154 void Picture::setPicture(const QImage &image)
156 detach();
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)
167 detach();
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() );
182 else
184 setPicture( picture.url() );
188 void Picture::detach()
190 // there is no detach in KSharedPtr.
191 if( d.count() == 1 )
192 return;
194 // Warning: this only works as long as the private object doesn't contain pointers to allocated objects.
195 d = new Private(*d);
198 } // END namespace Kopete