* major layout fixes (size, stacked widget, ...)
[kdenetwork.git] / krdc / remotedesktopsmodel.cpp
blob03a76798195b7cd7a848dea803621ab66f658573
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 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 return item->data(index.column());
88 case Qt::DecorationRole:
89 if (item->data(index.column()).toString().contains("://")) //TODO: clean impl
90 return KIcon("krdc");
91 #if 0
92 else if (item->data(index.column()).toString() == "Local Network") //TODO: clean impl
93 return KIcon("network-workgroup");
94 #endif
95 else if (item->data(index.column()).toString() == "...") //TODO: clean impl
96 return KIcon("view-history");
97 else
98 return KIcon("folder-bookmarks");
99 case Qt::UserRole: // tell if it is an address
100 return QVariant(item->data(index.column()).toString().contains("://"));
101 default:
102 return QVariant();
106 Qt::ItemFlags RemoteDesktopsModel::flags(const QModelIndex &index) const
108 if (!index.isValid())
109 return 0;
111 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
114 QVariant RemoteDesktopsModel::headerData(int section, Qt::Orientation orientation,
115 int role) const
117 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
118 return rootItem->data(section);
120 return QVariant();
123 QModelIndex RemoteDesktopsModel::index(int row, int column, const QModelIndex &parent) const
125 if (!hasIndex(row, column, parent))
126 return QModelIndex();
128 RemoteDesktopsItem *parentItem;
130 if (!parent.isValid())
131 parentItem = rootItem;
132 else
133 parentItem = static_cast<RemoteDesktopsItem*>(parent.internalPointer());
135 RemoteDesktopsItem *childItem = parentItem->child(row);
136 if (childItem)
137 return createIndex(row, column, childItem);
138 else
139 return QModelIndex();
142 QModelIndex RemoteDesktopsModel::parent(const QModelIndex &index) const
144 if (!index.isValid())
145 return QModelIndex();
147 RemoteDesktopsItem *childItem = static_cast<RemoteDesktopsItem*>(index.internalPointer());
148 RemoteDesktopsItem *parentItem = childItem->parent();
150 if (parentItem == rootItem)
151 return QModelIndex();
153 return createIndex(parentItem->row(), 0, parentItem);
156 int RemoteDesktopsModel::rowCount(const QModelIndex &parent) const
158 RemoteDesktopsItem *parentItem;
159 if (parent.column() > 0)
160 return 0;
162 if (!parent.isValid())
163 parentItem = rootItem;
164 else
165 parentItem = static_cast<RemoteDesktopsItem*>(parent.internalPointer());
167 return parentItem->childCount();
170 void RemoteDesktopsModel::buildModelFromBookmarkGroup(const KBookmarkGroup &group, RemoteDesktopsItem *item)
172 KBookmark bm = group.first();
173 while (!bm.isNull()) {
174 RemoteDesktopsItem *newItem = new RemoteDesktopsItem(QList<QVariant>() << bm.text(), item);
175 item->appendChild(newItem);
176 if (bm.isGroup())
177 buildModelFromBookmarkGroup(bm.toGroup(), newItem); //recursive
178 bm = group.next(bm);
182 #if 0
183 void RemoteDesktopsModel::scanLocalNetwork()
185 m_scanProcess = new KProcess(this);
186 m_scanProcess->setOutputChannelMode(KProcess::SeparateChannels);
187 QStringList args(QStringList() << "-vv" << "-PN" << "-p5901" << "192.168.1.0-255");
188 connect(m_scanProcess, SIGNAL(readyReadStandardOutput()),
189 SLOT(readInput()));
190 m_scanProcess->setProgram("nmap", args);
191 m_scanProcess->start();
194 void RemoteDesktopsModel::readInput()
196 // we do not know if the output array ends in the middle of an utf-8 sequence
197 m_output += m_scanProcess->readAllStandardOutput();
199 int pos;
200 while ((pos = m_output.indexOf('\n')) != -1) {
201 QString line = QString::fromLocal8Bit(m_output, pos + 1);
202 m_output.remove(0, pos + 1);
204 if (line.contains("open port")) {
205 kDebug(5010) << line;
207 QString ip(line.mid(line.lastIndexOf(' ') + 1));
208 ip = ip.left(ip.length() - 1);
210 QString port(line.left(line.indexOf('/')));
211 port = port.mid(port.lastIndexOf(' ') + 1);
212 RemoteDesktopsItem *item = new RemoteDesktopsItem(QList<QVariant>() << "vnc://" + ip + ':' + port, localNetworkItem);
213 localNetworkItem->appendChild(item);
214 emit dataChanged(QModelIndex(), QModelIndex());
218 #endif
220 #include "remotedesktopsmodel.moc"