Port things from MSN to WLM plugin:
[kdenetwork.git] / kopete / protocols / winpopup / wpaccount.cpp
blobd0d2bb0e125c10f33ef4a71dec9eb3f91da1b18b
1 /***************************************************************************
2 wpaccount.cpp - WP Plugin
3 -------------------
4 begin : Fri Apr 26 2002
5 copyright : (C) 2002 by Gav Wood
6 email : gav@indigoarchive.net
8 Based on code from : (C) 2002 by Duncan Mac-Vicar Prett
9 email : duncan@kde.org
10 ***************************************************************************
12 ***************************************************************************
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 ***************************************************************************/
21 // QT Includes
22 #include <qregexp.h>
23 #include <QIcon>
25 // KDE Includes
26 #include <kdebug.h>
27 #include <kmenu.h>
28 #include <klocale.h>
29 #include <kconfig.h>
30 #include <kactionmenu.h>
31 #include <kicon.h>
33 // Kopete Includes
35 // Local Includes
36 #include "wpaccount.h"
38 class KMenu;
40 WPAccount::WPAccount(WPProtocol *parent, const QString &accountID)
41 : Kopete::Account(parent, accountID)
43 // kDebug(14170) << "WPAccount::WPAccount()";
45 mProtocol = WPProtocol::protocol();
47 // we need this before initActions
48 Kopete::MetaContact *myself = Kopete::ContactList::self()->myself();
49 setMyself( new WPContact(this, accountID, myself->displayName(), myself) );
51 // if (excludeConnect()) connect(Kopete::OnlineStatus::Online); // ??
54 // Destructor
55 WPAccount::~WPAccount()
59 const QStringList WPAccount::getGroups()
61 return mProtocol->getGroups();
64 const QStringList WPAccount::getHosts(const QString &Group)
66 return mProtocol->getHosts(Group);
69 bool WPAccount::checkHost(const QString &Name)
71 // kDebug() << "WPAccount::checkHost: " << Name;
72 if (Name.toUpper() == QString::fromLatin1("LOCALHOST")) {
73 // Assume localhost is always there, but it will not appear in the samba output.
74 // Should never happen as localhost is now forbidden as contact, just for safety. GF
75 return true;
76 } else {
77 return mProtocol->checkHost(Name);
81 bool WPAccount::createContact(const QString &contactId, Kopete::MetaContact *parentContact )
83 // kDebug(14170) << "[WPAccount::createContact] contactId: " << contactId;
85 if (!contacts()[contactId]) {
86 WPContact *newContact = new WPContact(this, contactId, parentContact->displayName(), parentContact);
87 return newContact != 0;
88 } else {
89 kDebug(14170) << "[WPAccount::addContact] Contact already exists";
92 return false;
95 void WPAccount::slotGotNewMessage(const QString &Body, const QDateTime &Arrival, const QString &From)
97 // kDebug(14170) << "WPAccount::slotGotNewMessage(" << Body << ", " << Arrival.toString() << ", " << From << ")";
99 // Ignore messages from own host or IPs.
100 // IPs cannot be matched to an account anyway.
101 // This should happen rarely but they make kopete crash.
102 // The reason for this seems to be in ChatSessionManager? GF
103 QRegExp ip("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
105 // kDebug(14170) << "ip.search: " << From << " match: " << ip.search(From);
107 if (From == accountId() || ip.exactMatch(From)) {
108 kDebug(14170) << "Ignoring message from own host/account or IP.";
109 return;
112 if (isConnected()) {
113 if (!isAway()) {
114 if(!contacts()[From]) {
115 addContact(From, From, 0, Kopete::Account::DontChangeKABC);
117 static_cast<WPContact *>(contacts()[From])->slotNewMessage(Body, Arrival);
119 else {
120 if (!theAwayMessage.isEmpty()) mProtocol->sendMessage(theAwayMessage, From);
122 } else {
123 // What to do with offline received messages?
124 kDebug(14170) << "That's strange - we got a message while offline! Ignoring.";
128 void WPAccount::connect(const Kopete::OnlineStatus &)
130 // kDebug(14170) << "WPAccount::Connect()";
131 myself()->setOnlineStatus(mProtocol->WPOnline);
134 void WPAccount::disconnect()
136 // kDebug(14170) << "WPAccount::Disconnect()";
137 myself()->setOnlineStatus(mProtocol->WPOffline);
140 /* I commented this code because deleting myself may have *dangerous* side effect, for example, for the status tracking.
141 void WPAccount::updateAccountId()
143 delete myself();
144 theInterface->setHostName(accountId());
145 myself() = new WPContact(this, accountId(), accountId(), 0);
148 void WPAccount::setAway(bool status, const QString &awayMessage)
150 // kDebug(14170) << "WPAccount::setAway()";
152 theAwayMessage = awayMessage;
154 // if(!isConnected())
155 // theInterface->goOnline();
156 myself()->setOnlineStatus(status ? mProtocol->WPAway : mProtocol->WPOnline);
157 myself()->setStatusMessage( Kopete::StatusMessage(theAwayMessage) );
160 void WPAccount::fillActionMenu( KActionMenu *actionMenu )
162 kDebug(14170);
164 /// How to remove an action from Kopete::Account::actionMenu()? GF
166 actionMenu->setIcon( myself()->onlineStatus().iconFor(this) );
167 actionMenu->menu()->addTitle( QIcon(myself()->onlineStatus().iconFor(this)), i18n("WinPopup (%1)", accountId()));
169 if (mProtocol)
171 KAction *goOnline = new KAction( KIcon(QIcon(mProtocol->WPOnline.iconFor(this))), i18n("Online"), this );
172 //, "actionGoAvailable" );
173 QObject::connect( goOnline, SIGNAL(triggered(bool)), this, SLOT(connect()) );
174 goOnline->setEnabled(isConnected() && isAway());
175 actionMenu->addAction(goOnline);
177 KAction *goAway = new KAction( KIcon(QIcon(mProtocol->WPAway.iconFor(this))), i18n("Away"), this );
178 //, "actionGoAway" );
179 QObject::connect( goAway, SIGNAL(triggered(bool)), this, SLOT(goAway()) );
180 goAway->setEnabled(isConnected() && !isAway());
181 actionMenu->addAction(goAway);
183 /// One cannot really go offline manually - appears online as long as samba server is running. GF
185 actionMenu->addSeparator();
186 KAction *properties = new KAction( i18n("Properties"), this );
187 // "actionAccountProperties" );
188 QObject::connect( properties, SIGNAL(triggered(bool)), this, SLOT(editAccount()) );
189 actionMenu->addAction( properties );
193 bool WPAccount::hasCustomStatusMenu() const
195 return true;
198 void WPAccount::slotSendMessage(const QString &Body, const QString &Destination)
200 kDebug(14170) << "WPAccount::slotSendMessage(" << Body << ", " << Destination << ")";
202 if (myself()->onlineStatus().status() == Kopete::OnlineStatus::Away) myself()->setOnlineStatus(mProtocol->WPOnline);
203 mProtocol->sendMessage(Body, Destination);
206 void WPAccount::setOnlineStatus(const Kopete::OnlineStatus &status, const Kopete::StatusMessage &reason)
208 if (myself()->onlineStatus().status() == Kopete::OnlineStatus::Offline && status.status() == Kopete::OnlineStatus::Online)
209 connect( status );
210 else if (myself()->onlineStatus().status() != Kopete::OnlineStatus::Offline && status.status() == Kopete::OnlineStatus::Offline)
211 disconnect();
212 else if (myself()->onlineStatus().status() != Kopete::OnlineStatus::Offline && status.status() == Kopete::OnlineStatus::Away)
213 setAway( true, reason.message() );
216 void WPAccount::setStatusMessage(const Kopete::StatusMessage &statusMessage)
218 if(myself()->onlineStatus().status() == Kopete::OnlineStatus::Online)
219 setAway( false, statusMessage.message() );
220 else if(myself()->onlineStatus().status() == Kopete::OnlineStatus::Away)
221 setAway( true, statusMessage.message() );
223 #include "wpaccount.moc"
225 // vim: set noet ts=4 sts=4 sw=4:
226 // kate: tab-width 4; indent-width 4; replace-trailing-space-save on;