mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-02 15:15:50 +08:00
refs #429 Rename swiftcorectrl to swiftcore
This commit is contained in:
167
src/swiftcore/main.cpp
Normal file
167
src/swiftcore/main.cpp
Normal file
@@ -0,0 +1,167 @@
|
||||
#include "swiftcore.h"
|
||||
|
||||
#include "blackcore/context_runtime.h"
|
||||
#include "blackcore/context_settings.h"
|
||||
#include "blackcore/context_application.h"
|
||||
#include "blackcore/context_application_impl.h"
|
||||
#include "blackcore/dbus_server.h"
|
||||
#include "blackmisc/icons.h"
|
||||
#include "blackmisc/worker.h"
|
||||
#include "blackmisc/networkutils.h"
|
||||
#include "blackmisc/blackmiscfreefunctions.h"
|
||||
#include "blackmisc/project.h"
|
||||
#include "blackmisc/loghandler.h"
|
||||
#include "blackgui/stylesheetutility.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QMessageBox>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackCore;
|
||||
using namespace BlackGui;
|
||||
|
||||
enum CommandLineParseResult
|
||||
{
|
||||
CommandLineOk,
|
||||
CommandLineError,
|
||||
CommandLineVersionRequested,
|
||||
CommandLineHelpRequested
|
||||
};
|
||||
|
||||
CommandLineParseResult parseCommandLine(QCommandLineParser &parser, CSwiftCore::SetupInfo *setup, QString *errorMessage)
|
||||
{
|
||||
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
|
||||
parser.addOption({{"s", "session"}, QCoreApplication::translate("main", "Use session bus.")});
|
||||
parser.addOption({{"y", "system"}, QCoreApplication::translate("main", "Use system bus.")});
|
||||
parser.addOption({{"p", "p2p"}, QCoreApplication::translate("main", "Use P2P bus with <address>."),
|
||||
QCoreApplication::translate("main", "address")
|
||||
});
|
||||
parser.addOption({{"m", "minimized"}, QCoreApplication::translate("main", "Start minimized in system tray.")});
|
||||
|
||||
if (!parser.parse(QCoreApplication::arguments()))
|
||||
{
|
||||
*errorMessage = parser.errorText();
|
||||
return CommandLineError;
|
||||
}
|
||||
|
||||
if (parser.isSet("session"))
|
||||
{
|
||||
if (parser.isSet("system") || parser.isSet("p2p"))
|
||||
{
|
||||
*errorMessage = "Multiple DBus types set at the same time.";
|
||||
return CommandLineError;
|
||||
}
|
||||
setup->m_dbusAddress = CDBusServer::sessionDBusServer();
|
||||
}
|
||||
|
||||
if (parser.isSet("system"))
|
||||
{
|
||||
if (parser.isSet("session") || parser.isSet("p2p"))
|
||||
{
|
||||
*errorMessage = "Multiple DBus types set at the same time.";
|
||||
return CommandLineError;
|
||||
}
|
||||
setup->m_dbusAddress = CDBusServer::systemDBusServer();
|
||||
}
|
||||
|
||||
if (parser.isSet("p2p"))
|
||||
{
|
||||
const QString address = CDBusServer::fixAddressToDBusAddress(parser.value("p2p"));
|
||||
|
||||
if (parser.isSet("session") || parser.isSet("system"))
|
||||
{
|
||||
*errorMessage = "Multiple DBus types set at the same time.";
|
||||
return CommandLineError;
|
||||
}
|
||||
}
|
||||
|
||||
if (parser.isSet("minimized"))
|
||||
{
|
||||
setup->m_minimzed = true;
|
||||
}
|
||||
|
||||
return CommandLineOk;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
CRuntime::registerMetadata(); // register metadata
|
||||
QApplication a(argc, argv);
|
||||
QApplication::setApplicationName("swiftcore");
|
||||
QApplication::setApplicationVersion(CProject::version());
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription("swiftcore control");
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
|
||||
CSwiftCore::SetupInfo setup;
|
||||
QString errorMessage;
|
||||
switch (parseCommandLine(parser, &setup, &errorMessage))
|
||||
{
|
||||
case CommandLineOk:
|
||||
break;
|
||||
case CommandLineError:
|
||||
#ifdef Q_OS_WIN
|
||||
QMessageBox::warning(0, QGuiApplication::applicationDisplayName(),
|
||||
"<html><head/><body><h2>" + errorMessage + "</h2><pre>"
|
||||
+ parser.helpText() + "</pre></body></html>");
|
||||
#else
|
||||
fputs(qPrintable(errorMessage), stderr);
|
||||
fputs("\n\n", stderr);
|
||||
fputs(qPrintable(parser.helpText()), stderr);
|
||||
#endif
|
||||
return 1;
|
||||
case CommandLineVersionRequested:
|
||||
#ifdef Q_OS_WIN
|
||||
QMessageBox::information(0, QGuiApplication::applicationDisplayName(),
|
||||
QGuiApplication::applicationDisplayName() + ' '+ QCoreApplication::applicationVersion());
|
||||
#else
|
||||
printf("%s %s\n", qPrintable(QCoreApplication::applicationName()),
|
||||
qPrintable(QCoreApplication::applicationVersion()));
|
||||
#endif
|
||||
return 0;
|
||||
case CommandLineHelpRequested:
|
||||
#ifdef Q_OS_WIN
|
||||
QMessageBox::warning(0, QGuiApplication::applicationDisplayName(),
|
||||
"<html><head/><body><pre>"
|
||||
+ parser.helpText() + "</pre></body></html>");
|
||||
return 0;
|
||||
#else
|
||||
parser.showHelp();
|
||||
Q_UNREACHABLE();
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!QSystemTrayIcon::isSystemTrayAvailable())
|
||||
{
|
||||
QMessageBox::critical(0, QObject::tr("Systray"),
|
||||
QObject::tr("I couldn't detect any system tray on this system."));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Translations
|
||||
QFile file(":blackmisc/translations/blackmisc_i18n_de.qm");
|
||||
CLogMessage("swift.standardgui.main").debug() << (file.exists() ? "Found translations in resources" : "No translations in resources");
|
||||
QTranslator translator;
|
||||
if (translator.load("blackmisc_i18n_de", ":blackmisc/translations/"))
|
||||
{
|
||||
CLogMessage("swift.standardgui.main").debug() << "Translator loaded";
|
||||
}
|
||||
|
||||
QIcon icon(BlackMisc::CIcons::swift24());
|
||||
QApplication::setWindowIcon(icon);
|
||||
const QString s = CStyleSheetUtility::instance().styles(
|
||||
{
|
||||
CStyleSheetUtility::fileNameFonts(),
|
||||
CStyleSheetUtility::fileNameSwiftCore()
|
||||
}
|
||||
);
|
||||
a.installTranslator(&translator);
|
||||
a.setStyleSheet(s);
|
||||
|
||||
CSwiftCore w(setup);
|
||||
if (!setup.m_minimzed) w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
142
src/swiftcore/swiftcore.cpp
Normal file
142
src/swiftcore/swiftcore.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
/* Copyright (C) 2015
|
||||
* 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 "swiftcore.h"
|
||||
#include "ui_swiftcore.h"
|
||||
#include "blackmisc/icon.h"
|
||||
#include "blackmisc/loghandler.h"
|
||||
#include "blackcore/dbus_server.h"
|
||||
#include "blackgui/stylesheetutility.h"
|
||||
#include "blackgui/components/enableforruntime.h"
|
||||
#include <QMenu>
|
||||
#include <QMessageBox>
|
||||
#include <QCloseEvent>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackCore;
|
||||
using namespace BlackGui;
|
||||
using namespace BlackGui::Components;
|
||||
|
||||
CSwiftCore::CSwiftCore(const SetupInfo &info, QWidget *parent) :
|
||||
CSystemTrayWindow(BlackMisc::CIcons::swiftNova24(), parent),
|
||||
ui(new Ui::CSwiftCore)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
SystemTrayMode mode = MinimizeToTray | QuitOnClose;
|
||||
setSystemTrayMode(mode);
|
||||
setToolTip(QStringLiteral("swiftcore"));
|
||||
|
||||
setupLogDisplay();
|
||||
|
||||
connectSlots();
|
||||
ps_onStyleSheetsChanged();
|
||||
startCore(info);
|
||||
}
|
||||
|
||||
CSwiftCore::~CSwiftCore()
|
||||
{
|
||||
}
|
||||
|
||||
void CSwiftCore::ps_startCorePressed()
|
||||
{
|
||||
SetupInfo setup;
|
||||
setup.m_dbusAddress = getDBusAddress();
|
||||
startCore(setup);
|
||||
}
|
||||
|
||||
void CSwiftCore::ps_stopCorePressed()
|
||||
{
|
||||
stopCore();
|
||||
}
|
||||
|
||||
void CSwiftCore::ps_appendLogMessage(const CStatusMessage &message)
|
||||
{
|
||||
ui->comp_log->appendStatusMessageToList(message);
|
||||
}
|
||||
|
||||
void CSwiftCore::ps_p2pModeToggled(bool checked)
|
||||
{
|
||||
if (checked)
|
||||
{
|
||||
ui->le_p2pAddress->setEnabled(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
ui->le_p2pAddress->setText(QString());
|
||||
ui->le_p2pAddress->setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
void CSwiftCore::ps_onStyleSheetsChanged()
|
||||
{
|
||||
const QString s = CStyleSheetUtility::instance().styles(
|
||||
{
|
||||
CStyleSheetUtility::fileNameFonts(),
|
||||
CStyleSheetUtility::fileNameSwiftCore()
|
||||
}
|
||||
);
|
||||
setStyleSheet(s);
|
||||
}
|
||||
|
||||
void CSwiftCore::connectSlots()
|
||||
{
|
||||
connect(ui->pb_startCore, &QPushButton::clicked, this, &CSwiftCore::ps_startCorePressed);
|
||||
connect(ui->pb_stopCore, &QPushButton::clicked, this, &CSwiftCore::ps_stopCorePressed);
|
||||
connect(ui->rb_p2pBus, &QRadioButton::toggled, this, &CSwiftCore::ps_p2pModeToggled);
|
||||
connect(&CStyleSheetUtility::instance(), &CStyleSheetUtility::styleSheetsChanged, this, &CSwiftCore::ps_onStyleSheetsChanged);
|
||||
}
|
||||
|
||||
void CSwiftCore::setupLogDisplay()
|
||||
{
|
||||
CLogHandler::instance()->install();
|
||||
CLogHandler::instance()->enableConsoleOutput(false); // default disable
|
||||
auto logHandler = CLogHandler::instance()->handlerForPattern(
|
||||
CLogPattern().withSeverityAtOrAbove(CStatusMessage::SeverityInfo)
|
||||
);
|
||||
|
||||
logHandler->subscribe(this, &CSwiftCore::ps_appendLogMessage);
|
||||
}
|
||||
|
||||
void CSwiftCore::startCore(const SetupInfo &setup)
|
||||
{
|
||||
if (getRuntime()) { return; }
|
||||
if (setup.m_dbusAddress.isEmpty()) { return; }
|
||||
|
||||
ui->pb_startCore->setEnabled(false);
|
||||
ui->pb_stopCore->setEnabled(true);
|
||||
ui->gb_dbusMode->setDisabled(true);
|
||||
|
||||
// context
|
||||
createRuntime(CRuntimeConfig::forCoreAllLocalInDBus(setup.m_dbusAddress), this);
|
||||
CEnableForRuntime::setRuntimeForComponents(this->getRuntime(), this);
|
||||
connect(ui->le_CommandLineInput, &CCommandInput::commandEntered, getRuntime(), &CRuntime::parseCommandLine);
|
||||
}
|
||||
|
||||
void CSwiftCore::stopCore()
|
||||
{
|
||||
if (!getRuntime()) { return; }
|
||||
|
||||
ui->pb_startCore->setEnabled(true);
|
||||
ui->pb_stopCore->setEnabled(false);
|
||||
ui->gb_dbusMode->setDisabled(false);
|
||||
|
||||
// Force quit, since we cannot close the runtime
|
||||
qApp->quit();
|
||||
}
|
||||
|
||||
QString CSwiftCore::getDBusAddress() const
|
||||
{
|
||||
if (ui->rb_sessionBus->isChecked()) { return CDBusServer::sessionDBusServer(); }
|
||||
if (ui->rb_systemBus->isChecked()) { return CDBusServer::systemDBusServer(); }
|
||||
if (ui->rb_p2pBus->isChecked()) { return CDBusServer::fixAddressToDBusAddress(ui->le_p2pAddress->text()); }
|
||||
|
||||
Q_ASSERT_X(false, "CSwiftCore::getDBusAddress()", "The impossible happended!");
|
||||
return "";
|
||||
}
|
||||
73
src/swiftcore/swiftcore.h
Normal file
73
src/swiftcore/swiftcore.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/* Copyright (C) 2015
|
||||
* 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 SWIFTCORE_H
|
||||
#define SWIFTCORE_H
|
||||
|
||||
#include "blackmisc/statusmessage.h"
|
||||
#include "blackcore/context_runtime.h"
|
||||
#include "blackgui/systemtraywindow.h"
|
||||
#include "blackgui/components/enableforruntime.h"
|
||||
#include <QScopedPointer>
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class CSwiftCore;
|
||||
}
|
||||
|
||||
//! swift core control
|
||||
class CSwiftCore :
|
||||
public BlackGui::CSystemTrayWindow,
|
||||
public BlackGui::Components::CEnableForRuntime
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
//! SwiftCore setup information
|
||||
struct SetupInfo
|
||||
{
|
||||
SetupInfo() {}
|
||||
|
||||
bool m_minimzed = false; //!< Start minimized to tray
|
||||
QString m_dbusAddress; //!< DBus address (session, system, p2p)
|
||||
};
|
||||
|
||||
//! Constructor
|
||||
CSwiftCore(const SetupInfo &info, QWidget *parent = nullptr);
|
||||
|
||||
//! Destructor
|
||||
~CSwiftCore();
|
||||
|
||||
private slots:
|
||||
|
||||
// PushButton slots
|
||||
void ps_startCorePressed();
|
||||
void ps_stopCorePressed();
|
||||
void ps_appendLogMessage(const BlackMisc::CStatusMessage &message);
|
||||
void ps_p2pModeToggled(bool checked);
|
||||
|
||||
//! Style sheet has changed
|
||||
virtual void ps_onStyleSheetsChanged();
|
||||
|
||||
private:
|
||||
|
||||
void connectSlots();
|
||||
void setupLogDisplay();
|
||||
void startCore(const SetupInfo &setup);
|
||||
void stopCore();
|
||||
|
||||
QString getDBusAddress() const;
|
||||
|
||||
QScopedPointer<Ui::CSwiftCore> ui;
|
||||
};
|
||||
|
||||
#endif // SWIFTCORE_H
|
||||
25
src/swiftcore/swiftcore.pro
Normal file
25
src/swiftcore/swiftcore.pro
Normal file
@@ -0,0 +1,25 @@
|
||||
include ($$SourceRoot/config.pri)
|
||||
include ($$SourceRoot/build.pri)
|
||||
|
||||
QT += core dbus network xml multimedia gui svg
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
TARGET = swiftcore
|
||||
TEMPLATE = app
|
||||
|
||||
SOURCES += *.cpp
|
||||
HEADERS += *.h
|
||||
FORMS += *.ui
|
||||
CONFIG += blackmisc blacksound blackinput blackcore blackgui
|
||||
|
||||
DEPENDPATH += . $$SourceRoot/src/blackmisc \
|
||||
$$SourceRoot/src/blacksound \
|
||||
$$SourceRoot/src/blackcore \
|
||||
$$SourceRoot/src/blackinput
|
||||
|
||||
INCLUDEPATH += . $$SourceRoot/src
|
||||
DESTDIR = $$BuildRoot/bin
|
||||
OTHER_FILES += *.qss
|
||||
|
||||
include ($$SourceRoot/libraries.pri)
|
||||
167
src/swiftcore/swiftcore.ui
Normal file
167
src/swiftcore/swiftcore.ui
Normal file
@@ -0,0 +1,167 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>CSwiftCore</class>
|
||||
<widget class="BlackGui::CSystemTrayWindow" name="CSwiftCore">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>460</width>
|
||||
<height>382</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>swiftcore</string>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true"/>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<item row="1" column="2">
|
||||
<widget class="QGroupBox" name="gb_Controls">
|
||||
<property name="title">
|
||||
<string>Controls</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="1">
|
||||
<widget class="QPushButton" name="pb_startCore">
|
||||
<property name="styleSheet">
|
||||
<string notr="true">/** Main window **/
|
||||
QTextEdit {
|
||||
background-color: DimGray;
|
||||
}</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Start</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="QPushButton" name="pb_stopCore">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true">/** Main window **/
|
||||
QTextEdit {
|
||||
background-color: DimGray;
|
||||
}</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Stop</string>
|
||||
</property>
|
||||
<property name="checkable">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0" colspan="3">
|
||||
<widget class="BlackGui::Components::CInfoBarStatusComponent" name="comp_InfoBarStatus" native="true">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="layoutDirection">
|
||||
<enum>Qt::LeftToRight</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0" colspan="3">
|
||||
<widget class="BlackGui::Components::CLogComponent" name="comp_log" native="true"/>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="2">
|
||||
<widget class="QGroupBox" name="gb_dbusMode">
|
||||
<property name="title">
|
||||
<string>DBus Mode</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<item row="0" column="0">
|
||||
<widget class="QRadioButton" name="rb_sessionBus">
|
||||
<property name="text">
|
||||
<string>Session</string>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="QRadioButton" name="rb_p2pBus">
|
||||
<property name="text">
|
||||
<string>P2P</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QRadioButton" name="rb_systemBus">
|
||||
<property name="text">
|
||||
<string>System</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="3">
|
||||
<widget class="QLineEdit" name="le_p2pAddress">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="placeholderText">
|
||||
<string>P2P address</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0" colspan="3">
|
||||
<widget class="BlackGui::CCommandInput" name="le_CommandLineInput"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>460</width>
|
||||
<height>25</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar"/>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>BlackGui::CSystemTrayWindow</class>
|
||||
<extends>QMainWindow</extends>
|
||||
<header>blackgui/systemtraywindow.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>BlackGui::Components::CInfoBarStatusComponent</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>blackgui/components/infobarstatuscomponent.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>BlackGui::Components::CLogComponent</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>blackgui/components/logcomponent.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>BlackGui::CCommandInput</class>
|
||||
<extends>QLineEdit</extends>
|
||||
<header>blackgui/commandinput.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user