Skip to content

Commit 2df8115

Browse files
committed
refactor: update clipboard handling to Gtk4 API and rename SMS message handler to handleDigest
1 parent 1395c56 commit 2df8115

5 files changed

Lines changed: 76 additions & 62 deletions

File tree

installed-tests/suites/components/testClipboardComponent.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,60 @@ describe('The Clipboard component', function () {
2929
clipboard.destroy();
3030
});
3131

32-
it('pulls changes from the session clipboard', function (done) {
32+
it('pulls changes from the session clipboard', async function () {
3333
const text = GLib.uuid_string_random();
3434

35-
const id = clipboard.connect('notify::text', (clipboard) => {
36-
clipboard.disconnect(id);
37-
38-
expect(clipboard.text).toBe(text);
39-
done();
35+
const promise = new Promise((resolve) => {
36+
const id = clipboard.connect('notify::text', (clipboard) => {
37+
if (clipboard.text === text) {
38+
clipboard.disconnect(id);
39+
resolve();
40+
}
41+
});
4042
});
4143

42-
const provider = Gdk.ContentProvider.new_for_value(new GLib.Variant('s', text));
44+
const provider = Gdk.ContentProvider.new_for_value(text);
4345
gtkClipboard.set_content(provider);
46+
47+
await promise;
48+
expect(clipboard.text).toBe(text);
4449
});
4550

46-
it('pushes changes to the session clipboard', function (done) {
51+
it('pushes changes to the session clipboard', async function () {
4752
const text = GLib.uuid_string_random();
4853

49-
const id = gtkClipboard.connect('changed', async (gtkClipboard) => {
50-
gtkClipboard.disconnect(id);
51-
52-
const value = await gtkClipboard.read_text_async(null);
53-
expect(value).toBe(text);
54-
done();
54+
const promise = new Promise((resolve) => {
55+
const id = gtkClipboard.connect('changed', async (gtkClipboard) => {
56+
const value = await new Promise((resolve, reject) => {
57+
gtkClipboard.read_text_async(null, (source, res) => {
58+
try {
59+
resolve(source.read_text_finish(res));
60+
} catch (e) {
61+
reject(e);
62+
}
63+
});
64+
});
65+
66+
if (value === text) {
67+
gtkClipboard.disconnect(id);
68+
resolve();
69+
}
70+
});
5571
});
5672

5773
clipboard.text = text;
74+
75+
await promise;
76+
const value = await new Promise((resolve, reject) => {
77+
gtkClipboard.read_text_async(null, (source, res) => {
78+
try {
79+
resolve(source.read_text_finish(res));
80+
} catch (e) {
81+
reject(e);
82+
}
83+
});
84+
});
85+
expect(value).toBe(text);
5886
});
5987
});
6088

installed-tests/suites/plugins/testSmsPlugin.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -223,44 +223,25 @@ describe('The sms plugin', function () {
223223
});
224224

225225
it('can request a list of conversations', async function () {
226-
spyOn(localPlugin, '_handleMessages');
226+
spyOn(localPlugin, '_handleDigest');
227227

228228
localPlugin._requestConversations();
229229

230230
await localPlugin.awaitPacket('kdeconnect.sms.messages');
231-
expect(localPlugin._handleMessages).toHaveBeenCalled();
231+
expect(localPlugin._handleDigest).toHaveBeenCalled();
232232
});
233233

234234
it('can request full conversations', async function () {
235-
spyOn(localPlugin, '_handleMessages').and.callThrough();
236235
spyOn(localPlugin, '_handleThread').and.callThrough();
237236
spyOn(localPlugin, '_requestConversation').and.callThrough();
238237

239-
localPlugin._requestConversations();
240-
241-
await localPlugin.awaitPacket('kdeconnect.sms.messages');
242-
expect(localPlugin._handleMessages).toHaveBeenCalled();
243-
expect(localPlugin._requestConversation).toHaveBeenCalledTimes(2);
244-
245-
localPlugin.handlePacket.calls.reset();
238+
localPlugin.requestMore('1', -1);
246239

247240
await localPlugin.awaitPacket('kdeconnect.sms.messages');
241+
expect(localPlugin._requestConversation).toHaveBeenCalledWith('1', 10, -1);
248242
expect(localPlugin._handleThread).toHaveBeenCalled();
249243
});
250244

251-
it('only requests new or updated converations', async function () {
252-
spyOn(localPlugin, '_handleMessages').and.callThrough();
253-
spyOn(localPlugin, '_handleThread').and.callThrough();
254-
spyOn(localPlugin, '_requestConversation').and.callThrough();
255-
256-
localPlugin._requestConversations();
257-
258-
await localPlugin.awaitPacket('kdeconnect.sms.messages');
259-
expect(localPlugin._handleMessages).toHaveBeenCalled();
260-
261-
expect(localPlugin._requestConversation).not.toHaveBeenCalled();
262-
});
263-
264245
it('can send SMS messages', async function () {
265246
spyOn(remoteDevice, 'handlePacket').and.callThrough();
266247

src/gsconnect-preferences.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ import './preferences/init.js';
2020
import {Window} from './preferences/service.js';
2121
import Config from './config.js';
2222

23-
let GioUnix;
24-
try {
25-
GioUnix = (await import('gi://GioUnix?version=2.0')).default;
26-
} catch {
27-
GioUnix = Gio
28-
}
29-
3023
/**
3124
* Class representing the GSConnect service daemon.
3225
*/

src/service/components/clipboard.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,35 @@ const Clipboard = GObject.registerClass({
181181
* GtkClipboard
182182
*/
183183
async _gtkUpdateText() {
184-
const mimetypes = await new Promise((resolve, reject) => {
185-
this._clipboard.request_targets((clipboard, atoms) => resolve(atoms));
186-
});
187-
188-
// Special case for a cleared clipboard
189-
if (mimetypes.length === 0)
190-
return this._applyUpdate('');
191-
192-
// Special case to ignore copied files
193-
if (mimetypes.includes('text/uri-list'))
194-
return;
195-
196-
const text = await new Promise((resolve, reject) => {
197-
this._clipboard.request_text((clipboard, text) => resolve(text));
198-
});
184+
try {
185+
const formats = this._clipboard.get_formats();
186+
const mimetypes = formats.get_mime_types() || [];
187+
188+
// Special case for a cleared clipboard
189+
if (mimetypes.length === 0)
190+
return this._applyUpdate('');
191+
192+
// Special case to ignore copied files
193+
if (mimetypes.includes('text/uri-list'))
194+
return;
195+
196+
const text = await new Promise((resolve, reject) => {
197+
this._clipboard.read_text_async(this._cancellable, (source, res) => {
198+
try {
199+
resolve(source.read_text_finish(res));
200+
} catch (e) {
201+
reject(e);
202+
}
203+
});
204+
});
205+
this._applyUpdate(text);
206+
} catch (e) {
207+
if (e instanceof Error && e.matches && e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
208+
return;
199209

200-
this._applyUpdate(text);
210+
if (e instanceof Error)
211+
debug(e);
212+
}
201213
}
202214

203215
destroy() {

src/service/plugins/sms.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const SMSPlugin = GObject.registerClass({
199199
handlePacket(packet) {
200200
switch (packet.type) {
201201
case 'kdeconnect.sms.messages':
202-
this._handleMessages(packet.body.messages);
202+
this._handleDigest(packet.body.messages);
203203
break;
204204
}
205205
}
@@ -255,7 +255,7 @@ const SMSPlugin = GObject.registerClass({
255255
*
256256
* @param {object[]} messages - A list of sms message objects
257257
*/
258-
_handleMessages(messages) {
258+
_handleDigest(messages) {
259259
try {
260260
// If messages is empty there's nothing to do...
261261
if (messages.length === 0)

0 commit comments

Comments
 (0)