void ClipboardClient::start(const QStringList &arguments) { QScriptEngine engine; ScriptableProxy scriptableProxy(nullptr, nullptr); Scriptable scriptable(&engine, &scriptableProxy); const auto serverName = clipboardServerName(); ClientSocket socket(serverName); connect( &socket, &ClientSocket::messageReceived, this, &ClipboardClient::onMessageReceived ); connect( &socket, &ClientSocket::disconnected, this, &ClipboardClient::onDisconnected ); connect( &socket, &ClientSocket::connectionFailed, this, &ClipboardClient::onConnectionFailed ); connect( &scriptableProxy, &ScriptableProxy::sendMessage, &socket, &ClientSocket::sendMessage ); connect( this, &ClipboardClient::functionCallResultReceived, &scriptableProxy, &ScriptableProxy::setFunctionCallReturnValue ); connect( this, &ClipboardClient::inputDialogFinished, &scriptableProxy, &ScriptableProxy::setInputDialogResult ); connect( &socket, &ClientSocket::disconnected, &scriptable, &Scriptable::abort ); connect( &socket, &ClientSocket::disconnected, &scriptableProxy, &ScriptableProxy::clientDisconnected ); connect( &scriptable, &Scriptable::finished, &scriptableProxy, &ScriptableProxy::clientDisconnected ); bool hasActionId; #if QT_VERSION < QT_VERSION_CHECK(5,5,0) auto actionId = qgetenv("COPYQ_ACTION_ID").toInt(&hasActionId); #else auto actionId = qEnvironmentVariableIntValue("COPYQ_ACTION_ID", &hasActionId); #endif const auto actionName = getTextData( qgetenv("COPYQ_ACTION_NAME") ); if ( socket.start() ) { if (hasActionId) scriptable.setActionId(actionId); scriptable.setActionName(actionName); const int exitCode = scriptable.executeArguments(arguments); exit(exitCode); } }
ClipboardServer::ClipboardServer(QApplication *app, const QString &sessionName) : QObject() , App(app, sessionName) , m_wnd(nullptr) , m_shortcutActions() , m_ignoreKeysTimer() { setCurrentThreadName("Server"); const QString serverName = clipboardServerName(); m_server = new Server(serverName, this); if ( m_server->isListening() ) { ::createSessionMutex(); restoreSettings(true); COPYQ_LOG("Server \"" + serverName + "\" started."); } else { restoreSettings(false); COPYQ_LOG("Server \"" + serverName + "\" already running!"); log( tr("CopyQ server is already running."), LogWarning ); exit(0); return; } QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true); QApplication::setQuitOnLastWindowClosed(false); m_itemFactory = new ItemFactory(this); m_wnd = new MainWindow(m_itemFactory); m_itemFactory->loadPlugins(); if ( !m_itemFactory->hasLoaders() ) log("No plugins loaded", LogNote); m_wnd->loadSettings(); m_wnd->setCurrentTab(0); m_wnd->enterBrowseMode(); connect( m_server, &Server::newConnection, this, &ClipboardServer::onClientNewConnection ); connect( qApp, &QCoreApplication::aboutToQuit, this, &ClipboardServer::onAboutToQuit ); connect( qApp, &QGuiApplication::commitDataRequest, this, &ClipboardServer::onCommitData ); connect( qApp, &QGuiApplication::saveStateRequest, this, &ClipboardServer::onSaveState ); #if QT_VERSION >= QT_VERSION_CHECK(5,6,0) qApp->setFallbackSessionManagementEnabled(false); #endif connect( m_wnd, &MainWindow::requestExit, this, &ClipboardServer::maybeQuit ); connect( m_wnd, &MainWindow::disableClipboardStoringRequest, this, &ClipboardServer::onDisableClipboardStoringRequest ); loadSettings(); // notify window if configuration changes connect( m_wnd, &MainWindow::configurationChanged, this, &ClipboardServer::loadSettings ); connect( m_wnd, &MainWindow::commandsSaved, this, &ClipboardServer::onCommandsSaved ); onCommandsSaved(); qApp->installEventFilter(this); m_server->start(); // Ignore global shortcut key presses in any widget. m_ignoreKeysTimer.setInterval(100); m_ignoreKeysTimer.setSingleShot(true); startMonitoring(); }