Commit 9d1edbb9 authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

implement blocking send

add KATE_SKIP_DBUS env var to enforce single application usage
this allows for much easier testing
parent 0ed2ff4b
......@@ -33,6 +33,7 @@
#include <QUrl>
#include <QVariant>
#include <qglobal.h>
#include <urlinfo.h>
#include "SingleApplication/SingleApplication"
......@@ -254,8 +255,10 @@ int main(int argc, char **argv)
/**
* use dbus, if available for linux and co.
* allows for reuse of running Kate instances
* we have some env var to forbid this for easier testing of the single application code paths: KATE_SKIP_DBUS
*/
if (QDBusConnectionInterface *const sessionBusInterface = QDBusConnection::sessionBus().interface()) {
if (QDBusConnectionInterface *const sessionBusInterface = QDBusConnection::sessionBus().interface();
sessionBusInterface && qEnvironmentVariableIsEmpty("KATE_SKIP_DBUS")) {
/**
* try to get the current running kate instances
*/
......@@ -579,7 +582,7 @@ int main(int argc, char **argv)
/**
* try to send message, return success
*/
return !app.sendMessage(QJsonDocument::fromVariant(QVariant(message)).toJson());
return !app.sendMessage(QJsonDocument::fromVariant(QVariant(message)).toJson(), 1000, needToBlock);
}
}
......
......@@ -235,9 +235,10 @@ QString SingleApplication::currentUser() const
* Sends message to the Primary Instance.
* @param message The message to send.
* @param timeout the maximum timeout in milliseconds for blocking functions.
* @param block block until the primary application exits
* @return true if the message was sent successfuly, false otherwise.
*/
bool SingleApplication::sendMessage( const QByteArray &message, int timeout )
bool SingleApplication::sendMessage( const QByteArray &message, int timeout, bool block )
{
Q_D( SingleApplication );
......@@ -248,7 +249,7 @@ bool SingleApplication::sendMessage( const QByteArray &message, int timeout )
if( ! d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ) )
return false;
return d->writeConfirmedMessage( timeout, message );
return d->writeConfirmedMessage( timeout, message, block );
}
/**
......
......@@ -127,11 +127,12 @@ public:
/**
* @brief Sends a message to the primary instance. Returns true on success.
* @param {int} timeout - Timeout for connecting
* @param {bool} block - Block until the primary application exits.
* @returns {bool}
* @note sendMessage() will return false if invoked from the primary
* instance.
*/
bool sendMessage( const QByteArray &message, int timeout = 100 );
bool sendMessage( const QByteArray &message, int timeout = 100, bool block = false );
/**
* @brief Get the set user data.
......
......@@ -283,7 +283,7 @@ void SingleApplicationPrivate::writeAck( QLocalSocket *sock ) {
sock->putChar('\n');
}
bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArray &msg)
bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArray &msg, bool block)
{
QElapsedTimer time;
time.start();
......@@ -301,7 +301,13 @@ bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArra
return false;
// Frame 2: The message
return writeConfirmedFrame( static_cast<int>(msecs - time.elapsed()), msg );
const bool result = writeConfirmedFrame( static_cast<int>(msecs - time.elapsed()), msg );
// block if needed
if (socket && block)
socket->waitForDisconnected(-1);
return result;
}
bool SingleApplicationPrivate::writeConfirmedFrame( int msecs, const QByteArray &msg )
......
......@@ -85,7 +85,7 @@ public:
void readInitMessageBody(QLocalSocket *socket);
void writeAck(QLocalSocket *sock);
bool writeConfirmedFrame(int msecs, const QByteArray &msg);
bool writeConfirmedMessage(int msecs, const QByteArray &msg);
bool writeConfirmedMessage(int msecs, const QByteArray &msg, bool block = false);
static void randomSleep();
void addAppData(const QString &data);
QStringList appData() const;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment