Changeset View
Changeset View
Standalone View
Standalone View
plugins/clipboard/clipboardplugin.cpp
Show All 29 Lines | |||||
30 | 30 | | |||
31 | ClipboardPlugin::ClipboardPlugin(QObject* parent, const QVariantList& args) | 31 | ClipboardPlugin::ClipboardPlugin(QObject* parent, const QVariantList& args) | ||
32 | : KdeConnectPlugin(parent, args) | 32 | : KdeConnectPlugin(parent, args) | ||
33 | { | 33 | { | ||
34 | connect(ClipboardListener::instance(), &ClipboardListener::clipboardChanged, | 34 | connect(ClipboardListener::instance(), &ClipboardListener::clipboardChanged, | ||
35 | this, &ClipboardPlugin::propagateClipboard); | 35 | this, &ClipboardPlugin::propagateClipboard); | ||
36 | } | 36 | } | ||
37 | 37 | | |||
38 | void ClipboardPlugin::connected() | ||||
39 | { | ||||
40 | sendConnectPacket(); | ||||
albertvaka: As discussed, use a new packet type so this doesn't affect old apps. Also on Android. | |||||
41 | } | ||||
42 | | ||||
38 | void ClipboardPlugin::propagateClipboard(const QString& content) | 43 | void ClipboardPlugin::propagateClipboard(const QString& content) | ||
39 | { | 44 | { | ||
40 | NetworkPacket np(PACKET_TYPE_CLIPBOARD, {{QStringLiteral("content"), content}}); | 45 | NetworkPacket np(PACKET_TYPE_CLIPBOARD, {{QStringLiteral("content"), content}}); | ||
41 | sendPacket(np); | 46 | sendPacket(np); | ||
42 | } | 47 | } | ||
43 | 48 | | |||
49 | void ClipboardPlugin::sendConnectPacket() | ||||
50 | { | ||||
51 | NetworkPacket np(PACKET_TYPE_CLIPBOARD_CONNECT, { | ||||
52 | {QStringLiteral("content"), ClipboardListener::instance()->currentContent()}, | ||||
53 | {QStringLiteral("timestamp"), ClipboardListener::instance()->updateTimestamp()} | ||||
54 | }); | ||||
55 | sendPacket(np); | ||||
56 | } | ||||
57 | | ||||
58 | | ||||
44 | bool ClipboardPlugin::receivePacket(const NetworkPacket& np) | 59 | bool ClipboardPlugin::receivePacket(const NetworkPacket& np) | ||
45 | { | 60 | { | ||
46 | QString content = np.get<QString>(QStringLiteral("content")); | 61 | QString content = np.get<QString>(QStringLiteral("content")); | ||
sredman: Space before `{` | |||||
62 | if (np.type() == PACKET_TYPE_CLIPBOARD) { | ||||
63 | ClipboardListener::instance()->setText(content); | ||||
64 | return true; | ||||
sredman: `} else {` (Spaces) | |||||
65 | } else if (np.type() == PACKET_TYPE_CLIPBOARD_CONNECT) { | ||||
albertvaka: This can be simplified with the other if branch. | |||||
66 | qint64 packetTime = np.get<qint64>(QStringLiteral("timestamp")); | ||||
67 | // If the packetTime is 0, it means the timestamp is unknown (so do nothing). | ||||
68 | if (packetTime == 0 || packetTime < ClipboardListener::instance()->updateTimestamp()) { | ||||
69 | return false; | ||||
70 | } | ||||
71 | | ||||
47 | ClipboardListener::instance()->setText(content); | 72 | ClipboardListener::instance()->setText(content); | ||
48 | return true; | 73 | return true; | ||
49 | } | 74 | } | ||
75 | return false; | ||||
76 | } | ||||
50 | 77 | | |||
51 | #include "clipboardplugin.moc" | 78 | #include "clipboardplugin.moc" |
As discussed, use a new packet type so this doesn't affect old apps. Also on Android.