regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / plugins / epan / pluginifdemo / ui / pluginifdemo_main.cpp
blob5434ca00c4716a6161ea2dd8e912c69442f8f9d2
1 /* pluginifdemo_main.cpp
3 * Author: Roland Knall <rknall@gmail.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include <plugins/epan/pluginifdemo/ui/pluginifdemo_main.h>
13 #include <ui_pluginifdemo_main.h>
15 #include <config.h>
17 #include "uihandler.h"
19 #include <QWidget>
20 #include <QLineEdit>
21 #include <QListView>
22 #include <QStandardItemModel>
24 PluginIfType::PluginIfType(const QString &label, const ext_toolbar_item_t &itemType)
25 : m_label(label), m_itemType(itemType)
28 QString PluginIfType::label() const { return m_label; }
29 ext_toolbar_item_t PluginIfType::itemType() const { return m_itemType; }
31 PluginIfTypeModel::PluginIfTypeModel(QObject * parent)
32 :QAbstractListModel(parent)
36 void PluginIfTypeModel::addPluginIfType(const PluginIfType &ifType)
38 beginInsertRows(QModelIndex(), rowCount(), rowCount());
39 m_pluginIfTypes << ifType;
40 endInsertRows();
43 int PluginIfTypeModel::rowCount(const QModelIndex &) const
45 return static_cast<int>(m_pluginIfTypes.count());
48 QVariant PluginIfTypeModel::data(const QModelIndex & idx, int role) const
50 if ( idx.row() < 0 || idx.row() >= m_pluginIfTypes.count() )
51 return QVariant();
53 const PluginIfType &ifType = m_pluginIfTypes[idx.row()];
54 if ( role == Qt::UserRole )
56 return ifType.itemType();
57 } else if ( role == Qt::DisplayRole ) {
58 return ifType.label();
61 return QVariant();
64 PluginIfTypeSortFilterProxyModel::PluginIfTypeSortFilterProxyModel(QObject * parent)
65 :QSortFilterProxyModel(parent)
67 m_filterType = EXT_TOOLBAR_BOOLEAN;
70 void PluginIfTypeSortFilterProxyModel::setFilterElement(ext_toolbar_item_t filterType)
72 m_filterType = filterType;
73 invalidateFilter();
76 bool PluginIfTypeSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
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;
83 return false;
86 PluginIFDemo_Main::PluginIFDemo_Main(QWidget *parent) :
87 QDialog(parent),
88 ui(new Ui::PluginIFDemo_Main)
90 ui->setupUi(this);
92 _toolbar = 0;
93 sourceModel = new PluginIfTypeModel(this);
94 proxyModel = new PluginIfTypeSortFilterProxyModel(this);
95 proxyModel->setSourceModel(sourceModel);
96 ui->cmbElements->setModel(proxyModel);
98 listModel = new QStandardItemModel(this);
99 ui->lstItems->setModel(listModel);
101 indexModel = new QStandardItemModel(this);
102 ui->cmbEntryIndex->setModel(indexModel);
104 ui->logView->setModel(new QStandardItemModel(ui->logView));
106 ui->tabInterfaceTypes->setCurrentIndex(0);
108 connect ( GuiHandler::getInstance(), SIGNAL(reset(void)), this, SLOT(closeDialog()) );
109 connect ( GuiHandler::getInstance(), SIGNAL(logChanged(QString)), this, SLOT(logChanged(QString)) );
112 PluginIFDemo_Main::~PluginIFDemo_Main()
114 delete ui;
117 void PluginIFDemo_Main::setToolbar(ext_toolbar_t * &toolbar)
119 _toolbar = toolbar;
121 GList * walker = toolbar->children;
122 while ( walker && walker->data )
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);
131 void PluginIFDemo_Main::closeDialog()
133 this->close();
136 void PluginIFDemo_Main::on_buttonBox_clicked(QAbstractButton *button _U_)
138 this->close();
141 void PluginIFDemo_Main::logChanged(QString message)
143 QStandardItemModel * model = (QStandardItemModel *) ui->logView->model();
144 model->appendRow(new QStandardItem(message));
147 void PluginIFDemo_Main::on_btnSendButtonText_clicked()
149 if ( ! _toolbar )
150 return;
152 ext_toolbar_t *item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
153 if ( ! item )
154 return;
156 QString entryText = ui->txtButtonName->text();
157 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
159 ext_toolbar_update_value(item, (gpointer) entryText.toStdString().c_str(), silent);
162 void PluginIFDemo_Main::on_btnSendText_clicked()
164 if ( ! _toolbar )
165 return;
167 ext_toolbar_t *item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
168 if ( ! item )
169 return;
171 QString entryText = ui->txtEdit->text();
172 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
174 ext_toolbar_update_value(item, (gpointer) entryText.toStdString().c_str(), silent);
177 void PluginIFDemo_Main::on_chkTestCheckbox_stateChanged(int newState)
179 if ( ! _toolbar )
180 return;
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;
187 ext_toolbar_update_value(item, GINT_TO_POINTER(newState == Qt::Checked ? 1 : 0), silent);
190 void PluginIFDemo_Main::on_tabInterfaceTypes_currentChanged(int newTab)
192 proxyModel->setFilterElement((ext_toolbar_item_t) newTab);
195 void PluginIFDemo_Main::on_cmbElements_currentTextChanged(const QString & newText)
197 if ( ! _toolbar )
198 return;
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;
204 listModel->clear();
205 indexModel->clear();
207 GList * walker = item->values;
208 while ( walker && walker->data )
210 ext_toolbar_value_t * listItem = (ext_toolbar_value_t *)walker->data;
211 QString content = QStringLiteral("%1: %2").arg(listItem->value).arg(listItem->display);
212 listModel->appendRow(new QStandardItem(content));
213 indexModel->appendRow(new QStandardItem(listItem->value));
215 walker = g_list_next(walker);
220 void PluginIFDemo_Main::on_btnEnable_clicked()
222 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
223 if ( ! item )
224 return;
226 ext_toolbar_update_data_set_active(item, true);
229 void PluginIFDemo_Main::on_btnDisable_clicked()
231 ext_toolbar_t * item = ext_toolbar_entry_by_label(_toolbar, ui->cmbElements->currentText().toStdString().c_str());
232 if ( ! item )
233 return;
235 ext_toolbar_update_data_set_active(item, false);
238 void PluginIFDemo_Main::on_btnAddItem_clicked()
240 if ( ui->txtNewItemDisplay->text().length() <= 0 || ui->txtNewItemValue->text().length() <= 0 )
241 return;
243 QString content = QStringLiteral("%1: %2").arg(ui->txtNewItemValue->text()).arg(ui->txtNewItemDisplay->text());
245 QList<QStandardItem *> items = listModel->findItems(content);
246 if ( items.count() > 0 )
247 return;
248 items = listModel->findItems(QStringLiteral("%1: ").arg(ui->txtNewItemValue->text()), Qt::MatchStartsWith);
249 if ( items.count() > 0 )
250 return;
252 listModel->appendRow(new QStandardItem(content));
254 if ( ui->chkAddRemoveImmediate->checkState() == Qt::Checked )
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;
260 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
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);
270 void PluginIFDemo_Main::on_btnRemoveItem_clicked()
272 QItemSelectionModel * selModel = ui->lstItems->selectionModel();
274 if ( selModel->selectedIndexes().count() == 0 )
275 return;
277 QModelIndexList selIndeces = selModel-> selectedIndexes();
278 foreach(QModelIndex idx, selIndeces)
280 if ( ui->chkAddRemoveImmediate->checkState() == Qt::Checked )
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;
286 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
288 QString content = listModel->data(idx).toString();
289 int pos = static_cast<int>(content.indexOf(":"));
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);
299 listModel->removeRow(idx.row());
303 void PluginIFDemo_Main::on_btnSendList_clicked()
305 if ( ! _toolbar )
306 return;
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;
312 GList * items = NULL;
314 for( int i = 0; i < listModel->rowCount(); i++ )
316 QString content = listModel->data(listModel->index(i, 0)).toString();
317 int pos = static_cast<int>(content.indexOf(":"));
319 ext_toolbar_value_t * valEntry = g_new0(ext_toolbar_value_t, 1);
320 valEntry->value = g_strdup(content.left(pos).toUtf8().constData());
321 valEntry->display = g_strdup(content.right(content.size() - pos + 1).toUtf8().constData());
323 items = g_list_append(items, valEntry);
326 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
328 ext_toolbar_update_data(item, items , silent);
331 void PluginIFDemo_Main::on_btnSendUpdateItem_clicked()
333 if ( ! _toolbar )
334 return;
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;
340 QString cmbIndexText = ui->cmbEntryIndex->currentText();
341 QString displayValue = ui->txtUpdateDisplayValue->text();
342 if ( displayValue.length() == 0 )
343 return;
345 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
347 ext_toolbar_update_data_by_index(item,
348 (gpointer) displayValue.toStdString().c_str(), (gpointer) cmbIndexText.toStdString().c_str(), silent );
351 void PluginIFDemo_Main::on_lstItems_clicked(const QModelIndex &idx)
353 if ( ! _toolbar || ! idx.isValid() )
354 return;
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;
360 bool silent = ui->chkSilent->checkState() == Qt::Checked ? true : false;
362 QString content = listModel->data(listModel->index(idx.row(), 0)).toString();
363 int pos = static_cast<int>(content.indexOf(":"));
365 gchar * idxData = g_strdup(content.left(pos).toUtf8().constData() );
367 ext_toolbar_update_value(item, idxData, silent);
368 g_free(idxData);
372 * Editor modelines
374 * Local Variables:
375 * c-basic-offset: 4
376 * tab-width: 8
377 * indent-tabs-mode: nil
378 * End:
380 * ex: set shiftwidth=4 tabstop=8 expandtab:
381 * :indentSize=4:tabSize=8:noTabs=true: