Migrate to cmake and refine the code structure (#92)

* migrate to cmake

* move the 3rd party libs

* organize 3rdparty

* update the 3rd party version

* refine the CMakeLists.txt

* update install command

* update install command

* update install command

* update install command

* update dependency

* update the dependency

* update the dependency

* remove CPM.cmake

* remove QtCreator project file

* update cmake file

* improve cmake file

* add cmakew

* use wget

* remove echo

* update the doc

* remove the screenshot

* update the doc

* update the install steps

* check the openconnect version

* update the doc

* update install scripts

* fix install scripts

* improve message

* improve message

* improve install scripts

* improve the version check

* improve the version check

* improve install script

* add version

* organize includes

* add version bump

* update CI

* update CI

* add the release flag

* update message
This commit is contained in:
Kevin Yue
2021-09-19 14:32:12 +08:00
committed by GitHub
parent 5c97b2df7a
commit 8a65099ca7
59 changed files with 1034 additions and 357 deletions

57
GPService/CMakeLists.txt Normal file
View File

@@ -0,0 +1,57 @@
include("${CMAKE_SOURCE_DIR}/cmake/Add3rdParty.cmake")
project(GPService)
set(gpservice_GENERATED_SOURCES)
# generate the dbus xml definition
qt5_generate_dbus_interface(gpservice.h ${CMAKE_BINARY_DIR}/com.yuezk.qt.GPService.xml)
# generate dbus adaptor
qt5_add_dbus_adaptor(
gpservice_GENERATED_SOURCES
${CMAKE_BINARY_DIR}/com.yuezk.qt.GPService.xml
gpservice.h
GPService
)
add_executable(gpservice
gpservice.cpp
main.cpp
sigwatch.cpp
${gpservice_GENERATED_SOURCES}
)
add_3rdparty(
SingleApplication
GIT_REPOSITORY https://github.com/itay-grudev/SingleApplication.git
GIT_TAG v3.3.0
CMAKE_ARGS -DQAPPLICATION_CLASS=QCoreApplication
)
ExternalProject_Get_Property(SingleApplication-${PROJECT_NAME} SOURCE_DIR BINARY_DIR)
set(SingleApplication_INCLUDE_DIR ${SOURCE_DIR})
set(SingleApplication_LIBRARY ${BINARY_DIR}/libSingleApplication.a)
add_dependencies(gpservice SingleApplication-${PROJECT_NAME})
target_include_directories(gpservice PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${SingleApplication_INCLUDE_DIR}
)
target_link_libraries(gpservice
${SingleApplication_LIBRARY}
Qt5::Core
Qt5::Network
Qt5::DBus
)
target_compile_definitions(gpservice PUBLIC QAPPLICATION_CLASS=QCoreApplication)
install(TARGETS gpservice DESTINATION "/usr/bin")
install(FILES "dbus/com.yuezk.qt.GPService.conf" DESTINATION "/usr/share/dbus-1/system.d" )
install(FILES "dbus/com.yuezk.qt.GPService.service" DESTINATION "/usr/share/dbus-1/system-services")
install(FILES "systemd/gpservice.service" DESTINATION "/etc/systemd/system")

View File

@@ -1,52 +0,0 @@
TARGET = gpservice
QT += dbus
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
include(../singleapplication/singleapplication.pri)
DEFINES += QAPPLICATION_CLASS=QCoreApplication
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
HEADERS += \
gpservice.h \
sigwatch.h
SOURCES += \
gpservice.cpp \
main.cpp \
sigwatch.cpp
DBUS_ADAPTORS += gpservice.xml
# Default rules for deployment.
target.path = /usr/bin
INSTALLS += target
DISTFILES += \
dbus/com.yuezk.qt.GPService.conf \
dbus/com.yuezk.qt.GPService.service \
systemd/gpservice.service
dbus_config.path = /usr/share/dbus-1/system.d/
dbus_config.files = dbus/com.yuezk.qt.GPService.conf
dbus_service.path = /usr/share/dbus-1/system-services/
dbus_service.files = dbus/com.yuezk.qt.GPService.service
systemd_service.path = /etc/systemd/system/
systemd_service.files = systemd/gpservice.service
INSTALLS += dbus_config dbus_service systemd_service

View File

@@ -1,10 +1,12 @@
#include "gpservice.h"
#include "gpservice_adaptor.h"
#include <QtCore/QFileInfo>
#include <QtCore/QDateTime>
#include <QtCore/QVariant>
#include <QtCore/QRegularExpression>
#include <QtCore/QRegularExpressionMatch>
#include <QtDBus/QtDBus>
#include <QFileInfo>
#include <QtDBus>
#include <QDateTime>
#include <QVariant>
#include "gpservice.h"
#include "gpserviceadaptor.h"
GPService::GPService(QObject *parent)
: QObject(parent)
@@ -104,19 +106,49 @@ void GPService::connect(QString server, QString username, QString passwd, QStrin
return;
}
if (!isValidVersion(bin)) {
return;
}
QStringList args;
args << QCoreApplication::arguments().mid(1)
<< "--protocol=gp"
<< splitCommand(extraArgs)
<< "-u" << username
<< "-C" << passwd
<< server;
<< "--protocol=gp"
<< splitCommand(extraArgs)
<< "-u" << username
<< "-C" << passwd
<< server;
log("Start process with arugments: " + args.join(" "));
openconnect->start(bin, args);
}
bool GPService::isValidVersion(QString &bin) {
QProcess p;
p.start(bin, QStringList("--version"));
p.waitForFinished();
QString output = p.readAllStandardError() + p.readAllStandardOutput();
QRegularExpression re("v(\\d+).*?(\\s|\\n)");
QRegularExpressionMatch match = re.match(output);
if (match.hasMatch()) {
log("Output of `openconnect --version`: " + output);
QString fullVersion = match.captured(0);
QString majorVersion = match.captured(1);
if (majorVersion.toInt() < 8) {
emit error("The OpenConnect version must greater than v8.0.0, but got " + fullVersion);
return false;
}
} else {
log("Failed to parse the OpenConnect version from " + output);
}
return true;
}
void GPService::disconnect()
{
if (openconnect->state() != QProcess::NotRunning) {

View File

@@ -1,8 +1,8 @@
#ifndef GLOBALPROTECTSERVICE_H
#define GLOBALPROTECTSERVICE_H
#include <QObject>
#include <QProcess>
#include <QtCore/QObject>
#include <QtCore/QProcess>
static const QString binaryPaths[] {
"/usr/local/bin/openconnect",
@@ -21,6 +21,8 @@ public:
explicit GPService(QObject *parent = nullptr);
~GPService();
void quit();
enum VpnStatus {
VpnNotConnected,
VpnConnecting,
@@ -38,7 +40,6 @@ public slots:
void connect(QString server, QString username, QString passwd, QString extraArgs);
void disconnect();
int status();
void quit();
private slots:
void onProcessStarted();
@@ -53,6 +54,7 @@ private:
int vpnStatus = GPService::VpnNotConnected;
void log(QString msg);
bool isValidVersion(QString &bin);
static QString findBinary();
static QStringList splitCommand(QString command);
};

View File

@@ -1,26 +0,0 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="com.yuezk.qt.GPService">
<signal name="connected">
</signal>
<signal name="disconnected">
</signal>
<signal name="logAvailable">
<arg name="log" type="s" />
</signal>
<signal name="error">
<arg name="errorMessage" type="s" />
</signal>
<method name="connect">
<arg name="server" type="s" direction="in"/>
<arg name="username" type="s" direction="in"/>
<arg name="passwd" type="s" direction="in"/>
<arg name="extraArgs" type="s" direction="in"/>
</method>
<method name="disconnect">
</method>
<method name="status">
<arg type="i" direction="out"/>
</method>
</interface>
</node>

View File

@@ -1,4 +1,5 @@
#include <QtDBus>
#include <QtDBus/QtDBus>
#include "gpservice.h"
#include "singleapplication.h"
#include "sigwatch.h"