2
Copyright (C) 2012 Rohan Garg <rohangarg@kubuntu.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Lesser General Public
6
License as published by the Free Software Foundation; either
7
version 2.1 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public
15
License along with this library; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
#include "contactnotify.h"
23
#include <TelepathyQt/ContactManager>
24
#include <TelepathyQt/AccountManager>
25
#include <TelepathyQt/Contact>
27
#include <KNotification>
30
#include <KTp/presence.h>
31
#include <KTp/global-contact-manager.h>
35
ContactNotify::ContactNotify(const Tp::AccountManagerPtr &accountMgr, QObject *parent) :
39
m_accountManager = accountMgr;
40
if (!m_accountManager) {
44
GlobalContactManager *contactManager = new GlobalContactManager(m_accountManager, this);
46
Tp::Presence currentPresence;
48
Q_FOREACH(const Tp::ContactPtr &contact, contactManager->allKnownContacts()) {
49
connect(contact.data(), SIGNAL(presenceChanged(Tp::Presence)),
50
SLOT(contactPresenceChanged(Tp::Presence)));
52
currentPresence = contact->presence();
53
m_presenceHash[contact->id()] = Presence::sortPriority(currentPresence.type());
56
connect(contactManager, SIGNAL(allKnownContactsChanged(Tp::Contacts,Tp::Contacts)),
57
SLOT(onContactsChanged(Tp::Contacts,Tp::Contacts)));
61
void ContactNotify::contactPresenceChanged(const Tp::Presence &presence)
63
KTp::Presence ktpPresence(presence);
64
Tp::ContactPtr contact(qobject_cast<Tp::Contact*>(QObject::sender()));
65
int priority = m_presenceHash[contact->id()];
67
// Don't show presence messages when moving from a higher priority to a lower
68
// priority, for eg : When a contact changes from Online -> Away, don't send
69
// a notification, but do send a notification when a contact changes from
71
if (KTp::Presence::sortPriority(presence.type()) < priority) {
72
sendNotification(i18nc("%1 is the contact name, %2 is the presence name",
75
ktpPresence.displayString()),
80
m_presenceHash.insert(contact->id(), Presence::sortPriority(presence.type()));
83
void ContactNotify::sendNotification(const QString &text, const KIcon &icon, const Tp::ContactPtr &contact)
85
//The pointer is automatically deleted when the event is closed
86
KNotification *notification;
87
notification = new KNotification(QLatin1String("contactInfo"), KNotification::CloseOnTimeout);
89
KAboutData aboutData("ktelepathy", 0, KLocalizedString(), 0);
90
notification->setComponentData(KComponentData(aboutData));
92
notification->setPixmap(icon.pixmap(48));
93
notification->setText(text);
94
notification->addContext(QLatin1String("contact"), contact.data()->id());
95
notification->sendEvent();
98
void ContactNotify::onContactsChanged(const Tp::Contacts &contactsAdded, const Tp::Contacts &contactsRemoved)
100
Tp::Presence currentPresence;
102
Q_FOREACH(const Tp::ContactPtr &contact, contactsAdded) {
103
connect(contact.data(), SIGNAL(presenceChanged(Tp::Presence)),
104
SLOT(contactPresenceChanged(Tp::Presence)));
106
currentPresence = contact->presence();
107
m_presenceHash[contact->id()] = Presence::sortPriority(currentPresence.type());
111
Q_FOREACH(const Tp::ContactPtr &contact, contactsRemoved) {
112
m_presenceHash.remove(contact->id());