app,core: Update LocalServer to work on Windows

Fixes #681 (in conjunction with Qt 5.6+, see QTBUG-23378)
This commit is contained in:
Oleg Shparber
2017-02-12 23:32:01 -05:00
parent 59109639ff
commit 4711e0cf3c
2 changed files with 30 additions and 24 deletions
+7 -2
View File
@@ -32,6 +32,7 @@
#include <QIcon>
#include <QMessageBox>
#include <QTextStream>
#include <QTimer>
#include <QUrlQuery>
#ifdef Q_OS_WIN32
@@ -104,6 +105,7 @@ CommandLineParameters parseCommandLine(const QStringList &arguments)
// TODO: Support dash-feed:// protocol
const QString arg
= QUrl::fromPercentEncoding(parser.positionalArguments().value(0).toUtf8());
if (arg.startsWith(QLatin1String("dash:"))) {
clParams.query.setQuery(stripParameterUrl(arg, QStringLiteral("dash")));
} else if (arg.startsWith(QLatin1String("dash-plugin:"))) {
@@ -248,8 +250,11 @@ int main(int argc, char *argv[])
QObject::connect(localServer.data(), &Core::LocalServer::newQuery,
app.data(), &Core::Application::executeQuery);
if (!clParams.query.isEmpty())
Core::LocalServer::sendQuery(clParams.query, clParams.preventActivation);
if (!clParams.query.isEmpty()) {
QTimer::singleShot(0, [clParams] {
Core::LocalServer::sendQuery(clParams.query, clParams.preventActivation);
});
}
return qapp->exec();
}
+23 -22
View File
@@ -27,7 +27,6 @@
#include <QDataStream>
#include <QLocalServer>
#include <QLocalSocket>
#include <QScopedPointer>
using namespace Zeal;
using namespace Zeal::Core;
@@ -40,19 +39,19 @@ LocalServer::LocalServer(QObject *parent)
: QObject(parent)
, m_localServer(new QLocalServer(this))
{
connect(m_localServer, &QLocalServer::newConnection, [this]() {
QScopedPointer<QLocalSocket> connection(m_localServer->nextPendingConnection());
// Wait while the other side writes the data.
if (!connection->waitForReadyRead(500))
return;
connect(m_localServer, &QLocalServer::newConnection, [this] {
QLocalSocket *socket = m_localServer->nextPendingConnection();
connect(socket, &QLocalSocket::readyRead, [this, socket] {
Registry::SearchQuery query;
bool preventActivation;
QDataStream in(connection.data());
Registry::SearchQuery query;
bool preventActivation;
in >> query;
in >> preventActivation;
QDataStream in(socket);
in >> query;
in >> preventActivation;
emit newQuery(query, preventActivation);
emit newQuery(query, preventActivation);
socket->deleteLater();
});
});
}
@@ -94,15 +93,17 @@ bool LocalServer::start(bool force)
*/
bool LocalServer::sendQuery(const Registry::SearchQuery &query, bool preventActivation)
{
QScopedPointer<QLocalSocket> socket(new QLocalSocket());
QByteArray ba;
QDataStream out(&ba, QIODevice::WriteOnly);
out << query << preventActivation;
QLocalSocket *socket = new QLocalSocket();
connect(socket, &QLocalSocket::connected, [socket, ba] {
socket->write(ba);
socket->flush(); // Required for Linux
socket->deleteLater();
});
socket->connectToServer(LocalServerName);
if (!socket->waitForConnected(500))
return false;
QDataStream out(socket.data());
out << query;
out << preventActivation;
socket->flush();
return true;
return socket->waitForConnected(500);
}