summaryrefslogtreecommitdiffstats
path: root/kopete/kopete/contactlist/tdeabcexport.cpp
blob: 6e67be96ba591dde2263181dd091e1383a4963b6 (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
/*
    tdeabcexport.cpp - Export Contacts to Address Book Wizard for Kopete

    Copyright (c) 2005 by Will Stephenson        <will@stevello.free-online.co.uk>
    Resource selector taken from KRES::SelectDialog
    Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
    Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
    Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>

    Kopete    (c) 2002-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 <tqpushbutton.h>
#include <tqlistbox.h>
#include <tqlistview.h>
#include <tqptrlist.h>
#include <tqmap.h>

#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdeabc/addressee.h>
#include <tdeabc/addressbook.h>
#include <tdeabc/phonenumber.h>
#include <tdeabc/picture.h>
#include <tdeabc/resource.h>
#include <tdeabc/stdaddressbook.h>

#include <tdeabcpersistence.h>
#include <kopetecontact.h>
#include <kopetecontactlist.h>
#include <kopetecontactproperty.h>
#include <kopeteglobal.h>
#include <kopetemetacontact.h>

#include "tdeabcpersistence.h"

#include "tdeabcexport.h"

class ContactLVI : public TQCheckListItem
{
	public:
		ContactLVI ( Kopete::MetaContact * mc, TQListView * parent, const TQString & text, Type tt = RadioButtonController ) : TQCheckListItem( parent, text, tt ), mc( mc )
		{	}
		Kopete::MetaContact * mc;
		TQString uid;
};

// ctor populates the resource list and contact list, and enables the next button on the first page 
KabcExportWizard::KabcExportWizard( TQWidget *parent, const char *name )
	: KabcExportWizard_Base( parent, name )
{
	connect( m_addrBooks, TQ_SIGNAL( selectionChanged( TQListBoxItem * ) ), TQ_SLOT( slotResourceSelectionChanged( TQListBoxItem * ) ) );

	connect( m_btnSelectAll, TQ_SIGNAL( clicked() ), TQ_SLOT( slotSelectAll() ) );
	connect( m_btnDeselectAll, TQ_SIGNAL( clicked() ), TQ_SLOT( slotDeselectAll() ) );
	
	// fill resource selector
	m_addressBook = Kopete::KABCPersistence::self()->addressBook();

	TQPtrList<TDEABC::Resource> tdeabcResources = m_addressBook->resources();

	TQPtrListIterator<TDEABC::Resource> resIt( tdeabcResources );
	TDEABC::Resource *resource;
	
	uint counter = 0;
	while ( ( resource = resIt.current() ) != 0 ) 
	{
		++resIt;
		if ( !resource->readOnly() ) 
		{
			m_resourceMap.insert( counter, resource );
			m_addrBooks->insertItem( resource->resourceName() );
			counter++;
		}
	}
	setNextEnabled( TQWizard::page( 0 ), false );
	setFinishEnabled( TQWizard::page( 1 ), true );
	// if there were no writable address books, tell the user
	if ( counter == 0 )
	{
		m_addrBooks->insertItem( i18n( "No writeable addressbook resource found." ) );
		m_addrBooks->insertItem( i18n( "Add or enable one using the Trinity Control Center." ) );
		m_addrBooks->setEnabled( false );
	}

	if ( m_addrBooks->count() == 1 )
		m_addrBooks->setSelected( 0, true );
	
	// fill contact list
	TQPtrList<Kopete::MetaContact> contacts = Kopete::ContactList::self()->metaContacts();
	TQPtrListIterator<Kopete::MetaContact> it( contacts );
	counter = 0;
	TQString alreadyIn = i18n( " (already in address book)" );
	for (; it.current(); ++it)
	{
		m_contactMap.insert( counter, it.current() );
		TQCheckListItem * lvi = new ContactLVI( it.current(), m_contactList,
				it.current()->displayName(), TQCheckListItem::CheckBox );
		lvi->setOn( false );
		if ( it.current()->metaContactId().contains(':') )
		{
			lvi->setOn( true );
			lvi->setEnabled( true );
		}
		else
			lvi->setText( 0, lvi->text( 0 ) + alreadyIn );
	}
}

KabcExportWizard::~KabcExportWizard()
{
	
}

void KabcExportWizard::slotDeselectAll()
{
	TQListViewItemIterator it( m_contactList );
	while ( it.current() )
	{
		ContactLVI *item = static_cast<ContactLVI *>( it.current() );
		item->setOn( false );
		++it;
	}
}

void KabcExportWizard::slotSelectAll()
{
	TQListViewItemIterator it( m_contactList );
	while ( it.current() )
	{
		ContactLVI *item = static_cast<ContactLVI *>( it.current() );
		++it;
		if ( !item->isEnabled() )
			continue;
		item->setOn( true );
	}
}

void KabcExportWizard::slotResourceSelectionChanged( TQListBoxItem * lbi )
{
	setNextEnabled( TQWizard::page( 0 ), lbi->isSelected() );
}

// accept runs the export algorithm
void KabcExportWizard::accept()
{
	// first add an addressee to the selected resource 
	// then set the metacontactId of each MC to that of the new addressee
	TDEABC::Resource * selectedResource = 
			m_resourceMap[ ( m_addrBooks->index( m_addrBooks->selectedItem() ) ) ];
	// for each item checked
	{
		TQListViewItemIterator it( m_contactList );
		while ( it.current() )
		{
			ContactLVI *item = static_cast<ContactLVI *>( it.current() );
			// if it is checked and enabled
			if ( item->isEnabled() && item->isOn() )
			{
				TDEABC::Addressee addr;
				addr = m_addressBook->findByUid( item->mc->metaContactId() );
				if ( addr.isEmpty() ) // unassociated contact
				{
					kdDebug( 14000 ) << "creating addressee " << item->mc->displayName() << " in address book " << selectedResource->resourceName() << endl;
					// create a new addressee in the selected resource
					addr.setResource( selectedResource );

					// set name
					TQPtrList<Kopete::Contact> contacts = item->mc->contacts();
					if ( contacts.count() == 1 )
					{
						Kopete::ContactProperty prop;
						prop = contacts.first()->property(
								Kopete::Global::Properties::self()->fullName() );
						if ( prop.isNull() )
							addr.setNameFromString( item->mc->displayName() );
						else
							addr.setNameFromString(  prop.value().toString() );
					}
					else
						addr.setNameFromString( item->mc->displayName() );

					// set details
					exportDetails( item->mc, addr );
					m_addressBook->insertAddressee( addr );
					// set the metacontact's id to that of the new addressee 
					// - this causes the addressbook to be written by libkopete
					item->mc->setMetaContactId( addr.uid() );
				}
				else
				{
					exportDetails( item->mc, addr );
					m_addressBook->insertAddressee( addr );
				}
			}
			++it;
		}
	}
	// request a write in case we only changed details on existing linked addressee
	Kopete::KABCPersistence::self()->writeAddressBook( selectedResource );
	TQDialog::accept();
}

void KabcExportWizard::exportDetails( Kopete::MetaContact * mc, TDEABC::Addressee & addr )
{
	// for each contact
	TQPtrList<Kopete::Contact> contacts = mc->contacts();
	TQPtrListIterator<Kopete::Contact> cit( contacts );
	for( ; cit.current(); ++cit )
	{
		Kopete::ContactProperty prop;
		prop = (*cit)->property( Kopete::Global::Properties::self()->emailAddress() );
		if ( !prop.isNull() )
		{
			addr.insertEmail( prop.value().toString() );
		}
		prop = (*cit)->property( Kopete::Global::Properties::self()->privatePhone() );
		if ( !prop.isNull() )
		{
			addr.insertPhoneNumber( TDEABC::PhoneNumber( prop.value().toString(), TDEABC::PhoneNumber::Home ) );
		}
		prop = (*cit)->property( Kopete::Global::Properties::self()->workPhone() );
		if ( !prop.isNull() )
		{
			addr.insertPhoneNumber( TDEABC::PhoneNumber( prop.value().toString(), TDEABC::PhoneNumber::Work ) );
		}
		prop = (*cit)->property( Kopete::Global::Properties::self()->privateMobilePhone() );
		if ( !prop.isNull() )
		{
			addr.insertPhoneNumber( TDEABC::PhoneNumber( prop.value().toString(), TDEABC::PhoneNumber::Cell ) );
		}
	
	}
	// metacontact photo
	TQImage photo = mc->photo();
	if ( !photo.isNull() )
		addr.setPhoto( TDEABC::Picture( photo ) );
}

#include "tdeabcexport.moc"