conversation-target.cpp 4.27 KB
Newer Older
1
/*
2
    Copyright (C) 2011  Lasath Fernando <kde@lasath.org>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    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 "conversation-target.h"
David Edmundson's avatar
David Edmundson committed
20 21
#include <TelepathyQt/AvatarData>
#include <TelepathyQt/Presence>
22
#include <KDebug>
23
#include <KIconLoader>
24 25 26 27 28

class  ConversationTarget::ConversationTargetPrivate
{
public:
    Tp::ContactPtr contact;
29
    KIcon avatar;
30 31
};

32
ConversationTarget::ConversationTarget(Tp::ContactPtr contact, QObject *parent) :
33 34
    QObject(parent),
    d(new ConversationTargetPrivate)
35 36 37 38 39 40 41 42
{
    kDebug();

    if (contact) {
        setupContactSignals(contact);
    }

    d->contact = contact;
43
    updateAvatar();
44 45 46 47 48 49 50 51 52 53 54
}

void ConversationTarget::setupContactSignals(Tp::ContactPtr contact)
{
    connect(contact.constData(), SIGNAL(aliasChanged(QString)), SIGNAL(nickChanged(QString)));
    connect(contact.constData(), SIGNAL(avatarDataChanged(Tp::AvatarData)), SLOT(onAvatarDataChanged(Tp::AvatarData)));
    connect(contact.constData(), SIGNAL(presenceChanged(Tp::Presence)), SLOT(onPresenceChanged(Tp::Presence)));
}

QIcon ConversationTarget::avatar() const
{
55
    return d->avatar;
56
}
57

58 59 60
QString ConversationTarget::id() const
{
    return d->contact->id();
61 62 63 64 65 66 67
}

QString ConversationTarget::nick() const
{
    return d->contact->alias();
}

68
KIcon ConversationTarget::presenceIcon() const
69
{
70
    return KTp::Presence(d->contact->presence()).icon();
71 72
}

Lasath Fernando's avatar
Lasath Fernando committed
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
QString ConversationTarget::presenceIconSource() const
{
    return iconSourceForPresence(d->contact->presence().type());
}

QString ConversationTarget::iconSourceForPresence(Tp::ConnectionPresenceType presence)
{
    QString iconName;

    switch (presence) {
        case Tp::ConnectionPresenceTypeAvailable:
            iconName = QLatin1String("user-online");
            break;
        case Tp::ConnectionPresenceTypeAway:
            iconName = QLatin1String("user-away");
            break;
        case Tp::ConnectionPresenceTypeExtendedAway:
            iconName = QLatin1String("user-away-extended");
            break;
        case Tp::ConnectionPresenceTypeHidden:
            iconName = QLatin1String("user-invisible");
            break;
        case Tp::ConnectionPresenceTypeBusy:
            iconName = QLatin1String("user-busy");
            break;
        default:
            iconName = QLatin1String("user-offline");
            break;
    }

    return iconName;
}


107
void ConversationTarget::onPresenceChanged(const Tp::Presence&)
108
{
Lasath Fernando's avatar
Lasath Fernando committed
109
    Q_EMIT presenceIconSourceChanged(presenceIconSource());
110 111 112
    Q_EMIT presenceIconChanged(presenceIcon());
}

113
void ConversationTarget::onAvatarDataChanged(const Tp::AvatarData&)
114
{
115
    updateAvatar();
116 117 118
    Q_EMIT avatarChanged(avatar());
}

119 120 121 122 123 124 125 126 127 128 129
void ConversationTarget::updateAvatar()
{
    QString path = d->contact->avatarData().fileName;

    if(path.isEmpty()) {
        path = QLatin1String("im-user");
    }

    d->avatar = KIcon(path);
}

130 131 132 133 134
Tp::ContactPtr ConversationTarget::contact() const
{
    return d->contact;
}

135
void ConversationTarget::setContact(const Tp::ContactPtr &contact)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
{
    if (d->contact) {
        removeContactSignals(d->contact);
    }

    d->contact = contact;
    setupContactSignals(d->contact);
    Q_EMIT contactChanged(contact);
}

void ConversationTarget::removeContactSignals(Tp::ContactPtr contact)
{
    disconnect(contact.constData(), SIGNAL(aliasChanged(QString)), this, SIGNAL(nickChanged(QString)));
    disconnect(contact.constData(), SIGNAL(avatarDataChanged(Tp::AvatarData)), this, SLOT(onAvatarDataChanged(Tp::AvatarData)));
    disconnect(contact.constData(), SIGNAL(presenceChanged(Tp::Presence)), this, SLOT(onPresenceChanged(Tp::Presence)));
}

ConversationTarget::~ConversationTarget()
{
    delete d;
}