remotedesktopsmodel.cpp 8.96 KB
Newer Older
1 2 3
/****************************************************************************
**
** Copyright (C) 2008 Urs Wolfer <uwolfer @ kde.org>
4
** Copyright (C) 2009 Tony Murray <murraytony@gmail.com>
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
**
** This file is part of KDE.
**
** 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) any later version.
**
** 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; see the file COPYING. If not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Boston, MA 02110-1301, USA.
**
****************************************************************************/

#include "remotedesktopsmodel.h"

#include "remotedesktopsitem.h"

#include <KBookmarkManager>
30
#include <KIcon>
31 32
#include <KStandardDirs>
#include <KDebug>
33 34
#include <KLocale>
#include <KProcess>
35

36 37 38 39
#ifdef BUILD_ZEROCONF
#include <dnssd/remoteservice.h>
#endif

40 41 42
RemoteDesktopsModel::RemoteDesktopsModel(QObject *parent)
        : QAbstractItemModel(parent)
{
43
    rootItem = new RemoteDesktopsItem(QList<QVariant>() << "Remote Desktops");
44 45
    bookmarkItem = new RemoteDesktopsItem(QList<QVariant>() << "Bookmarks", rootItem);
    rootItem->appendChild(bookmarkItem);
46

Urs Wolfer's avatar
Urs Wolfer committed
47
    const QString file = KStandardDirs::locateLocal("data", "krdc/bookmarks.xml");
48 49 50

    m_manager = KBookmarkManager::managerForFile(file, "krdc");
    m_manager->setUpdate(true);
51 52 53
    connect(m_manager, SIGNAL(changed(const QString &, const QString &)), SLOT(bookmarksChanged()));

    buildModelFromBookmarkGroup(m_manager->root(), bookmarkItem);
54

55 56 57
#ifdef BUILD_ZEROCONF
    // Add RDP and NX if they start announcing via Zeroconf:
    m_protocols["_rfb._tcp"] = "vnc";
58

59 60 61 62 63 64 65
    zeroconfItem = new RemoteDesktopsItem(QList<QVariant>() << "Discovered Network Servers", rootItem);
    rootItem->appendChild(zeroconfItem);

    zeroconfBrowser = new DNSSD::ServiceBrowser("_rfb._tcp", true);
    connect(zeroconfBrowser, SIGNAL(finished()), this, SLOT(servicesChanged()));
    zeroconfBrowser->startBrowse();
#endif
66 67 68 69 70
#if 0
    localNetworkItem = new RemoteDesktopsItem(QList<QVariant>() << "Local Network", rootItem);
    rootItem->appendChild(localNetworkItem);

    scanLocalNetwork();
71

72 73
    localNetworkItem->appendChild(new RemoteDesktopsItem(QList<QVariant>() << "...", localNetworkItem));
#endif
74 75 76 77 78 79
}

RemoteDesktopsModel::~RemoteDesktopsModel()
{
}

80
void RemoteDesktopsModel::bookmarksChanged()
81 82
{
    kDebug(5010);
83 84 85
    emit layoutAboutToBeChanged();
    bookmarkItem->clearChildren();
    buildModelFromBookmarkGroup(m_manager->root(), bookmarkItem);
86
    reset();
87
    emit layoutChanged();
88 89
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103
int RemoteDesktopsModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return static_cast<RemoteDesktopsItem*>(parent.internalPointer())->columnCount();
    else
        return rootItem->columnCount();
}

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

    RemoteDesktopsItem *item = static_cast<RemoteDesktopsItem*>(index.internalPointer());
104
    const QString currentItemTitleString = item->data(0).toString();
105
    const QString currentItemString = item->data(1).toString();
106

107 108 109 110 111
#ifdef BUILD_ZEROCONF
    if (currentItemTitleString == "Discovered Network Servers" && zeroconfItem->childCount()<=0)
        return QVariant();
#endif

112 113 114
    switch (role) {
    case Qt::DisplayRole:
        return item->data(index.column());
115
    case Qt::ToolTipRole:
116
        return currentItemString;  //use the url for the tooltip
117
    case Qt::DecorationRole:
118
        if (!currentItemString.isEmpty()) // contains an url
119
            return KIcon("krdc");
120
#ifdef BUILD_ZEROCONF
121
        else if (currentItemTitleString == "Discovered Network Servers")
122 123
            return KIcon("network-workgroup");
#endif
124
#if 0
125
        else if (currentItemTitleString == "Local Network")
126
            return KIcon("network-workgroup");
127
        else if (currentItemTitleString == "...")
128
            return KIcon("view-history");
129
#endif
130 131
        else
            return KIcon("folder-bookmarks");
132
    case 10001: //url for dockwidget
133 134 135 136 137 138
        return currentItemString;
    case 10002: //filter
        if (!currentItemString.isEmpty())
            return currentItemString + item->data(0).toString(); // return both uservisible and data
        else
            return "IGNORE";
139 140 141
    default:
        return QVariant();
    }
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 179 180 181 182 183 184 185 186 187
}

Qt::ItemFlags RemoteDesktopsModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant RemoteDesktopsModel::headerData(int section, Qt::Orientation orientation,
                                         int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
        return rootItem->data(section);

    return QVariant();
}

QModelIndex RemoteDesktopsModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!hasIndex(row, column, parent))
        return QModelIndex();

    RemoteDesktopsItem *parentItem;

    if (!parent.isValid())
        parentItem = rootItem;
    else
        parentItem = static_cast<RemoteDesktopsItem*>(parent.internalPointer());

    RemoteDesktopsItem *childItem = parentItem->child(row);
    if (childItem)
        return createIndex(row, column, childItem);
    else
        return QModelIndex();
}

QModelIndex RemoteDesktopsModel::parent(const QModelIndex &index) const
{
    if (!index.isValid())
        return QModelIndex();

    RemoteDesktopsItem *childItem = static_cast<RemoteDesktopsItem*>(index.internalPointer());
    RemoteDesktopsItem *parentItem = childItem->parent();

188
    if (parentItem == 0 || parentItem == rootItem)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        return QModelIndex();

    return createIndex(parentItem->row(), 0, parentItem);
}

int RemoteDesktopsModel::rowCount(const QModelIndex &parent) const
{
    RemoteDesktopsItem *parentItem;
    if (parent.column() > 0)
        return 0;

    if (!parent.isValid())
        parentItem = rootItem;
    else
        parentItem = static_cast<RemoteDesktopsItem*>(parent.internalPointer());

    return parentItem->childCount();
}

208 209 210 211
void RemoteDesktopsModel::buildModelFromBookmarkGroup(const KBookmarkGroup &group, RemoteDesktopsItem *item)
{
    KBookmark bm = group.first();
    while (!bm.isNull()) {
212
        RemoteDesktopsItem *newItem = new RemoteDesktopsItem(QList<QVariant>() << bm.text() << bm.url().url(), item);
213 214 215 216 217 218 219
        item->appendChild(newItem);
        if (bm.isGroup())
            buildModelFromBookmarkGroup(bm.toGroup(), newItem); //recursive
        bm = group.next(bm);
    }
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#ifdef BUILD_ZEROCONF
void RemoteDesktopsModel::servicesChanged() {
    //redo list because it is easier than finding and removing one
    QList<DNSSD::RemoteService::Ptr> services = zeroconfBrowser->services();
    KUrl url;
    emit layoutAboutToBeChanged();
    zeroconfItem->clearChildren();
    foreach(DNSSD::RemoteService::Ptr service, services) {
        url.setProtocol(m_protocols[service->type()].toLower());
        url.setHost(service->hostName());
        url.setPort(service->port());
//tooltip        service->hostName()+"."+service->domain();
        RemoteDesktopsItem *newItem = new RemoteDesktopsItem(QList<QVariant>() << service->serviceName() << url.url(), zeroconfItem);
        zeroconfItem->appendChild(newItem);
    }
    emit layoutChanged();
}
#endif
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
#if 0
void RemoteDesktopsModel::scanLocalNetwork()
{
    m_scanProcess = new KProcess(this);
    m_scanProcess->setOutputChannelMode(KProcess::SeparateChannels);
    QStringList args(QStringList() << "-vv" << "-PN" << "-p5901" << "192.168.1.0-255");
    connect(m_scanProcess, SIGNAL(readyReadStandardOutput()),
                           SLOT(readInput()));
    m_scanProcess->setProgram("nmap", args);
    m_scanProcess->start();
}

void RemoteDesktopsModel::readInput()
{
    // we do not know if the output array ends in the middle of an utf-8 sequence
    m_output += m_scanProcess->readAllStandardOutput();

    int pos;
    while ((pos = m_output.indexOf('\n')) != -1) {
        QString line = QString::fromLocal8Bit(m_output, pos + 1);
        m_output.remove(0, pos + 1);

        if (line.contains("open port")) {
            kDebug(5010) << line;

            QString ip(line.mid(line.lastIndexOf(' ') + 1));
            ip = ip.left(ip.length() - 1);

            QString port(line.left(line.indexOf('/')));
            port = port.mid(port.lastIndexOf(' ') + 1);
            RemoteDesktopsItem *item = new RemoteDesktopsItem(QList<QVariant>() << "vnc://" + ip + ':' + port, localNetworkItem);
            localNetworkItem->appendChild(item);
            emit dataChanged(QModelIndex(), QModelIndex());
        }
    }
}
#endif

276
#include "remotedesktopsmodel.moc"