~ubuntu-branches/ubuntu/trusty/ktp-kded-integration-module/trusty-proposed

« back to all changes in this revision

Viewing changes to contactnotify.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2013-03-07 16:28:32 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20130307162832-p5cr1zm131gcedas
Tags: 0.5.80-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2012 Rohan Garg <rohangarg@kubuntu.org>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
*/
 
18
 
 
19
#include "contactnotify.h"
 
20
 
 
21
#include <KDebug>
 
22
 
 
23
#include <TelepathyQt/ContactManager>
 
24
#include <TelepathyQt/AccountManager>
 
25
#include <TelepathyQt/Contact>
 
26
 
 
27
#include <KNotification>
 
28
#include <KAboutData>
 
29
 
 
30
#include <KTp/presence.h>
 
31
#include <KTp/global-contact-manager.h>
 
32
 
 
33
using namespace KTp;
 
34
 
 
35
ContactNotify::ContactNotify(const Tp::AccountManagerPtr &accountMgr, QObject *parent) :
 
36
    QObject(parent)
 
37
{
 
38
    Q_ASSERT(accountMgr);
 
39
    m_accountManager = accountMgr;
 
40
    if (!m_accountManager) {
 
41
        return;
 
42
    }
 
43
 
 
44
    GlobalContactManager *contactManager = new GlobalContactManager(m_accountManager, this);
 
45
 
 
46
    Tp::Presence currentPresence;
 
47
 
 
48
    Q_FOREACH(const Tp::ContactPtr &contact, contactManager->allKnownContacts()) {
 
49
        connect(contact.data(), SIGNAL(presenceChanged(Tp::Presence)),
 
50
                SLOT(contactPresenceChanged(Tp::Presence)));
 
51
 
 
52
        currentPresence = contact->presence();
 
53
        m_presenceHash[contact->id()] = Presence::sortPriority(currentPresence.type());
 
54
    }
 
55
 
 
56
    connect(contactManager, SIGNAL(allKnownContactsChanged(Tp::Contacts,Tp::Contacts)),
 
57
            SLOT(onContactsChanged(Tp::Contacts,Tp::Contacts)));
 
58
}
 
59
 
 
60
 
 
61
void ContactNotify::contactPresenceChanged(const Tp::Presence &presence)
 
62
{
 
63
    KTp::Presence ktpPresence(presence);
 
64
    Tp::ContactPtr contact(qobject_cast<Tp::Contact*>(QObject::sender()));
 
65
    int priority = m_presenceHash[contact->id()];
 
66
 
 
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
 
70
    // Away -> Online
 
71
    if (KTp::Presence::sortPriority(presence.type()) < priority) {
 
72
        sendNotification(i18nc("%1 is the contact name, %2 is the presence name",
 
73
                               "%1 is now %2",
 
74
                               contact->alias(),
 
75
                               ktpPresence.displayString()),
 
76
                         ktpPresence.icon(),
 
77
                         contact);
 
78
    }
 
79
 
 
80
    m_presenceHash.insert(contact->id(), Presence::sortPriority(presence.type()));
 
81
}
 
82
 
 
83
void ContactNotify::sendNotification(const QString &text, const KIcon &icon, const Tp::ContactPtr &contact)
 
84
{
 
85
    //The pointer is automatically deleted when the event is closed
 
86
    KNotification *notification;
 
87
    notification = new KNotification(QLatin1String("contactInfo"), KNotification::CloseOnTimeout);
 
88
 
 
89
    KAboutData aboutData("ktelepathy", 0, KLocalizedString(), 0);
 
90
    notification->setComponentData(KComponentData(aboutData));
 
91
 
 
92
    notification->setPixmap(icon.pixmap(48));
 
93
    notification->setText(text);
 
94
    notification->addContext(QLatin1String("contact"), contact.data()->id());
 
95
    notification->sendEvent();
 
96
}
 
97
 
 
98
void ContactNotify::onContactsChanged(const Tp::Contacts &contactsAdded, const Tp::Contacts &contactsRemoved)
 
99
{
 
100
    Tp::Presence currentPresence;
 
101
 
 
102
    Q_FOREACH(const Tp::ContactPtr &contact, contactsAdded) {
 
103
        connect(contact.data(), SIGNAL(presenceChanged(Tp::Presence)),
 
104
                SLOT(contactPresenceChanged(Tp::Presence)));
 
105
 
 
106
        currentPresence = contact->presence();
 
107
        m_presenceHash[contact->id()] = Presence::sortPriority(currentPresence.type());
 
108
 
 
109
    }
 
110
 
 
111
    Q_FOREACH(const Tp::ContactPtr &contact, contactsRemoved) {
 
112
        m_presenceHash.remove(contact->id());
 
113
    }
 
114
 }