blob: a418e7b20ef539754e2ff3c1a178d565fecf69d9 [file] [log] [blame]
Andreas Traczykb8b13ba2018-08-21 16:30:16 -04001/***************************************************************************
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
32AccountListModel::AccountListModel(QObject *parent)
33 : QAbstractItemModel(parent)
34{
35}
36
37int 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
45int AccountListModel::columnCount(const QModelIndex &parent) const
46{
47 Q_UNUSED(parent);
48 return 1;
49}
50
51QVariant AccountListModel::data(const QModelIndex &index, int role) const
52{
53 auto accountList = LRCInstance::accountModel().getAccountList();
Andreas Traczyk55f92cb2018-10-23 12:26:25 -040054 if (!index.isValid() || accountList.size() <= index.row()) {
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040055 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 Nanicf7e07922018-10-29 10:52:05 -040065 return QVariant(QString::fromStdString(Utils::secondBestNameForAccount(accountInfo)));
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040066 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
79QModelIndex 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
92QModelIndex AccountListModel::parent(const QModelIndex &child) const
93{
94 Q_UNUSED(child);
95 return QModelIndex();
96}
97
98Qt::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}