summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/kopetepicture.cpp
blob: dad810c4a0033d3ebb045e3c30c9ab0feacae70d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
    kopetepicture.cpp - Kopete Picture

    Copyright (c) 2005      by Michaël Larouche       <michael.larouche@kdemail.net>

    Kopete    (c) 2002-2005 by the Kopete developers  <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/
#include "kopetepicture.h"

#include <tqbuffer.h>

#include <tdeabc/picture.h>

#include <kmdcodec.h>
#include <kstandarddirs.h>
#include <kdebug.h>

namespace Kopete
{

class Picture::Private : public TDEShared
{
public:
	Private()
	{}

	TQString pictureBase64;
	TQImage pictureImage;
	TQString picturePath;
};

Picture::Picture()
 : d(new Private)
{
}

Picture::Picture(const TQString &path)
 : d(new Private)
{
	setPicture(path);
}

Picture::Picture(const TQImage &image)
 : d(new Private)
{
	setPicture(image);
}

Picture::Picture(const KABC::Picture &picture)
 : d(new Private)
{
	setPicture(picture);
}

Picture::Picture(const Picture &other)
 : d(other.d)
{}

Picture::~Picture()
{}

Picture &Picture::operator=(const Picture &other)
{
	d = other.d;
	return *this;
}

TQImage Picture::image()
{
	// Do the conversion if only needed.
	// If the image is null, the path is not empty then.
	if( d->pictureImage.isNull() )
	{
		d->pictureImage = TQImage(d->picturePath);
	}

	return d->pictureImage;
}

TQString Picture::base64()
{
	if( d->pictureBase64.isEmpty() )
	{
		// Generate base64 cache for the picture.
		TQByteArray tempArray;
		TQBuffer tempBuffer( tempArray );
		tempBuffer.open( IO_WriteOnly );
		// Make sure it create a image cache.
		if( image().save( &tempBuffer, "PNG" ) )
		{
			d->pictureBase64 = KCodecs::base64Encode(tempArray);
		}
	}

	return d->pictureBase64;
}

TQString Picture::path()
{
	if( d->picturePath.isEmpty() )
	{
		// For a image source, finding a filename is tricky.
		// I decided to use MD5 Hash as the filename.
		TQString localPhotoPath;
		
		// Generate MD5 Hash for the image.
		TQByteArray tempArray;
		TQBuffer tempBuffer(tempArray);
		tempBuffer.open( IO_WriteOnly );
		image().save(&tempBuffer, "PNG");
		KMD5 context(tempArray);
		// Save the image to a file.
		localPhotoPath = context.hexDigest() + ".png";
		localPhotoPath = locateLocal( "appdata", TQString::fromUtf8("metacontactpicturecache/%1").arg( localPhotoPath) );
		if( image().save(localPhotoPath, "PNG") )
		{
			d->picturePath = localPhotoPath;
		}
	}

	return d->picturePath;
}

bool Picture::isNull()
{
	if( d->pictureBase64.isEmpty() && d->picturePath.isEmpty() && d->pictureImage.isNull() )
	{
		return true;
	}
	else
	{
		return false;
	}
}

void Picture::clear()
{
	detach();
	d->pictureBase64 = TQString();
	d->picturePath = TQString();
	d->pictureImage = TQImage();
}

void Picture::setPicture(const TQImage &image)
{
	detach();

	d->pictureImage = image;

	// Clear the path and base64, it will call the update of then when "getted"
	d->picturePath= TQString();
	d->pictureBase64 = TQString();
}

void Picture::setPicture(const TQString &path)
{
	detach();
	d->picturePath = path;
	
	// Clear the image and base64, it will call the update of then when "getted"
	d->pictureImage = TQImage();
	d->pictureBase64 = TQString();
}

void Picture::setPicture(const KABC::Picture &picture)
{
	// No need to call detach() here because setPicture will do it.
	if ( picture.isIntern())
	{
		setPicture( picture.data() );
	}
	else
	{
		setPicture( picture.url() );
	}
}

void Picture::detach()
{
	// there is no detach in TDESharedPtr.
	if( d.count() == 1 )
		return;

	// Warning: this only works as long as the private object doesn't contain pointers to allocated objects.
	d = new Private(*d);
}

} // END namespace Kopete