devicesmodel.cpp 3.81 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
#include <QDebug>
27
#include <qdbusinterface.h>
28
#include <KConfigGroup>
29
#include <kicon.h>
30 31

DevicesModel::DevicesModel(QObject *parent)
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
32
    : QAbstractListModel(parent)
33
    , m_dbusInterface(new DaemonDbusInterface(this))
34
{
35 36 37 38 39 40 41
    QList<QString> deviceIds = m_dbusInterface->devices();
    connect(m_dbusInterface,SIGNAL(newDeviceAdded(QString)),this,SLOT(deviceAdded(QString)));
    Q_FOREACH(const QString& id, deviceIds) {
        deviceAdded(id);
    }
    connect(m_dbusInterface,SIGNAL(deviceStatusChanged(QString)),this,SLOT(deviceStatusChanged(QString)));
    //connect(m_dbusInterface,SIGNAL(deviceRemoved(QString)),this,SLOT(deviceRemoved(QString));
42 43 44 45 46 47
}

DevicesModel::~DevicesModel()
{
}

48
void DevicesModel::deviceStatusChanged(const QString& id)
49
{
50
    Q_UNUSED(id);
51 52
    qDebug() << "deviceStatusChanged";
    emit dataChanged(index(0),index(rowCount()));
53 54
}

55
void DevicesModel::deviceAdded(const QString& id)
56
{
57 58 59
    beginInsertRows(QModelIndex(), rowCount(), rowCount() + 1);
    m_deviceList.append(new DeviceDbusInterface(id,this));
    endInsertRows();
60 61
}

62 63
/*
bool DevicesModel::insertRows(int row, int count, const QModelIndex &parent)
64
{
65 66
    if (row < 0 || row > m_deviceList.count() || count < 1) {
        return false;
67
    }
68 69 70
    beginInsertRows(parent, row, row + count - 1);
    for (int i = row; i < row + count; ++i) {
        m_deviceList.insert(i, new Device());
71
    }
72 73
    endInsertRows();
    return true;
74 75
}

76
bool DevicesModel::removeRows(int row, int count, const QModelIndex &parent)
77
{
78
    if (row < 0 || row > m_deviceList.count() || count < 1) {
79 80
        return false;
    }
81 82 83
    beginRemoveRows(parent, row, row + count - 1);
    for (int i = row; i < row + count; ++i) {
        m_deviceList.removeAt(row);
84
    }
85
    endRemoveRows();
86 87
    return true;
}
88 89
*/
QVariant DevicesModel::data(const QModelIndex &index, int role) const
90
{
91 92
    if (!index.isValid() || index.row() < 0 || index.row() >= m_deviceList.count()) {
        return QVariant();
93
    }
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    //FIXME: This function gets called lots of times per second, producing lots of dbus calls
    switch (role) {
        case IconModelRole: {
            bool paired = m_deviceList[index.row()]->paired();
            bool reachable = m_deviceList[index.row()]->reachable();
            QString icon = reachable? (paired? "user-online" : "user-busy") : "user-offline";
            return KIcon(icon).pixmap(32, 32);
        }
        case IdModelRole:
            return QString(m_deviceList[index.row()]->id());
        case NameModelRole:
            return QString(m_deviceList[index.row()]->name());
        default:
            break;
    }
    return QVariant();
110 111
}

112
DeviceDbusInterface* DevicesModel::getDevice(const QModelIndex& index)
113
{
114
    return m_deviceList[index.row()];
115 116 117 118 119 120 121 122 123
}

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

    return m_deviceList.count();
}