session.js 2.1 KB

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