devicesmodel.cpp 4.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * <one line to give the library's name and an idea of what it does.>
 * Copyright 2013  <copyright holder> <email>
 *
 * This program 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 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "devicesmodel.h"
24
#include <ksharedconfig.h>
25

26 27
#include <QDebug>
#include <KConfigGroup>
28 29 30 31

DevicesModel::DevicesModel(QObject *parent)
    : QAbstractItemModel(parent)
{
32

33 34 35 36 37 38
}

DevicesModel::~DevicesModel()
{
}

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
void DevicesModel::loadPaired()
{

    //TODO: Load from daemon, so we can know if they are currently connected or not

    removeRows(0,rowCount());

    KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
    const KConfigGroup& known = config->group("devices").group("paired");
    const QStringList& list = known.groupList();

    const QString defaultName("unnamed");

    Q_FOREACH(QString id, list) {

        const KConfigGroup& data = known.group(id);
        const QString& name = data.readEntry<QString>("name",defaultName);

        //qDebug() << id << name;

        addDevice(id,name,Visible);

    }

}

void DevicesModel::addDevice(QString id, QString name, DevicesModel::DeviceStatus status)
{
    int rown = rowCount();
    insertRows(rown,1);
    setData(index(rown,0),QVariant(id),IdModelRole);
    setData(index(rown,0),QVariant(name),NameModelRole);
    setData(index(rown,0),QVariant(PairedConnected),StatusModelRole);
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
72
    qDebug() << "Add device" << name;
73 74 75 76
    emit dataChanged(index(rown,0),index(rown,0));
}


77 78 79 80
int DevicesModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

81
    return 1; //We are not using the second dimension at all
82 83 84 85 86 87 88 89 90
}

QVariant DevicesModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() < 0 || index.row() >= m_deviceList.count()) {
        return QVariant();
    }
    switch (role) {
            case IconModelRole:
91 92 93
                return QPixmap(); //TODO: Return a pixmap to represent the status
            case IdModelRole:
                return m_deviceList[index.row()].id;
94
            case NameModelRole:
95 96 97
                return m_deviceList[index.row()].name;
            case StatusModelRole:
                return m_deviceList[index.row()].status;
98 99 100 101 102 103 104 105 106 107 108 109 110
            default:
                break;
    }
    return QVariant();
}

bool DevicesModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid() || index.row() < 0 || index.row() >= m_deviceList.count()) {
        return false;
    }
    switch (role) {
            case IconModelRole:
111
                qDebug() << "Icon can not be assigned to, change status instead";
112
                break;
113 114 115 116 117
            case IdModelRole:
                m_deviceList[index.row()].id = value.toString();
                break;
            case NameModelRole:
                m_deviceList[index.row()].name = value.toString();
118
                break;
119 120
            case StatusModelRole:
                m_deviceList[index.row()].status = (DeviceStatus)value.toInt();
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
                break;
            default:
                return false;
    }
    emit dataChanged(index, index);
    return true;
}

QModelIndex DevicesModel::index(int row, int column, const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    if (row < 0 || row >= m_deviceList.count() || column != 0) {
        return QModelIndex();
    }

    return createIndex(row, column);
}

QModelIndex DevicesModel::parent(const QModelIndex &index) const
{
    Q_UNUSED(index);

    return QModelIndex();
}

int DevicesModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return m_deviceList.count();
}

bool DevicesModel::insertRows(int row, int count, const QModelIndex &parent)
{
    if (row < 0 || row > m_deviceList.count() || count < 1) {
        return false;
    }
    beginInsertRows(parent, row, row + count - 1);
    for (int i = row; i < row + count; ++i) {
        m_deviceList.insert(i, Device());
    }
    endInsertRows();
    return true;
}

bool DevicesModel::removeRows(int row, int count, const QModelIndex &parent)
{
    if (row < 0 || row > m_deviceList.count() || count < 1) {
        return false;
    }
    beginRemoveRows(parent, row, row + count - 1);
    for (int i = row; i < row + count; ++i) {
        m_deviceList.removeAt(row);
    }
    endRemoveRows();
    return true;
}