1010#include " src/widgets/trayicon.h"
1111#include < QApplication>
1212#include < QClipboard>
13- #include < QDBusConnection>
14- #include < QDBusMessage>
1513#include < QIODevice>
1614#include < QPixmap>
1715#include < QRect>
1816
17+ #if !(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
18+ #include < QDBusConnection>
19+ #include < QDBusMessage>
20+ #endif
21+
1922#if !defined(DISABLE_UPDATE_CHECKER)
2023#include < QDesktopServices>
2124#include < QJsonDocument>
2629#include < QUrl>
2730#endif
2831
32+ #if defined(USE_KDSINGLEAPPLICATION) && \
33+ (defined (Q_OS_MACOS) || defined (Q_OS_WIN))
34+ #include < QBuffer>
35+ #include < kdsingleapplication.h>
36+ #endif
37+
2938#ifdef Q_OS_WIN
3039#include " src/core/globalshortcutfilter.h"
3140#endif
@@ -62,9 +71,9 @@ FlameshotDaemon::FlameshotDaemon()
6271 , m_clipboardSignalBlocked(false )
6372 , m_trayIcon(nullptr )
6473#if !defined(DISABLE_UPDATE_CHECKER)
65- , m_networkCheckUpdates(nullptr )
66- , m_showCheckAppUpdateStatus(false )
6774 , m_appLatestVersion(QStringLiteral(APP_VERSION).replace(" v" , " " ))
75+ , m_showCheckAppUpdateStatus(false )
76+ , m_networkCheckUpdates(nullptr )
6877#endif
6978{
7079 connect (
@@ -116,11 +125,18 @@ void FlameshotDaemon::createPin(const QPixmap& capture, QRect geometry)
116125
117126 QByteArray data;
118127 QDataStream stream (&data, QIODevice::WriteOnly);
119- stream << capture;
120- stream << geometry;
128+
129+ #if defined(USE_KDSINGLEAPPLICATION) && \
130+ (defined (Q_OS_MACOS) || defined (Q_OS_WIN))
131+ auto kdsa = KDSingleApplication (QStringLiteral (" org.flameshot.Flameshot" ));
132+ stream << QStringLiteral (" attachPin" ) << capture << geometry;
133+ kdsa.sendMessage (data);
134+ #else
135+ stream << capture << geometry;
121136 QDBusMessage m = createMethodCall (QStringLiteral (" attachPin" ));
122137 m << data;
123138 call (m);
139+ #endif
124140}
125141
126142void FlameshotDaemon::copyToClipboard (const QPixmap& capture)
@@ -130,15 +146,22 @@ void FlameshotDaemon::copyToClipboard(const QPixmap& capture)
130146 return ;
131147 }
132148
133- QDBusMessage m =
134- createMethodCall (QStringLiteral (" attachScreenshotToClipboard" ));
135-
136149 QByteArray data;
137150 QDataStream stream (&data, QIODevice::WriteOnly);
151+
152+ #if defined(USE_KDSINGLEAPPLICATION) && \
153+ (defined (Q_OS_MACOS) || defined (Q_OS_WIN))
154+ auto kdsa = KDSingleApplication (QStringLiteral (" org.flameshot.Flameshot" ));
155+ stream << QStringLiteral (" attachScreenshotToClipboard" ) << capture;
156+ kdsa.sendMessage (data);
157+ #else
138158 stream << capture;
159+ QDBusMessage m =
160+ createMethodCall (QStringLiteral (" attachScreenshotToClipboard" ));
139161
140162 m << data;
141163 call (m);
164+ #endif
142165}
143166
144167void FlameshotDaemon::copyToClipboard (const QString& text,
@@ -148,13 +171,22 @@ void FlameshotDaemon::copyToClipboard(const QString& text,
148171 instance ()->attachTextToClipboard (text, notification);
149172 return ;
150173 }
151- auto m = createMethodCall (QStringLiteral (" attachTextToClipboard" ));
152174
175+ #if defined(USE_KDSINGLEAPPLICATION) && \
176+ (defined (Q_OS_MACOS) || defined (Q_OS_WIN))
177+ auto kdsa = KDSingleApplication (QStringLiteral (" org.flameshot.Flameshot" ));
178+ QByteArray data;
179+ QDataStream stream (&data, QIODevice::WriteOnly);
180+ stream << QStringLiteral (" attachTextToClipboard" ) << text << notification;
181+ kdsa.sendMessage (data);
182+ #else
183+ auto m = createMethodCall (QStringLiteral (" attachTextToClipboard" ));
153184 m << text << notification;
154185
155186 QDBusConnection sessionBus = QDBusConnection::sessionBus ();
156187 checkDBusConnection (sessionBus);
157188 sessionBus.call (m);
189+ #endif
158190}
159191
160192/* *
@@ -282,7 +314,7 @@ void FlameshotDaemon::attachScreenshotToClipboard(const QPixmap& pixmap)
282314 clipboard->blockSignals (false );
283315}
284316
285- // D-BUS ADAPTER METHODS
317+ // D-BUS / KDSingleApplication METHODS
286318
287319void FlameshotDaemon::attachPin (const QByteArray& data)
288320{
@@ -396,6 +428,7 @@ void FlameshotDaemon::handleReplyCheckUpdates(QNetworkReply* reply)
396428}
397429#endif
398430
431+ #if !(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
399432QDBusMessage FlameshotDaemon::createMethodCall (const QString& method)
400433{
401434 QDBusMessage m =
@@ -420,6 +453,64 @@ void FlameshotDaemon::call(const QDBusMessage& m)
420453 checkDBusConnection (sessionBus);
421454 sessionBus.call (m);
422455}
456+ #endif
457+
458+ #if defined(USE_KDSINGLEAPPLICATION) && \
459+ (defined (Q_OS_MACOS) || defined (Q_OS_WIN))
460+ void FlameshotDaemon::messageReceivedFromSecondaryInstance (
461+ const QByteArray& message)
462+ {
463+ // qDebug() << "Received message from second instance:" << message;
464+
465+ QByteArray messageCopy = message;
466+ QBuffer buffer (&messageCopy);
467+ buffer.open (QIODevice::ReadOnly);
468+ QDataStream stream (&buffer);
469+ QString methodCall;
470+ stream >> methodCall;
471+ // qDebug() << "Method:" << methodCall;
472+
473+ if (methodCall == QStringLiteral (" attachPin" )) {
474+ QPixmap capture;
475+ QRect geometry;
476+ stream >> capture >> geometry;
477+ // qDebug() << "Pixmap:" << capture;
478+ // qDebug() << "Geometry:" << geometry;
479+ if (!capture.isNull ()) {
480+ FlameshotDaemon::instance ()->attachPin (capture, geometry);
481+ } else {
482+ qWarning () << " Received \" attachPin\" from second instance, but "
483+ " pixmap is empty!" ;
484+ }
485+ } else if (methodCall == QStringLiteral (" attachScreenshotToClipboard" )) {
486+ QPixmap capture;
487+ stream >> capture;
488+ // qDebug() << "Pixmap:" << capture;
489+ if (!capture.isNull ()) {
490+ FlameshotDaemon::instance ()->attachScreenshotToClipboard (capture);
491+ } else {
492+ qWarning () << " Received \" attachScreenshotToClipboard\" from "
493+ " second instance, but pixmap is empty!" ;
494+ }
495+ } else if (methodCall == (QStringLiteral (" attachTextToClipboard" ))) {
496+ QString text;
497+ QString notification;
498+ stream >> text >> notification;
499+ // qDebug() << "Text:" << text;
500+ // qDebug() << "Notification:" << notification;
501+ if (!text.isEmpty ()) {
502+ FlameshotDaemon::instance ()->attachTextToClipboard (text,
503+ notification);
504+ } else {
505+ qWarning () << " Received \" attachTextToClipboard\" from second "
506+ " instance, but text is empty!" ;
507+ }
508+ } else {
509+ qWarning () << " Received unknown message from second instance:"
510+ << message;
511+ }
512+ }
513+ #endif
423514
424515// STATIC ATTRIBUTES
425516FlameshotDaemon* FlameshotDaemon::m_instance = nullptr ;
0 commit comments