-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathinterfacemodel.cpp
233 lines (201 loc) · 8.16 KB
/
interfacemodel.cpp
1
2
3
4
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/****************************************************************************
**
** Copyright (C) 2008 Ben Klopfenstein <[email protected]>
**
** This file is part of QtEmu.
**
** 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 Library General Public License
** along with this library; see the file COPYING.LIB. If not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Boston, MA 02110-1301, USA.
**
****************************************************************************/
/****************************************************************************
**
** C++ Implementation: interfacemodel
**
** Description: provides a model for the model/view framework to describe the
** various options for a qemu network interface, both guest and host models
** are provided.
**
****************************************************************************/
#include <QStringList>
#include "interfacemodel.h"
#include "machineconfigobject.h"
InterfaceModel::InterfaceModel(MachineConfigObject * config, QString nodeType, QObject * parent)
: QAbstractTableModel(parent)
, config(config)
, nodeType(nodeType)
{
connect(config->getConfig(), SIGNAL(optionChanged(const QString&, const QString&, const QString&, const QVariant&)), this, SLOT(optionChanged(const QString&, const QString&, const QString&, const QVariant&)));
}
int InterfaceModel::rowCount(const QModelIndex & parent) const
{
if(parent.isValid())
return 0;
int rows = config->getConfig()->getNumOptions(nodeType, "");
//qDebug("rows %i", rows);
return rows;
}
int InterfaceModel::columnCount(const QModelIndex & parent) const
{
if(parent.isValid())
return 0;
return columns.size();
}
QVariant InterfaceModel::data(const QModelIndex & index, int role) const
{
if(!index.isValid())
return QVariant();
//maybe this could be optimized by caching the stringlists...
QString nodeName = rowName(index.row());
QString optionName = colName(index.column());
if (role == Qt::DisplayRole || role == Qt::EditRole)
return config->getOption(nodeType, nodeName, optionName, QVariant());
else
return QVariant();
}
QVariant InterfaceModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal)
{
if (role == Qt::DisplayRole)
return columns.at(section);
}
return QAbstractTableModel::headerData(section, orientation, role);
}
Qt::ItemFlags InterfaceModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
bool InterfaceModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
if (index.isValid() && (role == Qt::EditRole || role == Qt::DisplayRole))
{
QString nodeName = rowName(index.row());
QString optionName = colName(index.column());
config->getConfig()->setOption(nodeType, nodeName, optionName, value);
emit dataChanged(index, index);
return true;
}
return false;
}
QString InterfaceModel::rowName(int row) const
{
QStringList names = config->getConfig()->getAllOptionNames(nodeType, "");
if(row <= names.size())
return names.at(row);
else
return QString();
}
QString InterfaceModel::colName(int col) const
{
return(columns.at(col));
}
void InterfaceModel::optionChanged(const QString & nodeType, const QString & nodeName, const QString & optionName, const QVariant & value)
{
if(nodeType == this->nodeType)
{
reset();
}
}
GuestInterfaceModel::GuestInterfaceModel(MachineConfigObject * config, QObject * parent)
: InterfaceModel(config, QString("net-guest"), parent)
{
columns << "name" << "mac" << "enabled";
}
bool GuestInterfaceModel::insertRows(int row, int count, const QModelIndex & parent)
{
Q_UNUSED(parent);
QString nodeName;
int interfaceNumber = 0;
beginInsertRows(parent, row, row + count - 1);
for (int i=0; i<count; i++)
{
for(;config->getOption(nodeType, "", QString("guest" + QString::number(interfaceNumber)), QVariant()).isValid();interfaceNumber++);
nodeName = "guest" + QString::number(interfaceNumber);
//set all options
config->setOption(nodeType, nodeName, "name", QString(QString("Interface ") + QString::number(interfaceNumber)));
config->setOption(nodeType, nodeName, "nic", "rtl8139");
config->setOption(nodeType, nodeName, "mac", "random");
config->setOption(nodeType, nodeName, "randomize", false);
config->setOption(nodeType, nodeName, "host", QString(QString("Interface ") + QString::number(interfaceNumber)));
config->setOption(nodeType, nodeName, "enabled", true);
}
endInsertRows();
return true;
}
bool GuestInterfaceModel::removeRows(int row, int count, const QModelIndex & parent)
{
Q_UNUSED(parent);
QString nodeName;
beginRemoveRows(parent, row, row + count - 1);
for (int i = row; i < (row + count); i++)
{
nodeName = config->getConfig()->getAllOptionNames(nodeType, "").at(i);
config->getConfig()->clearOption(nodeType, "", nodeName);
}
endRemoveRows();
return true;
}
HostInterfaceModel::HostInterfaceModel(MachineConfigObject * config, QObject * parent)
: InterfaceModel(config, QString("net-host"), parent)
{
columns << "name" << "type";
}
bool HostInterfaceModel::insertRows(int row, int count, const QModelIndex & parent)
{
Q_UNUSED(parent);
QString nodeName;
int interfaceNumber = 0;
beginInsertRows(parent, row, count);
for (int i=0; i<count; i++)
{
for(;config->getOption(nodeType, "", QString("host" + QString::number(interfaceNumber)), QVariant()).isValid();interfaceNumber++);
nodeName = "host" + QString::number(interfaceNumber);
//set all options
config->setOption(nodeType, nodeName, "name", QString(QString("Interface ") + QString::number(interfaceNumber)));
config->setOption(nodeType, nodeName, "type", "User Mode");
config->setOption(nodeType, nodeName, "interface", "qtemu-" + config->getOption("name").toString().replace(' ', '_') + '-' + config->getOption(nodeType, nodeName, "name", "Interface_" + QString::number(interfaceNumber)).toString().replace(' ', '_'));
config->setOption(nodeType, nodeName, "bridgeInterface", "qtemu-" + config->getOption("name").toString().replace(' ', '_') + "-br" + QString::number(interfaceNumber));
config->setOption(nodeType, nodeName, "hardwareInterface", "eth0");
config->setOption(nodeType, nodeName, "spanningTree", false);
config->setOption(nodeType, nodeName, "ifUp", QString());
config->setOption(nodeType, nodeName, "ifDown", QString());
config->setOption(nodeType, nodeName, "hostname", "qtemu_guest");
config->setOption(nodeType, nodeName, "tftp", false);
config->setOption(nodeType, nodeName, "tftpPath", QString());
config->setOption(nodeType, nodeName, "bootp", false);
config->setOption(nodeType, nodeName, "bootpPath", QString());
config->setOption(nodeType, nodeName, "vlanType", "udp");
config->setOption(nodeType, nodeName, "address", "127.0.0.1");
config->setOption(nodeType, nodeName, "port", "9000");
}
endInsertRows();
return true;
}
bool HostInterfaceModel::removeRows(int row, int count, const QModelIndex & parent)
{
Q_UNUSED(parent);
QString nodeName;
beginRemoveRows(parent, row, row + count - 1);
for (int i = row; i < (row + count); i++)
{
nodeName = config->getConfig()->getAllOptionNames(nodeType, "").at(i);
config->getConfig()->clearOption(nodeType, "", nodeName);
}
endRemoveRows();
return true;
}