diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-01-30 19:23:34 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-01-30 19:23:34 +0100 |
commit | 705249718c3172cf845f8525d9867a29fe1240fa (patch) | |
tree | 7fafee76b2d371284ebd9ad8ba011df1e7664eab | |
parent | Profile menu lists loadable profiles (diff) | |
download | smolbote-705249718c3172cf845f8525d9867a29fe1240fa.tar.xz |
Always connect to local socket
-rw-r--r-- | src/browser.cpp | 5 | ||||
-rw-r--r-- | src/commandline.cpp | 13 | ||||
-rw-r--r-- | src/main.cpp | 31 | ||||
-rw-r--r-- | src/singleapplication.cpp | 18 | ||||
-rw-r--r-- | src/singleapplication.h | 10 |
5 files changed, 38 insertions, 39 deletions
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<Configuration> &config) QDir pluginsDir(QString::fromStdString(m_config->value<std::string>("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<Configuration> &config) } } - m_bookmarksManager = std::make_shared<BookmarksWidget>(QString::fromStdString(m_config->value<std::string>("bookmarks.path").value())); m_downloadManager = std::make_shared<DownloadsWidget>(QString::fromStdString(m_config->value<std::string>("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<std::string>("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<std::string>("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 <QApplication> @@ -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 |