summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/ui/jabberaddcontactpage.cpp
blob: 0f86a79b43d0c2512e2454b74acd538897c998ce (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

/***************************************************************************
                          jabberaddcontactpage.cpp  -  Add contact widget
                             -------------------
    begin                : Thu Aug 08 2002
    copyright            : (C) 2003 by Till Gerken <till@tantalo.net>
                           (C) 2003 by Daniel Stone <dstone@kde.org>
                           (C) 2006 by Olivier Goffart <ogoffart at kde.org>
    email                : 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 "jabberaddcontactpage.h"

#include <tqlayout.h>
#include <klineedit.h>
#include <klocale.h>
#include <kopeteaccount.h>
#include <tqlabel.h>
#include <kopetegroup.h>
#include <kopetemetacontact.h>

#include "dlgaddcontact.h"
#include "jabberaccount.h"
#include "jabbertransport.h"
#include "kopetecontact.h"
#include "jabberclient.h"
#include "xmpp_tasks.h"

JabberAddContactPage::JabberAddContactPage (Kopete::Account * owner, TQWidget * tqparent, const char *name):AddContactPage (tqparent, name)
{
	(new TQVBoxLayout (this))->setAutoAdd (true);
	
	JabberTransport *transport=dynamic_cast<JabberTransport*>(owner);
	JabberAccount *jaccount= transport ? transport->account() : dynamic_cast<JabberAccount*>(owner);
	
	if (jaccount->isConnected ())
	{
		jabData = new dlgAddContact (this);
		jabData->show ();
		
		if(transport)
		{
			jabData->textLabel1->setText( i18n("Loading instruction from gateway...") );
			XMPP::JT_Gateway * gatewayTask = new XMPP::JT_Gateway ( jaccount->client()->rootTask () );
			TQObject::connect (gatewayTask, TQT_SIGNAL (finished ()), this, TQT_SLOT (slotPromtReceived()));
			gatewayTask->get ( transport->myself()->contactId() );
			gatewayTask->go ( true );
		}
		canadd = true;
	}
	else
	{
		noaddMsg1 = new TQLabel (i18n ("You need to be connected to be able to add contacts."), this);
		noaddMsg2 = new TQLabel (i18n ("Connect to the Jabber network and try again."), this);
		canadd = false;
	}

}

JabberAddContactPage::~JabberAddContactPage ()
{
}

bool JabberAddContactPage::validateData ()
{
	return true;
}


bool JabberAddContactPage::apply ( Kopete::Account *account, Kopete::MetaContact *parentContact )
{

	if( canadd && validateData () )
	{
		JabberTransport *transport=dynamic_cast<JabberTransport*>(account);
		JabberAccount *jaccount=transport?transport->account():dynamic_cast<JabberAccount*>(account);
				
		TQString contactId = jabData->addID->text ();
		
		if(transport)
		{
			XMPP::JT_Gateway * gatewayTask = new XMPP::JT_Gateway ( jaccount->client()->rootTask () );
			JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND *workaround = 
					new JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND( transport , parentContact , gatewayTask );
			TQObject::connect (gatewayTask, TQT_SIGNAL (finished ()), workaround, TQT_SLOT (slotJidReceived()));
			gatewayTask->set ( transport->myself()->contactId() , contactId );
			gatewayTask->go ( true );
			return true;
		}
		
		TQString displayName = parentContact->displayName ();
		/*		
		if ( displayName.isEmpty () )
			displayName = contactId;
		*/
		// collect all group names
		TQStringList groupNames;
		Kopete::GroupList groupList = parentContact->groups();
		for(Kopete::Group *group = groupList.first(); group; group = groupList.next())
			groupNames += group->displayName();

		if ( jaccount->addContact ( contactId, parentContact, Kopete::Account::ChangeKABC ) )
		{
			XMPP::RosterItem item;
			XMPP::Jid jid ( contactId );

			item.setJid ( jid );
			item.setName ( displayName );
			item.setGroups ( groupNames );

			// add the new contact to our roster.
			XMPP::JT_Roster * rosterTask = new XMPP::JT_Roster ( jaccount->client()->rootTask () );

			rosterTask->set ( item.jid(), item.name(), item.groups() );
			rosterTask->go ( true );

			// send a subscription request.
			XMPP::JT_Presence *presenceTask = new XMPP::JT_Presence ( jaccount->client()->rootTask () );

			presenceTask->sub ( jid, "subscribe" );
			presenceTask->go ( true );

			return true;
		}
	}

	return false;
}

void JabberAddContactPage::slotPromtReceived( )
{
	XMPP::JT_Gateway * task = (XMPP::JT_Gateway *) sender ();

	if (task->success ())
	{
		jabData->lblID->setText( task->prompt() );
		jabData->textLabel1->setText( task->desc() );
	}
	else
	{
		jabData->textLabel1->setText( i18n("An error occured while loading instructions from gateway.") );
	}
}

JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND::JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND( JabberTransport *t, Kopete::MetaContact * mc, TQObject* task )
	: TQObject(task) , metacontact(mc) ,  transport(t)
{}

void JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND::slotJidReceived( )
{
	XMPP::JT_Gateway * task = (XMPP::JT_Gateway *) sender ();
	
	if (!task->success ())
	{
		return;
		// maybe we should show an error message, but i don't like showing error message  - Olivier
	}

	TQString contactId=task->prompt();
	
	Kopete::MetaContact* parentContact=metacontact;
	JabberAccount *jaccount=transport->account();;
	
	/*\
	 *   this is a copy of the end of JabberAddContactPage::apply
	\*/
	
	TQString displayName = parentContact->displayName ();
		/*		
	if ( displayName.isEmpty () )
	displayName = contactId;
		*/
		// collect all group names
	TQStringList groupNames;
	Kopete::GroupList groupList = parentContact->groups();
	for(Kopete::Group *group = groupList.first(); group; group = groupList.next())
		groupNames += group->displayName();

	if ( jaccount->addContact ( contactId, parentContact, Kopete::Account::ChangeKABC ) )
	{
		XMPP::RosterItem item;
		XMPP::Jid jid ( contactId );

		item.setJid ( jid );
		item.setName ( displayName );
		item.setGroups ( groupNames );

			// add the new contact to our roster.
		XMPP::JT_Roster * rosterTask = new XMPP::JT_Roster ( jaccount->client()->rootTask () );

		rosterTask->set ( item.jid(), item.name(), item.groups() );
		rosterTask->go ( true );

			// send a subscription request.
		XMPP::JT_Presence *presenceTask = new XMPP::JT_Presence ( jaccount->client()->rootTask () );

		presenceTask->sub ( jid, "subscribe" );
		presenceTask->go ( true );

		return;
	}
}



#include "jabberaddcontactpage.moc"

/*
 * Local variables:
 * c-indentation-style: k&r
 * c-basic-offset: 8
 * indent-tabs-mode: t
 * End:
 */
// vim: set noet ts=4 sts=4 sw=4: