~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/winpopup/wpcontact.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          wpcontact.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Fri Apr 12 2002
 
5
    copyright            : (C) 2002 by Gav Wood
 
6
    email                : gav@kde.org
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
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.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
#include "wpcontact.h"
 
19
 
 
20
// Qt Includes
 
21
#include <qregexp.h>
 
22
//Added by qt3to4:
 
23
#include <QList>
 
24
 
 
25
// KDE Includes
 
26
#include <kdebug.h>
 
27
 
 
28
// Kopete Includes
 
29
 
 
30
// Local Includes
 
31
#include "wpaccount.h"
 
32
 
 
33
WPContact::WPContact(Kopete::Account *account, const QString &newHostName, const QString &nickName, Kopete::MetaContact *metaContact)
 
34
        : Kopete::Contact(account, newHostName, metaContact)
 
35
{
 
36
//      kDebug(14170) << "WPContact::WPContact(<account>, " << newHostName << ", " << nickName << ", <parent>)";
 
37
        kDebug(14170) << "WPContact::WPContact: " << this;
 
38
 
 
39
        QString theNickName = nickName;
 
40
 
 
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());
 
45
        }
 
46
 
 
47
        setNickName(theNickName);
 
48
        myWasConnected = false;
 
49
 
 
50
 
 
51
        m_manager = 0;
 
52
        m_infoDialog = 0;
 
53
 
 
54
        // Initialise and start the periodical checking for contact's status
 
55
        setOnlineStatus(static_cast<WPProtocol *>(protocol())->WPOffline);
 
56
 
 
57
        connect(&checkStatus, SIGNAL(timeout()), this, SLOT(slotCheckStatus()));
 
58
        checkStatus.setSingleShot(false);
 
59
        checkStatus.start(1000);
 
60
}
 
61
 
 
62
QList<KAction*> *WPContact::customContextMenuActions()
 
63
{
 
64
        //myActionCollection = new KActionCollection(parent);
 
65
        return 0;
 
66
}
 
67
 
 
68
void WPContact::serialize(QMap<QString, QString> &serializedData, QMap<QString, QString> &addressBookData)
 
69
{
 
70
//      kDebug(14170) << "WP::serialize(...)";
 
71
 
 
72
        Kopete::Contact::serialize(serializedData, addressBookData);
 
73
}
 
74
 
 
75
Kopete::ChatSession* WPContact::manager( Kopete::Contact::CanCreateFlags /*canCreate*/ )        // TODO: use the parameter as canCreate
 
76
{
 
77
        if (m_manager == 0) {
 
78
                // Set up the message managers
 
79
                QList<Kopete::Contact*> singleContact;
 
80
                singleContact.append(this);
 
81
 
 
82
                m_manager = Kopete::ChatSessionManager::self()->create( account()->myself(), singleContact, protocol() );
 
83
 
 
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()));
 
87
        }
 
88
 
 
89
        return m_manager;
 
90
}
 
91
 
 
92
/*
 
93
bool WPContact::isOnline() const
 
94
{
 
95
        kDebug(14170) << "[WPContact::isOnline()]";
 
96
        return onlineStatus().status() != Kopete::OnlineStatus::Offline && onlineStatus().status() != Kopete::OnlineStatus::Unknown;
 
97
}
 
98
*/
 
99
 
 
100
bool WPContact::isReachable()
 
101
{
 
102
//      kDebug(14170) << "[WPContact::isReachable()]";
 
103
        return onlineStatus().status() != Kopete::OnlineStatus::Offline && onlineStatus().status() != Kopete::OnlineStatus::Unknown;
 
104
}
 
105
 
 
106
void WPContact::slotChatSessionDestroyed()
 
107
{
 
108
        m_manager = 0;
 
109
}
 
110
 
 
111
void WPContact::slotUserInfo()
 
112
{
 
113
        kDebug( 14170 ) ;
 
114
 
 
115
        if (!m_infoDialog) {
 
116
                m_infoDialog = new WPUserInfo( this );
 
117
                if (!m_infoDialog) return;
 
118
                connect( m_infoDialog, SIGNAL(closing()), this, SLOT(slotCloseUserInfoDialog()) );
 
119
                m_infoDialog->show();
 
120
        } else {
 
121
                m_infoDialog->raise();
 
122
        }
 
123
}
 
124
 
 
125
void WPContact::slotCloseUserInfoDialog()
 
126
{
 
127
        m_infoDialog->deleteLater();
 
128
        m_infoDialog = 0;
 
129
}
 
130
 
 
131
/*
 
132
void deleteContact()
 
133
{
 
134
//      deleteLater();
 
135
}
 
136
*/
 
137
 
 
138
void WPContact::slotCheckStatus()
 
139
{
 
140
        bool oldWasConnected = myWasConnected;
 
141
        bool newIsOnline = false;
 
142
 
 
143
        myWasConnected = protocol() != 0 && account() != 0;
 
144
        WPAccount *acct = dynamic_cast<WPAccount *>(account());
 
145
        if (acct) newIsOnline = acct->checkHost(contactId());
 
146
 
 
147
        if(newIsOnline != isOnline() || myWasConnected != oldWasConnected) {
 
148
                Kopete::OnlineStatus tmpStatus = WPProtocol::protocol()->WPOffline;
 
149
                if (myWasConnected && newIsOnline) {
 
150
                                tmpStatus = WPProtocol::protocol()->WPOnline;
 
151
                }
 
152
                setOnlineStatus(tmpStatus);
 
153
        }
 
154
}
 
155
 
 
156
void WPContact::slotNewMessage(const QString &Body, const QDateTime &Arrival)
 
157
{
 
158
        kDebug(14170) << "WPContact::slotNewMessage(" << Body << ", " << Arrival.toString() << ')';
 
159
 
 
160
        QList<Kopete::Contact*> contactList;
 
161
        contactList.append(account()->myself());
 
162
 
 
163
        QRegExp subj("^Subject: ([^\n]*)\n(.*)$");
 
164
        Kopete::Message msg(this, contactList);
 
165
        msg.setDirection( Kopete::Message::Inbound );
 
166
        msg.setTimestamp(Arrival);
 
167
 
 
168
        if(subj.indexIn(Body) == -1) {
 
169
                msg.setPlainBody( Body );
 
170
 
 
171
        } else {
 
172
                msg.setPlainBody( subj.cap(2) );
 
173
                msg.setSubject( subj.cap(1) );
 
174
        }
 
175
 
 
176
        manager(Kopete::Contact::CannotCreate)->appendMessage(msg);
 
177
}
 
178
 
 
179
void WPContact::slotSendMessage( Kopete::Message& message )
 
180
{
 
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();
 
184
 
 
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();
 
191
        }
 
192
}
 
193
 
 
194
#include "wpcontact.moc"
 
195
 
 
196
// vim: set noet ts=4 sts=4 sw=4:
 
197
// kate: tab-width 4; indent-width 4; replace-trailing-space-save on;