~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to libkopete/kopetepicture.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
Import upstream version 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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