Bonjour nick name is in Latin1 encoding
[kdenetwork.git] / kopete / protocols / meanwhile / meanwhileaccount.cpp
blob1aacb24135abad64ab3b30297d25a5b09fe19964
1 /*
2 meanwhileaccount.cpp - a meanwhile account
4 Copyright (c) 2003-2004 by Sivaram Gottimukkala <suppandi@gmail.com>
5 Copyright (c) 2005 by Jeremy Kerr <jk@ozlabs.org>
7 Kopete (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
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 *************************************************************************
18 #include <signal.h>
19 #include "meanwhileprotocol.h"
20 #include "meanwhileplugin.h"
21 #include "meanwhileaccount.h"
22 #include "meanwhilecontact.h"
23 #include "meanwhilesession.h"
24 #include "kopetechatsession.h"
25 #include "kopetecontactlist.h"
26 #include "kopetepassword.h"
28 #include <kaction.h>
29 #include <kactionmenu.h>
30 #include <kmenu.h>
31 #include <klocale.h>
32 #include <kconfigbase.h>
33 #include <kinputdialog.h>
34 #include <kmessagebox.h>
36 MeanwhileAccount::MeanwhileAccount(
37 MeanwhileProtocol *parent,
38 const QString &accountID)
39 : Kopete::PasswordedAccount(parent, accountID)
41 HERE;
42 m_meanwhileId = accountID;
43 m_session = 0L;
44 setMyself(new MeanwhileContact(m_meanwhileId, m_meanwhileId, this,
45 Kopete::ContactList::self()->myself()));
46 setOnlineStatus(parent->statusOffline);
47 infoPlugin = new MeanwhilePlugin();
50 MeanwhileAccount::~MeanwhileAccount()
52 if (m_session)
53 delete m_session;
56 void MeanwhileAccount::setPlugin(MeanwhilePlugin *plugin)
58 delete infoPlugin;
59 infoPlugin = plugin;
62 bool MeanwhileAccount::createContact(
63 const QString & contactId ,
64 Kopete::MetaContact * parentContact)
66 MeanwhileContact* newContact = new MeanwhileContact(contactId,
67 parentContact->displayName(), this, parentContact);
69 MeanwhileProtocol *p = static_cast<MeanwhileProtocol *>(protocol());
71 if ((newContact != 0L) && (m_session != 0L)
72 && (myself()->onlineStatus() !=
73 p->statusOffline))
74 m_session->addContact(newContact);
76 return newContact != 0L;
79 void MeanwhileAccount::connectWithPassword(const QString &password)
81 if (password.isEmpty()) {
82 disconnect(Kopete::Account::Manual);
83 return;
86 if (m_session == 0L) {
87 m_session = new MeanwhileSession(this);
88 if (m_session == 0L) {
89 mwDebug() << "session creation failed" << endl;
90 return;
93 QObject::connect(m_session,
94 SIGNAL(sessionStateChange(Kopete::OnlineStatus)),
95 this, SLOT(slotSessionStateChange(Kopete::OnlineStatus)));
96 QObject::connect(m_session,
97 SIGNAL(serverNotification(const QString &)),
98 this, SLOT(slotServerNotification(const QString&)));
102 if (m_session == 0L) {
103 mwDebug() << "No memory for session" << endl;
104 return;
107 // void connect(QString host, int port, QString account, QString password);
110 if (!m_session->isConnected() && !m_session->isConnecting())
111 m_session->connect(password);
113 m_session->setStatus(initialStatus());
116 void MeanwhileAccount::disconnect()
118 disconnect(Manual);
121 void MeanwhileAccount::disconnect(Kopete::Account::DisconnectReason reason)
123 if (m_session == 0L)
124 return;
126 MeanwhileProtocol *p = static_cast<MeanwhileProtocol *>(protocol());
127 setAllContactsStatus(p->statusOffline);
128 disconnected(reason);
129 emit isConnectedChanged();
131 delete m_session;
132 m_session = 0L;
135 void MeanwhileAccount::fillActionMenu( KActionMenu *actionMenu )
137 Kopete::Account::fillActionMenu( actionMenu );
139 actionMenu->menu()->addSeparator();
141 #if 0
142 actionMenu->insert(new KAction(i18n("&Change Status Message"), QString(), 0,
143 this, SLOT(meanwhileChangeStatus()), this,
144 "meanwhileChangeStatus"));
145 //infoPlugin->addCustomMenus(theMenu);
146 #endif
149 QString MeanwhileAccount::getServerName()
151 return configGroup()->readEntry("Server");
154 int MeanwhileAccount::getServerPort()
156 return configGroup()->readEntry("Port").toInt();
159 void MeanwhileAccount::setServerName(const QString &server)
161 configGroup()->writeEntry("Server", server);
164 void MeanwhileAccount::setServerPort(int port)
166 configGroup()->writeEntry("Port", port);
169 void MeanwhileAccount::setClientID(int client, int major, int minor)
171 configGroup()->writeEntry("clientID", client);
172 configGroup()->writeEntry("clientVersionMajor", major);
173 configGroup()->writeEntry("clientVersionMinor", minor);
176 void MeanwhileAccount::resetClientID()
178 configGroup()->deleteEntry("clientID");
179 configGroup()->deleteEntry("clientVersionMajor");
180 configGroup()->deleteEntry("clientVersionMinor");
183 bool MeanwhileAccount::getClientIDParams(int *clientID,
184 int *verMajor, int *verMinor)
186 bool custom_id = configGroup()->hasKey("clientID");
188 MeanwhileSession::getDefaultClientIDParams(clientID, verMajor, verMinor);
190 if (custom_id) {
191 *clientID = configGroup()->readEntry("clientID", *clientID);
192 *verMajor = configGroup()->readEntry("clientVersionMajor", *verMinor);
193 *verMinor = configGroup()->readEntry("clientVersionMinor", *verMinor);
196 return custom_id;
199 void MeanwhileAccount::setForceLogin(bool force)
201 configGroup()->writeEntry("forceLogin", force);
204 bool MeanwhileAccount::getForceLogin()
206 return configGroup()->readEntry("forceLogin", false);
209 void MeanwhileAccount::slotServerNotification(const QString &mesg)
211 KMessageBox::queuedMessageBox(0, KMessageBox::Error , mesg,
212 i18n("Meanwhile Plugin: Message from server"), KMessageBox::Notify);
215 QString MeanwhileAccount::meanwhileId() const
217 return m_meanwhileId;
220 void MeanwhileAccount::setAway(bool away, const QString &reason)
222 MeanwhileProtocol *p = static_cast<MeanwhileProtocol *>(protocol());
223 setOnlineStatus(away ? p->statusIdle : p->statusOnline, reason);
226 void MeanwhileAccount::setStatusMessage(const Kopete::StatusMessage &statusMessage)
228 /* ### TODO ! */
231 void MeanwhileAccount::setOnlineStatus(const Kopete::OnlineStatus &status,
232 const Kopete::StatusMessage &reason, const OnlineStatusOptions& options)
234 HERE;
235 Kopete::OnlineStatus oldstatus = myself()->onlineStatus();
237 mwDebug() << "From: " << oldstatus.description() << "(" <<
238 oldstatus.internalStatus() << "):" << oldstatus.isDefinitelyOnline()
239 << endl;
240 mwDebug() << "To: " << status.description() << "(" <<
241 status.internalStatus() << "):" << status.isDefinitelyOnline() << endl;
243 if (oldstatus == status)
244 return;
246 if (!oldstatus.isDefinitelyOnline() && status.isDefinitelyOnline()) {
247 connect();
249 } else if (oldstatus.isDefinitelyOnline() && !status.isDefinitelyOnline()) {
250 disconnect(Kopete::Account::Manual);
252 } else if (m_session)
253 /* todo: check session state? */
254 m_session->setStatus(status, reason);
256 else
257 mwDebug() << "Trying to set status, but no session exists" << endl;
259 /* we should set this on the callback below */
260 //myself()->setOnlineStatus(status);
263 void MeanwhileAccount::syncContactsToServer()
265 if (m_session != 0L)
266 m_session->syncContactsToServer();
269 void MeanwhileAccount::slotSessionStateChange(Kopete::OnlineStatus status)
271 HERE;
272 Kopete::OnlineStatus oldstatus = myself()->onlineStatus();
273 myself()->setOnlineStatus(status);
275 if (status.isDefinitelyOnline() != oldstatus.isDefinitelyOnline()) {
276 if (status.isDefinitelyOnline())
277 m_session->addContacts(contacts());
278 emit isConnectedChanged();
282 MeanwhileSession *MeanwhileAccount::session()
284 return m_session;
287 #include "meanwhileaccount.moc"