1 /****************************************************************************
3 ** Copyright (C) 2008 Urs Wolfer <uwolfer @ kde.org>
5 ** This file is part of KDE.
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>
30 #include <KStandardDirs>
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
);
49 localNetworkItem
= new RemoteDesktopsItem(QList
<QVariant
>() << "Local Network", rootItem
);
50 rootItem
->appendChild(localNetworkItem
);
54 localNetworkItem
->appendChild(new RemoteDesktopsItem(QList
<QVariant
>() << "...", localNetworkItem
));
58 RemoteDesktopsModel::~RemoteDesktopsModel()
62 void RemoteDesktopsModel::changed()
65 rootItem
->clearChildren();
66 buildModelFromBookmarkGroup(m_manager
->root(), rootItem
);
70 int RemoteDesktopsModel::columnCount(const QModelIndex
&parent
) const
73 return static_cast<RemoteDesktopsItem
*>(parent
.internalPointer())->columnCount();
75 return rootItem
->columnCount();
78 QVariant
RemoteDesktopsModel::data(const QModelIndex
&index
, int role
) const
83 RemoteDesktopsItem
*item
= static_cast<RemoteDesktopsItem
*>(index
.internalPointer());
88 return item
->data(index
.column());
89 case Qt::DecorationRole
:
90 if (item
->data(index
.column()).toString().contains("://")) //TODO: clean impl
93 else if (item
->data(index
.column()).toString() == "Local Network") //TODO: clean impl
94 return KIcon("network-workgroup");
96 else if (item
->data(index
.column()).toString() == "...") //TODO: clean impl
97 return KIcon("view-history");
99 return KIcon("folder-bookmarks");
101 if (item
->data(index
.column() + 1).toString().contains("://"))
102 return QVariant(item
->data(index
.column() + 1).toString());
110 Qt::ItemFlags
RemoteDesktopsModel::flags(const QModelIndex
&index
) const
112 if (!index
.isValid())
115 return Qt::ItemIsEnabled
| Qt::ItemIsSelectable
;
118 QVariant
RemoteDesktopsModel::headerData(int section
, Qt::Orientation orientation
,
121 if (orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
)
122 return rootItem
->data(section
);
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
;
137 parentItem
= static_cast<RemoteDesktopsItem
*>(parent
.internalPointer());
139 RemoteDesktopsItem
*childItem
= parentItem
->child(row
);
141 return createIndex(row
, column
, childItem
);
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)
166 if (!parent
.isValid())
167 parentItem
= rootItem
;
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
);
181 buildModelFromBookmarkGroup(bm
.toGroup(), newItem
); //recursive
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()),
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();
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());
224 #include "remotedesktopsmodel.moc"