| 1 |
const { Gio, GLib, GObject, Gtk } = imports.gi; |
| 2 |
|
| 3 |
const { PortalTestWindow } = imports.window; |
| 4 |
const ByteArray = imports.byteArray; |
| 5 |
|
| 6 |
var Application = GObject.registerClass({ |
| 7 |
GTypeName: 'Gtk4PortalTestApplication', |
| 8 |
}, class Application extends Gtk.Application { |
| 9 |
_init(params) { |
| 10 |
super._init(params); |
| 11 |
|
| 12 |
this.add_main_option('replace', 0, 0, 0, 'Replace the running instance', null); |
| 13 |
|
| 14 |
let action = new Gio.SimpleAction({ name: 'ack' }); |
| 15 |
action.connect('activate', () => this._window.ack()); |
| 16 |
this.add_action(action); |
| 17 |
|
| 18 |
action = new Gio.SimpleAction({ name: 'restart' }); |
| 19 |
action.connect('activate', () => this.restart()); |
| 20 |
this.add_action(action); |
| 21 |
|
| 22 |
action = new Gio.SimpleAction({ name: 'quit' }); |
| 23 |
action.connect('activate', () => this.quit()); |
| 24 |
this.add_action(action); |
| 25 |
} |
| 26 |
|
| 27 |
vfunc_activate() { |
| 28 |
super.vfunc_activate(); |
| 29 |
|
| 30 |
this._window.present(); |
| 31 |
} |
| 32 |
|
| 33 |
vfunc_startup() { |
| 34 |
super.vfunc_startup(); |
| 35 |
|
| 36 |
if (!this._window) |
| 37 |
this._window = new PortalTestWindow(this); |
| 38 |
} |
| 39 |
|
| 40 |
restart() { |
| 41 |
const bus = this.get_dbus_connection(); |
| 42 |
|
| 43 |
const s2ay = value => { |
| 44 |
let bytes; |
| 45 |
if (typeof value === 'string') { |
| 46 |
let byteArray = ByteArray.fromString(value); |
| 47 |
if (byteArray[byteArray.length - 1] !== 0) |
| 48 |
byteArray = Uint8Array.of(...byteArray, 0); |
| 49 |
bytes = ByteArray.toGBytes(byteArray); |
| 50 |
} else { |
| 51 |
bytes = new GLib.Bytes(value); |
| 52 |
} |
| 53 |
return GLib.Variant.new_from_bytes(new GLib.VariantType('ay'), |
| 54 |
bytes, true); |
| 55 |
}; |
| 56 |
|
| 57 |
bus.call( |
| 58 |
'org.freedesktop.portal.Flatpak', |
| 59 |
'/org/freedesktop/portal/Flatpak', |
| 60 |
'org.freedesktop.portal.Flatpak', |
| 61 |
'Spawn', GLib.Variant.new_tuple([ |
| 62 |
s2ay('/'), |
| 63 |
GLib.Variant.new_array(new GLib.VariantType('ay'), [ |
| 64 |
s2ay('org.gnome.PortalTest.Gtk4'), |
| 65 |
s2ay('--replace'), |
| 66 |
]), |
| 67 |
GLib.Variant.new_array(new GLib.VariantType('{uh}'), []), |
| 68 |
GLib.Variant.new_array(new GLib.VariantType('{ss}'), []), |
| 69 |
GLib.Variant.new_uint32(2), |
| 70 |
GLib.Variant.new_array(new GLib.VariantType('{sv}'), []), |
| 71 |
]), |
| 72 |
new GLib.VariantType('(u)'), |
| 73 |
0, -1, null, |
| 74 |
(connection, result) => { |
| 75 |
try { |
| 76 |
const res = bus.call_finish(result); |
| 77 |
log(`Restarted with pid ${res.deepUnpack()}`); |
| 78 |
} catch(e) { |
| 79 |
logError(e); |
| 80 |
return; |
| 81 |
} |
| 82 |
}); |
| 83 |
} |
| 84 |
}); |