Port things from MSN to WLM plugin:
[kdenetwork.git] / kopete / protocols / wlm / wlmcontact.cpp
blob779a5b96ed849cf3389881b86dc94d3f3ca8d803
1 /*
2 wlmcontact.cpp - Kopete Wlm Protocol
4 Copyright (c) 2008 by Tiago Salem Herrmann <tiagosh@gmail.com>
5 Kopete (c) 2002-2003 by the Kopete developers <kopete-devel@kde.org>
7 *************************************************************************
8 * *
9 * This library is free software; you can redistribute it and/or *
10 * modify it under the terms of the GNU General Public *
11 * License as published by the Free Software Foundation; either *
12 * version 2 of the License, or (at your option) any later version. *
13 * *
14 *************************************************************************
17 #include "wlmcontact.h"
19 #include <kaction.h>
20 #include <kdebug.h>
21 #include <klocale.h>
22 #include <ktoggleaction.h>
23 #include <kactioncollection.h>
24 #include <kmessagebox.h>
25 #include <kfiledialog.h>
26 #include <KToolInvocation>
27 #include <KUrl>
29 #include "kopeteaccount.h"
30 #include "kopetechatsessionmanager.h"
31 #include "kopetemetacontact.h"
32 #include "kopeteuiglobal.h"
34 #include "ui_wlminfo.h"
35 #include "wlmaccount.h"
36 #include "wlmprotocol.h"
38 WlmContact::WlmContact (Kopete::Account * _account,
39 const QString & uniqueName,
40 const QString & contactSerial,
41 const QString & displayName,
42 Kopete::MetaContact * parent):
43 Kopete::Contact (_account, uniqueName, parent)
45 kDebug (14210) << k_funcinfo << " uniqueName: " << uniqueName <<
46 ", displayName: " << displayName;
47 m_msgManager = 0L;
48 m_account = qobject_cast<WlmAccount*>(account());
49 setFileCapable (true);
50 setOnlineStatus (WlmProtocol::protocol ()->wlmOffline);
51 m_contactSerial = contactSerial;
53 m_actionBlockContact = new KToggleAction(KIcon("wlm_blocked"), i18n("Block Contact"), this );
54 QObject::connect( m_actionBlockContact, SIGNAL(triggered(bool)), this, SLOT(blockContact(bool)) );
56 m_actionShowProfile = new KAction(i18n("Show Profile"), this);
57 QObject::connect(m_actionShowProfile, SIGNAL(triggered(bool)), this, SLOT(slotShowProfile()));
60 WlmContact::~WlmContact ()
64 bool
65 WlmContact::isReachable ()
67 // always true, as we can send offline messages
68 return true;
71 void
72 WlmContact::slotShowProfile()
74 KToolInvocation::invokeBrowser(QString::fromLatin1("http://members.msn.com/default.msnw?mem=") + contactId()) ;
77 void
78 WlmContact::sendFile (const KUrl & sourceURL, const QString & fileName,
79 uint fileSize)
81 Q_UNUSED( fileName );
82 Q_UNUSED( fileSize );
84 QString filePath;
86 if (!sourceURL.isValid ())
87 filePath =
88 KFileDialog::getOpenFileName (KUrl (), "*", 0l,
89 i18n ("Kopete File Transfer"));
90 else
91 filePath = sourceURL.path (KUrl::RemoveTrailingSlash);
93 if (!filePath.isEmpty ())
95 quint32 fileSize = QFileInfo (filePath).size ();
96 //Send the file
97 static_cast <WlmChatSession *>
98 (manager (Kopete::Contact::CanCreate))->sendFile (filePath,
99 fileSize);
103 void
104 WlmContact::serialize (QMap < QString, QString > &serializedData,
105 QMap < QString, QString > & /* addressBookData */ )
107 serializedData["displayPicture"] =
108 property (Kopete::Global::Properties::self ()->photo ()).value ().
109 toString ();
110 serializedData["contactSerial"] = m_contactSerial;
113 Kopete::ChatSession *
114 WlmContact::manager (Kopete::Contact::CanCreateFlags canCreate)
116 Kopete::ContactPtrList chatmembers;
117 chatmembers.append (this);
119 Kopete::ChatSession * _manager =
120 Kopete::ChatSessionManager::self ()->
121 findChatSession (account ()->myself (), chatmembers, protocol ());
122 WlmChatSession *manager = qobject_cast <WlmChatSession *>(_manager);
123 if (!manager && canCreate == Kopete::Contact::CanCreate)
125 manager =
126 new WlmChatSession (protocol (), account ()->myself (),
127 chatmembers);
129 return manager;
132 QList < KAction * >* WlmContact::customContextMenuActions () //OBSOLETE
134 QList<KAction*> *actions = new QList<KAction*>();
136 m_actionBlockContact->setEnabled(m_account->isConnected());
137 m_actionBlockContact->setChecked(m_account->isBlocked(contactId()));
138 actions->append(m_actionBlockContact);
139 actions->append(m_actionShowProfile);
141 // temporary action collection, used to apply Kiosk policy to the actions
142 KActionCollection tempCollection((QObject*)0);
143 tempCollection.addAction(QLatin1String("contactBlock"), m_actionBlockContact);
144 tempCollection.addAction(QLatin1String("contactViewProfile"), m_actionShowProfile);
146 return actions;
149 void WlmContact::blockContact(bool block)
151 if (!m_account->isConnected())
152 return;
154 m_account->blockContact(contactId(), block);
157 void WlmContact::slotUserInfo()
159 KDialog infoDialog;
160 infoDialog.setButtons( KDialog::Close);
161 infoDialog.setDefaultButton(KDialog::Close);
162 const QString nick = property(Kopete::Global::Properties::self()->nickName()).value().toString();
163 const QString personalMessage = statusMessage().message();
164 Ui::WLMInfo info;
165 info.setupUi(infoDialog.mainWidget());
166 info.m_id->setText(contactId());
167 info.m_displayName->setText(nick);
168 info.m_personalMessage->setText(personalMessage);
169 // info.m_phh->setText(m_phoneHome); //TODO enable and fill
170 // info.m_phw->setText(m_phoneWork);
171 // info.m_phm->setText(m_phoneMobile);
172 // info.m_reversed->setChecked(m_reversed);
174 // connect( info.m_reversed, SIGNAL(toggled(bool)) , this, SLOT(slotUserInfoDialogReversedToggled()));
176 info.groupBox->setVisible(false);
177 info.m_reversed->setVisible(false);
179 infoDialog.setCaption(nick);
180 infoDialog.exec();
183 void
184 WlmContact::showContactSettings ()
186 //WlmContactSettings* p = new WlmContactSettings( this );
187 //p->show();
190 void
191 WlmContact::sendMessage (Kopete::Message & message)
193 kDebug (14210) << k_funcinfo;
194 // give it back to the manager to display
195 manager ()->appendMessage (message);
196 // tell the manager it was sent successfully
197 manager ()->messageSucceeded ();
200 void
201 WlmContact::deleteContact ()
203 if (account ()->isConnected ())
205 qobject_cast <WlmAccount *>(account ())->server ()->mainConnection->
206 delFromAddressBook (m_contactSerial.toLatin1 ().data (),
207 contactId ().toLatin1 ().data ());
208 deleteLater ();
210 else
212 KMessageBox::error (Kopete::UI::Global::mainWidget (),
213 i18n
214 ("<qt>You need to go online to remove a contact from your contact list. This contact will appear again when you reconnect.</qt>"),
215 i18n ("WLM Plugin"));
219 void
220 WlmContact::receivedMessage (const QString & message)
222 // Create a Kopete::Message
223 Kopete::ContactPtrList contactList;
224 account ();
225 contactList.append (account ()->myself ());
226 Kopete::Message newMessage (this, contactList);
227 newMessage.setPlainBody (message);
228 newMessage.setDirection (Kopete::Message::Inbound);
230 // Add it to the manager
231 manager ()->appendMessage (newMessage);
234 void
235 WlmContact::slotChatSessionDestroyed ()
237 //FIXME: the chat window was closed? Take appropriate steps.
238 m_msgManager = 0L;
241 void
242 WlmContact::setOnlineStatus(const Kopete::OnlineStatus& status)
244 bool isBlocked = qobject_cast <WlmAccount *>(account())->isOnBlockList(contactId());
246 // if this contact is blocked, and currently has a regular status,
247 // create a custom status and add wlm_blocked to ovelayIcons
248 if(isBlocked && status.internalStatus() < 15)
250 Kopete::Contact::setOnlineStatus(
251 Kopete::OnlineStatus(status.status() ,
252 (status.weight()==0) ? 0 : (status.weight() -1),
253 protocol(),
254 status.internalStatus()+15,
255 status.overlayIcons() + QStringList("wlm_blocked"),
256 i18n("%1|Blocked", status.description() ) ) );
258 else if (!isBlocked && status.internalStatus() >= 15)
260 // if this contact was previously blocked, set a regular status again
261 switch(status.internalStatus()-15)
263 case 1:
264 Kopete::Contact::setOnlineStatus(WlmProtocol::protocol()->wlmOnline);
265 break;
266 case 2:
267 Kopete::Contact::setOnlineStatus(WlmProtocol::protocol()->wlmAway);
268 break;
269 case 3:
270 Kopete::Contact::setOnlineStatus(WlmProtocol::protocol()->wlmBusy);
271 break;
272 case 4:
273 Kopete::Contact::setOnlineStatus(WlmProtocol::protocol()->wlmBeRightBack);
274 break;
275 case 5:
276 Kopete::Contact::setOnlineStatus(WlmProtocol::protocol()->wlmOnThePhone);
277 break;
278 case 6:
279 Kopete::Contact::setOnlineStatus(WlmProtocol::protocol()->wlmOutToLunch);
280 break;
281 case 7:
282 Kopete::Contact::setOnlineStatus(WlmProtocol::protocol()->wlmInvisible);
283 break;
284 case 8:
285 Kopete::Contact::setOnlineStatus(WlmProtocol::protocol()->wlmOffline);
286 break;
287 case 9:
288 Kopete::Contact::setOnlineStatus(WlmProtocol::protocol()->wlmIdle);
289 break;
290 default:
291 Kopete::Contact::setOnlineStatus(WlmProtocol::protocol()->wlmUnknown);
292 break;
296 else
297 Kopete::Contact::setOnlineStatus(status);
300 #include "wlmcontact.moc"
302 // vim: set noet ts=4 sts=4 sw=4: