1
/***************************************************************************
2
wpcontact.cpp - description
4
begin : Fri Apr 12 2002
5
copyright : (C) 2002 by Gav Wood
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
18
#include "wpcontact.h"
31
#include "wpaccount.h"
33
WPContact::WPContact(Kopete::Account *account, const QString &newHostName, const QString &nickName, Kopete::MetaContact *metaContact)
34
: Kopete::Contact(account, newHostName, metaContact)
36
// kDebug(14170) << "WPContact::WPContact(<account>, " << newHostName << ", " << nickName << ", <parent>)";
37
kDebug(14170) << "WPContact::WPContact: " << this;
39
QString theNickName = nickName;
41
if (theNickName.isEmpty()) {
42
// Construct nickname from hostname with first letter to upper. GF
43
theNickName = newHostName.toLower();
44
theNickName = theNickName.replace(0, 1, theNickName[0].toUpper());
47
setNickName(theNickName);
48
myWasConnected = false;
54
// Initialise and start the periodical checking for contact's status
55
setOnlineStatus(static_cast<WPProtocol *>(protocol())->WPOffline);
57
connect(&checkStatus, SIGNAL(timeout()), this, SLOT(slotCheckStatus()));
58
checkStatus.setSingleShot(false);
59
checkStatus.start(1000);
62
QList<KAction*> *WPContact::customContextMenuActions()
64
//myActionCollection = new KActionCollection(parent);
68
void WPContact::serialize(QMap<QString, QString> &serializedData, QMap<QString, QString> &addressBookData)
70
// kDebug(14170) << "WP::serialize(...)";
72
Kopete::Contact::serialize(serializedData, addressBookData);
75
Kopete::ChatSession* WPContact::manager( Kopete::Contact::CanCreateFlags /*canCreate*/ ) // TODO: use the parameter as canCreate
78
// Set up the message managers
79
QList<Kopete::Contact*> singleContact;
80
singleContact.append(this);
82
m_manager = Kopete::ChatSessionManager::self()->create( account()->myself(), singleContact, protocol() );
84
connect(m_manager, SIGNAL(messageSent(Kopete::Message&,Kopete::ChatSession*)), this, SLOT(slotSendMessage(Kopete::Message&)));
85
connect(m_manager, SIGNAL(messageSent(Kopete::Message&,Kopete::ChatSession*)), m_manager, SLOT(appendMessage(Kopete::Message&)));
86
connect(m_manager, SIGNAL(destroyed()), this, SLOT(slotChatSessionDestroyed()));
93
bool WPContact::isOnline() const
95
kDebug(14170) << "[WPContact::isOnline()]";
96
return onlineStatus().status() != Kopete::OnlineStatus::Offline && onlineStatus().status() != Kopete::OnlineStatus::Unknown;
100
bool WPContact::isReachable()
102
// kDebug(14170) << "[WPContact::isReachable()]";
103
return onlineStatus().status() != Kopete::OnlineStatus::Offline && onlineStatus().status() != Kopete::OnlineStatus::Unknown;
106
void WPContact::slotChatSessionDestroyed()
111
void WPContact::slotUserInfo()
116
m_infoDialog = new WPUserInfo( this );
117
if (!m_infoDialog) return;
118
connect( m_infoDialog, SIGNAL(closing()), this, SLOT(slotCloseUserInfoDialog()) );
119
m_infoDialog->show();
121
m_infoDialog->raise();
125
void WPContact::slotCloseUserInfoDialog()
127
m_infoDialog->deleteLater();
138
void WPContact::slotCheckStatus()
140
bool oldWasConnected = myWasConnected;
141
bool newIsOnline = false;
143
myWasConnected = protocol() != 0 && account() != 0;
144
WPAccount *acct = dynamic_cast<WPAccount *>(account());
145
if (acct) newIsOnline = acct->checkHost(contactId());
147
if(newIsOnline != isOnline() || myWasConnected != oldWasConnected) {
148
Kopete::OnlineStatus tmpStatus = WPProtocol::protocol()->WPOffline;
149
if (myWasConnected && newIsOnline) {
150
tmpStatus = WPProtocol::protocol()->WPOnline;
152
setOnlineStatus(tmpStatus);
156
void WPContact::slotNewMessage(const QString &Body, const QDateTime &Arrival)
158
kDebug(14170) << "WPContact::slotNewMessage(" << Body << ", " << Arrival.toString() << ')';
160
QList<Kopete::Contact*> contactList;
161
contactList.append(account()->myself());
163
QRegExp subj("^Subject: ([^\n]*)\n(.*)$");
164
Kopete::Message msg(this, contactList);
165
msg.setDirection( Kopete::Message::Inbound );
166
msg.setTimestamp(Arrival);
168
if(subj.indexIn(Body) == -1) {
169
msg.setPlainBody( Body );
172
msg.setPlainBody( subj.cap(2) );
173
msg.setSubject( subj.cap(1) );
176
manager(Kopete::Contact::CannotCreate)->appendMessage(msg);
179
void WPContact::slotSendMessage( Kopete::Message& message )
181
// kDebug(14170) << "WPContact::slotSendMessage(<message>)";
182
// Warning: this could crash
183
kDebug(14170) << message.to().first() << " is " << dynamic_cast<WPContact *>( message.to().first() )->contactId();
185
QString Message = QString((!message.subject().isEmpty() ? "Subject: " + message.subject() + '\n' : QString()) + message.plainBody()).trimmed();
186
WPAccount *acct = dynamic_cast<WPAccount *>(account());
187
WPContact *contact = dynamic_cast<WPContact *>( message.to().first() );
188
if (acct && contact) {
189
acct->slotSendMessage( Message, contact->contactId() );
190
m_manager->messageSucceeded();
194
#include "wpcontact.moc"
196
// vim: set noet ts=4 sts=4 sw=4:
197
// kate: tab-width 4; indent-width 4; replace-trailing-space-save on;