File indexing completed on 2024-02-25 16:44:40

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "statusmodel.h"
0008 #include "customusers/customuserstatus.h"
0009 #include "utils.h"
0010 #include <KLocalizedString>
0011 
0012 StatusModel::StatusModel(QObject *parent)
0013     : QAbstractListModel(parent)
0014 {
0015     fillModel();
0016 }
0017 
0018 StatusModel::~StatusModel() = default;
0019 
0020 User::PresenceStatus StatusModel::currentUserStatus() const
0021 {
0022     return mStatusList.at(mCurrentStatus).status;
0023 }
0024 
0025 StatusModel::StatusInfo StatusModel::currentStatusInfo() const
0026 {
0027     const DisplayStatusInfo info = mStatusList.at(mCurrentStatus);
0028     StatusInfo currentStatusInfo;
0029     currentStatusInfo.status = info.status;
0030     currentStatusInfo.statusStr = info.statusStr;
0031     if (!mCustomText.isEmpty()) {
0032         currentStatusInfo.statusStr = mCustomText;
0033     }
0034     return currentStatusInfo;
0035 }
0036 
0037 User::PresenceStatus StatusModel::status(int index) const
0038 {
0039     return mStatusList.at(index).status;
0040 }
0041 
0042 void StatusModel::setCurrentPresenceStatus(User::PresenceStatus status)
0043 {
0044     int newStatusIndex = 0;
0045     for (int i = 0, total = mStatusList.count(); i < total; ++i) {
0046         if (mStatusList.at(i).status == status) {
0047             newStatusIndex = i;
0048             break;
0049         }
0050     }
0051     if (mCurrentStatus != newStatusIndex) {
0052         mCurrentStatus = newStatusIndex;
0053         Q_EMIT currentStatusChanged();
0054     }
0055 }
0056 
0057 QString StatusModel::textFromPresenceStatus(User::PresenceStatus status) const
0058 {
0059     switch (status) {
0060     case User::PresenceStatus::PresenceOnline:
0061         return i18n("Online");
0062     case User::PresenceStatus::PresenceBusy:
0063         return i18n("Busy");
0064     case User::PresenceStatus::PresenceAway:
0065         return i18n("Away");
0066     case User::PresenceStatus::PresenceOffline:
0067         return i18n("Offline");
0068     case User::PresenceStatus::Unknown:
0069         return i18n("Modify Status...");
0070     }
0071     return {};
0072 }
0073 
0074 QIcon StatusModel::iconFromPresenceStatus(User::PresenceStatus status) const
0075 {
0076     switch (status) {
0077     case User::PresenceStatus::PresenceOnline:
0078         return QIcon::fromTheme(QStringLiteral("user-online"));
0079     case User::PresenceStatus::PresenceBusy:
0080         return QIcon::fromTheme(QStringLiteral("user-busy"));
0081     case User::PresenceStatus::PresenceAway:
0082         return QIcon::fromTheme(QStringLiteral("user-away"));
0083     case User::PresenceStatus::PresenceOffline:
0084         return QIcon::fromTheme(QStringLiteral("user-offline"));
0085     case User::PresenceStatus::Unknown:
0086         return {};
0087     }
0088     return {};
0089 }
0090 
0091 StatusModel::DisplayStatusInfo StatusModel::createStatusInfo(User::PresenceStatus status, int order)
0092 {
0093     DisplayStatusInfo statusInfo;
0094     statusInfo.status = status;
0095     statusInfo.displayText = textFromPresenceStatus(statusInfo.status);
0096     statusInfo.icon = iconFromPresenceStatus(statusInfo.status);
0097     statusInfo.order = order;
0098     statusInfo.statusStr = Utils::presenceStatusToString(status);
0099     return statusInfo;
0100 }
0101 
0102 void StatusModel::fillModel()
0103 {
0104     mStatusList.clear();
0105     {
0106         const DisplayStatusInfo statusInfo = createStatusInfo(User::PresenceStatus::PresenceOnline, 20);
0107         mStatusList.append(std::move(statusInfo));
0108     }
0109     {
0110         const DisplayStatusInfo statusInfo = createStatusInfo(User::PresenceStatus::PresenceBusy, 19);
0111         mStatusList.append(std::move(statusInfo));
0112     }
0113     {
0114         const DisplayStatusInfo statusInfo = createStatusInfo(User::PresenceStatus::PresenceAway, 18);
0115         mStatusList.append(std::move(statusInfo));
0116     }
0117     {
0118         const DisplayStatusInfo statusInfo = createStatusInfo(User::PresenceStatus::PresenceOffline, 17);
0119         mStatusList.append(std::move(statusInfo));
0120     }
0121     {
0122         const DisplayStatusInfo statusInfo = createStatusInfo(User::PresenceStatus::Unknown, -1);
0123         mStatusList.append(std::move(statusInfo));
0124     }
0125 }
0126 
0127 QString StatusModel::customText() const
0128 {
0129     return mCustomText;
0130 }
0131 
0132 void StatusModel::setCustomText(const QString &customText)
0133 {
0134     mCustomText = customText;
0135 }
0136 
0137 void StatusModel::updateCustomStatus(const QVector<CustomUserStatus> &customUserStatuses)
0138 {
0139     beginResetModel();
0140     fillModel();
0141     for (const CustomUserStatus &status : customUserStatuses) {
0142         DisplayStatusInfo statusInfo;
0143         statusInfo.status = status.statusType();
0144         statusInfo.displayText = status.name();
0145         statusInfo.icon = iconFromPresenceStatus(statusInfo.status);
0146         statusInfo.order = 5;
0147         statusInfo.statusStr = status.name();
0148         mStatusList.append(statusInfo);
0149     }
0150     endResetModel();
0151 }
0152 
0153 int StatusModel::currentStatus() const
0154 {
0155     return mCurrentStatus;
0156 }
0157 
0158 int StatusModel::rowCount(const QModelIndex &parent) const
0159 {
0160     Q_UNUSED(parent)
0161     return mStatusList.count();
0162 }
0163 
0164 QVariant StatusModel::data(const QModelIndex &index, int role) const
0165 {
0166     if (index.row() < 0 || index.row() >= mStatusList.count()) {
0167         return {};
0168     }
0169     const DisplayStatusInfo statusInfo = mStatusList.at(index.row());
0170     switch (role) {
0171     case Qt::DisplayRole:
0172     case StatusI18n:
0173         return statusInfo.displayText;
0174     case Status:
0175         return QVariant::fromValue(statusInfo.status);
0176     case Order:
0177         return statusInfo.order;
0178     case Qt::DecorationRole:
0179     case Icon:
0180         return statusInfo.icon;
0181     }
0182 
0183     return {};
0184 }
0185 
0186 #include "moc_statusmodel.cpp"