diff --git a/src/model/filesforroommodel.cpp b/src/model/filesforroommodel.cpp index 58ab99a2..54a145c4 100644 --- a/src/model/filesforroommodel.cpp +++ b/src/model/filesforroommodel.cpp @@ -1,96 +1,95 @@ /* Copyright (c) 2018 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "filesforroommodel.h" #include "rocketchataccount.h" #include "ruqola_debug.h" FilesForRoomModel::FilesForRoomModel(RocketChatAccount *account, QObject *parent) : QAbstractListModel(parent) , mRochetChantAccount(account) { } FilesForRoomModel::~FilesForRoomModel() { } void FilesForRoomModel::setFiles(const QVector &files) { if (rowCount() != 0) { beginRemoveRows(QModelIndex(), 0, mFiles.count() - 1); mFiles.clear(); endRemoveRows(); } if (!files.isEmpty()) { beginInsertRows(QModelIndex(), 0, files.count() - 1); mFiles = files; endInsertRows(); } } int FilesForRoomModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return mFiles.count(); } QVariant FilesForRoomModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= mFiles.count()) { return QVariant(); } const File file = mFiles.at(index.row()); switch (role) { case UserName: return file.name(); case UserId: return file.userId(); case MimeType: return file.mimeType(); case Url: return file.url(); case Description: return file.description(); case CanBeDeleted: return mRochetChantAccount->userID() == file.userId(); case FileId: return file.fileId(); case TimeStamp: return file.uploadedAt(); } - qCWarning(RUQOLA_LOG) << "Unknown filesmodel roles: " << role; return {}; } QHash FilesForRoomModel::roleNames() const { QHash roles; roles[UserName] = QByteArrayLiteral("username"); roles[UserId] = QByteArrayLiteral("userid"); roles[MimeType] = QByteArrayLiteral("mimetype"); roles[Url] = QByteArrayLiteral("url"); roles[Description] = QByteArrayLiteral("description"); roles[CanBeDeleted] = QByteArrayLiteral("canbedeleted"); roles[FileId] = QByteArrayLiteral("fileid"); roles[TimeStamp] = QByteArrayLiteral("timestamp"); return roles; } diff --git a/src/model/inputcompletermodel.cpp b/src/model/inputcompletermodel.cpp index 66ba7cc4..681cdba3 100644 --- a/src/model/inputcompletermodel.cpp +++ b/src/model/inputcompletermodel.cpp @@ -1,177 +1,175 @@ /* Copyright (c) 2018 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "inputcompletermodel.h" #include "ruqola_debug.h" #include #include #include #include #include InputCompleterModel::InputCompleterModel(QObject *parent) : QAbstractListModel(parent) { } InputCompleterModel::~InputCompleterModel() { } void InputCompleterModel::setChannels(const QVector &channels) { if (rowCount() != 0) { beginRemoveRows(QModelIndex(), 0, mChannel.count() - 1); mChannel.clear(); endRemoveRows(); } if (!channels.isEmpty()) { beginInsertRows(QModelIndex(), 0, channels.count() - 1); mChannel = channels; endInsertRows(); } } void InputCompleterModel::parseChannels(const QJsonObject &obj) { QVector channelList; const QJsonArray rooms = obj.value(QLatin1String("rooms")).toArray(); for (int i = 0; i < rooms.size(); i++) { const QJsonObject o = rooms.at(i).toObject(); Channel channel; channel.parseChannel(o, Channel::ChannelType::Room); //Verify that it's valid channelList.append(channel); } const QJsonArray users = obj.value(QLatin1String("users")).toArray(); for (int i = 0; i < users.size(); i++) { const QJsonObject o = users.at(i).toObject(); Channel channel; channel.parseChannel(o, Channel::ChannelType::PrivateChannel); //Verify that it's valid channelList.append(channel); } setChannels(channelList); } void InputCompleterModel::clear() { if (!mChannel.isEmpty()) { beginRemoveRows(QModelIndex(), 0, mChannel.count() - 1); mChannel.clear(); endRemoveRows(); } } int InputCompleterModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return mChannel.count(); } QVariant InputCompleterModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= mChannel.count()) { return {}; } const Channel channel = mChannel.at(index.row()); switch (role) { case InputCompleterModel::DisplayName: return channelName(channel); case InputCompleterModel::CompleterName: return completerName(channel); case InputCompleterModel::IconName: return channelIconName(channel); case InputCompleterModel::ChannelType: return channel.type(); } - - qCWarning(RUQOLA_LOG) << "Unknown InputCompleterModel roles: " << role; return {}; } QString InputCompleterModel::completerName(const Channel &channel) const { //Specific channelId for opening room //For private channel we need to use username for channel we need roomId switch (channel.type()) { case Channel::ChannelType::PrivateChannel: return channel.user().userName(); case Channel::ChannelType::Room: return channel.roomName(); case Channel::ChannelType::Unknown: qCWarning(RUQOLA_LOG) << "Unknown channel type!"; return {}; } return {}; } QString InputCompleterModel::channelName(const Channel &channel) const { switch (channel.type()) { case Channel::ChannelType::PrivateChannel: { QString text = channel.user().userName(); const QString name = channel.user().name(); if (!name.isEmpty()) { text += QStringLiteral(" (") + channel.user().name() + QLatin1Char(')'); } return text; } case Channel::ChannelType::Room: return channel.roomName(); case Channel::ChannelType::Unknown: qCWarning(RUQOLA_LOG) << "Unknown channel type!"; return {}; } return {}; } QIcon InputCompleterModel::channelIconName(const Channel &channel) const { switch (channel.type()) { case Channel::ChannelType::PrivateChannel: return QIcon::fromTheme(channel.user().iconFromStatus()); case Channel::ChannelType::Room: if (channel.roomType() == QLatin1String("c")) { return QIcon::fromTheme(QStringLiteral("irc-channel-inactive")); } else if (channel.roomType() == QLatin1String("p")) { return QIcon::fromTheme(QStringLiteral("lock")); } qCWarning(RUQOLA_LOG) << "Unknown room type!" << channel.roomType(); return {}; case Channel::ChannelType::Unknown: qCWarning(RUQOLA_LOG) << "Unknown channel type!"; return {}; } return {}; } QHash InputCompleterModel::roleNames() const { QHash roles; roles[InputCompleterModel::DisplayName] = QByteArrayLiteral("displayname"); roles[InputCompleterModel::CompleterName] = QByteArrayLiteral("completername"); roles[InputCompleterModel::IconName] = QByteArrayLiteral("iconname"); roles[InputCompleterModel::ChannelType] = QByteArrayLiteral("channeltype"); return roles; }