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>
17 #include "uihandler.h"
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
;
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() )
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();
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
;
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
)
86 PluginIFDemo_Main::PluginIFDemo_Main(QWidget
*parent
) :
88 ui(new Ui::PluginIFDemo_Main
)
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()
117 void PluginIFDemo_Main::setToolbar(ext_toolbar_t
* &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()
136 void PluginIFDemo_Main::on_buttonBox_clicked(QAbstractButton
*button _U_
)
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()
152 ext_toolbar_t
*item
= ext_toolbar_entry_by_label(_toolbar
, ui
->cmbElements
->currentText().toStdString().c_str());
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()
167 ext_toolbar_t
*item
= ext_toolbar_entry_by_label(_toolbar
, ui
->cmbElements
->currentText().toStdString().c_str());
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
)
182 ext_toolbar_t
* item
= ext_toolbar_entry_by_label(_toolbar
, ui
->cmbElements
->currentText().toStdString().c_str());
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
)
200 ext_toolbar_t
* item
= ext_toolbar_entry_by_label(_toolbar
, newText
.toStdString().c_str());
201 if ( ! item
|| item
->item_type
!= EXT_TOOLBAR_SELECTOR
)
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());
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());
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 )
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 )
248 items
= listModel
->findItems(QStringLiteral("%1: ").arg(ui
->txtNewItemValue
->text()), Qt::MatchStartsWith
);
249 if ( items
.count() > 0 )
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
)
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
);
270 void PluginIFDemo_Main::on_btnRemoveItem_clicked()
272 QItemSelectionModel
* selModel
= ui
->lstItems
->selectionModel();
274 if ( selModel
->selectedIndexes().count() == 0 )
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
)
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
);
299 listModel
->removeRow(idx
.row());
303 void PluginIFDemo_Main::on_btnSendList_clicked()
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
)
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()
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
)
340 QString cmbIndexText
= ui
->cmbEntryIndex
->currentText();
341 QString displayValue
= ui
->txtUpdateDisplayValue
->text();
342 if ( displayValue
.length() == 0 )
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() )
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
)
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
);
377 * indent-tabs-mode: nil
380 * ex: set shiftwidth=4 tabstop=8 expandtab:
381 * :indentSize=4:tabSize=8:noTabs=true: