summary refs log tree commit diff
path: root/src/ui/UserProfileModel.cpp
blob: ec0456cd94b42c3f328c7487af3aee1f53770c34 (plain) (blame)
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
#include "UserProfileModel.h"
#include "UserProfile.h"
#include <QModelIndex>

UserProfileModel::UserProfileModel(QObject *parent)
  : QAbstractListModel(parent)
  , deviceList(nullptr)
{}

int
UserProfileModel::rowCount(const QModelIndex &parent) const
{
        if (parent.isValid() || !this->deviceList)
                return 0;
        return this->deviceList->getDeviceList().size();
}

QVariant
UserProfileModel::data(const QModelIndex &index, int role) const
{
        if (!index.isValid() || !this->deviceList)
                return QVariant();

        const DeviceInfo device = this->deviceList->getDeviceList().at(index.row());
        switch (role) {
        case DEVICEID:
                return QVariant(device.device_id);
        case DISPLAYNAME:
                return QVariant(device.display_name);
        }
        return QVariant();
}

QHash<int, QByteArray>
UserProfileModel::roleNames() const
{
        QHash<int, QByteArray> names;
        names[DEVICEID]    = "deviceID";
        names[DISPLAYNAME] = "displayName";
        return names;
}

UserProfile *
UserProfileModel::getList() const
{
        return (this->deviceList);
}

void
UserProfileModel::setList(UserProfile *devices)
{
        beginResetModel();

        if (devices)
                devices->disconnect(this);

        if (this->deviceList) {
                const int index = this->deviceList->getDeviceList().size();
                beginInsertRows(QModelIndex(), index, index);
                endInsertRows();
        }

        this->deviceList = devices;

        endResetModel();
}