-
Notifications
You must be signed in to change notification settings - Fork 5
/
facebookaccount.cpp
358 lines (290 loc) · 11.8 KB
/
facebookaccount.cpp
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
Kopete Facebook Protocol implementation
This code is not associated with Facebook in any way.
Copyright (c) 2009 Duncan Mac-Vicar P. <[email protected]>
Kopete (c) 2002-2009 by the Kopete developers <[email protected]>
*************************************************************************
* *
* This library 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 "facebookaccount.h"
#include <kaction.h>
#include <kdebug.h>
#include <klocale.h>
#include <kactionmenu.h>
#include <kmenu.h>
#include <kicon.h>
#include <kopetechatsession.h>
#include <kopetemessage.h>
#include "kopetemetacontact.h"
#include "kopetecontactlist.h"
#include "kopetepassword.h"
#include "kopeteutils.h"
#include "facebookcontact.h"
#include "facebookprotocol.h"
#include "facebookchatsession.h"
using Facebook::ChatService;
using Facebook::ChatMessage;
using Facebook::BuddyInfo;
FacebookAccount::FacebookAccount( FacebookProtocol *parent, const QString& accountID )
: Kopete::PasswordedAccount ( parent, accountID )
, m_service(0L)
{
setMyself( new FacebookContact( this, accountId(), accountId(), Kopete::ContactList::self()->myself() ) );
myself()->setOnlineStatus( FacebookProtocol::protocol()->facebookOffline );
m_service = new Facebook::ChatService(this);
}
FacebookAccount::~FacebookAccount()
{
qDebug() << "destructing FacebookAccount";
}
FacebookContact * FacebookAccount::contact( const QString &id )
{
return static_cast<FacebookContact *>(contacts().value(id));
}
void FacebookAccount::fillActionMenu( KActionMenu *actionMenu )
{
Kopete::Account::fillActionMenu( actionMenu );
//actionMenu->addSeparator();
//KAction *action;
//action = new KAction (KIcon("facebook_showvideo"), i18n ("Show my own video..."), actionMenu );
//, "actionShowVideo");
//QObject::connect( action, SIGNAL(triggered(bool)), this, SLOT(slotShowVideo()) );
//actionMenu->addAction(action);
//action->setEnabled( isConnected() );
}
bool FacebookAccount::createContact(const QString& contactId, Kopete::MetaContact* parentContact)
{
if ( ! contact(contactId) )
{
FacebookContact* newContact = new FacebookContact( this, contactId, parentContact->displayName(), parentContact );
return newContact != 0L;
}
return false;
}
void FacebookAccount::setAway( bool away, const QString & /* reason */ )
{
if ( away )
slotGoAway();
else
slotGoOnline();
}
void FacebookAccount::setOnlineStatus(const Kopete::OnlineStatus& status, const Kopete::StatusMessage &reason, const OnlineStatusOptions& options)
{
Q_UNUSED(options);
if ( status.status() == Kopete::OnlineStatus::Online &&
myself()->onlineStatus().status() == Kopete::OnlineStatus::Offline )
slotGoOnline();
else if (status.status() == Kopete::OnlineStatus::Online &&
myself()->onlineStatus().status() == Kopete::OnlineStatus::Away )
setAway( false, reason.message() );
else if ( status.status() == Kopete::OnlineStatus::Offline )
slotGoOffline();
else if ( status.status() == Kopete::OnlineStatus::Away )
slotGoAway( /* reason */ );
}
void FacebookAccount::setStatusMessage(const Kopete::StatusMessage& statusMessage)
{
if ( ! statusMessage.isEmpty() )
m_service->setStatusMessage(statusMessage.title());
}
void FacebookAccount::connectWithPassword (const QString & pass)
{
if (myself ()->onlineStatus () != FacebookProtocol::protocol ()->facebookOffline)
return;
if (pass.isEmpty ())
{
password ().setWrong (true);
password ().setWrong (false);
return;
}
password ().setWrong (false);
QString login = accountId();
QString pass1 = pass;
m_service->setLoginInformation(login, pass1);
myself()->setOnlineStatus( FacebookProtocol::protocol()->facebookConnecting );
m_service->loginToService();
QObject::connect(m_service, SIGNAL(loginToServiceFinished()), this, SLOT(slotLoginToServiceFinished()));
QObject::connect(m_service, SIGNAL(loginToServiceError()), this, SLOT(slotLoginToServiceError()));
}
void FacebookAccount::slotLoginToServiceFinished()
{
myself()->setOnlineStatus( FacebookProtocol::protocol()->facebookOnline );
m_service->setVisibility(true);
// connect changed status and buddy signals
QObject::connect(m_service, SIGNAL(buddyAvailable(const BuddyInfo &, bool)), this, SLOT(slotBuddyAvailable(const BuddyInfo &, bool)));
QObject::connect(m_service, SIGNAL(buddyNotAvailable(const BuddyInfo &)), this, SLOT(slotBuddyNotAvailable(const BuddyInfo &)));
QObject::connect(m_service, SIGNAL(buddyInformation(const BuddyInfo &)), this, SLOT(slotBuddyInformation(const BuddyInfo &)));
QObject::connect(m_service, SIGNAL(messageAvailable(const ChatMessage &)), this, SLOT(slotMessageAvailable(const ChatMessage &)));
QObject::connect(m_service, SIGNAL(messageSendFinished(const ChatMessage &)), this, SLOT(slotMessageAckAvailable(const ChatMessage &)));
QObject::connect(m_service, SIGNAL(messageSendError(const ChatMessage &)), this, SLOT(slotMessageErrorAvailable(const ChatMessage &)));
QObject::connect(m_service, SIGNAL(buddyThumbAvailable( const QString &, const QImage & )), this, SLOT(slotBuddyThumbAvailable( const QString &, const QImage & )));
QObject::connect(m_service, SIGNAL(typingEventAvailable(const QString &, const QString &)), this, SLOT(slotTypingEventAvailable(const QString &, const QString &)));
QObject::connect(m_service, SIGNAL(error( int, const QString &)), this, SLOT(slotError(int, const QString &)));
QObject::connect(m_service, SIGNAL(logoutFromServiceFinished()), this, SLOT(slotLogoutFromServiceFinished()));
}
void FacebookAccount::slotLoginToServiceError()
{
kDebug (FBDBG) << k_funcinfo;
myself ()->setOnlineStatus (FacebookProtocol::protocol ()->facebookOffline);
Kopete::Utils::notifyCannotConnect(this);
}
void FacebookAccount::slotLogoutFromServiceFinished()
{
myself()->setOnlineStatus( FacebookProtocol::protocol()->facebookOffline );
QObject::disconnect ( m_service, 0, 0, 0 );
}
void FacebookAccount::slotLogoutFromServiceError()
{
}
void FacebookAccount::disconnect()
{
kDebug ( FBDBG ) ;
if ( m_service->isLoggedIn() )
{
m_service->setVisibility(false);
m_service->logoutFromService();
}
}
Facebook::ChatService * FacebookAccount::service()
{
return m_service;
}
void FacebookAccount::slotGoOnline ()
{
kDebug ( FBDBG ) ;
if (!isConnected ())
connect ();
}
void FacebookAccount::slotGoAway ()
{
kDebug ( FBDBG ) ;
if (!isConnected ())
connect();
m_service->setVisibility(true);
myself()->setOnlineStatus( FacebookProtocol::protocol()->facebookAway );
}
void FacebookAccount::slotGoOffline ()
{
kDebug ( FBDBG ) ;
if (isConnected ())
disconnect ();
}
void FacebookAccount::slotMessageAckAvailable( const ChatMessage &message )
{
FacebookChatSession *mm = static_cast<FacebookChatSession *>(contact(message.to())->manager(Kopete::Contact::CanCreate));
mm->slotMessageAck(message.messageId());
}
void FacebookAccount::slotMessageErrorAvailable( const ChatMessage &message )
{
FacebookChatSession *mm = static_cast<FacebookChatSession *>(contact(message.to())->manager(Kopete::Contact::CanCreate));
mm->slotMessageError(message.messageId());
}
void FacebookAccount::slotMessageAvailable( const Facebook::ChatMessage &message )
{
QFont msgFont;
QDateTime msgDT;
Kopete::ContactPtrList justMe;
// outgoing or incoming
if ( message.from() == m_service->userId() )
{
kDebug(FBDBG) << "got own sent message back (ack)";
// outgoing, we get our own messages back
// we should use this for confirmation or something
// like that
}
else if ( message.to() == m_service->userId() )
{
// incoming
if( !contact( message.from() ) )
{
// this would be rare... receiving a message from unknown buddy
kDebug(FBDBG) << "Adding contact " << message.from();
addContact( message.from(), message.from(), 0L, Kopete::Account::Temporary );
}
if (message.time().toTime_t() == 0)
msgDT = QDateTime( QDate::currentDate(), QTime::currentTime(), Qt::LocalTime );
else
msgDT = message.time();
Kopete::ChatSession *mm = contact(message.from())->manager(Kopete::Contact::CanCreate);
// Tell the message manager that the buddy is done typing
mm->receivedTypingMsg(contact(message.from()), false);
justMe.append(myself());
Kopete::Message kmsg(contact(message.from()), justMe);
kmsg.setTimestamp( msgDT );
kmsg.setPlainBody( message.text() );
kmsg.setDirection( Kopete::Message::Inbound );
mm->appendMessage(kmsg);
}
}
void FacebookAccount::slotBuddyAvailable( const Facebook::BuddyInfo &buddy, bool idle )
{
// server -> local
if ( !contact( buddy.buddyId() ) )
{
kDebug(FBDBG) << "Buddy " << buddy.buddyId() << "(" << buddy.name() << ")"<< " is not in the contact list. Ugh!";
return;
}
qDebug() << "Buddy " << buddy.buddyId() << "(" << buddy.name() << ")" << " available, " << ( idle ? "" : "not" ) << "idle";
contact( buddy.buddyId() )->setOnlineStatus( idle ? FacebookProtocol::protocol()->facebookAway : FacebookProtocol::protocol()->facebookOnline );
}
void FacebookAccount::slotBuddyNotAvailable( const Facebook::BuddyInfo &buddy )
{
// server -> local
if ( !contact( buddy.buddyId() ) )
{
kDebug(FBDBG) << "Buddy " << buddy.buddyId() << " is not in the contact list. Ugh!";
return;
}
contact( buddy.buddyId() )->setOnlineStatus( FacebookProtocol::protocol()->facebookOffline );
}
void FacebookAccount::slotBuddyInformation( const Facebook::BuddyInfo &buddy )
{
// server -> local
if ( !contact( buddy.buddyId() ) )
{
kDebug(FBDBG) << "Buddy " << buddy.buddyId() << "(" << buddy.name() << ")" << "is not in the contact list. Adding...";
Kopete::Group *g = Kopete::ContactList::self()->findGroup("Facebook");
addContact(buddy.buddyId(), buddy.name().isEmpty() ? buddy.buddyId() : buddy.name() , g, Kopete::Account::ChangeKABC);
contact( buddy.buddyId() )->setOnlineStatus( FacebookProtocol::protocol()->facebookOffline );
}
// set status message
if ( ! buddy.status().isEmpty() )
contact( buddy.buddyId() )->setStatusMessage(buddy.status());
// set name
if ( ! buddy.name().isEmpty() )
contact( buddy.buddyId() )->setNickName(buddy.name());
m_service->requestPicture(buddy.buddyId());
}
void FacebookAccount::slotBuddyThumbAvailable( const QString &buddyid, const QImage &image )
{
if ( ! contact(buddyid) )
return;
contact(buddyid)->setDisplayPicture(image);
}
void FacebookAccount::slotTypingEventAvailable( const QString &from, const QString &to )
{
Q_UNUSED(to);
if ( ! contact(from) )
return;
Kopete::ChatSession *mm = contact(from)->manager(Kopete::Contact::CanCreate);
// Tell the message manager that the buddy is typing
mm->receivedTypingMsg(contact(from), true);
}
void FacebookAccount::slotError( int error, const QString& )
{
switch ( error )
{
case ChatService::ErrorDisconnected:
Kopete::Utils::notifyConnectionLost(this);
break;
default:
break;
}
}
#include "facebookaccount.moc"