Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2018 by Savoir-faire Linux * |
| 3 | * Author: Andreas Traczyk <andreas.traczyk@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 "accountlistmodel.h" |
| 20 | |
| 21 | // Qt |
| 22 | #include <QDateTime> |
| 23 | |
| 24 | // LRC |
| 25 | #include "globalinstances.h" |
| 26 | |
| 27 | // Client |
| 28 | #include "pixbufmanipulator.h" |
| 29 | #include "lrcinstance.h" |
| 30 | #include "utils.h" |
| 31 | |
| 32 | AccountListModel::AccountListModel(QObject *parent) |
| 33 | : QAbstractItemModel(parent) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | int AccountListModel::rowCount(const QModelIndex &parent) const |
| 38 | { |
| 39 | if (!parent.isValid()) { |
| 40 | return LRCInstance::accountModel().getAccountList().size(); // count |
| 41 | } |
| 42 | return 0; // A valid QModelIndex returns 0 as no entry has sub-elements |
| 43 | } |
| 44 | |
| 45 | int AccountListModel::columnCount(const QModelIndex &parent) const |
| 46 | { |
| 47 | Q_UNUSED(parent); |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | QVariant AccountListModel::data(const QModelIndex &index, int role) const |
| 52 | { |
| 53 | auto accountList = LRCInstance::accountModel().getAccountList(); |
Andreas Traczyk | 55f92cb | 2018-10-23 12:26:25 -0400 | [diff] [blame] | 54 | if (!index.isValid() || accountList.size() <= index.row()) { |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 55 | return QVariant(); |
| 56 | } |
| 57 | |
| 58 | auto& accountInfo = LRCInstance::accountModel().getAccountInfo(accountList.at(index.row())); |
| 59 | |
| 60 | switch (role) { |
| 61 | case Role::Alias: |
| 62 | case Qt::DisplayRole: |
| 63 | return QVariant(QString::fromStdString(accountInfo.profileInfo.alias)); |
| 64 | case Role::Username: |
Isa Nanic | f7e0792 | 2018-10-29 10:52:05 -0400 | [diff] [blame] | 65 | return QVariant(QString::fromStdString(Utils::secondBestNameForAccount(accountInfo))); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 66 | case Role::Type: |
| 67 | return QVariant(Utils::toUnderlyingValue<lrc::api::profile::Type>(accountInfo.profileInfo.type)); |
| 68 | case Role::Status: |
| 69 | return QVariant(Utils::toUnderlyingValue<lrc::api::account::Status>(accountInfo.status)); |
| 70 | case Role::Picture: |
| 71 | case Qt::DecorationRole: |
| 72 | return PixbufManipulator::accountPhoto(accountInfo); |
| 73 | case Role::ID: |
| 74 | return QVariant(QString::fromStdString(accountInfo.id)); |
| 75 | } |
| 76 | return QVariant(); |
| 77 | } |
| 78 | |
| 79 | QModelIndex AccountListModel::index(int row, int column, const QModelIndex &parent) const |
| 80 | { |
| 81 | Q_UNUSED(parent); |
| 82 | if (column != 0) { |
| 83 | return QModelIndex(); |
| 84 | } |
| 85 | |
| 86 | if (row >= 0 && row < rowCount()) { |
| 87 | return createIndex(row, column); |
| 88 | } |
| 89 | return QModelIndex(); |
| 90 | } |
| 91 | |
| 92 | QModelIndex AccountListModel::parent(const QModelIndex &child) const |
| 93 | { |
| 94 | Q_UNUSED(child); |
| 95 | return QModelIndex(); |
| 96 | } |
| 97 | |
| 98 | Qt::ItemFlags AccountListModel::flags(const QModelIndex &index) const |
| 99 | { |
| 100 | auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable; |
| 101 | if (!index.isValid()) { |
| 102 | return QAbstractItemModel::flags(index); |
| 103 | } |
| 104 | return flags; |
| 105 | } |