From d7dedfeb1a6fdb69825799e74a1e968598d258e7 Mon Sep 17 00:00:00 2001 From: Carlo Castoldi Date: Tue, 15 Nov 2022 12:23:47 +0100 Subject: [PATCH] REFAC(ipc): extract getRuntimeDir() for Socket and OverlayPipe --- src/mumble/CMakeLists.txt | 5 +++++ src/mumble/IPCUtils.h | 20 ++++++++++++++++++++ src/mumble/IPCUtils_linux.cpp | 27 +++++++++++++++++++++++++++ src/mumble/IPCUtils_macx.cpp | 20 ++++++++++++++++++++ src/mumble/IPCUtils_unix.cpp | 17 +++++++++++++++++ src/mumble/Overlay.cpp | 11 ++--------- src/mumble/SocketRPC.cpp | 22 +++------------------- src/tests/OverlayTest/OverlayTest.cpp | 14 +++----------- 8 files changed, 97 insertions(+), 39 deletions(-) create mode 100644 src/mumble/IPCUtils.h create mode 100644 src/mumble/IPCUtils_linux.cpp create mode 100644 src/mumble/IPCUtils_macx.cpp create mode 100644 src/mumble/IPCUtils_unix.cpp diff --git a/src/mumble/CMakeLists.txt b/src/mumble/CMakeLists.txt index 984cdbc5d33..0326702c15e 100644 --- a/src/mumble/CMakeLists.txt +++ b/src/mumble/CMakeLists.txt @@ -155,6 +155,7 @@ set(MUMBLE_SOURCES "GlobalShortcutButtons.ui" "GlobalShortcutTarget.ui" "GlobalShortcutTypes.h" + "IPCUtils.h" "JSONSerialization.cpp" "JSONSerialization.h" "LCD.cpp" @@ -655,6 +656,9 @@ else() if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") find_library(LIB_RT rt) target_link_libraries(mumble_client_object_lib PUBLIC ${LIB_RT}) + target_sources(mumble_client_object_lib PRIVATE "IPCUtils_linux.cpp") + else() + target_sources(mumble_client_object_lib PRIVATE "IPCUtils_unix.cpp") endif() target_link_libraries(mumble_client_object_lib PUBLIC X11::Xext) @@ -672,6 +676,7 @@ else() "AppNap.mm" "GlobalShortcut_macx.h" "GlobalShortcut_macx.mm" + "IPCUtils_macx.cpp" "Log_macx.mm" "os_macx.mm" ) diff --git a/src/mumble/IPCUtils.h b/src/mumble/IPCUtils.h new file mode 100644 index 00000000000..1af0da1745d --- /dev/null +++ b/src/mumble/IPCUtils.h @@ -0,0 +1,20 @@ +// Copyright 2021-2022 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +#ifndef MUMBLE_MUMBLE_IPCUTILS_H_ +#define MUMBLE_MUMBLE_IPCUTILS_H_ + +#include + +namespace Mumble { + + /** + * @returns The path where Mumble sets up MumbleOverlayPipe and MumbleSocket + */ + QDir getRuntimeDir(void); + +}; // namespace Mumble + +#endif // MUMBLE_MUMBLE_IPCUTILS_H_ diff --git a/src/mumble/IPCUtils_linux.cpp b/src/mumble/IPCUtils_linux.cpp new file mode 100644 index 00000000000..2947e48bcd5 --- /dev/null +++ b/src/mumble/IPCUtils_linux.cpp @@ -0,0 +1,27 @@ +// Copyright 2021-2022 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +#include "IPCUtils.h" + +#include +#include +#include +#include + +namespace Mumble { + QDir getRuntimeDir(void) { + QString xdgRuntimePath = QProcessEnvironment::systemEnvironment().value(QLatin1String("XDG_RUNTIME_DIR")); + QString mumbleRuntimePath; + if (!xdgRuntimePath.isNull()) { + mumbleRuntimePath = QDir(xdgRuntimePath).absolutePath() + QLatin1String("/mumble/"); + } else { + mumbleRuntimePath = QLatin1String("/run/user/") + QString::number(getuid()) + QLatin1String("/mumble/"); + } + QDir mumbleRuntimeDir = QDir(mumbleRuntimePath); + mumbleRuntimeDir.mkpath("."); + return mumbleRuntimeDir; + } + +}; // namespace Mumble diff --git a/src/mumble/IPCUtils_macx.cpp b/src/mumble/IPCUtils_macx.cpp new file mode 100644 index 00000000000..ca847af5950 --- /dev/null +++ b/src/mumble/IPCUtils_macx.cpp @@ -0,0 +1,20 @@ +// Copyright 2021-2022 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +#include "IPCUtils.h" + +#include +//#include +#include + +namespace Mumble { + QDir getRuntimeDir(void) { + // QString runtimePath = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation); // defaults to "~/Library/Application Support" + // QDir mumbleRuntimeDir = QDir(runtimePath); + QDir mumbleRuntimeDir = QDir::home(); + return mumbleRuntimeDir; + } + +}; // namespace Mumble diff --git a/src/mumble/IPCUtils_unix.cpp b/src/mumble/IPCUtils_unix.cpp new file mode 100644 index 00000000000..85476f7954d --- /dev/null +++ b/src/mumble/IPCUtils_unix.cpp @@ -0,0 +1,17 @@ +// Copyright 2021-2022 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +#include "IPCUtils.h" + +#include +#include + +namespace Mumble { + QDir getRuntimeDir(void) { + QDir mumbleRuntimeDir = QDir::home(); + return mumbleRuntimeDir; + } + +}; // namespace Mumble diff --git a/src/mumble/Overlay.cpp b/src/mumble/Overlay.cpp index ed8c5f5d026..cf37e274096 100644 --- a/src/mumble/Overlay.cpp +++ b/src/mumble/Overlay.cpp @@ -8,6 +8,7 @@ #include "Channel.h" #include "ClientUser.h" #include "Database.h" +#include "IPCUtils.h" #include "MainWindow.h" #include "OverlayClient.h" #include "OverlayText.h" @@ -243,15 +244,7 @@ void Overlay::createPipe() { pipepath = QLatin1String("MumbleOverlayPipe"); #else { - QString xdgRuntimePath = QProcessEnvironment::systemEnvironment().value(QLatin1String("XDG_RUNTIME_DIR")); - QString mumbleRuntimePath; - if (!xdgRuntimePath.isNull()) { - mumbleRuntimePath = QDir(xdgRuntimePath).absolutePath() + QLatin1String("/mumble/"); - } else { - mumbleRuntimePath = QLatin1String("/run/user/") + QString::number(getuid()) + QLatin1String("/mumble/"); - } - QDir mumbleRuntimeDir = QDir(mumbleRuntimePath); - mumbleRuntimeDir.mkpath("."); + QDir mumbleRuntimeDir = Mumble::getRuntimeDir(); pipepath = mumbleRuntimeDir.absoluteFilePath(QLatin1String("MumbleOverlayPipe")); } diff --git a/src/mumble/SocketRPC.cpp b/src/mumble/SocketRPC.cpp index bf1bcb6c6ad..d2e4dddecd8 100644 --- a/src/mumble/SocketRPC.cpp +++ b/src/mumble/SocketRPC.cpp @@ -7,6 +7,7 @@ #include "Channel.h" #include "ClientUser.h" +#include "IPCUtils.h" #include "MainWindow.h" #include "ServerHandler.h" #include "Global.h" @@ -235,15 +236,7 @@ SocketRPC::SocketRPC(const QString &basename, QObject *p) : QObject(p) { pipepath = basename; #else { - QString xdgRuntimePath = QProcessEnvironment::systemEnvironment().value(QLatin1String("XDG_RUNTIME_DIR")); - QString mumbleRuntimePath; - if (!xdgRuntimePath.isNull()) { - mumbleRuntimePath = QDir(xdgRuntimePath).absolutePath() + QLatin1String("/mumble/"); - } else { - mumbleRuntimePath = QLatin1String("/run/user/") + QString::number(getuid()) + QLatin1String("/mumble/"); - } - QDir mumbleRuntimeDir = QDir(mumbleRuntimePath); - mumbleRuntimeDir.mkpath("."); + QDir mumbleRuntimeDir = Mumble::getRuntimeDir(); pipepath = mumbleRuntimeDir.absoluteFilePath(basename + QLatin1String("Socket")); } @@ -281,15 +274,7 @@ bool SocketRPC::send(const QString &basename, const QString &request, const QMap pipepath = basename; #else { - QString xdgRuntimePath = QProcessEnvironment::systemEnvironment().value(QLatin1String("XDG_RUNTIME_DIR")); - QString mumbleRuntimePath; - if (!xdgRuntimePath.isNull()) { - mumbleRuntimePath = QDir(xdgRuntimePath).absolutePath() + QLatin1String("/mumble/"); - } else { - mumbleRuntimePath = QLatin1String("/run/user/") + QString::number(getuid()) + QLatin1String("/mumble/"); - } - QDir mumbleRuntimeDir = QDir(mumbleRuntimePath); - mumbleRuntimeDir.mkpath("."); + QDir mumbleRuntimeDir = Mumble::getRuntimeDir(); pipepath = mumbleRuntimeDir.absoluteFilePath(basename + QLatin1String("Socket")); } #endif @@ -329,4 +314,3 @@ bool SocketRPC::send(const QString &basename, const QString &request, const QMap return QVariant(succ.text()).toBool(); } - diff --git a/src/tests/OverlayTest/OverlayTest.cpp b/src/tests/OverlayTest/OverlayTest.cpp index 573c05a63a4..7a4fd1d4b90 100644 --- a/src/tests/OverlayTest/OverlayTest.cpp +++ b/src/tests/OverlayTest/OverlayTest.cpp @@ -9,13 +9,14 @@ #include "../../overlay/overlay.h" +#include "IPCUtils.h" #include "SharedMemory.h" #include "Timer.h" #ifdef Q_OS_WIN # include "win.h" #else -# include +# include #endif #include @@ -26,7 +27,6 @@ #include #include - class OverlayWidget : public QWidget { Q_OBJECT @@ -101,15 +101,7 @@ void OverlayWidget::paintEvent(QPaintEvent *) { #ifdef Q_OS_WIN qlsSocket->connectToServer(QLatin1String("MumbleOverlayPipe")); #else - QString xdgRuntimePath = QProcessEnvironment::systemEnvironment().value(QLatin1String("XDG_RUNTIME_DIR")); - QString mumbleRuntimePath; - if (!xdgRuntimePath.isNull()) { - mumbleRuntimePath = QDir(xdgRuntimePath).absolutePath() + QLatin1String("/mumble/"); - } else { - mumbleRuntimePath = QLatin1String("/run/user/") + QString::number(getuid()) + QLatin1String("/mumble/"); - } - QDir mumbleRuntimeDir = QDir(mumbleRuntimePath); - mumbleRuntimeDir.mkpath("."); + QDir mumbleRuntimeDir = Mumble::getRuntimeDir(); QString pipepath = mumbleRuntimeDir.absoluteFilePath(QLatin1String("MumbleOverlayPipe")); qWarning() << "connectToServer(" << pipepath << ")"; qlsSocket->connectToServer(pipepath);