Bonjour nick name is in Latin1 encoding
[kdenetwork.git] / kopete / protocols / bonjour / bonjourcontact.cpp
blobff1e6576aa8f2c6242a4449d451ec477e2265685
1 /*
2 bonjourcontact.cpp - Kopete Bonjour Protocol
4 Copyright (c) 2007 by Tejas Dinkar <tejas@gja.in>
5 Copyright (c) 2003 by Will Stephenson <will@stevello.free-online.co.uk>
6 Kopete (c) 2002-2003 by the Kopete developers <kopete-devel@kde.org>
8 *************************************************************************
9 * *
10 * This library is free software; you can redistribute it and/or *
11 * modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either *
13 * version 2 of the License, or (at your option) any later version. *
14 * *
15 *************************************************************************
18 #include "bonjourcontact.h"
20 #include <QList>
21 #include <QHostAddress>
23 #include <kaction.h>
24 #include <kdebug.h>
25 #include <klocale.h>
27 #include <kopeteaccount.h>
28 #include <kopetechatsessionmanager.h>
29 #include <kopetemetacontact.h>
31 #include "bonjouraccount.h"
32 #include "bonjourprotocol.h"
34 // FIXME: Here, we hardcode the icon (bonjour_protocol) into the constructor.
35 // This shouldn't be necessary
36 BonjourContact::BonjourContact( Kopete::Account* _account, const QString &uniqueName,
37 const QString &displayName, Kopete::MetaContact *parent )
38 : Kopete::Contact( _account, uniqueName, parent, QString("bonjour_protocol") )
40 kDebug()<< " uniqueName: " << uniqueName << ", displayName: " << displayName;
42 // FIXME: ? setDisplayName( displayName );
43 m_msgManager = 0L;
45 setOnlineStatus( BonjourProtocol::protocol()->bonjourOffline );
47 connection = NULL;
48 remotePort = 0;
51 BonjourContact::~BonjourContact()
53 kDebug()<<"Deleting Contact!";
54 if (connection) {
55 delete connection;
57 remotePort = 0;
60 bool BonjourContact::isReachable()
62 return true;
65 void BonjourContact::serialize( QMap< QString, QString > &serializedData, QMap< QString, QString > & /* addressBookData */ )
67 // Really Do Nothing
70 Kopete::ChatSession* BonjourContact::manager( CanCreateFlags canCreateFlags )
72 kDebug();
73 if ( m_msgManager )
75 return m_msgManager;
77 else if ( canCreateFlags == CanCreate )
79 QList<Kopete::Contact*> contacts;
80 contacts.append(this);
81 Kopete::ChatSession::Form form = ( Kopete::ChatSession::Small );
82 m_msgManager = Kopete::ChatSessionManager::self()->create(account()->myself(), contacts, protocol(), form );
83 connect(m_msgManager, SIGNAL(messageSent(Kopete::Message&, Kopete::ChatSession*)),
84 this, SLOT( sendMessage( Kopete::Message& ) ) );
85 connect(m_msgManager, SIGNAL(destroyed()), this, SLOT(slotChatSessionDestroyed()));
86 return m_msgManager;
88 else
90 return 0;
95 QList<KAction *> *BonjourContact::customContextMenuActions() //OBSOLETE
97 //FIXME!!! this function is obsolete, we should use XMLGUI instead
98 /*m_actionCollection = new KActionCollection( this, "userColl" );
99 m_actionPrefs = new KAction(i18n( "&Contact Settings" ), 0, this,
100 SLOT( showContactSettings( )), m_actionCollection, "contactSettings" );
102 return m_actionCollection;*/
103 return 0L;
106 void BonjourContact::showContactSettings()
108 //BonjourContactSettings* p = new BonjourContactSettings( this );
109 //p->show();
112 void BonjourContact::sendMessage( Kopete::Message &message )
114 kDebug();
116 // This is Blocking, we may lose upto 5 seconds here
117 if (! connection) {
118 QString localName = account()->property("username").toString();
119 setConnection(new BonjourContactConnection(remoteAddress, remotePort, localName, username));
122 // Blocking Again. Upto another 3 seconds
123 connection->sendMessage(message);
125 // give it back to the manager to display
126 manager()->appendMessage( message );
127 // tell the manager it was sent successfully
128 manager()->messageSucceeded();
131 void BonjourContact::receivedMessage(Kopete::Message message)
133 Kopete::ChatSession *session = manager(CanCreate);
134 session->appendMessage(message);
137 void BonjourContact::slotChatSessionDestroyed()
139 //FIXME: the chat window was closed? Take appropriate steps.
141 if (connection) {
142 connection->sayGoodBye();
143 delete connection;
144 connection = NULL;
147 m_msgManager = 0L;
150 void BonjourContact::setremoteHostName(const QString &nremoteHostName)
152 remoteHostName = nremoteHostName;
155 const QString BonjourContact::getremoteHostName() const
157 return remoteHostName;
160 void BonjourContact::setremoteAddress(const QHostAddress &nremoteAddress)
162 remoteAddress = nremoteAddress;
165 const QHostAddress BonjourContact::getremoteAddress() const
167 return remoteAddress;
170 void BonjourContact::setremotePort(const short int &nremotePort)
172 remotePort = nremotePort;
175 short int BonjourContact::getremotePort() const
177 return remotePort;
180 void BonjourContact::setusername(const QString &n_username)
182 username = n_username;
185 const QString BonjourContact::getusername() const
187 return username;
190 void BonjourContact::settextdata(const QMap <QString, QByteArray> &n_textdata)
192 textdata = n_textdata;
195 const QMap <QString, QByteArray> BonjourContact::gettextdata() const
197 return textdata;
200 bool BonjourContact::isRemoteAddress(const QHostAddress &host) const
202 if (remoteAddress == host)
203 return true;
204 else
205 return false;
208 void BonjourContact::setConnection(BonjourContactConnection *c)
210 if (connection)
211 delete connection;
213 connection = c;
215 // We set the parent of the socket to us, so that we ensure that only we
216 // can delete the connection (and the socket hence)
217 connection->setParent(this);
219 connect(connection, SIGNAL(disconnected(BonjourContactConnection *)),
220 this, SLOT(connectionDisconnected(BonjourContactConnection *)));
222 connect(connection, SIGNAL(messageReceived(Kopete::Message)),
223 this, SLOT(receivedMessage(Kopete::Message)));
226 void BonjourContact::connectionDisconnected(BonjourContactConnection *c)
228 if (c == connection) {
229 delete connection;
230 connection = NULL;
234 #include "bonjourcontact.moc"
236 // vim: set noet ts=4 sts=4 sw=4: