deskflow/src/lib/gui/core/CoreProcess.h

131 lines
4.3 KiB
C
Raw Normal View History

Refactored core process management and IPC client (#7425) * Also ignore `command_pre` for Fedora itself * Improve warning message * Fixed layout issues on main window * Restore fusion theme for Windows dark mode * Further correct main window layout * Set CWD for launch on Windows to same dir as .env * Use signals for tray icon instead of callback * Reduce complexity for setting tray icon * Further reduce tray icon complexity * Reduce tray retry time * Fixed tray not showing on macOS * Refactor function names * Move tray icon to lib * Decouple server connection class * Move server connection to lib * Move client config to lib * Remove redundant forward declarations * Fixed some namespaces in the new lib * Move core process code to new class * Improve member names on new process class * Remove copy/pasta code * Move OSX helpers to lib * Add .mm to lib config * Fixed copyright * Improve reliability of log line handling * Fixed TLS certificate generate bugs * Remove client list * Refactor core process handling to fix various problems * Fixed process/connection status bugs * Fixed function signature issue on macOS * Fixed override warnings * Fixed string format warning * Save `wasStarted` state and use that instead * Use only filename in dialog * Use lambda for simple slot * Scroll to bottom if at bottome * Set value based on position before text added and set horizontal scroll too * Add 1px tollerance for Linux * Simplify start/stop mutex * Always stop service on restart * Increase scroll bottom threshold to 2 * Log warning instead of critical * Fixed long-standing dir CD-up hack * Remove include * Remove include * Fixed rogue dumbisense includes * Account for optional distro_like * Add QAction include to solve incomplete type * Remove rogue #pragma * Static cast log value * Solve event queue delete warning * Fixed integ test on Windows 10 * Reduce enum verbosity * Use static instance instead of global * Make function const * Use unique_ptr instead of new and delete, and made some functions const * Fixed smart pointer use * Fixed variable shadowing * Fixed wrong use of using * Fixed missing namespace using * Simplify TLS error handling * Improve UX around certificate errors and success * Decouple app config from core process through interface * First iteration of core process test * Mark dtor as override * Rename attach launch entry * Add TODO * Create proxy for process * Move IPC client to deps * Fixed warnings * Reorganize new GUI lib dir structure * Update includes to reflect new paths * Reorg GUI tests * Abstract IPC client * Refactor about screen * Fixed .ui warnings * Remove redundant include * Fixed typo * Fixed typos and add spelling * Fixed misleading function name * Improve comment * Improve code coverage for core process * Remove noisy log line * Make global const * Use default dtor * Use vector instead of new * Make ctor explicit * Make ctor explicit (2) * Use enum class * Fixed bad enum * Stub out core tool * Stub out dir operation * Extract byte functions * Add missing return path * Simplify byte/int functions * Fix truncation * Use ctor member assignment * Fixed segfault in process proxy * Fixed print warning * Make function const * Cleanup header * Fixed missing name * Update ChangeLog * Make more functions const
2024-08-07 14:05:18 +00:00
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2024 Symless Ltd.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "gui/config/IAppConfig.h"
#include "gui/config/IServerConfig.h"
#include "gui/ipc/QIpcClient.h"
#include "gui/proxy/QProcessProxy.h"
#include <QMutex>
#include <QObject>
#include <QString>
#include <QStringList>
#include <memory>
namespace synergy::gui {
class CoreProcess : public QObject {
using IServerConfig = synergy::gui::IServerConfig;
using QProcessProxy = synergy::gui::proxy::QProcessProxy;
using IQIpcClient = synergy::gui::ipc::IQIpcClient;
Q_OBJECT
public:
struct Deps {
virtual ~Deps() = default;
virtual QProcessProxy &process() { return m_process; }
virtual IQIpcClient &ipcClient() { return m_ipcClient; }
virtual QString appPath(const QString &name) const;
virtual bool fileExists(const QString &path) const;
virtual QString getProfileRoot() const;
private:
QProcessProxy m_process;
QIpcClient m_ipcClient;
};
enum class Mode { None, Client, Server };
enum class Error { AddressMissing, StartFailed };
enum class ProcessState { Starting, Started, Stopping, Stopped };
enum class ConnectionState { Disconnected, Connecting, Connected, Listening };
explicit CoreProcess(
IAppConfig &appConfig, IServerConfig &serverConfig,
std::shared_ptr<Deps> deps = std::make_shared<Deps>());
void extracted(QString &app, QStringList &args);
void start(std::optional<ProcessMode> processMode = std::nullopt);
void stop(std::optional<ProcessMode> processMode = std::nullopt);
void restart();
void cleanup();
// getters
Mode mode() const { return m_mode; }
QString secureSocketVersion() const { return m_secureSocketVersion; }
bool isStarted() const { return m_processState == ProcessState::Started; }
ProcessState processState() const { return m_processState; }
ConnectionState connectionState() const { return m_connectionState; }
// setters
void setAddress(const QString &address) { m_address = address.trimmed(); }
void setMode(Mode mode) { m_mode = mode; }
signals:
void starting();
void error(Error error);
void logLine(const QString &line);
void connectionStateChanged(ConnectionState state);
void processStateChanged(ProcessState state);
void secureSocket(bool enabled);
private slots:
void onIpcClientServiceReady();
void onIpcClientRead(const QString &text);
void onIpcClientError(const QString &text) const;
void onProcessFinished(int exitCode, QProcess::ExitStatus);
void onProcessReadyReadStandardOutput();
void onProcessReadyReadStandardError();
private:
void startDesktop(const QString &app, const QStringList &args);
void startService(const QString &app, const QStringList &args);
void stopDesktop() const;
void stopService();
bool serverArgs(QStringList &args, QString &app);
bool clientArgs(QStringList &args, QString &app);
QString persistConfig() const;
QString modeString() const;
QString processModeString() const;
void setConnectionState(ConnectionState state);
void setProcessState(ProcessState state);
void checkLogLine(const QString &line);
bool checkSecureSocket(const QString &line);
void handleLogLines(const QString &text);
QString correctedInterface() const;
QString correctedAddress() const;
#ifdef Q_OS_MAC
void checkOSXNotification(const QString &line);
#endif
IAppConfig &m_appConfig;
IServerConfig &m_serverConfig;
std::shared_ptr<Deps> m_pDeps;
QString m_address;
ProcessState m_processState = ProcessState::Stopped;
ConnectionState m_connectionState = ConnectionState::Disconnected;
Mode m_mode = Mode::None;
QMutex m_processMutex;
QString m_secureSocketVersion = "";
std::optional<ProcessMode> m_lastProcessMode = std::nullopt;
};
} // namespace synergy::gui