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 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());
87 return item
->data(index
.column());
88 case Qt::DecorationRole
:
89 if (item
->data(index
.column()).toString().contains("://")) //TODO: clean impl
92 else if (item
->data(index
.column()).toString() == "Local Network") //TODO: clean impl
93 return KIcon("network-workgroup");
95 else if (item
->data(index
.column()).toString() == "...") //TODO: clean impl
96 return KIcon("view-history");
98 return KIcon("folder-bookmarks");
99 case Qt::UserRole
: // tell if it is an address
100 return QVariant(item
->data(index
.column()).toString().contains("://"));
106 Qt::ItemFlags
RemoteDesktopsModel::flags(const QModelIndex
&index
) const
108 if (!index
.isValid())
111 return Qt::ItemIsEnabled
| Qt::ItemIsSelectable
;
114 QVariant
RemoteDesktopsModel::headerData(int section
, Qt::Orientation orientation
,
117 if (orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
)
118 return rootItem
->data(section
);
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
;
133 parentItem
= static_cast<RemoteDesktopsItem
*>(parent
.internalPointer());
135 RemoteDesktopsItem
*childItem
= parentItem
->child(row
);
137 return createIndex(row
, column
, childItem
);
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)
162 if (!parent
.isValid())
163 parentItem
= rootItem
;
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
);
177 buildModelFromBookmarkGroup(bm
.toGroup(), newItem
); //recursive
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()),
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();
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());
220 #include "remotedesktopsmodel.moc"