From 948a0c1eee07f7dbaeb13eac3e466e26fc0b7887 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 16 Nov 2017 02:16:05 +0100 Subject: [PATCH] Ref T195, DBus address selector UI to be used with launcher and core UI --- .../components/dbusserveraddressselector.cpp | 120 ++++++++++++++++++ .../components/dbusserveraddressselector.h | 62 +++++++++ .../components/dbusserveraddressselector.ui | 116 +++++++++++++++++ 3 files changed, 298 insertions(+) create mode 100644 src/blackgui/components/dbusserveraddressselector.cpp create mode 100644 src/blackgui/components/dbusserveraddressselector.h create mode 100644 src/blackgui/components/dbusserveraddressselector.ui diff --git a/src/blackgui/components/dbusserveraddressselector.cpp b/src/blackgui/components/dbusserveraddressselector.cpp new file mode 100644 index 000000000..5fe8c40cb --- /dev/null +++ b/src/blackgui/components/dbusserveraddressselector.cpp @@ -0,0 +1,120 @@ +/* Copyright (C) 2017 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "dbusserveraddressselector.h" +#include "ui_dbusserveraddressselector.h" +#include "blackgui/guiapplication.h" +#include "blackmisc/network/networkutils.h" +#include "blackmisc/dbusserver.h" +#include "blackconfig/buildconfig.h" + +#include + +using namespace BlackConfig; +using namespace BlackMisc; +using namespace BlackMisc::Network; + +namespace BlackGui +{ + namespace Components + { + CDBusServerAddressSelector::CDBusServerAddressSelector(QWidget *parent) : + QFrame(parent), + ui(new Ui::CDBusServerAddressSelector) + { + Q_ASSERT(sGui); + ui->setupUi(this); + + // normally no system Bus on Windows + if (CBuildConfig::isRunningOnWindowsNtPlatform() && !sGui->isRunningInDeveloperEnvironment()) + { + ui->rb_DBusSystem->setEnabled(false); + } + + ui->cb_DBusServerAddress->addItems(CNetworkUtils::getKnownLocalIpV4Addresses()); + ui->cb_DBusServerAddress->setCurrentIndex(0); + this->set(sGui->getCmdDBusAddressValue()); + + ui->le_DBusServerPort->setValidator(new QIntValidator(0, 65535, this)); + connect(ui->rb_DBusP2P, &QRadioButton::released, this, &CDBusServerAddressSelector::radioButtonReleased); + connect(ui->rb_DBusSession, &QRadioButton::released, this, &CDBusServerAddressSelector::radioButtonReleased); + connect(ui->rb_DBusSystem, &QRadioButton::released, this, &CDBusServerAddressSelector::radioButtonReleased); + connect(ui->cb_DBusServerAddress, &QComboBox::currentTextChanged, this, &CDBusServerAddressSelector::p2pAddressChanged); + } + + CDBusServerAddressSelector::~CDBusServerAddressSelector() + { } + + QString CDBusServerAddressSelector::getP2PAddress() const + { + if (!this->isP2P()) { return ""; } + return CDBusServer::p2pAddress( + ui->cb_DBusServerAddress->currentText() + ":" + + ui->le_DBusServerPort->text() + ); + } + + QString CDBusServerAddressSelector::getDBusAddress() const + { + if (ui->rb_DBusSession->isChecked()) { return CDBusServer::sessionBusAddress(); } + if (ui->rb_DBusSystem->isChecked()) { return CDBusServer::systemBusAddress(); } + return this->getP2PAddress(); + } + + QStringList CDBusServerAddressSelector::getDBusCmdLineArgs() const + { + return QStringList { "--dbus", this->getDBusAddress() }; + } + + bool CDBusServerAddressSelector::isP2P() const + { + return ui->rb_DBusP2P->isChecked(); + } + + void CDBusServerAddressSelector::set(const QString &dBus) + { + const QString dBusLc = dBus.toLower().trimmed(); + if (dBusLc.isEmpty() || dBusLc.startsWith("session")) + { + ui->rb_DBusSession->setChecked(true); + } + else if (dBusLc.startsWith("sys")) + { + ui->rb_DBusSystem->setChecked(true); + } + else + { + ui->rb_DBusP2P->setChecked(true); + QString host, port; + CDBusServer::dBusAddressToHostAndPort(dBusLc, host, port); + if (!host.isEmpty()) + { + if (ui->cb_DBusServerAddress->findText(host) < 0) + { + ui->cb_DBusServerAddress->addItem(host); + } + ui->cb_DBusServerAddress->setCurrentText(host); + ui->le_DBusServerPort->setText(port); + } + } + } + + void CDBusServerAddressSelector::radioButtonReleased() + { + const bool p2p = this->isP2P(); + ui->le_DBusServerPort->setEnabled(p2p); + ui->cb_DBusServerAddress->setEnabled(p2p); + } + + void CDBusServerAddressSelector::p2pAddressChanged() + { + // void + } + } // ns +} // ns diff --git a/src/blackgui/components/dbusserveraddressselector.h b/src/blackgui/components/dbusserveraddressselector.h new file mode 100644 index 000000000..58ee43230 --- /dev/null +++ b/src/blackgui/components/dbusserveraddressselector.h @@ -0,0 +1,62 @@ +/* Copyright (C) 2017 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKGUI_COMPONENTS_DBUSSERVERADDRESSSELECTOR_H +#define BLACKGUI_COMPONENTS_DBUSSERVERADDRESSSELECTOR_H + +#include "blackgui/blackguiexport.h" +#include +#include + +namespace Ui { class CDBusServerAddressSelector; } +namespace BlackGui +{ + namespace Components + { + //! Select DBus address such as session P2P, ... + class BLACKGUI_EXPORT CDBusServerAddressSelector : public QFrame + { + Q_OBJECT + + public: + //! Ctor + explicit CDBusServerAddressSelector(QWidget *parent = nullptr); + + //! Dtor + virtual ~CDBusServerAddressSelector(); + + //! DBus address for P2P or empty + QString getP2PAddress() const; + + //! DBus address for all 3 options + QString getDBusAddress() const; + + //! Get DBus cmd.line arguments + QStringList getDBusCmdLineArgs() const; + + //! P2P DBus address + bool isP2P() const; + + //! Set values + void set(const QString &dBus); + + private: + QScopedPointer ui; + + //! Radio button clicked + void radioButtonReleased(); + + //! P2P address changed + void p2pAddressChanged(); + }; + } // ns +} // ns +#endif // guard diff --git a/src/blackgui/components/dbusserveraddressselector.ui b/src/blackgui/components/dbusserveraddressselector.ui new file mode 100644 index 000000000..165a66d8e --- /dev/null +++ b/src/blackgui/components/dbusserveraddressselector.ui @@ -0,0 +1,116 @@ + + + CDBusServerAddressSelector + + + Frame + + + + 2 + + + 2 + + + 2 + + + 2 + + + + + DBus session server + + + true + + + + + + + DBus system server + + + + + + + DBus peer to peer server (P2P) + + + + + + + + 10 + + + 0 + + + 10 + + + 0 + + + 4 + + + + + + 0 + 0 + + + + true + + + + + + + Address: + + + Qt::LinksAccessibleByMouse + + + + + + + Port: + + + + + + + + 80 + 16777215 + + + + 45000 + + + 5 + + + + + + + + + + +