diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-09-16 14:58:12 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-09-16 14:58:12 +0200 |
commit | 6db714e70c1368eeed3542a92f3db2d4161b4de1 (patch) | |
tree | 2d5f7dc9ad09eb38664202ac48cf912d55db2599 /src | |
parent | Removed Settings::contains (diff) | |
download | smolbote-6db714e70c1368eeed3542a92f3db2d4161b4de1.tar.xz |
Fixed bug opening URLs from the command line while instance is running
Diffstat (limited to 'src')
-rw-r--r-- | src/browser.cpp | 31 | ||||
-rw-r--r-- | src/browser.h | 2 | ||||
-rw-r--r-- | src/main.cpp | 28 | ||||
-rw-r--r-- | src/singleapplication.cpp | 44 | ||||
-rw-r--r-- | src/singleapplication.h | 9 |
5 files changed, 71 insertions, 43 deletions
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<QString, QVariant> 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 <QCommandLineParser> /** - * @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<QString, QVariant> 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 <QLocalServer> #include <QLocalSocket> +#include <QDataStream> 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<QString, QVariant> ¶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<QString, QVariant> 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<QString, QVariant> ¶ms); signals: - void messageAvailable(const QStringList params); + void messageAvailable(const QHash<QString, QVariant> ¶ms); private slots: - void receiveMessage(); + void slot_receiveMessage(); private: const int LOCALSERVER_TIMEOUT = 500; |