Skip to content

Commit ea94c53

Browse files
authored
Windows cli copy (fix for flameshot-org#4118) (flameshot-org#4211)
* Use KDSingleApplication instead of D-Bus on Windows * Fix MacOS build * Fix MacOS build * Fix MacOS build * Fix MacOS build
1 parent 357b967 commit ea94c53

File tree

7 files changed

+163
-25
lines changed

7 files changed

+163
-25
lines changed

src/CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ find_package(
77
Widgets
88
Network
99
Svg
10-
DBus
1110
LinguistTools
1211
)
1312

13+
if (UNIX AND NOT APPLE)
14+
find_package(
15+
Qt${QT_VERSION_MAJOR}
16+
CONFIG
17+
REQUIRED
18+
DBus
19+
)
20+
endif()
21+
1422
if (USE_WAYLAND_CLIPBOARD)
1523
find_package(KF6GuiAddons)
1624
endif()
@@ -208,11 +216,16 @@ target_link_libraries(
208216
project_warnings
209217
project_options
210218
Qt${QT_VERSION_MAJOR}::Svg
211-
Qt${QT_VERSION_MAJOR}::DBus
212219
Qt${QT_VERSION_MAJOR}::Network
213220
Qt${QT_VERSION_MAJOR}::Widgets
214221
QtColorWidgets
215222
)
223+
if (UNIX AND NOT APPLE)
224+
target_link_libraries(
225+
flameshot
226+
Qt${QT_VERSION_MAJOR}::DBus
227+
)
228+
endif()
216229

217230
if (USE_KDSINGLEAPPLICATION)
218231
message(STATUS "KDSingleApplication is used!")

src/core/CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
target_sources(flameshot PRIVATE
22
flameshot.h
33
flameshotdaemon.h
4-
flameshotdbusadapter.h
54
qguiappcurrentscreen.h
65
)
76

87
target_sources(flameshot PRIVATE
98
capturerequest.cpp
109
flameshot.cpp
1110
flameshotdaemon.cpp
12-
flameshotdbusadapter.cpp
1311
qguiappcurrentscreen.cpp
1412
)
1513

14+
if (UNIX AND NOT APPLE)
15+
target_sources(flameshot PRIVATE
16+
flameshotdbusadapter.h
17+
flameshotdbusadapter.cpp
18+
)
19+
endif ()
20+
1621
if (USE_KDSINGLEAPPLICATION)
1722
if (NOT WIN32)
1823
target_sources(flameshot PRIVATE
@@ -24,4 +29,4 @@ endif ()
2429

2530
if (WIN32)
2631
target_sources(flameshot PRIVATE globalshortcutfilter.h globalshortcutfilter.cpp)
27-
endif ()
32+
endif ()

src/core/flameshotdaemon.cpp

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
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>
@@ -26,6 +29,12 @@
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

126142
void 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

144167
void 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

287319
void 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))
399432
QDBusMessage 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
425516
FlameshotDaemon* FlameshotDaemon::m_instance = nullptr;

src/core/flameshotdaemon.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
#include <QByteArray>
44
#include <QObject>
5+
6+
#if !(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
57
#include <QtDBus/QDBusAbstractAdaptor>
8+
#endif
69

710
class QPixmap;
811
class QRect;
@@ -34,7 +37,14 @@ class FlameshotDaemon : public QObject
3437
const QString& title = QStringLiteral("Flameshot Info"),
3538
const int timeout = 5000);
3639

40+
#if defined(USE_KDSINGLEAPPLICATION) && \
41+
(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
42+
public slots:
43+
void messageReceivedFromSecondaryInstance(const QByteArray& message);
44+
#endif
45+
3746
#if !defined(DISABLE_UPDATE_CHECKER)
47+
public:
3848
void showUpdateNotificationIfAvailable(CaptureWidget* widget);
3949

4050
public slots:
@@ -62,10 +72,11 @@ private slots:
6272
void initTrayIcon();
6373
void enableTrayIcon(bool enable);
6474

65-
private:
75+
#if !(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
6676
static QDBusMessage createMethodCall(const QString& method);
6777
static void checkDBusConnection(const QDBusConnection& connection);
6878
static void call(const QDBusMessage& m);
79+
#endif
6980

7081
bool m_persist;
7182
bool m_hostingClipboard;
@@ -82,5 +93,7 @@ private slots:
8293

8394
static FlameshotDaemon* m_instance;
8495

96+
#if !(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
8597
friend class FlameshotDBusAdapter;
98+
#endif
8699
};

src/main.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626
#include <QSharedMemory>
2727
#include <QTimer>
2828
#include <QTranslator>
29-
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
30-
#include "abstractlogger.h"
29+
#if !(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
3130
#include "src/core/flameshotdbusadapter.h"
32-
#include <QApplication>
3331
#include <QDBusConnection>
3432
#include <QDBusMessage>
3533
#include <desktopinfo.h>
@@ -191,7 +189,8 @@ int main(int argc, char* argv[])
191189
setup_unix_signal_handlers();
192190
auto signalDaemon = SignalDaemon();
193191
#endif
194-
auto kdsa = KDSingleApplication(QStringLiteral("flameshot"));
192+
auto kdsa =
193+
KDSingleApplication(QStringLiteral("org.flameshot.Flameshot"));
195194

196195
if (!kdsa.isPrimaryInstance()) {
197196
return 0; // Quit
@@ -202,6 +201,17 @@ int main(int argc, char* argv[])
202201
auto c = Flameshot::instance();
203202
FlameshotDaemon::start();
204203

204+
#if defined(USE_KDSINGLEAPPLICATION) && \
205+
(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
206+
if (kdsa.isPrimaryInstance()) {
207+
QObject::connect(
208+
&kdsa,
209+
&KDSingleApplication::messageReceived,
210+
FlameshotDaemon::instance(),
211+
&FlameshotDaemon::messageReceivedFromSecondaryInstance);
212+
}
213+
#endif
214+
205215
#if !(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
206216
new FlameshotDBusAdapter(c);
207217
QDBusConnection dbus = QDBusConnection::sessionBus();

src/utils/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ target_sources(
66
screengrabber.h
77
systemnotification.h
88
valuehandler.h
9-
request.h
109
strfparse.h
1110
)
1211

@@ -26,8 +25,15 @@ target_sources(
2625
colorutils.cpp
2726
history.cpp
2827
strfparse.cpp
28+
)
29+
30+
IF (UNIX AND NOT APPLE)
31+
target_sources(
32+
flameshot
33+
PRIVATE request.h
2934
request.cpp
3035
)
36+
ENDIF()
3137

3238
IF (WIN32)
3339
target_sources(

0 commit comments

Comments
 (0)