From 705249718c3172cf845f8525d9867a29fe1240fa Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 30 Jan 2018 19:23:34 +0100 Subject: Always connect to local socket --- src/browser.cpp | 5 ++--- src/commandline.cpp | 13 ++++++++++++- src/main.cpp | 31 +++++++++++++++++-------------- src/singleapplication.cpp | 18 ++---------------- src/singleapplication.h | 10 +++++----- 5 files changed, 38 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/browser.cpp b/src/browser.cpp index 4782512..2f2249a 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -48,10 +48,10 @@ void Browser::setConfiguration(std::shared_ptr &config) QDir pluginsDir(QString::fromStdString(m_config->value("plugins.path").value())); if(pluginsDir.exists()) { const QStringList entries = pluginsDir.entryList(QDir::Files | QDir::Readable); - for (const QString &name : entries) { + for(const QString &name : entries) { QPluginLoader loader(pluginsDir.absoluteFilePath(name)); qDebug("Loading plugin %s: %s", qUtf8Printable(name), loader.load() ? "ok" : "failed"); - if (!loader.isLoaded()) { + if(!loader.isLoaded()) { qDebug("Error: %s", qUtf8Printable(loader.errorString())); } else { Plugin d; @@ -63,7 +63,6 @@ void Browser::setConfiguration(std::shared_ptr &config) } } - m_bookmarksManager = std::make_shared(QString::fromStdString(m_config->value("bookmarks.path").value())); m_downloadManager = std::make_shared(QString::fromStdString(m_config->value("downloads.path").value())); diff --git a/src/commandline.cpp b/src/commandline.cpp index e10e638..ffd7957 100644 --- a/src/commandline.cpp +++ b/src/commandline.cpp @@ -33,6 +33,17 @@ QString defaultUserConfigLocation() return path; } +constexpr const char *socketPath() +{ +#ifdef Q_OS_UNIX + // could be a path such as "/tmp/foo" + return "/tmp/smolbote.socket"; +#elif Q_OS_WIN32 + // could be a pipe path such as "\\.\pipe\foo" + return = "\\\\.\\pipe\\smolbote_socket"; +#endif +} + CommandLine::CommandLine() : QCommandLineParser() , helpOption(addHelpOption()) @@ -40,7 +51,7 @@ CommandLine::CommandLine() , configOption({ "c", "config" }, "Set configuration file.", "path", defaultUserConfigLocation()) , defaultConfigOption("default-config", "Set the default configuration file.", "path", "") , profileOption({ "p", "profile" }, "Use this profile.", "profile", "") - , socketOption("socket", "Local server socket", "name", "") + , socketOption("socket", "Local server socket", "name", socketPath()) , newWindowOption("in-new-window", "Open URL in new window") { setApplicationDescription("yet another no-frills browser"); diff --git a/src/main.cpp b/src/main.cpp index e29e547..6efb882 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -77,23 +77,26 @@ int main(int argc, char **argv) } // check for other instances - // if we socket hasn't been disabled (socket is not none) - if(parser.value(parser.socketOption) != "none") { - bool bindOk = instance.bindLocalSocket(parser.value(parser.socketOption)); - if(bindOk) { - qDebug("Connected to local socket: %s", qUtf8Printable(instance.serverName())); - } else { - // pass arguments to new instance - return instance.sendMessage(parser.value(parser.profileOption), parser.isSet(parser.newWindowOption), parser.positionalArguments()); - } + bool isSingleInstance = instance.bindLocalSocket(parser.value(parser.socketOption)); + qDebug("Connected to local socket %s: %s", qUtf8Printable(instance.serverName()), isSingleInstance ? "ok" : "failed"); + if(isSingleInstance) { + instance.setConfiguration(config); } - instance.setConfiguration(config); + // create session + { + QString profile; + if(parser.isSet(parser.profileOption)) + profile = parser.value(parser.profileOption); + else + profile = QString::fromStdString(config->value("browser.profile").value()); + + instance.sendMessage(profile, parser.isSet(parser.newWindowOption), parser.positionalArguments()); + } - if(parser.isSet(parser.profileOption)) - instance.createSession(parser.value(parser.profileOption), parser.isSet(parser.newWindowOption), parser.positionalArguments()); - else - instance.createSession(QString::fromStdString(config->value("browser.profile").value()), parser.isSet(parser.newWindowOption), parser.positionalArguments()); + if(!isSingleInstance) { + return 0; + } #ifdef QT_DEBUG qDebug("Startup complete in %lldms", timer.elapsed()); diff --git a/src/singleapplication.cpp b/src/singleapplication.cpp index 2356fc2..720690d 100644 --- a/src/singleapplication.cpp +++ b/src/singleapplication.cpp @@ -14,13 +14,6 @@ SingleApplication::SingleApplication(int &argc, char **argv) : QApplication(argc, argv) { -#ifdef Q_OS_UNIX - // could be a path such as "/tmp/foo" - LOCALSERVER_KEY = "smolbote_socket"; -#elif Q_OS_WIN32 - // could be a pipe path such as "\\.\pipe\foo" - LOCALSERVER_KEY = "\\.\pipe\smolbote_socket"; -#endif } SingleApplication::~SingleApplication() @@ -39,10 +32,7 @@ SingleApplication::~SingleApplication() */ bool SingleApplication::bindLocalSocket(const QString &name) { - // if a name has been set - if(!name.isEmpty()) { - LOCALSERVER_KEY = name; - } + LOCALSERVER_KEY = name; QLocalSocket socket; socket.connectToServer(LOCALSERVER_KEY); @@ -60,11 +50,7 @@ bool SingleApplication::bindLocalSocket(const QString &name) // no other server QLocalServer::removeServer(LOCALSERVER_KEY); - if(!m_localServer->listen(LOCALSERVER_KEY)) { - // for some reason, we still can't bind the socket - return false; - } - return true; + return m_localServer->listen(LOCALSERVER_KEY); } } diff --git a/src/singleapplication.h b/src/singleapplication.h index 4d75da6..05b7b40 100644 --- a/src/singleapplication.h +++ b/src/singleapplication.h @@ -6,8 +6,8 @@ * SPDX-License-Identifier: GPL-3.0 */ -#ifndef SINGLEAPPLICATION_H -#define SINGLEAPPLICATION_H +#ifndef SMOLBOTE_SINGLEAPPLICATION_H +#define SMOLBOTE_SINGLEAPPLICATION_H #include @@ -18,9 +18,9 @@ class SingleApplication : public QApplication public: explicit SingleApplication(int &argc, char **argv); - ~SingleApplication(); + ~SingleApplication() override; - bool bindLocalSocket(const QString &name = QString()); + bool bindLocalSocket(const QString &name); QString serverName() const; int sendMessage(const QString &profileName, bool newWindow, const QStringList &urls); @@ -38,4 +38,4 @@ private: QLocalServer *m_localServer = nullptr; }; -#endif // SINGLEAPPLICATION_H +#endif // SMOLBOTE_SINGLEAPPLICATION_H -- cgit v1.2.1