summaryrefslogtreecommitdiffstats
path: root/kopete/kopete/config/identity/kopeteidentityconfig.cpp
blob: 26613b87e70dd615c76e5e673b985652989ab33f (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
    kopeteidentityconfig.cpp  -  Kopete Identity config page

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

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

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

#include "kopeteidentityconfig.h"

// Qt includes
#include <qlayout.h>
#include <qimage.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qcheckbox.h>
#include <qradiobutton.h>
#include <qcombobox.h>
#include <qapplication.h>
#include <qbuffer.h>

// KDE includes
#include <kcombobox.h>
#include <kfiledialog.h>
#include <kpushbutton.h>
#include <kdebug.h>
#include <kgenericfactory.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <kglobal.h>
#include <kurlrequester.h>
#include <kinputdialog.h>
#include <kpixmapregionselectordialog.h>
#include <kmdcodec.h>

// KDE KIO includes
#include <kio/netaccess.h>

// KDE KABC(AddressBook) includes
#include <kabc/addresseedialog.h>
#include <kabc/stdaddressbook.h>
#include <kabc/addressee.h>

// Kopete include
#include "kabcpersistence.h"
#include "kopeteglobal.h"
#include "kopeteaccountmanager.h"
#include "kopeteprotocol.h"
#include "kopeteaccount.h"
#include "kopetecontact.h"
#include "kopetecontactlist.h"
#include "addressbookselectordialog.h"
#include "kopeteconfig.h"

// Local includes
#include "kopeteidentityconfigbase.h"
#include "globalidentitiesmanager.h"
#include "kopeteidentityconfigpreferences.h"

class KopeteIdentityConfig::Private
{
public:
	Private() : m_view(0L), myself(0L), currentIdentity(0L), selectedIdentity("")
	{}

	KopeteIdentityConfigBase *m_view;
	Kopete::MetaContact *myself;
	Kopete::MetaContact *currentIdentity;
	
	QMap<int, Kopete::Contact*> contactPhotoSourceList;
	QString selectedIdentity;
};

typedef KGenericFactory<KopeteIdentityConfig, QWidget> KopeteIdentityConfigFactory;
K_EXPORT_COMPONENT_FACTORY( kcm_kopete_identityconfig, KopeteIdentityConfigFactory( "kcm_kopete_identityconfig" ) )

KopeteIdentityConfig::KopeteIdentityConfig(QWidget *parent, const char */*name*/, const QStringList &args) : KCModule( KopeteIdentityConfigFactory::instance(), parent, args)
{
	d = new Private;
	( new QVBoxLayout( this ) )->setAutoAdd( true );
	d->m_view = new KopeteIdentityConfigBase( this, "KopeteIdentityConfig::m_view" );
	
	// Setup KConfigXT link with GUI.
	addConfig( Kopete::Config::self(), d->m_view );

	// Load config
	KopeteIdentityConfigPreferences::self()->readConfig();

	// Load from XML the identities.
	GlobalIdentitiesManager::self()->loadXML();

	d->myself = Kopete::ContactList::self()->myself();
	
	// Set the latest selected Identity.
	d->selectedIdentity = KopeteIdentityConfigPreferences::self()->selectedIdentity();
	kdDebug() << k_funcinfo << "Latest loaded identity: " << d->selectedIdentity << endl;

	// If the latest selected Identity is not present anymore, use a fallback identity.
	if( !GlobalIdentitiesManager::self()->isIdentityPresent(d->selectedIdentity) )
	{
		QMapIterator<QString, Kopete::MetaContact*> it = GlobalIdentitiesManager::self()->getGlobalIdentitiesList().begin();
		d->selectedIdentity = it.key();
	}
	else
	{
		// Update the latest identity with myself Metacontact.
		GlobalIdentitiesManager::self()->updateIdentity(d->selectedIdentity, d->myself);
	}
	d->currentIdentity = GlobalIdentitiesManager::self()->getIdentity(d->selectedIdentity);
	
	// Set icon for KPushButton
	d->m_view->buttonNewIdentity->setIconSet(SmallIconSet("new"));
	d->m_view->buttonCopyIdentity->setIconSet(SmallIconSet("editcopy"));
	d->m_view->buttonRenameIdentity->setIconSet(SmallIconSet("edit"));
	d->m_view->buttonRemoveIdentity->setIconSet(SmallIconSet("delete_user"));
	d->m_view->buttonClearPhoto->setIconSet(  SmallIconSet( QApplication::reverseLayout() ? "locationbar_erase" : "clear_left" ) );

	load(); // Load Configuration

	// Action signal/slots
	connect(d->m_view->buttonChangeAddressee, SIGNAL(clicked()), this, SLOT(slotChangeAddressee()));
	connect(d->m_view->comboSelectIdentity, SIGNAL(activated(const QString &)), this, SLOT(slotUpdateCurrentIdentity(const QString& )));
	connect(d->m_view->buttonNewIdentity, SIGNAL(clicked()), this, SLOT(slotNewIdentity()));
	connect(d->m_view->buttonCopyIdentity, SIGNAL(clicked()), this, SLOT(slotCopyIdentity()));
	connect(d->m_view->buttonRenameIdentity, SIGNAL(clicked()), this, SLOT(slotRenameIdentity()));
	connect(d->m_view->buttonRemoveIdentity, SIGNAL(clicked()), this, SLOT(slotRemoveIdentity()));
	connect(d->m_view->comboPhotoURL, SIGNAL(urlSelected(const QString& )), this, SLOT(slotChangePhoto(const QString& )));
	connect(d->m_view->buttonClearPhoto, SIGNAL(clicked()), this, SLOT(slotClearPhoto()));

	// Settings signal/slots
	connect(d->m_view->radioNicknameContact, SIGNAL(toggled(bool )), this, SLOT(slotEnableAndDisableWidgets()));
	connect(d->m_view->radioNicknameCustom, SIGNAL(toggled(bool )), this, SLOT(slotEnableAndDisableWidgets()));
	connect(d->m_view->radioNicknameKABC, SIGNAL(toggled(bool )), this, SLOT(slotEnableAndDisableWidgets()));

	connect(d->m_view->radioPhotoContact, SIGNAL(toggled(bool )), this, SLOT(slotEnableAndDisableWidgets()));
	connect(d->m_view->radioPhotoCustom, SIGNAL(toggled(bool )), this, SLOT(slotEnableAndDisableWidgets()));
	connect(d->m_view->radioPhotoKABC, SIGNAL(toggled(bool )), this, SLOT(slotEnableAndDisableWidgets()));

	connect(d->m_view->checkSyncPhotoKABC, SIGNAL(toggled(bool )), this, SLOT(slotSettingsChanged()));
	connect(d->m_view->lineNickname, SIGNAL(textChanged(const QString& )), this, SLOT(slotSettingsChanged()));
	connect(d->m_view->comboNameContact, SIGNAL(activated(int )), this, SLOT(slotSettingsChanged()));
	connect(d->m_view->comboPhotoContact, SIGNAL(activated(int )), this, SLOT(slotEnableAndDisableWidgets()));
}

KopeteIdentityConfig::~KopeteIdentityConfig()
{
	delete d;
}

void KopeteIdentityConfig::load()
{
	KCModule::load();

	// Populate the select Identity combo box.
	loadIdentities();
	// Populate the name contact ComboBox
	slotLoadNameSources();
	// Populate the photo contact ComboBOx
	slotLoadPhotoSources();

	KABC::Addressee a = KABC::StdAddressBook::self()->whoAmI();
	// Load the address book link
	if (!a.isEmpty())
	{
		d->m_view->lineAddressee->setText(a.realName());
	}

	slotEnableAndDisableWidgets();
}

void KopeteIdentityConfig::save()
{
	KCModule::save();

	saveCurrentIdentity();

	// Don't save the new global identity if it's not activated.
	if(d->m_view->kcfg_EnableGlobalIdentity->isChecked())
	{
		// Save the myself metacontact settings.
		// Nickname settings.
		if(d->m_view->lineNickname->text() != d->myself->customDisplayName())
			d->myself->setDisplayName(d->m_view->lineNickname->text());
		
		d->myself->setDisplayNameSource(selectedNameSource());
		d->myself->setDisplayNameSourceContact(selectedNameSourceContact());

		// Photo settings
		d->myself->setPhotoSource(selectedPhotoSource());
		d->myself->setPhotoSourceContact(selectedPhotoSourceContact());
		if(!d->m_view->comboPhotoURL->url().isEmpty())
			d->myself->setPhoto(d->m_view->comboPhotoURL->url());
		else
			d->myself->setPhoto( KURL() );
		d->myself->setPhotoSyncedWithKABC(d->m_view->checkSyncPhotoKABC->isChecked());
	}
	
	// Save global identities list.
	KopeteIdentityConfigPreferences::self()->setSelectedIdentity(d->selectedIdentity);
	GlobalIdentitiesManager::self()->saveXML();

	// (Re)made slot connections to apply Global Identity in protocols
	Kopete::ContactList::self()->loadGlobalIdentity();

	load();
}

void KopeteIdentityConfig::loadIdentities()
{
	d->m_view->comboSelectIdentity->clear();

	QMap<QString, Kopete::MetaContact*> identitiesList = GlobalIdentitiesManager::self()->getGlobalIdentitiesList();
	QMapIterator<QString, Kopete::MetaContact*> it;
	QMapIterator<QString, Kopete::MetaContact*> end = identitiesList.end();

	int count=0, selectedIndex=0;
	for(it = identitiesList.begin(); it != end; ++it)
	{
		d->m_view->comboSelectIdentity->insertItem(it.key());
		if(it.key() == d->selectedIdentity)
		{
			selectedIndex = count;
		}
		count++;
	}

	d->m_view->comboSelectIdentity->setCurrentItem(selectedIndex);
	d->m_view->buttonRemoveIdentity->setEnabled(count == 1 ? false : true);
}

void KopeteIdentityConfig::saveCurrentIdentity()
{
	kdDebug() << k_funcinfo << "Saving data of current identity." << endl;
	// Ignore saving when removing a identity
	if(!d->currentIdentity)
		return;

	if(d->m_view->lineNickname->text() != d->currentIdentity->customDisplayName())
		d->currentIdentity->setDisplayName(d->m_view->lineNickname->text());
		
	d->currentIdentity->setDisplayNameSource(selectedNameSource());
	d->currentIdentity->setDisplayNameSourceContact(selectedNameSourceContact());

	// Photo settings
	d->currentIdentity->setPhotoSource(selectedPhotoSource());
	d->currentIdentity->setPhotoSourceContact(selectedPhotoSourceContact());
	if(!d->m_view->comboPhotoURL->url().isEmpty())
		d->currentIdentity->setPhoto(d->m_view->comboPhotoURL->url());
	else
		d->currentIdentity->setPhoto( KURL() );
	d->currentIdentity->setPhotoSyncedWithKABC(d->m_view->checkSyncPhotoKABC->isChecked());
}

void KopeteIdentityConfig::slotLoadNameSources()
{
	Kopete::Contact *nameSourceContact = d->currentIdentity->displayNameSourceContact();

	QPtrList<Kopete::Contact> contactList = d->myself->contacts(); // Use myself contact PtrList. Safer.
	QPtrListIterator<Kopete::Contact> it(contactList);

	d->m_view->comboNameContact->clear();

	for(; it.current(); ++it)
	{
		QString account = it.current()->property(Kopete::Global::Properties::self()->nickName()).value().toString() + " <" + it.current()->contactId() + ">";
		QPixmap accountIcon = it.current()->account()->accountIcon();
		d->m_view->comboNameContact->insertItem(accountIcon,  account);

		// Select this item if it's the one we're tracking.
		if(it.current() == nameSourceContact)
		{
			d->m_view->comboNameContact->setCurrentItem(d->m_view->comboNameContact->count() - 1);
		}
	}

	d->m_view->lineNickname->setText(d->currentIdentity->customDisplayName());

	Kopete::MetaContact::PropertySource nameSource = d->currentIdentity->displayNameSource();

	d->m_view->radioNicknameCustom->setChecked(nameSource == Kopete::MetaContact::SourceCustom);
	d->m_view->radioNicknameKABC->setChecked(nameSource == Kopete::MetaContact::SourceKABC);
	d->m_view->radioNicknameContact->setChecked(nameSource == Kopete::MetaContact::SourceContact);
}

void KopeteIdentityConfig::slotLoadPhotoSources()
{
	Kopete::Contact *photoSourceContact = d->currentIdentity->photoSourceContact();

	QPtrList<Kopete::Contact> contactList = d->myself->contacts(); // Use myself contact PtrList. Safer.
	QPtrListIterator<Kopete::Contact> it(contactList);

	d->m_view->comboPhotoContact->clear();
	d->m_view->comboPhotoURL->clear();
	d->contactPhotoSourceList.clear();

	for(; it.current(); ++it)
	{
		Kopete::Contact *currentContact = it.current();
		if(currentContact->hasProperty(Kopete::Global::Properties::self()->photo().key()))
		{
			QString account = currentContact->property(Kopete::Global::Properties::self()->nickName()).value().toString() + " <" + currentContact->contactId() + ">";
			QPixmap accountIcon = currentContact->account()->accountIcon();

			d->m_view->comboPhotoContact->insertItem(accountIcon,  account);
			d->contactPhotoSourceList.insert(d->m_view->comboPhotoContact->count() - 1, currentContact);

			// Select this item if it's the one we're tracking.
			if(currentContact == photoSourceContact)
			{
				d->m_view->comboPhotoContact->setCurrentItem(d->m_view->comboPhotoContact->count() - 1);
			}
		}
	}

	d->m_view->comboPhotoURL->setURL(d->currentIdentity->customPhoto().pathOrURL());
	Kopete::MetaContact::PropertySource photoSource = d->currentIdentity->photoSource();

	d->m_view->radioPhotoCustom->setChecked(photoSource == Kopete::MetaContact::SourceCustom);
	d->m_view->radioPhotoContact->setChecked(photoSource == Kopete::MetaContact::SourceContact);
	d->m_view->radioPhotoKABC->setChecked(photoSource == Kopete::MetaContact::SourceKABC);

	d->m_view->checkSyncPhotoKABC->setChecked(d->currentIdentity->isPhotoSyncedWithKABC());
}

void KopeteIdentityConfig::slotEnableAndDisableWidgets()
{
	KABC::Addressee a = KABC::StdAddressBook::self()->whoAmI();
	bool hasKABCLink = !a.isEmpty();

	d->m_view->radioNicknameKABC->setEnabled(hasKABCLink);
	d->m_view->radioPhotoKABC->setEnabled(hasKABCLink);

	// Don't sync global photo with KABC if KABC is the source
	// or if they are no KABC link. (would create a break in timeline)
	if( selectedPhotoSource() == Kopete::MetaContact::SourceKABC || !hasKABCLink )
	{
		d->m_view->checkSyncPhotoKABC->setEnabled(false);
	}
	else
	{
		d->m_view->checkSyncPhotoKABC->setEnabled(true);
	}

	d->m_view->radioNicknameContact->setEnabled(d->currentIdentity->contacts().count());
	d->m_view->radioPhotoContact->setEnabled(!d->contactPhotoSourceList.isEmpty());

	d->m_view->comboNameContact->setEnabled(selectedNameSource() == Kopete::MetaContact::SourceContact);
	d->m_view->lineNickname->setEnabled(selectedNameSource() == Kopete::MetaContact::SourceCustom);

	d->m_view->comboPhotoContact->setEnabled(selectedPhotoSource() == Kopete::MetaContact::SourceContact);
	d->m_view->comboPhotoURL->setEnabled(selectedPhotoSource() == Kopete::MetaContact::SourceCustom);

	if(d->contactPhotoSourceList.isEmpty() )
	{
		d->m_view->comboPhotoContact->clear();
		d->m_view->comboPhotoContact->insertItem(i18n("No Contacts with Photo Support"));
		d->m_view->comboPhotoContact->setEnabled(false);
	}

	QImage photo;
	switch ( selectedPhotoSource() )
	{
		case Kopete::MetaContact::SourceKABC:
			photo = Kopete::photoFromKABC(a.uid());
			break;
		case Kopete::MetaContact::SourceContact:
			photo = Kopete::photoFromContact(selectedNameSourceContact());
			break;
		case Kopete::MetaContact::SourceCustom:
			photo = QImage(d->m_view->comboPhotoURL->url());
			break;
	}

	if(!photo.isNull())
		d->m_view->labelPhoto->setPixmap(QPixmap(photo.smoothScale(64, 92, QImage::ScaleMin)));
	else
		d->m_view->labelPhoto->setPixmap(QPixmap());

	emit changed(true);
}

void KopeteIdentityConfig::slotUpdateCurrentIdentity(const QString &selectedIdentity)
{
	kdDebug() << k_funcinfo << "Updating current identity." << endl;

	// Save the current identity detail, so we don't loose information.
	saveCurrentIdentity();

	// Change the current identity reflecting the combo box.
	d->selectedIdentity = selectedIdentity;
	d->currentIdentity = GlobalIdentitiesManager::self()->getIdentity(d->selectedIdentity);
	KopeteIdentityConfigPreferences::self()->setSelectedIdentity(d->selectedIdentity);
	KopeteIdentityConfigPreferences::self()->writeConfig();
	// Save global identities list.
	GlobalIdentitiesManager::self()->saveXML();

	// Reload the details.
	slotLoadNameSources();
	slotLoadPhotoSources();
}
	
void KopeteIdentityConfig::slotNewIdentity()
{
	bool ok;
	QString newIdentityName = KInputDialog::getText(i18n("New Identity"), i18n("Identity name:") , QString::null , &ok);
	
	if(newIdentityName.isEmpty() || !ok)
		return;

	
	GlobalIdentitiesManager::self()->createNewIdentity(newIdentityName);

	slotUpdateCurrentIdentity(newIdentityName);
	loadIdentities();
}

void KopeteIdentityConfig::slotCopyIdentity()
{
	bool ok;
	QString copyName = KInputDialog::getText(i18n("Copy Identity"), i18n("Identity name:") , QString::null, &ok);
	
	if(copyName.isEmpty() || !ok)
		return;

	
	if(!GlobalIdentitiesManager::self()->isIdentityPresent(copyName))
	{
		GlobalIdentitiesManager::self()->copyIdentity(copyName, d->selectedIdentity);
		
		slotUpdateCurrentIdentity(copyName);
		loadIdentities();
	}
	else
	{
		KMessageBox::error(this, i18n("An identity with the same name was found."), i18n("Identity Configuration"));
	}
}

void KopeteIdentityConfig::slotRenameIdentity()
{
	if(d->selectedIdentity.isNull())
		return;
	
	bool ok;
	QString renamedName = KInputDialog::getText(i18n("Rename Identity"), i18n("Identity name:") , d->selectedIdentity, &ok);

	if(renamedName.isEmpty() || !ok)
		return;


	if(renamedName.isEmpty())
		return;

	if(!GlobalIdentitiesManager::self()->isIdentityPresent(renamedName))
	{
		GlobalIdentitiesManager::self()->renameIdentity(d->selectedIdentity, renamedName);
		
		slotUpdateCurrentIdentity(renamedName);
		loadIdentities();
	}
	else
	{
		KMessageBox::error(this, i18n("An identity with the same name was found."), i18n("Identity Configuration"));
	}
}

void KopeteIdentityConfig::slotRemoveIdentity()
{
	kdDebug() << k_funcinfo << "Removing current identity." << endl;
	GlobalIdentitiesManager::self()->removeIdentity(d->selectedIdentity);
	// Reset the currentIdentity pointer. The currentIdentity object was deleted in GlobalIdentitiesManager.
	d->currentIdentity = 0;
	
	// Select the entry before(or after) the removed identity.
	int currentItem = d->m_view->comboSelectIdentity->currentItem();
	// Use the next item if the removed identity is the first in the comboBox.
	if(currentItem - 1 < 0)
	{
		currentItem++;
	}
	else
	{
		currentItem--;
	}
	d->m_view->comboSelectIdentity->setCurrentItem(currentItem);

	slotUpdateCurrentIdentity(d->m_view->comboSelectIdentity->currentText());
	loadIdentities();
}



void KopeteIdentityConfig::slotChangeAddressee()
{
	KABC::Addressee a = Kopete::UI::AddressBookSelectorDialog::getAddressee(i18n("Addressbook Association"), i18n("Choose the person who is yourself."), d->myself->metaContactId(), this);

	if ( !a.isEmpty() )
	{
		d->m_view->lineAddressee->setText(a.realName());
		KABC::StdAddressBook::self()->setWhoAmI(a);
		d->myself->setMetaContactId(a.uid());
	}

	emit changed(true);
}

void KopeteIdentityConfig::slotChangePhoto(const QString &photoUrl)
{
	QString saveLocation;
	
	QImage photo(photoUrl);
	// use KABC photo size 100x140
	photo = KPixmapRegionSelectorDialog::getSelectedImage( QPixmap(photo), 96, 96, this );

	if(!photo.isNull())
	{
		if(photo.width() > 96 || photo.height() > 96)
		{
			// Scale and crop the picture.
			photo = photo.smoothScale( 96, 96, QImage::ScaleMin );
			// crop image if not square
			if(photo.width() < photo.height()) 
				photo = photo.copy((photo.width()-photo.height())/2, 0, 96, 96);
			else if (photo.width() > photo.height())
				photo = photo.copy(0, (photo.height()-photo.width())/2, 96, 96);

		}
		else if (photo.width() < 32 || photo.height() < 32)
		{
			// Scale and crop the picture.
			photo = photo.smoothScale( 32, 32, QImage::ScaleMin );
			// crop image if not square
			if(photo.width() < photo.height())
				photo = photo.copy((photo.width()-photo.height())/2, 0, 32, 32);
			else if (photo.width() > photo.height())
				photo = photo.copy(0, (photo.height()-photo.width())/2, 32, 32);
	
		}
		else if (photo.width() != photo.height())
		{
			if(photo.width() < photo.height())
				photo = photo.copy((photo.width()-photo.height())/2, 0, photo.height(), photo.height());
			else if (photo.width() > photo.height())
				photo = photo.copy(0, (photo.height()-photo.width())/2, photo.height(), photo.height());
		}

		// Use MD5 hash to save the filename, so no problems will occur with the filename because of non-ASCII characters.
		// Bug 124175: My personnal picture doesn't appear cause of l10n
		QByteArray tempArray;
		QBuffer tempBuffer(tempArray);
		tempBuffer.open( IO_WriteOnly );
		photo.save(&tempBuffer, "PNG");
		KMD5 context(tempArray);
		// Save the image to a file.
		saveLocation = context.hexDigest() + ".png";
		saveLocation = locateLocal( "appdata", QString::fromUtf8("globalidentitiespictures/%1").arg( saveLocation ) );

		if(!photo.save(saveLocation, "PNG"))
		{
			KMessageBox::sorry(this, 
					i18n("An error occurred when trying to save the custom photo for %1 identity.").arg(d->selectedIdentity),
					i18n("Identity Configuration"));
		}
		d->m_view->comboPhotoURL->setURL(saveLocation);
		slotEnableAndDisableWidgets();
	}
	else
	{
		KMessageBox::sorry(this, 
					i18n("An error occurred when trying to save the custom photo for %1 identity.").arg(d->selectedIdentity),
					i18n("Identity Configuration"));
	}
}

void KopeteIdentityConfig::slotClearPhoto()
{
	d->m_view->comboPhotoURL->setURL( QString::null );
	slotEnableAndDisableWidgets();
}

void KopeteIdentityConfig::slotSettingsChanged()
{
	emit changed(true);
}

Kopete::MetaContact::PropertySource KopeteIdentityConfig::selectedNameSource() const
{
	if (d->m_view->radioNicknameKABC->isChecked())
		return Kopete::MetaContact::SourceKABC;
	if (d->m_view->radioNicknameContact->isChecked())
		return Kopete::MetaContact::SourceContact;
	if (d->m_view->radioNicknameCustom->isChecked())
		return Kopete::MetaContact::SourceCustom;
	else
		return Kopete::MetaContact::SourceCustom;
}

Kopete::MetaContact::PropertySource KopeteIdentityConfig::selectedPhotoSource() const
{
	if (d->m_view->radioPhotoKABC->isChecked())
		return Kopete::MetaContact::SourceKABC;
	if (d->m_view->radioPhotoContact->isChecked())
		return Kopete::MetaContact::SourceContact;
	if (d->m_view->radioPhotoCustom->isChecked())
		return Kopete::MetaContact::SourceCustom;
	else
		return Kopete::MetaContact::SourceCustom;
}

Kopete::Contact* KopeteIdentityConfig::selectedNameSourceContact() const
{
	Kopete::Contact *c = d->myself->contacts().at(d->m_view->comboNameContact->currentItem());
	return c ? c : 0L;
}

Kopete::Contact* KopeteIdentityConfig::selectedPhotoSourceContact() const
{
	if (d->contactPhotoSourceList.isEmpty())
		return 0L;

	Kopete::Contact *c = d->contactPhotoSourceList[d->m_view->comboPhotoContact->currentItem()];
	return c ? c : 0L;
}

#include "kopeteidentityconfig.moc"