| 1 | // For license of this file, see <project-root-folder>/LICENSE.md.cxx . . $ `<file>/src/librssguard/miscellaneous/singleapplication.cpp`/ |
| 2 | |
| 3 | #include "miscellaneous/singleapplication.h"cxx . . $ `<file>/src/librssguard/miscellaneous/singleapplication.h`/ |
| 4 | |
| 5 | #include <QDataStream> |
| 6 | #include <QLocalServer> |
| 7 | #include <QLocalSocket> |
| 8 | |
| 9 | SingleApplicationcxx . . $ SingleApplication#::SingleApplicationcxx . . $ SingleApplication#SingleApplication(9820833414d10e6f).(const QStringcxx . . $ QString#& id, int& argc, char** argv) |
| 10 | : QApplicationcxx . . $ QApplication#QApplication(6e9b1c7df49e191d).(argc, argv), m_idcxx . . $ SingleApplication#m_id.(id), m_servercxx . . $ SingleApplication#m_server.(new QLocalServercxx . . $ QLocalServer#QLocalServer(749de1b6ae65bddd).(this)) {} |
| 11 | |
| 12 | SingleApplicationcxx . . $ SingleApplication#::~cxx . . $ SingleApplication#`~SingleApplication`(ced63f7c635d850d).SingleApplicationcxx . . $ SingleApplication#() { |
| 13 | finishcxx . . $ SingleApplication#finish(49f6e7a06ebc5aa8).(); |
| 14 | } |
| 15 | |
| 16 | void SingleApplicationcxx . . $ SingleApplication#::finishcxx . . $ SingleApplication#finish(49f6e7a06ebc5aa8).() { |
| 17 | if (m_servercxx . . $ SingleApplication#m_server.->isListeningcxx . . $ QLocalServer#isListening(50ce9a9e25b4a850).()) { |
| 18 | m_servercxx . . $ SingleApplication#m_server.->closecxx . . $ QLocalServer#close(49f6e7a06ebc5aa8).(); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | bool SingleApplicationcxx . . $ SingleApplication#::isOtherInstanceRunningcxx . . $ SingleApplication#isOtherInstanceRunning(e1defb592416720d).(const QStringcxx . . $ QString#& message) { |
| 23 | QLocalSocketcxx . . $ QLocalSocket# sckcxx . . $ QLocalSocket#QLocalSocket(749de1b6ae65bddd).; |
| 24 | |
| 25 | sck.connectToServercxx . . $ QLocalSocket#connectToServer(edf9378e65f09e41).(m_idcxx . . $ SingleApplication#m_id.); |
| 26 | |
| 27 | if (sck.waitForConnectedcxx . . $ QLocalSocket#waitForConnected(83c267b20bac841a).(200)) { |
| 28 | if (!message.isEmptycxx . . $ QString#isEmpty(3482b152b9333168).()) { |
| 29 | QDataStreamcxx . . $ QDataStream# outcxx . . $ QDataStream#QDataStream(bebe8f2dee7f9e86).(&sck); |
| 30 | |
| 31 | out.setVersioncxx . . $ QDataStream#setVersion(d4f767463ce0a6b3).(QDataStreamcxx . . $ QDataStream#::Versioncxx . . $ QDataStream#Version#::Qt_5_5cxx . . $ QDataStream#Qt_5_5.); |
| 32 | |
| 33 | out <<cxx . . $ QDataStream#`operator<<`(1483334db06c9178). quint32cxx . . $ quint32#(message.sizecxx . . $ QString#size(6b0412d51016192f).()); |
| 34 | out <<cxx . . $ `operator<<`(840080fcc32a7f1c). message; |
| 35 | |
| 36 | sck.flushcxx . . $ QLocalSocket#flush(c9857a73d68e59ba).(); |
| 37 | sck.waitForBytesWrittencxx . . $ QLocalSocket#waitForBytesWritten(83c267b20bac841a).(200); |
| 38 | } |
| 39 | |
| 40 | sck.disconnectFromServercxx . . $ QLocalSocket#disconnectFromServer(49f6e7a06ebc5aa8).(); |
| 41 | sck.abortcxx . . $ QLocalSocket#abort(49f6e7a06ebc5aa8).(); |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | QLocalServercxx . . $ QLocalServer#::removeServercxx . . $ QLocalServer#removeServer(e1defb592416720d).(m_idcxx . . $ SingleApplication#m_id.); |
| 46 | auto i_am_first = m_servercxx . . $ SingleApplication#m_server.->listencxx . . $ QLocalServer#listen(e1defb592416720d).(m_idcxx . . $ SingleApplication#m_id.); |
| 47 | |
| 48 | if (i_am_first) { |
| 49 | connectcxx . . $ QObject#connect(dad3a9587f3dce3c).(m_servercxx . . $ SingleApplication#m_server., &QLocalServercxx . . $ QLocalServer#::newConnectioncxx . . $ QLocalServer#newConnection(49f6e7a06ebc5aa8)., this, &SingleApplicationcxx . . $ SingleApplication#::processMessageFromOtherInstancecxx . . $ SingleApplication#processMessageFromOtherInstance(49f6e7a06ebc5aa8).); |
| 50 | } |
| 51 | |
| 52 | return !i_am_first; |
| 53 | } |
| 54 | |
| 55 | bool SingleApplicationcxx . . $ SingleApplication#::sendMessagecxx . . $ SingleApplication#sendMessage(e1defb592416720d).(const QStringcxx . . $ QString#& message) { |
| 56 | return isOtherInstanceRunningcxx . . $ SingleApplication#isOtherInstanceRunning(e1defb592416720d).(message); |
| 57 | } |
| 58 | |
| 59 | void SingleApplicationcxx . . $ SingleApplication#::processMessageFromOtherInstancecxx . . $ SingleApplication#processMessageFromOtherInstance(49f6e7a06ebc5aa8).() { |
| 60 | auto* sck = m_servercxx . . $ SingleApplication#m_server.->nextPendingConnectioncxx . . $ QLocalServer#nextPendingConnection(b3b2ebecfc56d9aa).(); |
| 61 | |
| 62 | if (sck == nullptr) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | quint32cxx . . $ quint32# block_size = 0; |
| 67 | QEventLoopcxx . . $ QEventLoop# loopcxx . . $ QEventLoop#QEventLoop(749de1b6ae65bddd).; |
| 68 | QDataStreamcxx . . $ QDataStream# incxx . . $ QDataStream#QDataStream(bebe8f2dee7f9e86).(sck); |
| 69 | |
| 70 | in.setVersioncxx . . $ QDataStream#setVersion(d4f767463ce0a6b3).(QDataStreamcxx . . $ QDataStream#::Versioncxx . . $ QDataStream#Version#::Qt_5_5cxx . . $ QDataStream#Qt_5_5.); |
| 71 | |
| 72 | connectcxx . . $ QObject#connect(dad3a9587f3dce3c).(sck, &QLocalSocketcxx . . $ QLocalSocket#::disconnectedcxx . . $ QLocalSocket#disconnected(49f6e7a06ebc5aa8)., sck, &QLocalSocketcxx . . $ QLocalSocket#::deleteLatercxx . . $ QObject#deleteLater(49f6e7a06ebc5aa8).); |
| 73 | connectcxx . . $ QObject#connect(dad3a9587f3dce3c).(sck, &QLocalSocketcxx . . $ QLocalSocket#::readyReadcxx . . $ QIODevice#readyRead(49f6e7a06ebc5aa8)., this, [this, sck, &loop, &in, &block_size]() { |
| 74 | if (block_size == 0) { |
| 75 | if (sck->bytesAvailablecxx . . $ QLocalSocket#bytesAvailable(98833e9ab7de59a4).() < int(sizeof(quint32cxx . . $ quint32#))) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | in >>cxx . . $ QDataStream#`operator>>`(a72c8f61547c6abb). block_size; |
| 80 | } |
| 81 | |
| 82 | if (sck->bytesAvailablecxx . . $ QLocalSocket#bytesAvailable(98833e9ab7de59a4).() < block_size || in.atEndcxx . . $ QDataStream#atEnd(50ce9a9e25b4a850).()) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | QStringcxx . . $ QString# datcxx . . $ QString#QString(ced63f7c635d850d).; |
| 87 | in >>cxx . . $ `operator>>`(66d14281a2151d). dat; |
| 88 | |
| 89 | emitcxx . . $ `<external>/4830994369567095248/qtmetamacros.h:51:10`! messageReceivedcxx . . $ SingleApplication#messageReceived(3520e46b33348bf6).(dat); |
| 90 | loop.exitcxx . . $ QEventLoop#exit(d4f767463ce0a6b3).(); |
| 91 | }); |
| 92 | |
| 93 | sck->flushcxx . . $ QLocalSocket#flush(c9857a73d68e59ba).(); |
| 94 | loop.execcxx . . $ QEventLoop#exec(f981dedee5b32481).(); |
| 95 | } |