File: contactnotify.cpp

package info (click to toggle)
ktp-kded-integration-module 22.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,232 kB
  • sloc: cpp: 2,720; sh: 3; makefile: 2
file content (134 lines) | stat: -rw-r--r-- 4,842 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
    Copyright (C) 2012 Rohan Garg <rohangarg@kubuntu.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "contactnotify.h"

#include <KTp/core.h>
#include <KTp/presence.h>
#include <KTp/global-contact-manager.h>

#include <TelepathyQt/ContactManager>
#include <TelepathyQt/AccountManager>
#include <TelepathyQt/Contact>

#include <KSharedConfig>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KNotification>

ContactNotify::ContactNotify(QObject *parent) :
    QObject(parent)
{
    KTp::GlobalContactManager *contactManager = KTp::contactManager();
    Tp::Presence currentPresence;

    Q_FOREACH(const Tp::ContactPtr &contact, contactManager->allKnownContacts()) {
        connect(contact.data(), SIGNAL(presenceChanged(Tp::Presence)),
                SLOT(contactPresenceChanged(Tp::Presence)));

        currentPresence = contact->presence();
        m_presenceHash[contact->id()] = KTp::Presence::sortPriority(currentPresence.type());
    }

    connect(contactManager, SIGNAL(allKnownContactsChanged(Tp::Contacts,Tp::Contacts)),
            SLOT(onContactsChanged(Tp::Contacts,Tp::Contacts)));
}


void ContactNotify::contactPresenceChanged(const Tp::Presence &presence)
{
    KTp::Presence ktpPresence(presence);
    KTp::ContactPtr contact(qobject_cast<KTp::Contact*>(QObject::sender()));
    int priority = m_presenceHash[contact->id()];

    // Don't show presence messages when moving from a higher priority to a lower
    // priority, for eg : When a contact changes from Online -> Away, don't send
    // a notification, but do send a notification when a contact changes from
    // Away -> Online
    if (KTp::Presence::sortPriority(presence.type()) < priority) {
        sendNotification(i18nc("%1 is the contact name, %2 is the presence name",
                               "%1 is now %2",
                               contact->alias(),
                               ktpPresence.displayString()),
                         contact->avatarPixmap(),
                         contact);
    }

    m_presenceHash.insert(contact->id(), KTp::Presence::sortPriority(presence.type()));
}

void ContactNotify::sendNotification(const QString &text, const QPixmap &pixmap, const Tp::ContactPtr &contact)
{
    //The pointer is automatically deleted when the event is closed
    KNotification *notification;
    notification = new KNotification(QLatin1String("contactInfo"), KNotification::CloseOnTimeout);

    notification->setComponentName(QStringLiteral("ktelepathy"));

    notification->setPixmap(pixmap);
    notification->setText(text);
    notification->addContext(QLatin1String("contact"), contact.data()->id());
    notification->sendEvent();
}

void ContactNotify::onContactsChanged(const Tp::Contacts &contactsAdded, const Tp::Contacts &contactsRemoved)
{
    Tp::Presence currentPresence;

    Q_FOREACH(const Tp::ContactPtr &contact, contactsAdded) {
        connect(contact.data(), SIGNAL(presenceChanged(Tp::Presence)),
                SLOT(contactPresenceChanged(Tp::Presence)));
        connect(contact.data(), SIGNAL(avatarTokenChanged(QString)),
                SLOT(contactAvatarTokenChanged(QString)));

        currentPresence = contact->presence();
        m_presenceHash[contact->id()] = KTp::Presence::sortPriority(currentPresence.type());

    }

    Q_FOREACH(const Tp::ContactPtr &contact, contactsRemoved) {
        m_presenceHash.remove(contact->id());
    }
 }

void ContactNotify::saveAvatarTokens()
{
    KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("ktelepathy-avatarsrc"));

    QHashIterator<QString, QString> i(m_avatarTokensHash);

    while (i.hasNext()) {
        i.next();
        KConfigGroup avatarsGroup = config->group(i.key());
        avatarsGroup.writeEntry(QLatin1String("avatarToken"), i.value());
    }

    config->sync();
}

void ContactNotify::contactAvatarTokenChanged(const QString &avatarToken)
{
    Tp::ContactPtr contact(qobject_cast<Tp::Contact*>(sender()));

    if (!contact) {
        return;
    }

    m_avatarTokensHash[contact->id()] = avatarToken;
    QTimer::singleShot(0, this, SLOT(saveAvatarTokens()));
}