From 6db714e70c1368eeed3542a92f3db2d4161b4de1 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 16 Sep 2017 14:58:12 +0200 Subject: Fixed bug opening URLs from the command line while instance is running --- data/poi.toml | 3 --- mach | 2 +- src/browser.cpp | 31 +++++++++++++------------------ src/browser.h | 2 +- src/main.cpp | 28 ++++++++++++++++++++-------- src/singleapplication.cpp | 44 +++++++++++++++++++++++++++++++------------- src/singleapplication.h | 9 ++++++--- 7 files changed, 72 insertions(+), 47 deletions(-) diff --git a/data/poi.toml b/data/poi.toml index ed6b5b9..4a84692 100644 --- a/data/poi.toml +++ b/data/poi.toml @@ -1,9 +1,6 @@ # # poi.conf # -## -# This file is read-only; smolbote should never be able to change it. -# ## Settings and default settings # There are two parts to the settings - default values and user overrides. # The default settings are read from: diff --git a/mach b/mach index ce94972..17cef25 100755 --- a/mach +++ b/mach @@ -53,7 +53,7 @@ if not ARGV.empty? then system "gpg -u #{options[:email]} -b --armor #{filename}" system "sha512sum --binary #{filename}* > #{filename}.sha512" else - puts "Unknown argument #{a}; use ./mach -h for more details" + puts "Unknown argument #{arg}; use ./mach -h for more details" end end diff --git a/src/browser.cpp b/src/browser.cpp index 60cdb03..897b0ad 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -108,7 +108,16 @@ void Browser::loadProfiles() } qDebug("<< Profiles end..."); - connect(this, SIGNAL(messageAvailable(QStringList)), this, SLOT(addWindow(QStringList))); + //connect(this, SIGNAL(messageAvailable(QStringList)), this, SLOT(addWindow(QStringList))); + connect(this, &Browser::messageAvailable, [this](const QHash params) { + + //qDebug("Creating new window for [%s]", qUtf8Printable(params["urls"].toString())); + + MainWindow *w = addWindow(params["urls"].toStringList()); + if(params.contains("profile")) { + w->setProfile(profile(params["profile"].toString())); + } + }); } Browser *Browser::instance() @@ -221,25 +230,11 @@ void Browser::addWindow(MainWindow *window) window->show(); } -void Browser::addWindow(const QStringList params) +MainWindow *Browser::addWindow(const QStringList params) { - QString p; // get default profile - QStringList urls; - - for(int i = 0; i < params.length(); i++) { - if(params.at(i) == "-p" || params.at(i) == "--profile") { - i++; - p = params.at(i); - } else if(!params.at(i).startsWith('-') && i > 0) { - qDebug("Appending url: %s", qUtf8Printable(params.at(i))); - urls.append(params.at(i)); - } - } - - MainWindow *w = new MainWindow(urls); - w->setProfile(profile(p)); - + MainWindow *w = new MainWindow(params); addWindow(w); + return w; } void Browser::removeWindow(MainWindow *window) diff --git a/src/browser.h b/src/browser.h index 35ca0df..0edaf0c 100644 --- a/src/browser.h +++ b/src/browser.h @@ -72,7 +72,7 @@ public: public slots: void addWindow(MainWindow* window); - void addWindow(const QStringList params); + MainWindow *addWindow(const QStringList params); void removeWindow(MainWindow* window); private: diff --git a/src/main.cpp b/src/main.cpp index 9dd77fe..36a30da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,7 +25,7 @@ #include /** - * @brief printPoiToml Print the built-in config (if any) + * @brief printPoiToml Print the built-in config * @param output std::ostream to print to */ void printPoiToml(std::ostream &output) @@ -39,7 +39,7 @@ void printPoiToml(std::ostream &output) int main(int argc, char *argv[]) { - // + // Create application object Browser app(argc, argv); app.setApplicationName("smolbote"); #ifdef GIT_VERSION @@ -48,11 +48,7 @@ int main(int argc, char *argv[]) app.setApplicationVersion("1.0.0"); #endif - if(app.isRunning()) { - qDebug("Another instance is running, returning..."); - return 0; - } - + // parse command line options QCommandLineParser parser; parser.setApplicationDescription("yet another Qt browser"); parser.addHelpOption(); @@ -61,7 +57,7 @@ int main(int argc, char *argv[]) QCommandLineOption configOption(QStringList() << "c" << "config", "Set configuration file.", "PATH"); parser.addOption(configOption); - QCommandLineOption defaultConfigOption(QStringList() << "default-config", "Print default configuration."); + QCommandLineOption defaultConfigOption(QStringList() << "print-defaults", "Print default configuration."); parser.addOption(defaultConfigOption); QCommandLineOption profileOption(QStringList() << "p" << "profile", "Use this profile.", "PROFILE"); @@ -83,6 +79,22 @@ int main(int argc, char *argv[]) parser.process(app); + // Check for another instance + // unless specified not to + if(!parser.isSet(newInstanceOption)) { + app.bindLocalSocket(); + if(app.isRunning()) { + qDebug("Another instance is running, returning..."); + QHash params; + if(parser.isSet(profileOption)) { + params.insert("profile", parser.value(profileOption)); + } + params.insert("urls", QVariant(parser.positionalArguments())); + app.sendMessage(params); + return 0; + } + } + if(parser.isSet(defaultConfigOption)) { printPoiToml(std::cout); return 0; diff --git a/src/singleapplication.cpp b/src/singleapplication.cpp index cb08e35..01b2e65 100644 --- a/src/singleapplication.cpp +++ b/src/singleapplication.cpp @@ -21,19 +21,27 @@ #include "singleapplication.h" #include #include +#include SingleApplication::SingleApplication(int &argc, char **argv) : QApplication(argc, argv) { - m_localServer = new QLocalServer(this); - connect(m_localServer, SIGNAL(newConnection()), this, SLOT(receiveMessage())); +} - // if we can't send the arguments to a listening local server, start one - if(!sendMessage(arguments())) { - // remove any unused socket +SingleApplication::~SingleApplication() +{ + if(m_localServer->isListening()) { + m_localServer->close(); QLocalServer::removeServer(LOCALSERVER_KEY); - if(!m_localServer->listen(LOCALSERVER_KEY)) { - qWarning("Cannot bind local server [%s]", qUtf8Printable(LOCALSERVER_KEY)); - } + } +} + +void SingleApplication::bindLocalSocket() +{ + m_localServer = new QLocalServer(this); + connect(m_localServer, SIGNAL(newConnection()), this, SLOT(slot_receiveMessage())); + + if(!m_localServer->listen(LOCALSERVER_KEY)) { + qWarning("Cannot bind local server [%s]", qUtf8Printable(LOCALSERVER_KEY)); } } @@ -43,20 +51,24 @@ bool SingleApplication::isRunning() return !m_localServer->isListening(); } -bool SingleApplication::sendMessage(const QStringList ¶ms) +bool SingleApplication::sendMessage(const QHash ¶ms) { QLocalSocket socket; socket.connectToServer(LOCALSERVER_KEY); if(socket.waitForConnected(LOCALSERVER_TIMEOUT)) { - socket.write(params.join('|').toUtf8()); - socket.waitForBytesWritten(); + QByteArray argumentData; + QDataStream ds(&argumentData, QIODevice::WriteOnly); + ds << params; + + socket.write(argumentData); + socket.waitForBytesWritten(LOCALSERVER_TIMEOUT); return true; } return false; } -void SingleApplication::receiveMessage() +void SingleApplication::slot_receiveMessage() { QLocalSocket *socket = m_localServer->nextPendingConnection(); if(!socket) { @@ -65,7 +77,13 @@ void SingleApplication::receiveMessage() } socket->waitForReadyRead(); - const QStringList params = QString(socket->readAll()).split('|'); + + QByteArray argumentData = socket->readAll(); + + QHash params; + QDataStream ds(argumentData); + ds >> params; + socket->deleteLater(); emit messageAvailable(params); diff --git a/src/singleapplication.h b/src/singleapplication.h index 5d979cd..e14625a 100644 --- a/src/singleapplication.h +++ b/src/singleapplication.h @@ -29,15 +29,18 @@ class SingleApplication : public QApplication Q_OBJECT public: explicit SingleApplication(int &argc, char **argv); + ~SingleApplication(); + + void bindLocalSocket(); bool isRunning(); - bool sendMessage(const QStringList ¶ms); + bool sendMessage(const QHash ¶ms); signals: - void messageAvailable(const QStringList params); + void messageAvailable(const QHash ¶ms); private slots: - void receiveMessage(); + void slot_receiveMessage(); private: const int LOCALSERVER_TIMEOUT = 500; -- cgit v1.2.1