agsantos | 655d8e2 | 2020-08-10 17:36:47 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2020 by Savoir-faire Linux |
| 3 | * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "mediahandleritemlistmodel.h" |
| 20 | |
| 21 | MediaHandlerItemListModel::MediaHandlerItemListModel(QObject *parent) |
| 22 | : QAbstractListModel(parent) |
| 23 | {} |
| 24 | |
| 25 | MediaHandlerItemListModel::~MediaHandlerItemListModel() {} |
| 26 | |
| 27 | int |
| 28 | MediaHandlerItemListModel::rowCount(const QModelIndex &parent) const |
| 29 | { |
| 30 | if (!parent.isValid()) { |
| 31 | /* |
| 32 | * Count. |
| 33 | */ |
| 34 | return LRCInstance::pluginModel().listCallMediaHandlers().size(); |
| 35 | } |
| 36 | /* |
| 37 | * A valid QModelIndex returns 0 as no entry has sub-elements. |
| 38 | */ |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | int |
| 43 | MediaHandlerItemListModel::columnCount(const QModelIndex &parent) const |
| 44 | { |
| 45 | Q_UNUSED(parent); |
| 46 | /* |
| 47 | * Only need one column. |
| 48 | */ |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | QVariant |
| 53 | MediaHandlerItemListModel::data(const QModelIndex &index, int role) const |
| 54 | { |
| 55 | auto mediahandlerList = LRCInstance::pluginModel().listCallMediaHandlers(); |
| 56 | if (!index.isValid() || mediahandlerList.size() <= index.row()) { |
| 57 | return QVariant(); |
| 58 | } |
| 59 | |
| 60 | auto details = LRCInstance::pluginModel().getCallMediaHandlerDetails( |
| 61 | mediahandlerList.at(index.row())); |
| 62 | auto status = LRCInstance::pluginModel().getCallMediaHandlerStatus(); |
| 63 | bool loaded = false; |
| 64 | if (status["name"] == details.id) |
| 65 | loaded = true; |
| 66 | |
| 67 | switch (role) { |
| 68 | case Role::MediaHandlerName: |
| 69 | return QVariant(details.name); |
| 70 | case Role::MediaHandlerId: |
| 71 | return QVariant(mediahandlerList.at(index.row())); |
| 72 | case Role::MediaHandlerIcon: |
| 73 | return QVariant(details.iconPath); |
| 74 | case Role::IsLoaded: |
| 75 | return QVariant(loaded); |
| 76 | } |
| 77 | return QVariant(); |
| 78 | } |
| 79 | |
| 80 | QHash<int, QByteArray> |
| 81 | MediaHandlerItemListModel::roleNames() const |
| 82 | { |
| 83 | QHash<int, QByteArray> roles; |
| 84 | roles[MediaHandlerName] = "MediaHandlerName"; |
| 85 | roles[MediaHandlerId] = "MediaHandlerId"; |
| 86 | roles[MediaHandlerIcon] = "MediaHandlerIcon"; |
| 87 | roles[IsLoaded] = "IsLoaded"; |
| 88 | |
| 89 | return roles; |
| 90 | } |
| 91 | |
| 92 | QModelIndex |
| 93 | MediaHandlerItemListModel::index(int row, int column, const QModelIndex &parent) const |
| 94 | { |
| 95 | Q_UNUSED(parent); |
| 96 | if (column != 0) { |
| 97 | return QModelIndex(); |
| 98 | } |
| 99 | |
| 100 | if (row >= 0 && row < rowCount()) { |
| 101 | return createIndex(row, column); |
| 102 | } |
| 103 | return QModelIndex(); |
| 104 | } |
| 105 | |
| 106 | QModelIndex |
| 107 | MediaHandlerItemListModel::parent(const QModelIndex &child) const |
| 108 | { |
| 109 | Q_UNUSED(child); |
| 110 | return QModelIndex(); |
| 111 | } |
| 112 | |
| 113 | Qt::ItemFlags |
| 114 | MediaHandlerItemListModel::flags(const QModelIndex &index) const |
| 115 | { |
| 116 | auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable; |
| 117 | if (!index.isValid()) { |
| 118 | return QAbstractItemModel::flags(index); |
| 119 | } |
| 120 | return flags; |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | MediaHandlerItemListModel::reset() |
| 125 | { |
| 126 | beginResetModel(); |
| 127 | endResetModel(); |
| 128 | } |