File indexing completed on 2024-07-21 07:58:05

0001 /*
0002     SPDX-FileCopyrightText: 2013 Lukas Tinkl <ltinkl@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "vlanwidget.h"
0008 #include "ui_vlan.h"
0009 #include "uiutils.h"
0010 
0011 #include <NetworkManagerQt/Settings>
0012 #include <NetworkManagerQt/VlanSetting>
0013 
0014 VlanWidget::VlanWidget(const NetworkManager::Setting::Ptr &setting, QWidget *parent, Qt::WindowFlags f)
0015     : SettingWidget(setting, parent, f)
0016     , m_ui(new Ui::VlanWidget)
0017 {
0018     m_ui->setupUi(this);
0019 
0020     fillConnections();
0021 
0022     connect(m_ui->ifaceName, &KLineEdit::textChanged, this, &VlanWidget::slotWidgetChanged);
0023     connect(m_ui->parent, QOverload<int>::of(&KComboBox::currentIndexChanged), this, &VlanWidget::slotWidgetChanged);
0024     connect(m_ui->parent->lineEdit(), &QLineEdit::textChanged, this, &VlanWidget::slotWidgetChanged);
0025 
0026     // Connect for setting check
0027     watchChangedSetting();
0028 
0029     KAcceleratorManager::manage(this);
0030 
0031     if (setting) {
0032         loadConfig(setting);
0033     }
0034 }
0035 
0036 VlanWidget::~VlanWidget()
0037 {
0038     delete m_ui;
0039 }
0040 
0041 void VlanWidget::loadConfig(const NetworkManager::Setting::Ptr &setting)
0042 {
0043     NetworkManager::VlanSetting::Ptr vlanSetting = setting.staticCast<NetworkManager::VlanSetting>();
0044 
0045     m_ui->parent->setCurrentIndex(m_ui->parent->findData(vlanSetting->parent()));
0046 
0047     m_ui->id->setValue(vlanSetting->id());
0048     m_ui->ifaceName->setText(vlanSetting->interfaceName());
0049 
0050     m_ui->reorderHeaders->setChecked(vlanSetting->flags().testFlag(NetworkManager::VlanSetting::ReorderHeaders));
0051     m_ui->gvrp->setChecked(vlanSetting->flags().testFlag(NetworkManager::VlanSetting::Gvrp));
0052     m_ui->looseBinding->setChecked(vlanSetting->flags().testFlag(NetworkManager::VlanSetting::LooseBinding));
0053 }
0054 
0055 QVariantMap VlanWidget::setting() const
0056 {
0057     NetworkManager::VlanSetting setting;
0058 
0059     setting.setParent(m_ui->parent->itemData(m_ui->parent->currentIndex()).toString());
0060     setting.setId(m_ui->id->value());
0061 
0062     const QString ifaceName = m_ui->ifaceName->text();
0063     if (!ifaceName.isEmpty())
0064         setting.setInterfaceName(ifaceName);
0065 
0066     NetworkManager::VlanSetting::Flags flags;
0067     if (m_ui->reorderHeaders->isChecked())
0068         flags |= NetworkManager::VlanSetting::ReorderHeaders;
0069     if (m_ui->gvrp->isChecked())
0070         flags |= NetworkManager::VlanSetting::Gvrp;
0071     if (m_ui->looseBinding->isChecked())
0072         flags |= NetworkManager::VlanSetting::LooseBinding;
0073     if (flags)
0074         setting.setFlags(flags);
0075 
0076     return setting.toMap();
0077 }
0078 
0079 void VlanWidget::fillConnections()
0080 {
0081     m_ui->parent->clear();
0082 
0083     for (const NetworkManager::Connection::Ptr &con : NetworkManager::listConnections()) {
0084         if (!con->settings()->isSlave() && con->settings()->connectionType() == NetworkManager::ConnectionSettings::Wired)
0085             m_ui->parent->addItem(con->name(), con->uuid());
0086     }
0087 }
0088 
0089 bool VlanWidget::isValid() const
0090 {
0091     return !m_ui->parent->currentText().isEmpty() || !m_ui->ifaceName->text().isEmpty();
0092 }
0093 
0094 #include "moc_vlanwidget.cpp"