QtPass 1.4.0
Multi-platform GUI for pass, the standard unix password manager.
Loading...
Searching...
No Matches
singleapplication.cpp
Go to the documentation of this file.
1#include "singleapplication.h"
2#include <QLocalSocket>
3#include <utility>
4#ifdef QT_DEBUG
5#include "debughelper.h"
6#endif
7
15SingleApplication::SingleApplication(int &argc, char *argv[], QString uniqueKey)
16 : QApplication(argc, argv), _uniqueKey(std::move(uniqueKey)) {
17 sharedMemory.setKey(_uniqueKey);
18 if (sharedMemory.attach()) {
19 _isRunning = true;
20 } else {
21 _isRunning = false;
22 // create shared memory.
23 if (!sharedMemory.create(1)) {
24#ifdef QT_DEBUG
25 dbg() << "Unable to create single instance.";
26#endif
27 return;
28 }
29 // create local server and listen to incomming messages from other
30 // instances.
31 localServer.reset(new QLocalServer(this));
32 connect(localServer.data(), &QLocalServer::newConnection, this,
34 localServer->listen(_uniqueKey);
35 }
36}
37
38// public slots.
39
45 QLocalSocket *localSocket = localServer->nextPendingConnection();
46 if (!localSocket->waitForReadyRead(timeout)) {
47#ifdef QT_DEBUG
48 dbg() << localSocket->errorString().toLatin1();
49#endif
50 return;
51 }
52 QByteArray byteArray = localSocket->readAll();
53 QString message = QString::fromUtf8(byteArray.constData());
54 emit messageAvailable(message);
55 localSocket->disconnectFromServer();
56}
57
58// public functions.
64bool SingleApplication::isRunning() { return _isRunning; }
65
72bool SingleApplication::sendMessage(const QString &message) {
73 if (!_isRunning)
74 return false;
75 QLocalSocket localSocket(this);
76 localSocket.connectToServer(_uniqueKey, QIODevice::WriteOnly);
77 if (!localSocket.waitForConnected(timeout)) {
78#ifdef QT_DEBUG
79 dbg() << localSocket.errorString().toLatin1();
80#endif
81 return false;
82 }
83 localSocket.write(message.toUtf8());
84 if (!localSocket.waitForBytesWritten(timeout)) {
85#ifdef QT_DEBUG
86 dbg() << localSocket.errorString().toLatin1();
87#endif
88 return false;
89 }
90 localSocket.disconnectFromServer();
91 return true;
92}
bool isRunning()
SingleApplication::isRunning is there already a QtPass instance running, to check wether to be server...
void receiveMessage()
SingleApplication::receiveMessage we have received (a command line) message.
SingleApplication(int &argc, char *argv[], QString uniqueKey)
SingleApplication::SingleApplication this replaces the QApplication allowing for local socket based c...
bool sendMessage(const QString &message)
SingleApplication::sendMessage send a message (from commandline) to an already running QtPass instanc...
void messageAvailable(QString message)
messageAvailable notification from commandline
#define dbg()
Definition: debughelper.h:7