File indexing completed on 2023-05-30 11:40:28

0001 /*
0002     Copyright (C) 2012 Rohan Garg <rohangarg@kubuntu.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Lesser General Public License for more details.
0013 
0014     You should have received a copy of the GNU Lesser General Public
0015     License along with this library; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0017 */
0018 
0019 #include "contactnotify.h"
0020 
0021 #include <KTp/core.h>
0022 #include <KTp/presence.h>
0023 #include <KTp/global-contact-manager.h>
0024 
0025 #include <TelepathyQt/ContactManager>
0026 #include <TelepathyQt/AccountManager>
0027 #include <TelepathyQt/Contact>
0028 
0029 #include <KSharedConfig>
0030 #include <KConfigGroup>
0031 #include <KLocalizedString>
0032 #include <KNotification>
0033 
0034 ContactNotify::ContactNotify(QObject *parent) :
0035     QObject(parent)
0036 {
0037     KTp::GlobalContactManager *contactManager = KTp::contactManager();
0038     Tp::Presence currentPresence;
0039 
0040     Q_FOREACH(const Tp::ContactPtr &contact, contactManager->allKnownContacts()) {
0041         connect(contact.data(), SIGNAL(presenceChanged(Tp::Presence)),
0042                 SLOT(contactPresenceChanged(Tp::Presence)));
0043 
0044         currentPresence = contact->presence();
0045         m_presenceHash[contact->id()] = KTp::Presence::sortPriority(currentPresence.type());
0046     }
0047 
0048     connect(contactManager, SIGNAL(allKnownContactsChanged(Tp::Contacts,Tp::Contacts)),
0049             SLOT(onContactsChanged(Tp::Contacts,Tp::Contacts)));
0050 }
0051 
0052 
0053 void ContactNotify::contactPresenceChanged(const Tp::Presence &presence)
0054 {
0055     KTp::Presence ktpPresence(presence);
0056     KTp::ContactPtr contact(qobject_cast<KTp::Contact*>(QObject::sender()));
0057     int priority = m_presenceHash[contact->id()];
0058 
0059     // Don't show presence messages when moving from a higher priority to a lower
0060     // priority, for eg : When a contact changes from Online -> Away, don't send
0061     // a notification, but do send a notification when a contact changes from
0062     // Away -> Online
0063     if (KTp::Presence::sortPriority(presence.type()) < priority) {
0064         sendNotification(i18nc("%1 is the contact name, %2 is the presence name",
0065                                "%1 is now %2",
0066                                contact->alias(),
0067                                ktpPresence.displayString()),
0068                          contact->avatarPixmap(),
0069                          contact);
0070     }
0071 
0072     m_presenceHash.insert(contact->id(), KTp::Presence::sortPriority(presence.type()));
0073 }
0074 
0075 void ContactNotify::sendNotification(const QString &text, const QPixmap &pixmap, const Tp::ContactPtr &contact)
0076 {
0077     //The pointer is automatically deleted when the event is closed
0078     KNotification *notification;
0079     notification = new KNotification(QLatin1String("contactInfo"), KNotification::CloseOnTimeout);
0080 
0081     notification->setComponentName(QStringLiteral("ktelepathy"));
0082 
0083     notification->setPixmap(pixmap);
0084     notification->setText(text);
0085     notification->addContext(QLatin1String("contact"), contact.data()->id());
0086     notification->sendEvent();
0087 }
0088 
0089 void ContactNotify::onContactsChanged(const Tp::Contacts &contactsAdded, const Tp::Contacts &contactsRemoved)
0090 {
0091     Tp::Presence currentPresence;
0092 
0093     Q_FOREACH(const Tp::ContactPtr &contact, contactsAdded) {
0094         connect(contact.data(), SIGNAL(presenceChanged(Tp::Presence)),
0095                 SLOT(contactPresenceChanged(Tp::Presence)));
0096         connect(contact.data(), SIGNAL(avatarTokenChanged(QString)),
0097                 SLOT(contactAvatarTokenChanged(QString)));
0098 
0099         currentPresence = contact->presence();
0100         m_presenceHash[contact->id()] = KTp::Presence::sortPriority(currentPresence.type());
0101 
0102     }
0103 
0104     Q_FOREACH(const Tp::ContactPtr &contact, contactsRemoved) {
0105         m_presenceHash.remove(contact->id());
0106     }
0107  }
0108 
0109 void ContactNotify::saveAvatarTokens()
0110 {
0111     KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("ktelepathy-avatarsrc"));
0112 
0113     QHashIterator<QString, QString> i(m_avatarTokensHash);
0114 
0115     while (i.hasNext()) {
0116         i.next();
0117         KConfigGroup avatarsGroup = config->group(i.key());
0118         avatarsGroup.writeEntry(QLatin1String("avatarToken"), i.value());
0119     }
0120 
0121     config->sync();
0122 }
0123 
0124 void ContactNotify::contactAvatarTokenChanged(const QString &avatarToken)
0125 {
0126     Tp::ContactPtr contact(qobject_cast<Tp::Contact*>(sender()));
0127 
0128     if (!contact) {
0129         return;
0130     }
0131 
0132     m_avatarTokensHash[contact->id()] = avatarToken;
0133     QTimer::singleShot(0, this, SLOT(saveAvatarTokens()));
0134 }