session.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 GLib = imports.gi.GLib;
  7. const Session = class {
  8. constructor() {
  9. this._connection = Gio.DBus.system;
  10. this._session = null;
  11. this._initAsync();
  12. }
  13. async _initAsync() {
  14. try {
  15. const reply = await this._connection.call(
  16. 'org.freedesktop.login1',
  17. '/org/freedesktop/login1',
  18. 'org.freedesktop.login1.Manager',
  19. 'ListSessions',
  20. null,
  21. null,
  22. Gio.DBusCallFlags.NONE,
  23. -1,
  24. null);
  25. const sessions = reply.deepUnpack()[0];
  26. const userName = GLib.get_user_name();
  27. let sessionPath = '/org/freedesktop/login1/session/auto';
  28. // eslint-disable-next-line no-unused-vars
  29. for (const [num, uid, name, seat, objectPath] of sessions) {
  30. if (name === userName) {
  31. sessionPath = objectPath;
  32. break;
  33. }
  34. }
  35. this._session = new Gio.DBusProxy({
  36. g_connection: this._connection,
  37. g_name: 'org.freedesktop.login1',
  38. g_object_path: sessionPath,
  39. g_interface_name: 'org.freedesktop.login1.Session',
  40. });
  41. await this._session.init_async(GLib.PRIORITY_DEFAULT, null);
  42. } catch (e) {
  43. this._session = null;
  44. logError(e);
  45. }
  46. }
  47. get idle() {
  48. if (this._session === null)
  49. return false;
  50. return this._session.get_cached_property('IdleHint').unpack();
  51. }
  52. get locked() {
  53. if (this._session === null)
  54. return false;
  55. return this._session.get_cached_property('LockedHint').unpack();
  56. }
  57. get active() {
  58. // Active if not idle and not locked
  59. return !(this.idle || this.locked);
  60. }
  61. destroy() {
  62. this._session = null;
  63. }
  64. };
  65. /**
  66. * The service class for this component
  67. */
  68. var Component = Session;