notification.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-FileCopyrightText: GSConnect Developers https://github.com/GSConnect
  2. //
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. 'use strict';
  5. const Gio = imports.gi.Gio;
  6. const GObject = imports.gi.GObject;
  7. const Gtk = imports.gi.Gtk;
  8. const URI = imports.service.utils.uri;
  9. const _ui = imports.service.utils.ui;
  10. /**
  11. * A dialog for repliable notifications.
  12. */
  13. var ReplyDialog = GObject.registerClass({
  14. GTypeName: 'GSConnectNotificationReplyDialog',
  15. Properties: {
  16. 'device': GObject.ParamSpec.object(
  17. 'device',
  18. 'Device',
  19. 'The device associated with this window',
  20. GObject.ParamFlags.READWRITE,
  21. GObject.Object
  22. ),
  23. 'plugin': GObject.ParamSpec.object(
  24. 'plugin',
  25. 'Plugin',
  26. 'The plugin that owns this notification',
  27. GObject.ParamFlags.READWRITE,
  28. GObject.Object
  29. ),
  30. 'uuid': GObject.ParamSpec.string(
  31. 'uuid',
  32. 'UUID',
  33. 'The notification reply UUID',
  34. GObject.ParamFlags.READWRITE,
  35. null
  36. ),
  37. },
  38. Template: 'resource:///org/gnome/Shell/Extensions/GSConnect/ui/notification-reply-dialog.ui',
  39. Children: ['infobar', 'notification-title', 'notification-body', 'entry'],
  40. }, class ReplyDialog extends Gtk.Dialog {
  41. _init(params) {
  42. super._init({
  43. application: Gio.Application.get_default(),
  44. device: params.device,
  45. plugin: params.plugin,
  46. uuid: params.uuid,
  47. use_header_bar: true,
  48. });
  49. this.set_response_sensitive(Gtk.ResponseType.OK, false);
  50. // Info bar
  51. this.device.bind_property(
  52. 'connected',
  53. this.infobar,
  54. 'reveal-child',
  55. GObject.BindingFlags.INVERT_BOOLEAN
  56. );
  57. // Notification Data
  58. const headerbar = this.get_titlebar();
  59. headerbar.title = params.notification.appName;
  60. headerbar.subtitle = this.device.name;
  61. this.notification_title.label = params.notification.title;
  62. this.notification_body.label = URI.linkify(params.notification.text);
  63. // Message Entry/Send Button
  64. this.device.bind_property(
  65. 'connected',
  66. this.entry,
  67. 'sensitive',
  68. GObject.BindingFlags.DEFAULT
  69. );
  70. this._connectedId = this.device.connect(
  71. 'notify::connected',
  72. this._onStateChanged.bind(this)
  73. );
  74. this._entryChangedId = this.entry.buffer.connect(
  75. 'changed',
  76. this._onStateChanged.bind(this)
  77. );
  78. this.restoreGeometry('notification-reply-dialog');
  79. this.connect('destroy', this._onDestroy);
  80. }
  81. _onDestroy(dialog) {
  82. dialog.entry.buffer.disconnect(dialog._entryChangedId);
  83. dialog.device.disconnect(dialog._connectedId);
  84. }
  85. vfunc_delete_event() {
  86. this.saveGeometry();
  87. return false;
  88. }
  89. vfunc_response(response_id) {
  90. if (response_id === Gtk.ResponseType.OK) {
  91. // Refuse to send empty or whitespace only messages
  92. if (!this.entry.buffer.text.trim())
  93. return;
  94. this.plugin.replyNotification(
  95. this.uuid,
  96. this.entry.buffer.text
  97. );
  98. }
  99. this.destroy();
  100. }
  101. get device() {
  102. if (this._device === undefined)
  103. this._device = null;
  104. return this._device;
  105. }
  106. set device(device) {
  107. this._device = device;
  108. }
  109. get plugin() {
  110. if (this._plugin === undefined)
  111. this._plugin = null;
  112. return this._plugin;
  113. }
  114. set plugin(plugin) {
  115. this._plugin = plugin;
  116. }
  117. get uuid() {
  118. if (this._uuid === undefined)
  119. this._uuid = null;
  120. return this._uuid;
  121. }
  122. set uuid(uuid) {
  123. this._uuid = uuid;
  124. // We must have a UUID
  125. if (!uuid) {
  126. this.destroy();
  127. debug('no uuid for repliable notification');
  128. }
  129. }
  130. _onActivateLink(label, uri) {
  131. Gtk.show_uri_on_window(
  132. this.get_toplevel(),
  133. uri.includes('://') ? uri : `https://${uri}`,
  134. Gtk.get_current_event_time()
  135. );
  136. return true;
  137. }
  138. _onStateChanged() {
  139. if (this.device.connected && this.entry.buffer.text.trim())
  140. this.set_response_sensitive(Gtk.ResponseType.OK, true);
  141. else
  142. this.set_response_sensitive(Gtk.ResponseType.OK, false);
  143. }
  144. });