swift
dbusserveraddressselector.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QIntValidator>
7 
8 #include "ui_dbusserveraddressselector.h"
9 
10 #include "config/buildconfig.h"
11 #include "gui/guiapplication.h"
12 #include "misc/dbusserver.h"
14 
15 using namespace swift::config;
16 using namespace swift::misc;
17 using namespace swift::misc::network;
18 
19 namespace swift::gui::components
20 {
21  CDBusServerAddressSelector::CDBusServerAddressSelector(QWidget *parent)
22  : QFrame(parent), ui(new Ui::CDBusServerAddressSelector)
23  {
24  Q_ASSERT(sGui);
25  ui->setupUi(this);
26 
27  this->setSystemDBusVisible(false);
28 
29  ui->cb_DBusServerAddress->addItems(CNetworkUtils::getKnownLocalIpV4Addresses());
30  ui->cb_DBusServerAddress->setCurrentIndex(0);
32 
33  ui->le_DBusServerPort->setValidator(new QIntValidator(0, 65535, this));
34  connect(ui->rb_DBusP2P, &QRadioButton::released, this, &CDBusServerAddressSelector::onRadioButtonReleased);
35  connect(ui->rb_DBusSession, &QRadioButton::released, this, &CDBusServerAddressSelector::onRadioButtonReleased);
36  connect(ui->rb_DBusSystem, &QRadioButton::released, this, &CDBusServerAddressSelector::onRadioButtonReleased);
37  connect(ui->le_DBusServerPort, &QLineEdit::editingFinished, this, &CDBusServerAddressSelector::editingFinished);
38  connect(ui->cb_DBusServerAddress, &QComboBox::currentTextChanged, this,
40  }
41 
43 
45  {
46  if (!this->isP2P()) { return {}; }
47  return CDBusServer::p2pAddress(ui->cb_DBusServerAddress->currentText() + ":" + ui->le_DBusServerPort->text());
48  }
49 
51  {
52  if (ui->rb_DBusSession->isChecked()) { return CDBusServer::sessionBusAddress(); }
53  if (ui->rb_DBusSystem->isChecked()) { return CDBusServer::systemBusAddress(); }
54  return this->getP2PAddress();
55  }
56 
58  {
59  return QStringList { "--dbus", this->getDBusAddress() };
60  }
61 
62  bool CDBusServerAddressSelector::isP2P() const { return ui->rb_DBusP2P->isChecked(); }
63 
65  {
66  const QString dBusLc = dBus.toLower().trimmed();
67  QString host, port;
68  CDBusServer::dBusAddressToHostAndPort(dBusLc, host, port);
69  if (!host.isEmpty())
70  {
71  if (ui->cb_DBusServerAddress->findText(host) < 0) { ui->cb_DBusServerAddress->addItem(host); }
72  ui->cb_DBusServerAddress->setCurrentText(host);
73  ui->le_DBusServerPort->setText(port);
74  }
75  }
76 
77  void CDBusServerAddressSelector::set(const QString &dBus)
78  {
79  const QString dBusLc = dBus.toLower().trimmed();
80  if (dBusLc.isEmpty() || dBusLc.startsWith("session")) { ui->rb_DBusSession->setChecked(true); }
81  else if (dBusLc.startsWith("sys")) { ui->rb_DBusSystem->setChecked(true); }
82  else
83  {
84  ui->rb_DBusP2P->setChecked(true);
85  QString host, port;
86  CDBusServer::dBusAddressToHostAndPort(dBusLc, host, port);
87  if (!host.isEmpty())
88  {
89  if (ui->cb_DBusServerAddress->findText(host) < 0) { ui->cb_DBusServerAddress->addItem(host); }
90  ui->cb_DBusServerAddress->setCurrentText(host);
91  ui->le_DBusServerPort->setText(port);
92  }
93  }
94  this->onRadioButtonReleased();
95  }
96 
98  {
99  const bool wasChecked = ui->rb_DBusSystem->isChecked();
100  ui->rb_DBusSystem->setVisible(visible);
101  if (!visible && wasChecked) { ui->rb_DBusSession->setChecked(true); }
102  }
103 
105  {
106  ui->rb_DBusP2P->setVisible(visible);
107  ui->fr_DBusServerAddress->setVisible(visible);
108  }
109 
111  {
112  this->setP2PDBusVisible(true);
113  this->setSystemDBusVisible(false);
114  }
115 
116  void CDBusServerAddressSelector::onRadioButtonReleased()
117  {
118  const bool p2p = this->isP2P();
119  ui->le_DBusServerPort->setEnabled(p2p);
120  ui->cb_DBusServerAddress->setEnabled(p2p);
121  emit this->editingFinished();
122  }
123 } // namespace swift::gui::components
QString getCmdDBusAddressValue() const
DBus address from CMD line, otherwise "".
Select DBus address such as session P2P, ...
QStringList getDBusCmdLineArgs() const
Get DBus cmd.line arguments.
QString getDBusAddress() const
DBus address for all 3 options.
QString getP2PAddress() const
DBus address for P2P or empty.
void setSystemDBusVisible(bool visible)
Set system DBus radio button visible/invisible.
void setDefaultP2PAddress(const QString &address)
Set default P2P address.
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Free functions in swift::misc.