Use a nicer way to check if rdesktop is installed.
[kdenetwork.git] / krdc / remotedesktopsmodel.cpp
blobe7b3564dad6a2e8bcd1b576fd6126f21911d1327
1 /****************************************************************************
2 **
3 ** Copyright (C) 2008 Urs Wolfer <uwolfer @ kde.org>
4 **
5 ** This file is part of KDE.
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; see the file COPYING. If not, write to
19 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ** Boston, MA 02110-1301, USA.
22 ****************************************************************************/
24 #include "remotedesktopsmodel.h"
26 #include "remotedesktopsitem.h"
28 #include <KBookmarkManager>
29 #include <KIcon>
30 #include <KStandardDirs>
31 #include <KDebug>
32 #include <KLocale>
33 #include <KProcess>
35 RemoteDesktopsModel::RemoteDesktopsModel(QObject *parent)
36 : QAbstractItemModel(parent)
38 rootItem = new RemoteDesktopsItem(QList<QVariant>() << "Remote desktops");
40 const QString file = KStandardDirs::locateLocal("data", "krdc/bookmarks.xml");
42 m_manager = KBookmarkManager::managerForFile(file, "krdc");
43 m_manager->setUpdate(true);
44 connect(m_manager, SIGNAL(changed(const QString &, const QString &)), SLOT(changed()));
46 buildModelFromBookmarkGroup(m_manager->root(), rootItem);
48 #if 0
49 localNetworkItem = new RemoteDesktopsItem(QList<QVariant>() << "Local Network", rootItem);
50 rootItem->appendChild(localNetworkItem);
52 scanLocalNetwork();
54 localNetworkItem->appendChild(new RemoteDesktopsItem(QList<QVariant>() << "...", localNetworkItem));
55 #endif
58 RemoteDesktopsModel::~RemoteDesktopsModel()
62 void RemoteDesktopsModel::changed()
64 kDebug(5010);
65 rootItem->clearChildren();
66 buildModelFromBookmarkGroup(m_manager->root(), rootItem);
67 reset();
70 int RemoteDesktopsModel::columnCount(const QModelIndex &parent) const
72 if (parent.isValid())
73 return static_cast<RemoteDesktopsItem*>(parent.internalPointer())->columnCount();
74 else
75 return rootItem->columnCount();
78 QVariant RemoteDesktopsModel::data(const QModelIndex &index, int role) const
80 if (!index.isValid())
81 return QVariant();
83 RemoteDesktopsItem *item = static_cast<RemoteDesktopsItem*>(index.internalPointer());
85 switch (role) {
86 case Qt::DisplayRole:
87 case Qt::ToolTipRole:
88 return item->data(index.column());
89 case Qt::DecorationRole:
90 if (item->data(index.column()).toString().contains("://")) //TODO: clean impl
91 return KIcon("krdc");
92 #if 0
93 else if (item->data(index.column()).toString() == "Local Network") //TODO: clean impl
94 return KIcon("network-workgroup");
95 #endif
96 else if (item->data(index.column()).toString() == "...") //TODO: clean impl
97 return KIcon("view-history");
98 else
99 return KIcon("folder-bookmarks");
100 case Qt::UserRole:
101 if (item->data(index.column() + 1).toString().contains("://"))
102 return QVariant(item->data(index.column() + 1).toString());
103 else
104 return QVariant();
105 default:
106 return QVariant();
110 Qt::ItemFlags RemoteDesktopsModel::flags(const QModelIndex &index) const
112 if (!index.isValid())
113 return 0;
115 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
118 QVariant RemoteDesktopsModel::headerData(int section, Qt::Orientation orientation,
119 int role) const
121 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
122 return rootItem->data(section);
124 return QVariant();
127 QModelIndex RemoteDesktopsModel::index(int row, int column, const QModelIndex &parent) const
129 if (!hasIndex(row, column, parent))
130 return QModelIndex();
132 RemoteDesktopsItem *parentItem;
134 if (!parent.isValid())
135 parentItem = rootItem;
136 else
137 parentItem = static_cast<RemoteDesktopsItem*>(parent.internalPointer());
139 RemoteDesktopsItem *childItem = parentItem->child(row);
140 if (childItem)
141 return createIndex(row, column, childItem);
142 else
143 return QModelIndex();
146 QModelIndex RemoteDesktopsModel::parent(const QModelIndex &index) const
148 if (!index.isValid())
149 return QModelIndex();
151 RemoteDesktopsItem *childItem = static_cast<RemoteDesktopsItem*>(index.internalPointer());
152 RemoteDesktopsItem *parentItem = childItem->parent();
154 if (parentItem == rootItem)
155 return QModelIndex();
157 return createIndex(parentItem->row(), 0, parentItem);
160 int RemoteDesktopsModel::rowCount(const QModelIndex &parent) const
162 RemoteDesktopsItem *parentItem;
163 if (parent.column() > 0)
164 return 0;
166 if (!parent.isValid())
167 parentItem = rootItem;
168 else
169 parentItem = static_cast<RemoteDesktopsItem*>(parent.internalPointer());
171 return parentItem->childCount();
174 void RemoteDesktopsModel::buildModelFromBookmarkGroup(const KBookmarkGroup &group, RemoteDesktopsItem *item)
176 KBookmark bm = group.first();
177 while (!bm.isNull()) {
178 RemoteDesktopsItem *newItem = new RemoteDesktopsItem(QList<QVariant>() << bm.text() << bm.url().url(), item);
179 item->appendChild(newItem);
180 if (bm.isGroup())
181 buildModelFromBookmarkGroup(bm.toGroup(), newItem); //recursive
182 bm = group.next(bm);
186 #if 0
187 void RemoteDesktopsModel::scanLocalNetwork()
189 m_scanProcess = new KProcess(this);
190 m_scanProcess->setOutputChannelMode(KProcess::SeparateChannels);
191 QStringList args(QStringList() << "-vv" << "-PN" << "-p5901" << "192.168.1.0-255");
192 connect(m_scanProcess, SIGNAL(readyReadStandardOutput()),
193 SLOT(readInput()));
194 m_scanProcess->setProgram("nmap", args);
195 m_scanProcess->start();
198 void RemoteDesktopsModel::readInput()
200 // we do not know if the output array ends in the middle of an utf-8 sequence
201 m_output += m_scanProcess->readAllStandardOutput();
203 int pos;
204 while ((pos = m_output.indexOf('\n')) != -1) {
205 QString line = QString::fromLocal8Bit(m_output, pos + 1);
206 m_output.remove(0, pos + 1);
208 if (line.contains("open port")) {
209 kDebug(5010) << line;
211 QString ip(line.mid(line.lastIndexOf(' ') + 1));
212 ip = ip.left(ip.length() - 1);
214 QString port(line.left(line.indexOf('/')));
215 port = port.mid(port.lastIndexOf(' ') + 1);
216 RemoteDesktopsItem *item = new RemoteDesktopsItem(QList<QVariant>() << "vnc://" + ip + ':' + port, localNetworkItem);
217 localNetworkItem->appendChild(item);
218 emit dataChanged(QModelIndex(), QModelIndex());
222 #endif
224 #include "remotedesktopsmodel.moc"