summaryrefslogtreecommitdiffstats
path: root/kopete/kopete/addcontactwizard/fastaddcontactwizard.cpp
blob: ecddf632fd3da0b976ff373937700ebaa173dc19 (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
/*
    fastaddcontactwizard.cpp - Kopete's FastAdd Contact Wizard

	Copyright (c) 2003 by Will Stephenson		 <will@stevello.free-online.co.uk>
	Derived from AddContactWizard
    Copyright (c) 2002 by Nick Betcher           <nbetcher@kde.org>
    Copyright (c) 2002 by Duncan Mac-Vicar Prett <duncan@kde.org>

    Kopete    (c) 2002 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 <tqptrlist.h>
#include <addcontactpage.h>

#include <kiconloader.h>
#include <klocale.h>

#include "kopetecontactlist.h"
#include "kopetemetacontact.h"
#include "kopeteaccountmanager.h"
#include "kopeteaccount.h"

#include "fastaddcontactwizard.h"

FastAddContactWizard::FastAddContactWizard( TQWidget *parent, const char *name )
: FastAddContactWizard_Base( parent, name )
{
	m_accountItems.clear();

	// Populate the accounts list
	TQListViewItem* accountLVI = 0L;
	TQPtrList<Kopete::Account>  accounts = Kopete::AccountManager::self()->accounts();
	for(Kopete::Account *i=accounts.first() ; i; i=accounts.next() )
	{
		accountLVI= new TQListViewItem( protocolListView, i->accountLabel() );
		accountLVI->setText(1,i->protocol()->displayName() + TQString::tqfromLatin1(" ") );
		accountLVI->setPixmap( 1, SmallIcon( i->protocol()->pluginIcon() ) );
		m_accountItems.insert(accountLVI,i);
	}

	if ( accounts.count() == 1 )
		protocolListView->setCurrentItem( accountLVI );

	// Account choice validation connections
	connect( protocolListView, TQT_SIGNAL(clicked(TQListViewItem *)), this, TQT_SLOT(slotProtocolListClicked(TQListViewItem *)));
	connect( protocolListView, TQT_SIGNAL(selectionChanged(TQListViewItem *)), this, TQT_SLOT(slotProtocolListClicked(TQListViewItem *)));
	connect( protocolListView, TQT_SIGNAL(spacePressed(TQListViewItem *)), this, TQT_SLOT(slotProtocolListClicked(TQListViewItem *)));

	setNextEnabled( selectService, false );
	setFinishEnabled(finis, true);
}

FastAddContactWizard::~FastAddContactWizard()
{
}

void FastAddContactWizard::slotProtocolListClicked( TQListViewItem *account)
{
	setNextEnabled( selectService, account? account->isSelected() : false );
}

void FastAddContactWizard::next()
{
	// If we're on the select account page
	// follow it with the add contact page for
	// the chosen protocol
	if ( currentPage() == selectService )
	{
		TQMap <Kopete::Account*,AddContactPage*>::Iterator it;
		for ( it = protocolPages.begin(); it != protocolPages.end(); ++it )
		{
			delete it.data();
		}
		protocolPages.clear();

		TQListViewItem* item = protocolListView->selectedItem();
		AddContactPage *addPage = m_accountItems[item]->protocol()->createAddContactWidget(this, m_accountItems[item] );
		if (addPage)
		{
			TQString title = i18n( "The account name is prepended here",
								 "%1 contact information" )
								 .tqarg( item->text(0) );
			addPage->show();
			insertPage( addPage, title, indexOf( finis ) );
			protocolPages.insert( m_accountItems[item] , addPage );
		}
		TQWizard::next();
		return;
	}

	// If we're not on any account specific pages,
	// we must be on an add account page, so make sure it validates
	if ( currentPage() != selectService && currentPage() != finis )
	{
		AddContactPage *ePage = dynamic_cast<AddContactPage *>(currentPage());
		if (!ePage || !ePage->validateData())
			return;
	}

	TQWizard::next();
}

void FastAddContactWizard::accept()
{
	Kopete::MetaContact *metaContact = new Kopete::MetaContact();

	metaContact->addToGroup( Kopete::Group::topLevel() );

	bool ok = protocolPages.isEmpty();

	// get each protocol's contact
	TQMap <Kopete::Account*,AddContactPage*>::Iterator it;
	for ( it = protocolPages.begin(); it != protocolPages.end(); ++it )
		ok |= it.data()->apply( it.key(), metaContact );

	if ( ok )
	{
		// add it to the contact list
		Kopete::ContactList::self()->addMetaContact( metaContact );
	}
	else
		delete metaContact;

	deleteLater();
}

#include "fastaddcontactwizard.moc"