drumstick 2.9.0
C++ MIDI libraries using Qt objects, idioms, and style.
networksettingsdialog.cpp
Go to the documentation of this file.
1/*
2 Virtual Piano test using the MIDI Sequencer C++ library
3 Copyright (C) 2006-2023, Pedro Lopez-Cabanillas <plcl@users.sf.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <QDialogButtonBox>
20#include <QNetworkInterface>
21#include <QPushButton>
22#include <QMessageBox>
23
25#include "ui_networksettingsdialog.h"
28
34namespace drumstick { namespace widgets {
35
36const QString NetworkSettingsDialog::QSTR_ADDRESS_IPV4 = QStringLiteral("225.0.0.37");
37const QString NetworkSettingsDialog::QSTR_ADDRESS_IPV6 = QStringLiteral("ff12::37");
38
39NetworkSettingsDialog::NetworkSettingsDialog(const bool forInput, QWidget *parent) :
40 QDialog(parent),
41 ui(new Ui::NetworkSettingsDialog),
42 m_input(forInput)
43{
44 ui->setupUi(this);
45 connect(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::pressed,
46 this, &NetworkSettingsDialog::restoreDefaults);
47 connect(ui->checkIPv6, &QCheckBox::toggled, this, &NetworkSettingsDialog::toggledIPv6);
48
50 if (m_input) {
51 m_driver = man.inputBackendByName("Network");
52 } else {
53 m_driver = man.outputBackendByName("Network");
54 }
55}
56
57NetworkSettingsDialog::~NetworkSettingsDialog()
58{
59 if (m_driver != nullptr) {
60 if (m_input) {
61 static_cast<drumstick::rt::MIDIInput*>(m_driver)->close();
62 } else {
63 static_cast<drumstick::rt::MIDIOutput*>(m_driver)->close();
64 }
65 }
66 delete ui;
67}
68
69void NetworkSettingsDialog::accept()
70{
71 writeSettings();
72 if (m_driver != nullptr) {
73 QString title;
74 QVariant varStatus = m_driver->property("status");
75 if (varStatus.isValid()) {
76 title = varStatus.toBool() ? tr("Network Initialized") : tr("Network Initialization Failed");
77 QVariant varDiag = m_driver->property("diagnostics");
78 if (varDiag.isValid()) {
79 QString text = varDiag.toStringList().join(QChar::LineFeed).trimmed();
80 if (varStatus.toBool()) {
81 if (!text.isEmpty()) {
82 QMessageBox::information(this, title, text);
83 }
84 } else {
85 QMessageBox::critical(this, title, text);
86 return;
87 }
88 }
89 }
90 }
91 QDialog::accept();
92}
93
94void NetworkSettingsDialog::showEvent(QShowEvent *event)
95{
96 readSettings();
97 event->accept();
98}
99
100void NetworkSettingsDialog::readSettings()
101{
102 SettingsFactory settings;
103 settings->beginGroup("Network");
104 QString ifaceName = settings->value("interface", QString()).toString();
105 bool ipv6 = settings->value("ipv6", false).toBool();
106 QString address = settings->value("address", ipv6 ? QSTR_ADDRESS_IPV6 : QSTR_ADDRESS_IPV4).toString();
107 settings->endGroup();
108
109 ui->txtAddress->setText(address);
110 ui->checkIPv6->setChecked(ipv6);
111 ui->comboInterface->addItem(tr("Any"), "");
112 foreach( const QNetworkInterface& iface, QNetworkInterface::allInterfaces() ) {
113 if ( iface.isValid() &&
114 iface.flags().testFlag(QNetworkInterface::CanMulticast) &&
115 iface.flags().testFlag(QNetworkInterface::IsUp) &&
116 iface.flags().testFlag(QNetworkInterface::IsRunning) &&
117 !iface.flags().testFlag(QNetworkInterface::IsLoopBack) ) {
118 QString name = iface.name();
119 QString text = iface.humanReadableName();
120 ui->comboInterface->addItem(text, name);
121 if (name == ifaceName) {
122 ui->comboInterface->setCurrentText(text);
123 }
124 }
125 }
126 chkInitialization(settings.getQSettings());
127}
128
129void NetworkSettingsDialog::writeSettings()
130{
131 SettingsFactory settings;
132 QString networkAddr = ui->txtAddress->text();
133 QString networkIface = ui->comboInterface->currentData().toString();
134 bool ipv6 = ui->checkIPv6->isChecked();
135
136 settings->beginGroup("Network");
137 settings->setValue("address", networkAddr);
138 settings->setValue("interface", networkIface);
139 settings->setValue("ipv6", ipv6);
140 settings->endGroup();
141 settings->sync();
142
143 chkInitialization(settings.getQSettings());
144}
145
146void NetworkSettingsDialog::chkInitialization(QSettings *settings)
147{
148 if (m_driver != nullptr) {
149 drumstick::rt::MIDIConnection conn("21928", 21928);
150 if (m_input) {
151 auto d = static_cast<drumstick::rt::MIDIInput*>(m_driver);
152 d->close();
153 d->initialize(settings);
154 d->open(conn);
155 } else {
156 auto d = static_cast<drumstick::rt::MIDIOutput*>(m_driver);
157 d->close();
158 d->initialize(settings);
159 d->open(conn);
160 }
161 QVariant varStatus = m_driver->property("status");
162 if (varStatus.isValid()) {
163 ui->lblStatusText->clear();
164 ui->lblStatusText->setText(varStatus.toBool() ? tr("Ready") : tr("Failed") );
165 ui->lblStatusIcon->setPixmap(varStatus.toBool() ? QPixmap(":/checked.png") : QPixmap(":/error.png") );
166 }
167 }
168}
169
170void NetworkSettingsDialog::restoreDefaults()
171{
172 ui->checkIPv6->setChecked(false);
173 ui->txtAddress->setText(QSTR_ADDRESS_IPV4);
174 ui->comboInterface->setCurrentText(tr("Any"));
175}
176
177void NetworkSettingsDialog::toggledIPv6(bool checked)
178{
179 ui->txtAddress->setText(checked ? QSTR_ADDRESS_IPV6 : QSTR_ADDRESS_IPV4);
180}
181
182} // namespace widgets
183} // namespace drumstick
BackendManager class declaration.
The QSettings class provides persistent platform-independent application settings.
The BackendManager class manages lists of dynamic and static backends for applications based on drums...
MIDIOutput * outputBackendByName(const QString name)
outputBackendByName
MIDIInput * inputBackendByName(const QString name)
inputBackendByName
MIDI IN interface.
Definition: rtmidiinput.h:56
virtual void close()=0
close the MIDI port
MIDI OUT interface.
Definition: rtmidioutput.h:122
virtual void close()=0
close the MIDI port
QPair< QString, QVariant > MIDIConnection
MIDIConnection represents a connection identifier.
Definition: rtmidioutput.h:116
Drumstick common.
Definition: alsaclient.cpp:68
Declaration of the Network configuration dialog.
SettingsFactory class declaration.