1 /* pluginifdemo_main.cpp
2 *
3 * Author: Roland Knall <rknall@gmail.com>
4 *
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12 #include <plugins/epan/pluginifdemo/ui/pluginifdemo_main.h>
13 #include <ui_pluginifdemo_main.h>
14
15 #include <config.h>
16
17 #include "uihandler.h"
18
19 #include <QWidget>
20 #include <QLineEdit>
21 #include <QListView>
22 #include <QStandardItemModel>
23
PluginIfType(const QString & label,const ext_toolbar_item_t & itemType)24 PluginIfType::PluginIfType(const QString &label, const ext_toolbar_item_t &itemType)
25 : m_label(label), m_itemType(itemType)
26 {}
27
label() const28 QString PluginIfType::label() const { return m_label; }
itemType() const29 ext_toolbar_item_t PluginIfType::itemType() const { return m_itemType; }
30
PluginIfTypeModel(QObject * parent)31 PluginIfTypeModel::PluginIfTypeModel(QObject * parent)
32 :QAbstractListModel(parent)
33 {
34 }
35
addPluginIfType(const PluginIfType & ifType)36 void PluginIfTypeModel::addPluginIfType(const PluginIfType &ifType)
37 {
38 beginInsertRows(QModelIndex(), rowCount(), rowCount());
39 m_pluginIfTypes << ifType;
40 endInsertRows();
41 }
42
rowCount(const QModelIndex &) const43 int PluginIfTypeModel::rowCount(const QModelIndex &) const
44 {
45 return m_pluginIfTypes.count();
46 }
47
data(const QModelIndex & idx,int role) const48 QVariant PluginIfTypeModel::data(const QModelIndex & idx, int role) const
49 {
50 if ( idx.row() < 0 || idx.row() >= m_pluginIfTypes.count() )
51 return QVariant();
52
53 const PluginIfType &ifType = m_pluginIfTypes[idx.row()];
54 if ( role == Qt::UserRole )
55 {
56 return ifType.itemType();
57 } else if ( role == Qt::DisplayRole ) {
58 return ifType.label();
59 }
60
61 return QVariant();
62 }
63
PluginIfTypeSortFilterProxyModel(QObject * parent)64 PluginIfTypeSortFilterProxyModel::PluginIfTypeSortFilterProxyModel(QObject * parent)
65 :QSortFilterProxyModel(parent)
66 {
67 m_filterType = EXT_TOOLBAR_BOOLEAN;
68 }
69
setFilterElement(ext_toolbar_item_t filterType)70 void PluginIfTypeSortFilterProxyModel::setFilterElement(ext_toolbar_item_t filterType)
71 {
72 m_filterType = filterType;
73 invalidateFilter();
74 }
75
filterAcceptsRow(int sourceRow,const QModelIndex & sourceParent) const76 bool PluginIfTypeSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
77 {
78 QModelIndex dataIndex = sourceModel()->index(sourceRow, 0, sourceParent);
79 QVariant varData = sourceModel()->data(dataIndex, Qt::UserRole);
80 if ( varData.isValid() && varData.toInt() == (int) m_filterType )
81 return true;
82
83 return false;
84 }
85
PluginIFDemo_Main(QWidget * parent)86 PluginIFDemo_Main::PluginIFDemo_Main(QWidget *parent) :
87 QDialog(parent),
88 ui(new Ui::PluginIFDemo_Main)
89 {
90 ui->setupUi(this);
91
92 _toolbar = 0;
93 sourceModel = new PluginIfTypeModel(this);
94 proxyModel = new PluginIfTypeSortFilterProxyModel(this);
95 proxyModel->setSourceModel(sourceModel);
96 ui->cmbElements->setModel(proxyModel);
97
98 listModel = new QStandardItemModel(this);
99 ui->lstItems->setModel(listModel);
100
101 indexModel = new QStandardItemModel(this);
102 ui->cmbEntryIndex->setModel(indexModel);
103
104 ui->logView->setModel(new QStandardItemModel(ui->logView));
105
106 ui->tabInterfaceTypes->setCurrentIndex(0);
107
108 connect ( GuiHandler::getInstance(), SIGNAL(reset(void)), this, SLOT(closeDialog()) );
109 connect ( GuiHandler::getInstance(), SIGNAL(logChanged(QString)), this, SLOT(logChanged(QString)) );
110 }
111
~PluginIFDemo_Main()112 PluginIFDemo_Main::~PluginIFDemo_Main()
113 {
114 delete ui;
115 }
116
setToolbar(ext_toolbar_t * & toolbar)117 void PluginIFDemo_Main::setToolbar(ext_toolbar_t * &toolbar)
118 {
119 _toolbar = toolbar;
120
121 GList * walker = toolbar->children;
122 while ( walker && walker->data )
123 {
124 ext_toolbar_t * entry = (ext_toolbar_t *)walker->data;
125 if ( entry && entry->type == EXT_TOOLBAR_ITEM && entry->name )
126 sourceModel->addPluginIfType(PluginIfType(QString(entry->name), entry->item_type));
127 walker = g_list_next(walker);
128 }
129 }
130
closeDialog()131 void PluginIFDemo_Main::closeDialog()
132 {
133 this->close();
134 }
135
on_buttonBox_clicked(QAbstractButton * button _U_)136 void PluginIFDemo_Main::on_buttonBox_clicked(QAbstractButton *button _U_)
137 {
138 this->close();
139 }
140
logChanged(QString message)141 void PluginIFDemo_Main::logChanged(QString message)
142 {
143 QStandardItemModel * model = (QStandardItemModel *) ui->logView->model();
144 model->appendRow(new QStandardItem(message));
145 }
146
on_btnSendButtonText_clicked()147 void PluginIFDemo_Main::on_btnSendButtonText_clicked()
148 {
149 if ( ! _toolbar )
150 return;
151
152 ext_toolbar_t *item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
153 if ( ! item )
154 return;
155
156 QString entryText = ui->txtButtonName->text();
157 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
158
159 ext_toolbar_update_value(item, (gpointer) entryText.toStdString().c_str(), silent);
160 }
161
on_btnSendText_clicked()162 void PluginIFDemo_Main::on_btnSendText_clicked()
163 {
164 if ( ! _toolbar )
165 return;
166
167 ext_toolbar_t *item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
168 if ( ! item )
169 return;
170
171 QString entryText = ui->txtEdit->text();
172 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
173
174 ext_toolbar_update_value(item, (gpointer) entryText.toStdString().c_str(), silent);
175 }
176
on_chkTestCheckbox_stateChanged(int newState)177 void PluginIFDemo_Main::on_chkTestCheckbox_stateChanged(int newState)
178 {
179 if ( ! _toolbar )
180 return;
181
182 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
183 if ( ! item )
184 return;
185 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
186
187 ext_toolbar_update_value(item, GINT_TO_POINTER(newState == Qt::Checked ? 1 : 0), silent);
188 }
189
on_tabInterfaceTypes_currentChanged(int newTab)190 void PluginIFDemo_Main::on_tabInterfaceTypes_currentChanged(int newTab)
191 {
192 proxyModel->setFilterElement((ext_toolbar_item_t) newTab);
193 }
194
on_cmbElements_currentTextChanged(const QString & newText)195 void PluginIFDemo_Main::on_cmbElements_currentTextChanged(const QString & newText)
196 {
197 if ( ! _toolbar )
198 return;
199
200 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, newText.toStdString().c_str());
201 if ( ! item || item->item_type != EXT_TOOLBAR_SELECTOR )
202 return;
203
204 listModel->clear();
205 indexModel->clear();
206
207 GList * walker = item->values;
208 while ( walker && walker->data )
209 {
210 ext_toolbar_value_t * listItem = (ext_toolbar_value_t *)walker->data;
211 QString content = QString("%1: %2").arg(listItem->value).arg(listItem->display);
212 listModel->appendRow(new QStandardItem(content));
213 indexModel->appendRow(new QStandardItem(listItem->value));
214
215 walker = g_list_next(walker);
216 }
217
218 }
219
on_btnEnable_clicked()220 void PluginIFDemo_Main::on_btnEnable_clicked()
221 {
222 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
223 if ( ! item )
224 return;
225
226 ext_toolbar_update_data_set_active(item, true);
227 }
228
on_btnDisable_clicked()229 void PluginIFDemo_Main::on_btnDisable_clicked()
230 {
231 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
232 if ( ! item )
233 return;
234
235 ext_toolbar_update_data_set_active(item, false);
236 }
237
on_btnAddItem_clicked()238 void PluginIFDemo_Main::on_btnAddItem_clicked()
239 {
240 if ( ui->txtNewItemDisplay->text().length() <= 0 || ui->txtNewItemValue->text().length() <= 0 )
241 return;
242
243 QString content = QString("%1: %2").arg(ui->txtNewItemValue->text()).arg(ui->txtNewItemDisplay->text());
244
245 QList<QStandardItem *> items = listModel->findItems(content);
246 if ( items.count() > 0 )
247 return;
248 items = listModel->findItems(QString("%1: ").arg(ui->txtNewItemValue->text()), Qt::MatchStartsWith);
249 if ( items.count() > 0 )
250 return;
251
252 listModel->appendRow(new QStandardItem(content));
253
254 if ( ui->chkAddRemoveImmediate->checkState() == Qt::Checked )
255 {
256 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
257 if ( ! item || item->item_type != EXT_TOOLBAR_SELECTOR )
258 return;
259
260 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
261
262 gchar * value = g_strdup(ui->txtNewItemValue->text().toUtf8().constData());
263 gchar * display = g_strdup(ui->txtNewItemDisplay->text().toUtf8().constData());
264 ext_toolbar_update_data_add_entry(item, display, value, silent);
265 g_free(value);
266 g_free(display);
267 }
268 }
269
on_btnRemoveItem_clicked()270 void PluginIFDemo_Main::on_btnRemoveItem_clicked()
271 {
272 QItemSelectionModel * selModel = ui->lstItems->selectionModel();
273
274 if ( selModel->selectedIndexes().count() == 0 )
275 return;
276
277 QModelIndexList selIndeces = selModel-> selectedIndexes();
278 foreach(QModelIndex idx, selIndeces)
279 {
280 if ( ui->chkAddRemoveImmediate->checkState() == Qt::Checked )
281 {
282 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
283 if ( ! item || item->item_type != EXT_TOOLBAR_SELECTOR )
284 return;
285
286 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
287
288 QString content = listModel->data(idx).toString();
289 int pos = content.indexOf(":");
290
291 gchar * value = g_strdup(content.left(pos).toUtf8().constData() );
292 /* -2 because removal of : and space */
293 gchar * display = g_strdup(content.right(content.size() - pos - 2).toUtf8().constData());
294 ext_toolbar_update_data_remove_entry(item, display, value, silent);
295 g_free(value);
296 g_free(display);
297 }
298
299 listModel->removeRow(idx.row());
300 }
301 }
302
on_btnSendList_clicked()303 void PluginIFDemo_Main::on_btnSendList_clicked()
304 {
305 if ( ! _toolbar )
306 return;
307
308 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
309 if ( ! item || item->item_type != EXT_TOOLBAR_SELECTOR )
310 return;
311
312 GList * items = NULL;
313
314 for( int i = 0; i < listModel->rowCount(); i++ )
315 {
316 QString content = listModel->data(listModel->index(i, 0)).toString();
317 int pos = content.indexOf(":");
318
319 ext_toolbar_value_t * valEntry = g_new0(ext_toolbar_value_t, 1);
320 valEntry->value = g_strdup(content.left(pos).toStdString().c_str() );
321 valEntry->display = g_strdup(content.right(content.size() - pos + 1).toStdString().c_str());
322
323 items = g_list_append(items, valEntry);
324 }
325
326 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
327
328 ext_toolbar_update_data(item, items , silent);
329 }
330
on_btnSendUpdateItem_clicked()331 void PluginIFDemo_Main::on_btnSendUpdateItem_clicked()
332 {
333 if ( ! _toolbar )
334 return;
335
336 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
337 if ( ! item || item->item_type != EXT_TOOLBAR_SELECTOR )
338 return;
339
340 QString cmbIndexText = ui->cmbEntryIndex->currentText();
341 QString displayValue = ui->txtUpdateDisplayValue->text();
342 if ( displayValue.length() == 0 )
343 return;
344
345 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
346
347 ext_toolbar_update_data_by_index(item,
348 (gpointer) displayValue.toStdString().c_str(), (gpointer) cmbIndexText.toStdString().c_str(), silent );
349 }
350
on_lstItems_clicked(const QModelIndex & idx)351 void PluginIFDemo_Main::on_lstItems_clicked(const QModelIndex &idx)
352 {
353 if ( ! _toolbar || ! idx.isValid() )
354 return;
355
356 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
357 if ( ! item || item->item_type != EXT_TOOLBAR_SELECTOR )
358 return;
359
360 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
361
362 QString content = listModel->data(listModel->index(idx.row(), 0)).toString();
363 int pos = content.indexOf(":");
364
365 gchar * idxData = g_strdup(content.left(pos).toUtf8().constData() );
366
367 ext_toolbar_update_value(item, idxData, silent);
368 g_free(idxData);
369
370 }
371 /*
372 * Editor modelines
373 *
374 * Local Variables:
375 * c-basic-offset: 4
376 * tab-width: 8
377 * indent-tabs-mode: nil
378 * End:
379 *
380 * ex: set shiftwidth=4 tabstop=8 expandtab:
381 * :indentSize=4:tabSize=8:noTabs=true:
382 */
383