mirror of
https://github.com/zealdocs/zeal.git
synced 2026-08-29 16:40:51 +08:00
app, core: Move interinstance communication to Application
This commit is contained in:
@@ -46,6 +46,8 @@ using namespace Zeal;
|
||||
using namespace Zeal::Core;
|
||||
|
||||
namespace {
|
||||
const char LocalServerName[] = "ZealLocalServer";
|
||||
|
||||
const char ReleasesApiUrl[] = "http://api.zealdocs.org/v1/releases";
|
||||
}
|
||||
|
||||
@@ -94,9 +96,11 @@ Application::Application(const SearchQuery &query, QObject *parent) :
|
||||
|
||||
m_mainWindow->bringToFront();
|
||||
});
|
||||
|
||||
// Remove in case previous instance crashed
|
||||
/// TODO: Verify if removeServer() is needed
|
||||
QLocalServer::removeServer(localServerName()); // remove in case previous instance crashed
|
||||
m_localServer->listen(localServerName());
|
||||
QLocalServer::removeServer(LocalServerName);
|
||||
m_localServer->listen(LocalServerName);
|
||||
|
||||
// Extractor setup
|
||||
m_extractor->moveToThread(m_extractorThread);
|
||||
@@ -133,9 +137,26 @@ Application::~Application()
|
||||
delete m_docsetRegistry;
|
||||
}
|
||||
|
||||
QString Application::localServerName()
|
||||
/*!
|
||||
* \internal
|
||||
* \brief Hands over \a query to already running application instance, if it exists.
|
||||
* \param query A query to execute search with.
|
||||
* \param preventActivation If \c true, application window will not activated.
|
||||
* \return \c true if communication with another instance was successful.
|
||||
*/
|
||||
bool Application::send(const SearchQuery &query, bool preventActivation)
|
||||
{
|
||||
return QStringLiteral("ZealLocalServer");
|
||||
QScopedPointer<QLocalSocket> socket(new QLocalSocket());
|
||||
socket->connectToServer(LocalServerName);
|
||||
|
||||
if (!socket->waitForConnected(500))
|
||||
return false;
|
||||
|
||||
QDataStream out(socket.data());
|
||||
out << query;
|
||||
out << preventActivation;
|
||||
socket->flush();
|
||||
return true;
|
||||
}
|
||||
|
||||
QNetworkAccessManager *Application::networkManager() const
|
||||
|
||||
@@ -50,7 +50,7 @@ public:
|
||||
explicit Application(const SearchQuery &query, QObject *parent = nullptr);
|
||||
~Application() override;
|
||||
|
||||
static QString localServerName();
|
||||
static bool send(const SearchQuery &query, bool preventActivation);
|
||||
|
||||
QNetworkAccessManager *networkManager() const;
|
||||
Settings *settings() const;
|
||||
|
||||
+3
-14
@@ -26,10 +26,8 @@
|
||||
|
||||
#include <QApplication>
|
||||
#include <QCommandLineParser>
|
||||
#include <QDataStream>
|
||||
#include <QDesktopServices>
|
||||
#include <QDir>
|
||||
#include <QLocalSocket>
|
||||
#include <QMessageBox>
|
||||
#include <QSqlDatabase>
|
||||
#include <QTextStream>
|
||||
@@ -191,19 +189,10 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// Detect already running instance and optionally pass a search query to it.
|
||||
if (!clParams.force) {
|
||||
QScopedPointer<QLocalSocket> socket(new QLocalSocket());
|
||||
socket->connectToServer(Zeal::Core::Application::localServerName());
|
||||
|
||||
if (socket->waitForConnected(500)) {
|
||||
QDataStream out(socket.data());
|
||||
out << clParams.query;
|
||||
out << clParams.preventActivation;
|
||||
socket->flush();
|
||||
return 0;
|
||||
}
|
||||
if (!clParams.force
|
||||
&& Zeal::Core::Application::send(clParams.query, clParams.preventActivation)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Check for SQLite plugin
|
||||
|
||||
Reference in New Issue
Block a user