File indexing completed on 2023-10-01 08:41:45

0001 /*
0002  * Global Presence - wrap Tp::Presence with KDE functionality
0003  *
0004  * Copyright (C) 2011 David Edmundson <kde@davidedmundson.co.uk>
0005  * Copyright (C) 2012 Daniele E. Domenichelli <daniele.domenichelli@gmail.com>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Lesser General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2.1 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library; if not, write to the Free Software
0019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0020  */
0021 
0022 #include "presence.h"
0023 
0024 #include <KLocalizedString>
0025 #include <KIconLoader>
0026 #include <QIcon>
0027 
0028 namespace KTp
0029 {
0030 
0031 Presence::Presence() :
0032     Tp::Presence()
0033 {
0034 }
0035 
0036 Presence::Presence(const Tp::Presence &presence) :
0037     Tp::Presence(presence)
0038 {
0039 }
0040 
0041 QIcon Presence::icon(bool useImIcons) const
0042 {
0043     const QString &name(iconName(useImIcons));
0044     if (!name.isEmpty()) {
0045         return QIcon::fromTheme(name);
0046     }
0047     return QIcon();
0048 }
0049 QIcon Presence::icon(QStringList overlays, bool useImIcons) const
0050 {
0051     const QString &name(iconName(useImIcons));
0052     if (!name.isEmpty()) {
0053         return KIconLoader::global()->loadIcon(name, KIconLoader::NoGroup, 0, KIconLoader::DefaultState, overlays);
0054     }
0055     return QIcon();
0056 }
0057 
0058 QString Presence::iconName(bool useImIcons) const
0059 {
0060     switch (type()) {
0061     case Tp::ConnectionPresenceTypeAvailable:
0062         return useImIcons ? QLatin1String("im-user") : QLatin1String("user-online");
0063     case Tp::ConnectionPresenceTypeBusy:
0064         return useImIcons ? QLatin1String("im-user-busy") : QLatin1String("user-busy");
0065     case Tp::ConnectionPresenceTypeAway:
0066         return useImIcons ? QLatin1String("im-user-away") : QLatin1String("user-away");
0067     case Tp::ConnectionPresenceTypeExtendedAway:
0068         // FIXME Request an icon "im-user-away-extended"
0069         return useImIcons ? QLatin1String("im-user-away") : QLatin1String("user-away-extended");
0070     case Tp::ConnectionPresenceTypeHidden:
0071         return useImIcons ? QLatin1String("im-invisible-user") : QLatin1String("user-invisible");
0072     case Tp::ConnectionPresenceTypeOffline:
0073         return useImIcons ? QLatin1String("im-user-offline") : QLatin1String("user-offline");
0074     default:
0075         return useImIcons ? QLatin1String("im-user-offline") : QLatin1String("user-offline");
0076     }
0077 }
0078 
0079 bool Presence::operator ==(const Presence &other) const
0080 {
0081     if (sortPriority(type()) == sortPriority(other.type())) {
0082         if (statusMessage() == other.statusMessage()) {
0083             return true;
0084         }
0085     }
0086 
0087     return false;
0088 }
0089 
0090 bool Presence::operator !=(const Presence &other) const
0091 {
0092     return !(other == *this);
0093 }
0094 
0095 bool Presence::operator <(const Presence &other) const
0096 {
0097     if (sortPriority(type()) > sortPriority(other.type())) {
0098         return true;
0099     } else if (sortPriority(type()) == sortPriority(other.type())) {
0100         return (statusMessage() < other.statusMessage());
0101     } else {
0102         return false;
0103     }
0104 }
0105 
0106 bool Presence::operator >(const Presence &other) const
0107 {
0108     return (other < *this);
0109 }
0110 
0111 QString Presence::displayString() const
0112 {
0113     switch (type()) {
0114         case Tp::ConnectionPresenceTypeAvailable:
0115             return i18nc("IM presence: a person is available", "Available");
0116         case Tp::ConnectionPresenceTypeBusy:
0117             return i18nc("IM presence: a person is busy", "Busy");
0118         case Tp::ConnectionPresenceTypeAway:
0119             return i18nc("IM presence: a person is away", "Away");
0120         case Tp::ConnectionPresenceTypeExtendedAway:
0121             return i18nc("IM presence: a person is not available", "Not Available");
0122         case Tp::ConnectionPresenceTypeHidden:
0123             return i18nc("IM presence: a person is invisible", "Invisible");
0124         case Tp::ConnectionPresenceTypeOffline:
0125             return i18nc("IM presence: a person is offline", "Offline");
0126         default:
0127             return QString();
0128     }
0129 }
0130 
0131 int Presence::sortPriority(const Tp::ConnectionPresenceType &type)
0132 {
0133     switch(type) {
0134         case Tp::ConnectionPresenceTypeAvailable:
0135             return 0;
0136         case Tp::ConnectionPresenceTypeBusy:
0137             return 1;
0138         case Tp::ConnectionPresenceTypeAway:
0139             return 2;
0140         case Tp::ConnectionPresenceTypeExtendedAway:
0141             return 3;
0142         case Tp::ConnectionPresenceTypeHidden:
0143             return 4;
0144         //don't distinguish between the following three presences
0145         case Tp::ConnectionPresenceTypeError:
0146         case Tp::ConnectionPresenceTypeUnknown:
0147         case Tp::ConnectionPresenceTypeUnset:
0148             return 5;
0149         case Tp::ConnectionPresenceTypeOffline:
0150         default:
0151             return 6;
0152     }
0153 
0154     return -1;
0155 }
0156 
0157 }