Port things from MSN to WLM plugin:
[kdenetwork.git] / kopete / protocols / telepathy / telepathyaddpendingcontactjob.cpp
blob8e1853830732f1203685c3ce52c6cb671549c195
1 /*
2 telepathyaddpendingcontactjob.cpp - Telepathy Add Pending Contact Job
4 Copyright (c) 2006 by Michaƫl Larouche <larouche@kde.org>
6 Kopete (c) 2002-2006 by the Kopete developers <kopete-devel@kde.org>
8 *************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 *************************************************************************
17 #include "telepathyaddpendingcontactjob.h"
19 // Qt includes
20 #include <QtCore/QPointer>
22 // KDE includes
23 #include <kdebug.h>
25 // Kopete includes
26 #include <kopeteaddedinfoevent.h>
27 #include <kopetemetacontact.h>
28 #include <kopetecontactlist.h>
30 // QtTapioca includes
31 #include <QtTapioca/Contact>
33 // Local includes
34 #include "telepathyprotocol.h"
35 #include "telepathyaccount.h"
36 #include "telepathycontact.h"
38 using namespace Kopete::UI;
40 class TelepathyAddPendingContactJob::Private
42 public:
43 Private()
44 : authorizationOnly(false)
47 QPointer<QtTapioca::Contact> pendingContact;
48 QPointer<TelepathyAccount> account;
49 bool authorizationOnly;
52 TelepathyAddPendingContactJob::TelepathyAddPendingContactJob(TelepathyAccount *parent)
53 : KJob(parent), d(new Private)
55 d->account = parent;
58 TelepathyAddPendingContactJob::~TelepathyAddPendingContactJob()
60 delete d;
63 void TelepathyAddPendingContactJob::setPendingContact(QtTapioca::Contact *contact)
65 d->pendingContact = contact;
68 void TelepathyAddPendingContactJob::setAuthorizeOnly(bool value)
70 d->authorizationOnly = value;
73 void TelepathyAddPendingContactJob::start()
75 kDebug(TELEPATHY_DEBUG_AREA) ;
77 Q_ASSERT( !d->pendingContact.isNull() );
79 Kopete::AddedInfoEvent::ShowActionOptions actions = Kopete::AddedInfoEvent::AuthorizeAction;
80 if( !d->authorizationOnly )
82 actions |= Kopete::AddedInfoEvent::AddAction;
85 Kopete::AddedInfoEvent* event = new Kopete::AddedInfoEvent( d->pendingContact->uri(), d->account );
86 QObject::connect( event, SIGNAL(actionActivated(uint)),
87 this, SLOT(slotAddedInfoEventActionActivated(uint)) );
88 QObject::connect( event, SIGNAL(eventClosed(Kopete::InfoEvent*)),
89 this, SLOT(slotAddedInfoEventClosed()) );
91 event->showActions( actions );
92 event->setContactNickname( d->pendingContact->alias() );
93 event->sendEvent();
96 void TelepathyAddPendingContactJob::slotAddedInfoEventActionActivated( uint actionId )
98 const Kopete::AddedInfoEvent *event = dynamic_cast<const Kopete::AddedInfoEvent *>(sender());
99 if( !event )
101 setError( KJob::UserDefinedError );
102 return;
105 if ( actionId == Kopete::AddedInfoEvent::AddContactAction )
107 // Get the new metacontact
108 Kopete::MetaContact *newMetaContact = event->addContact();
110 // Set internal contact pointer to new contact
111 QString contactUri = d->pendingContact->uri();
112 TelepathyContact *newContact = static_cast<TelepathyContact*>( d->account->contacts()[contactUri] );
113 if( newContact )
115 newContact->setInternalContact( d->pendingContact );
116 // Subscribe to contact presence.
117 d->pendingContact->subscribe(true);
119 // Add to contact list
120 Kopete::ContactList::self()->addMetaContact( newMetaContact );
122 else
124 kDebug(TELEPATHY_DEBUG_AREA) << "Could not find new Telepathy contact " << contactUri;
125 setError( KJob::UserDefinedError );
128 else if ( actionId == Kopete::AddedInfoEvent::AuthorizeAction )
130 // Authorize new contact
131 d->pendingContact->authorize(true);
135 void TelepathyAddPendingContactJob::slotAddedInfoEventClosed()
137 emitResult();
140 #include "telepathyaddpendingcontactjob.moc"