File: accountsModel.cpp

package info (click to toggle)
plasma-gmailfeed 2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: cpp: 459; xml: 19; sh: 6; makefile: 3
file content (103 lines) | stat: -rw-r--r-- 3,398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/****************************************************************************
 *  Copyright (c) 2015 Anthony Vital <anthony.vital@gmail.com>              *
 *                                                                          *
 *  This file is part of Gmail Feed.                                        *
 *                                                                          *
 *  Gmail Feed is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation, either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  Gmail Feed 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 General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with Gmail Feed.  If not, see <http://www.gnu.org/licenses/>.     *
 ****************************************************************************/

#include "accountsModel.h"

#include <KAccounts/Core>

#include <Accounts/Manager>

AccountsModel::AccountsModel(QObject *parent)
    : QAbstractListModel(parent)
{
    auto accounts =  KAccounts::accountsManager()->accountList(QStringLiteral("gmail-feed"));
    Q_FOREACH(Accounts::AccountId id, accounts) {
        m_accounts.append(id);
    }

    connect(KAccounts::accountsManager(), &Accounts::Manager::accountCreated, this, &AccountsModel::accountCreated);
    connect(KAccounts::accountsManager(), &Accounts::Manager::accountRemoved, this, &AccountsModel::accountRemoved);
}

AccountsModel::~AccountsModel()
{
}

QHash<int, QByteArray> AccountsModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[NameRole] = "name";
    roles[IdRole] = "id";
    return roles;
}


int AccountsModel::rowCount(const QModelIndex & parent) const
{
    Q_UNUSED(parent)
    return m_accounts.size();
}

QVariant AccountsModel::data(const QModelIndex & index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    if (index.row() >= m_accounts.size() || index.row() < 0) {
        return QVariant();
    }

    switch (role) {
        case IdRole:
            return m_accounts.at(index.row());
        case NameRole:
            return KAccounts::accountsManager()->account(m_accounts.at(index.row()))->displayName();
        default:
            return QVariant();
    }
}

void AccountsModel::accountCreated(Accounts::AccountId id)
{
    beginInsertRows(QModelIndex(), m_accounts.size(), m_accounts.size());
    m_accounts.append(id);
    endInsertRows();
}

void AccountsModel::accountRemoved(Accounts::AccountId id)
{
    int i = m_accounts.indexOf(id);
    if (i < 0) {
        return;
    }
    beginRemoveRows(QModelIndex(), i, i);
    m_accounts.removeAt(i);
    endRemoveRows();
}

int AccountsModel::getId(int index) const
{
    return int(m_accounts.at(index));
}

int AccountsModel::indexOf(int id) const
{
    return m_accounts.indexOf(id);
}