1
2 /***************************************************************************
3 jabberaddcontactpage.cpp - Add contact widget
4 -------------------
5 begin : Thu Aug 08 2002
6 copyright : (C) 2003 by Till Gerken <till@tantalo.net>
7 (C) 2003 by Daniel Stone <dstone@kde.org>
8 (C) 2006 by Olivier Goffart <ogoffart at kde.org>
9 email : kopete-devel@kde.org
10 ***************************************************************************/
11
12 /***************************************************************************
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 ***************************************************************************/
20
21 #include "jabberaddcontactpage.h"
22
23 #include <qlayout.h>
24 #include <QLineEdit>
25 #include <KLocalizedString>
26 #include <kopeteaccount.h>
27 #include <qlabel.h>
28 #include <kopetegroup.h>
29 #include <kopetemetacontact.h>
30
31 #include "ui_dlgaddcontact.h"
32 #include "jabberaccount.h"
33 #include "jabbertransport.h"
34 #include "kopetecontact.h"
35 #include "jabberclient.h"
36 #include "xmpp_tasks.h"
37
JabberAddContactPage(Kopete::Account * owner,QWidget * parent)38 JabberAddContactPage::JabberAddContactPage (Kopete::Account * owner, QWidget * parent)
39 : AddContactPage (parent),
40 jabData( 0 )
41 {
42 QVBoxLayout *layout = new QVBoxLayout(this);
43
44 JabberTransport *transport=dynamic_cast<JabberTransport*>(owner);
45 JabberAccount *jaccount= transport ? transport->account() : dynamic_cast<JabberAccount*>(owner);
46
47 if (jaccount->isConnected ())
48 {
49 QWidget*w = new QWidget( this );
50 jabData = new Ui::dlgAddContact;
51 jabData->setupUi( w );
52 layout->addWidget(w);
53 jabData->addID->setFocus();
54
55 if(transport)
56 {
57 jabData->textLabel1->setText( i18n("Loading instructions from gateway...") );
58 XMPP::JT_Gateway * gatewayTask = new XMPP::JT_Gateway ( jaccount->client()->rootTask () );
59 QObject::connect (gatewayTask, SIGNAL (finished()), this, SLOT (slotPromtReceived()));
60 gatewayTask->get ( transport->myself()->contactId() );
61 gatewayTask->go ( true );
62 }
63 canadd = true;
64 }
65 else
66 {
67 noaddMsg1 = new QLabel (i18n ("You need to be connected to be able to add contacts."), this);
68 layout->addWidget(noaddMsg1);
69 noaddMsg2 = new QLabel (i18n ("Connect to the Jabber network and try again."), this);
70 layout->addWidget(noaddMsg2);
71 canadd = false;
72 }
73
74 }
75
~JabberAddContactPage()76 JabberAddContactPage::~JabberAddContactPage ()
77 {
78 delete jabData;
79 }
80
validateData()81 bool JabberAddContactPage::validateData ()
82 {
83 return true;
84 }
85
apply(Kopete::Account * account,Kopete::MetaContact * parentContact)86 bool JabberAddContactPage::apply ( Kopete::Account *account, Kopete::MetaContact *parentContact )
87 {
88
89 if( canadd && validateData () )
90 {
91 JabberTransport *transport=dynamic_cast<JabberTransport*>(account);
92 JabberAccount *jaccount=transport?transport->account():dynamic_cast<JabberAccount*>(account);
93
94 QString contactId = jabData->addID->text ();
95
96 if(transport)
97 {
98 XMPP::JT_Gateway * gatewayTask = new XMPP::JT_Gateway ( jaccount->client()->rootTask () );
99 JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND *workaround =
100 new JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND( transport , parentContact , gatewayTask );
101 QObject::connect (gatewayTask, SIGNAL (finished()), workaround, SLOT (slotJidReceived()));
102 gatewayTask->set ( transport->myself()->contactId() , contactId );
103 gatewayTask->go ( true );
104 return true;
105 }
106
107 QString displayName = parentContact->displayName ();
108 /*
109 if ( displayName.isEmpty () )
110 displayName = contactId;
111 */
112 // collect all group names
113 QStringList groupNames;
114 Kopete::GroupList groupList = parentContact->groups();
115 foreach(Kopete::Group *group, groupList)
116 {
117 if (group->type() == Kopete::Group::Normal)
118 groupNames += group->displayName();
119 else if (group->type() == Kopete::Group::TopLevel)
120 groupNames += QString();
121 }
122
123 if(groupNames.size() == 1 && groupNames.at(0).isEmpty())
124 groupNames.clear();
125
126 if ( jaccount->addContact ( contactId, parentContact, Kopete::Account::ChangeKABC ) )
127 {
128 XMPP::RosterItem item;
129 XMPP::Jid jid ( contactId );
130
131 item.setJid ( jid );
132 item.setName ( displayName );
133 item.setGroups ( groupNames );
134
135 // add the new contact to our roster.
136 XMPP::JT_Roster * rosterTask = new XMPP::JT_Roster ( jaccount->client()->rootTask () );
137
138 rosterTask->set ( item.jid(), item.name(), item.groups() );
139 rosterTask->go ( true );
140
141 // send a subscription request.
142 XMPP::JT_Presence *presenceTask = new XMPP::JT_Presence ( jaccount->client()->rootTask () );
143
144 presenceTask->sub ( jid, QStringLiteral("subscribe") );
145 presenceTask->go ( true );
146
147 return true;
148 }
149 }
150
151 return false;
152 }
153
slotPromtReceived()154 void JabberAddContactPage::slotPromtReceived( )
155 {
156 XMPP::JT_Gateway * task = (XMPP::JT_Gateway *) sender ();
157
158 if (task->success ())
159 {
160 jabData->lblID->setText( task->prompt() );
161 jabData->textLabel1->setText( task->desc() );
162 }
163 else
164 {
165 jabData->textLabel1->setText( i18n("An error occurred while loading instructions from the gateway.") );
166 }
167 }
168
JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND(JabberTransport * t,Kopete::MetaContact * mc,QObject * task)169 JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND::JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND( JabberTransport *t, Kopete::MetaContact * mc, QObject* task )
170 : QObject(task) , metacontact(mc) , transport(t)
171 {}
172
slotJidReceived()173 void JabberAddContactPage_there_is_no_possibility_to_add_assync_WORKAROUND::slotJidReceived( )
174 {
175 XMPP::JT_Gateway * task = (XMPP::JT_Gateway *) sender ();
176
177 if (!task->success ())
178 {
179 return;
180 // maybe we should show an error message, but i don't like showing error message - Olivier
181 }
182
183 QString contactId=task->prompt();
184
185 Kopete::MetaContact* parentContact=metacontact;
186 JabberAccount *jaccount=transport->account();;
187
188 /*\
189 * this is a copy of the end of JabberAddContactPage::apply
190 \*/
191
192 QString displayName = parentContact->displayName ();
193 /*
194 if ( displayName.isEmpty () )
195 displayName = contactId;
196 */
197 // collect all group names
198 QStringList groupNames;
199 Kopete::GroupList groupList = parentContact->groups();
200 foreach(Kopete::Group *group,groupList)
201 {
202 if (group->type() == Kopete::Group::Normal)
203 groupNames += group->displayName();
204 else if (group->type() == Kopete::Group::TopLevel)
205 groupNames += QString();
206 }
207
208 if(groupNames.size() == 1 && groupNames.at(0).isEmpty())
209 groupNames.clear();
210
211 if ( jaccount->addContact ( contactId, parentContact, Kopete::Account::ChangeKABC ) )
212 {
213 XMPP::RosterItem item;
214 XMPP::Jid jid ( contactId );
215
216 item.setJid ( jid );
217 item.setName ( displayName );
218 item.setGroups ( groupNames );
219
220 // add the new contact to our roster.
221 XMPP::JT_Roster * rosterTask = new XMPP::JT_Roster ( jaccount->client()->rootTask () );
222
223 rosterTask->set ( item.jid(), item.name(), item.groups() );
224 rosterTask->go ( true );
225
226 // send a subscription request.
227 XMPP::JT_Presence *presenceTask = new XMPP::JT_Presence ( jaccount->client()->rootTask () );
228
229 presenceTask->sub ( jid, QStringLiteral("subscribe") );
230 presenceTask->go ( true );
231
232 return;
233 }
234 }
235
236 /*
237 * Local variables:
238 * c-indentation-style: k&r
239 * indent-tabs-mode: t
240 * End:
241 */
242