File indexing completed on 2023-12-03 08:28:36

0001 /*
0002  * Rooms Model - A model of chatrooms.
0003  * Copyright (C) 2012  Dominik Cermak <d.cermak@arcor.de>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Lesser General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public
0016  * License along with this library; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  */
0019 
0020 #include "rooms-model.h"
0021 #include <QIcon>
0022 #include <KLocalizedString>
0023 
0024 // RoomsModel
0025 KTp::RoomsModel::RoomsModel(QObject *parent): QAbstractListModel(parent)
0026 {
0027 }
0028 
0029 int KTp::RoomsModel::rowCount(const QModelIndex &parent) const
0030 {
0031     if (parent.isValid()) {
0032         return 0;
0033     } else {
0034         return m_roomInfoList.size();
0035     }
0036 }
0037 
0038 int KTp::RoomsModel::columnCount(const QModelIndex &parent) const
0039 {
0040     if (parent.isValid()) {
0041         return 0;
0042     } else {
0043         return 4;
0044     }
0045 }
0046 
0047 QVariant KTp::RoomsModel::data(const QModelIndex &index, int role) const
0048 {
0049     if (!index.isValid()) {
0050         return QVariant();
0051     }
0052 
0053     if (index.row() >= m_roomInfoList.count()) {
0054         return QVariant();
0055     }
0056 
0057     const int row = index.row();
0058     const Tp::RoomInfo &roomInfo = m_roomInfoList.at(row);
0059 
0060     // this is handled here because when putting it in the switch below
0061     // all columns get an empty space for the decoration
0062     if (index.column() == PasswordColumn) {
0063         switch (role) {
0064         case Qt::DisplayRole:
0065         case Qt::DecorationRole:
0066             if (roomInfo.info.value(QLatin1String("password")).toBool()) {
0067                 return QIcon::fromTheme(QStringLiteral("object-locked"));
0068             } else {
0069                 return QVariant();
0070             }
0071         case Qt::ToolTipRole:
0072             if (roomInfo.info.value(QLatin1String("password")).toBool()) {
0073                 return i18n("Password required");
0074             } else {
0075                 return i18n("No password required");
0076             }
0077         }
0078     }
0079 
0080     switch(role) {
0081     case Qt::DisplayRole:
0082         switch (index.column()) {
0083         case NameColumn:
0084             return roomInfo.info.value(QLatin1String("name"));
0085         case DescriptionColumn:
0086             return roomInfo.info.value(QLatin1String("description"));
0087         case MembersColumn:
0088             return roomInfo.info.value(QLatin1String("members"));
0089         }
0090     case Qt::ToolTipRole:
0091         switch (index.column()) {
0092         case MembersColumn:
0093             return i18n("Member count");
0094         }
0095     case RoomsModel::HandleNameRole:
0096         return roomInfo.info.value(QLatin1String("handle-name"));
0097     }
0098 
0099     return QVariant();
0100 }
0101 
0102 QVariant KTp::RoomsModel::headerData(int section, Qt::Orientation orientation, int role) const
0103 {
0104     if (role != Qt::DisplayRole && role != Qt::DecorationRole) {
0105         return QVariant();
0106     }
0107 
0108     if (orientation == Qt::Horizontal) {
0109         switch (role) {
0110         case Qt::DisplayRole:
0111             switch (section) {
0112             case NameColumn:
0113                 return i18nc("Chatrooms name", "Name");
0114             case DescriptionColumn:
0115                 return i18nc("Chatrooms description", "Description");
0116             }
0117         case Qt::DecorationRole:
0118             switch (section) {
0119             case PasswordColumn:
0120                 return QIcon::fromTheme(QStringLiteral("object-locked"));
0121             case MembersColumn:
0122                 return QIcon::fromTheme(QStringLiteral("meeting-participant"));
0123             }
0124         }
0125     }
0126 
0127     return QVariant();
0128 }
0129 
0130 void KTp::RoomsModel::addRooms(const Tp::RoomInfoList newRoomList)
0131 {
0132     if (newRoomList.size() > 0) {
0133         beginInsertRows(QModelIndex(), m_roomInfoList.size(), m_roomInfoList.size() + newRoomList.size() - 1);
0134         m_roomInfoList.append(newRoomList);
0135         endInsertRows();
0136     }
0137 }
0138 
0139 void KTp::RoomsModel::clearRoomInfoList()
0140 {
0141     if (m_roomInfoList.size() > 0) {
0142         beginRemoveRows(QModelIndex(), 0, m_roomInfoList.size() - 1);
0143         m_roomInfoList.clear();
0144         endRemoveRows();
0145     }
0146 }
0147 
0148 // FavoriteRoomsModel
0149 KTp::FavoriteRoomsModel::FavoriteRoomsModel(QObject *parent): QAbstractListModel(parent)
0150 {
0151 }
0152 
0153 int KTp::FavoriteRoomsModel::rowCount(const QModelIndex &parent) const
0154 {
0155     if (parent.isValid()) {
0156         return 0;
0157     } else {
0158         return m_favoriteRoomsList.size();
0159     }
0160 }
0161 
0162 int KTp::FavoriteRoomsModel::columnCount(const QModelIndex &parent) const
0163 {
0164     if (parent.isValid()) {
0165         return 0;
0166     } else {
0167         return 3;
0168     }
0169 }
0170 
0171 QVariant KTp::FavoriteRoomsModel::data(const QModelIndex &index, int role) const
0172 {
0173     if (!index.isValid()) {
0174         return QVariant();
0175     }
0176 
0177     if (index.row() >= m_favoriteRoomsList.size()) {
0178         return QVariant();
0179     }
0180 
0181     const int row = index.row();
0182     const QVariantMap &room = m_favoriteRoomsList.at(row);
0183 
0184     switch(role) {
0185     case Qt::EditRole: // Return same values for both Display and Edit roles
0186     case Qt::DisplayRole:
0187         switch (index.column()) {
0188         case BookmarkColumn :
0189             return QVariant();
0190         case HandleNameColumn:
0191             return room.value(QLatin1String("handle-name"));
0192         case AccountIdentifierColumn:
0193             return room.value(QLatin1String("account-identifier"));
0194         }
0195         break;
0196     case Qt::ToolTipRole:
0197         switch (index.column()) {
0198         case BookmarkColumn:
0199             if (room.value(QLatin1String("is-bookmarked")).toBool()) {
0200                 return i18n("Room bookmarked");
0201             } else {
0202                 return i18n("Room not bookmarked");
0203             }
0204         case HandleNameColumn:
0205         case AccountIdentifierColumn:
0206             return room.value(QLatin1String("handle-name"));
0207         }
0208         break;
0209     case Qt::DecorationRole:
0210         switch (index.column()) {
0211         case BookmarkColumn:
0212             if (room.value(QLatin1String("is-bookmarked")).toBool()) {
0213                 return QIcon::fromTheme(QStringLiteral("bookmarks"));
0214             } else {
0215                 return QIcon(QIcon::fromTheme(QStringLiteral("bookmarks")).pixmap(32, 32, QIcon::Disabled));
0216             }
0217         case HandleNameColumn:
0218         case AccountIdentifierColumn:
0219         default:
0220             return QVariant();
0221         }
0222         break;
0223     case Qt::CheckStateRole:
0224         switch (index.column()) {
0225         case BookmarkColumn:
0226             return room.value(QLatin1String("is-bookmarked")).toBool() ? Qt::Checked : Qt::Unchecked;
0227         case HandleNameColumn:
0228         case AccountIdentifierColumn:
0229             return QVariant();
0230         }
0231         break;
0232     case FavoriteRoomsModel::BookmarkRole:
0233         return room.value(QLatin1String("is-bookmarked"));
0234     case FavoriteRoomsModel::HandleNameRole:
0235         return room.value(QLatin1String("handle-name"));
0236     case FavoriteRoomsModel::AccountRole:
0237         return room.value(QLatin1String("account-identifier"));
0238     case FavoriteRoomsModel::FavoriteRoomRole:
0239         return QVariant::fromValue<QVariantMap>(room);
0240     }
0241 
0242     return QVariant();
0243 }
0244 
0245 bool KTp::FavoriteRoomsModel::setData(const QModelIndex &index, const QVariant &value, int role)
0246 {
0247     if (!index.isValid() || index.row() >= m_favoriteRoomsList.size()) {
0248         return false;
0249     }
0250 
0251     const int row = index.row();
0252     QVariantMap &room = m_favoriteRoomsList[row];
0253 
0254     if (role == Qt::EditRole) {
0255         switch (index.column()) {
0256         case BookmarkColumn:
0257             room.insert(QLatin1String("is-bookmarked"), value);
0258             break;
0259         case HandleNameColumn:
0260             room.insert(QLatin1String("handle-name"), value);
0261             break;
0262         case AccountIdentifierColumn:
0263             room.insert(QLatin1String("account-identifier"), value);
0264             break;
0265         default:
0266             return false;
0267         }
0268         Q_EMIT dataChanged(index, index);
0269         return true;
0270     }
0271     if (role == Qt::CheckStateRole) {
0272         switch (index.column()) {
0273         case BookmarkColumn:
0274             room.insert(QLatin1String("is-bookmarked"), value == Qt::Checked ? true : false);
0275             break;
0276         }
0277         Q_EMIT dataChanged(index, index);
0278         return true;
0279     }
0280     return false;
0281 }
0282 
0283 Qt::ItemFlags KTp::FavoriteRoomsModel::flags(const QModelIndex &index) const {
0284     if (index.column() == BookmarkColumn) {
0285         return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
0286     }
0287     return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
0288 }
0289 
0290 void KTp::FavoriteRoomsModel::addRooms(const QList<QVariantMap> newRoomList)
0291 {
0292     if (newRoomList.size() > 0) {
0293         beginInsertRows(QModelIndex(), m_favoriteRoomsList.size(), m_favoriteRoomsList.size() + newRoomList.size() - 1);
0294         m_favoriteRoomsList.append(newRoomList);
0295         endInsertRows();
0296     }
0297 }
0298 
0299 void KTp::FavoriteRoomsModel::addRoom(const QVariantMap &room)
0300 {
0301     beginInsertRows(QModelIndex(), m_favoriteRoomsList.size(), m_favoriteRoomsList.size());
0302     m_favoriteRoomsList.append(room);
0303     endInsertRows();
0304 }
0305 
0306 void KTp::FavoriteRoomsModel::removeRoom(const QVariantMap &room)
0307 {
0308     int row = m_favoriteRoomsList.indexOf(room);
0309     beginRemoveRows(QModelIndex(), row, row);
0310     m_favoriteRoomsList.removeOne(room);
0311     endRemoveRows();
0312 }
0313 
0314 void KTp::FavoriteRoomsModel::clearRooms()
0315 {
0316     beginResetModel();
0317     m_favoriteRoomsList.clear();
0318     endResetModel();
0319 }
0320 
0321 bool KTp::FavoriteRoomsModel::containsRoom(const QString &handle, const QString &account) const
0322 {
0323     bool contains = false;
0324 
0325     Q_FOREACH(const QVariantMap &room, m_favoriteRoomsList) {
0326         if ((room.value(QLatin1String("handle-name")) == handle) && (room.value(QLatin1String("account-identifier")) == account)) {
0327             contains = true;
0328         }
0329     }
0330 
0331     return contains;
0332 }
0333 
0334 int KTp::FavoriteRoomsModel::countForAccount(const QString &account) const
0335 {
0336     int count = 0;
0337 
0338     Q_FOREACH (const QVariantMap &room, m_favoriteRoomsList) {
0339         if (room.value(QLatin1String("account-identifier")) == account) {
0340             count++;
0341         }
0342     }
0343 
0344     return count;
0345 }