Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019-2020 by Savoir-faire Linux |
| 3 | * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> |
| 4 | * Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "accountlistmodel.h" |
| 21 | |
| 22 | #include <QDateTime> |
| 23 | |
| 24 | #include "globalinstances.h" |
| 25 | |
| 26 | #include "lrcinstance.h" |
| 27 | #include "pixbufmanipulator.h" |
| 28 | #include "utils.h" |
| 29 | |
| 30 | AccountListModel::AccountListModel(QObject *parent) |
| 31 | : QAbstractListModel(parent) |
| 32 | {} |
| 33 | |
| 34 | AccountListModel::~AccountListModel() {} |
| 35 | |
| 36 | int |
| 37 | AccountListModel::rowCount(const QModelIndex &parent) const |
| 38 | { |
| 39 | if (!parent.isValid()) { |
| 40 | /* |
| 41 | * Count. |
| 42 | */ |
| 43 | return LRCInstance::accountModel().getAccountList().size(); |
| 44 | } |
| 45 | /* |
| 46 | * A valid QModelIndex returns 0 as no entry has sub-elements. |
| 47 | */ |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | int |
| 52 | AccountListModel::columnCount(const QModelIndex &parent) const |
| 53 | { |
| 54 | Q_UNUSED(parent); |
| 55 | /* |
| 56 | * Only need one column. |
| 57 | */ |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | QVariant |
| 62 | AccountListModel::data(const QModelIndex &index, int role) const |
| 63 | { |
| 64 | auto accountList = LRCInstance::accountModel().getAccountList(); |
| 65 | if (!index.isValid() || accountList.size() <= index.row()) { |
| 66 | return QVariant(); |
| 67 | } |
| 68 | |
| 69 | auto &accountInfo = LRCInstance::accountModel().getAccountInfo(accountList.at(index.row())); |
| 70 | |
| 71 | switch (role) { |
| 72 | case Role::Alias: |
| 73 | return QVariant(Utils::bestNameForAccount(accountInfo)); |
| 74 | case Role::Username: |
| 75 | return QVariant(Utils::secondBestNameForAccount(accountInfo)); |
| 76 | case Role::Type: |
| 77 | return QVariant( |
| 78 | Utils::toUnderlyingValue<lrc::api::profile::Type>(accountInfo.profileInfo.type)); |
| 79 | case Role::Status: |
| 80 | return QVariant(Utils::toUnderlyingValue<lrc::api::account::Status>(accountInfo.status)); |
| 81 | case Role::Picture: |
| 82 | return QString::fromLatin1( |
| 83 | Utils::QImageToByteArray(Utils::accountPhoto(accountInfo)).toBase64().data()); |
| 84 | case Role::ID: |
| 85 | return QVariant(accountInfo.id); |
| 86 | } |
| 87 | return QVariant(); |
| 88 | } |
| 89 | |
| 90 | QHash<int, QByteArray> |
| 91 | AccountListModel::roleNames() const |
| 92 | { |
| 93 | QHash<int, QByteArray> roles; |
| 94 | roles[Alias] = "Alias"; |
| 95 | roles[Username] = "Username"; |
| 96 | roles[Picture] = "Picture"; |
| 97 | roles[Type] = "Type"; |
| 98 | roles[Status] = "Status"; |
| 99 | roles[ID] = "ID"; |
| 100 | return roles; |
| 101 | } |
| 102 | |
| 103 | QModelIndex |
| 104 | AccountListModel::index(int row, int column, const QModelIndex &parent) const |
| 105 | { |
| 106 | Q_UNUSED(parent); |
| 107 | if (column != 0) { |
| 108 | return QModelIndex(); |
| 109 | } |
| 110 | |
| 111 | if (row >= 0 && row < rowCount()) { |
| 112 | return createIndex(row, column); |
| 113 | } |
| 114 | return QModelIndex(); |
| 115 | } |
| 116 | |
| 117 | QModelIndex |
| 118 | AccountListModel::parent(const QModelIndex &child) const |
| 119 | { |
| 120 | Q_UNUSED(child); |
| 121 | return QModelIndex(); |
| 122 | } |
| 123 | |
| 124 | Qt::ItemFlags |
| 125 | AccountListModel::flags(const QModelIndex &index) const |
| 126 | { |
| 127 | auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable; |
| 128 | if (!index.isValid()) { |
| 129 | return QAbstractItemModel::flags(index); |
| 130 | } |
| 131 | return flags; |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | AccountListModel::reset() |
| 136 | { |
| 137 | beginResetModel(); |
| 138 | endResetModel(); |
| 139 | } |