File: debase-objectmanager.el

package info (click to toggle)
emacs-debase 0.7%2Bgit.20230105.0b6fc2af34%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 128 kB
  • sloc: lisp: 723; makefile: 5
file content (92 lines) | stat: -rw-r--r-- 3,727 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
;;; debase-objectmanager.el --- D-Bus ObjectManager  -*- lexical-binding: t; -*-

;; Copyright (C) 2021, 2022  Ian Eure

;; Author: Ian Eure <ian@retrospec.tv>
;; Keywords: lisp, unix
;; URL: https://github.com/ieure/debase
;; Version: 0.7
;; Package-Requires: ((emacs "25.1"))

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; DEBASE-OBJECTMANAGER implements the
;; org.freedesktop.DBus.ObjectManager D-Bus interface.

;;; Code:

(require 'debase)
(require 'eieio)
(require 'dbus)

(defclass debase-objectmanager (debase-object)
  ((managed-objects
    :type cons
    :documentation "List of objects this object manages.")
   (-objectmanager-on-change
    :initform '()
    :documentation "List of hook functions to call when MANAGED-OBJECTS changes.")
   (-objectmanager-signals
    :type cons
    :documentation "D-Bus signals this object has registered."))
  :documentation "A class representing the D-Bus ObjectManager interface.")

(cl-defmethod debase-objectmanager-onchange ((this debase-objectmanager) f)
  "When the state of objects manged by THIS changes, call function F."
  (with-slots (-objectmanager-on-change) this
    (add-to-list '-objectmanager-on-change f)))

(cl-defmethod debase-objectmanager--changed ((this debase-objectmanager) &rest ignore)
  "Refresh objects managed by THIS.

Calls hook functions in -OBJECTMANAGER-ON-CHANGE."
  (with-slots (managed-objects -objectmanager-on-change) this
    (setf managed-objects
          (debase-call
           (clone this :interface dbus-interface-objectmanager) "GetManagedObjects"))
    (dolist (f -objectmanager-on-change)
      (funcall f))))

(cl-defmethod initialize-instance :after ((this debase-objectmanager) &rest ignore)
  "Initialize instance THIS by populating managed objects."
  (unless (slot-boundp this 'interface)
    (error "Must target `%s' interface!" dbus-interface-objectmanager))
  (debase-object-assert-interface this dbus-interface-objectmanager)
  (debase-objectmanager--changed this))

(cl-defmethod debase-objectmanager-start ((this debase-objectmanager))
  "Begin listening for updates to managed objects on THIS."
  (with-slots (-objectmanager-signals) this
    (let ((om (clone this :interface dbus-interface-objectmanager)))
      (setf -objectmanager-signals
            (cl-loop for signal in '("InterfacesAdded" "InterfacesRemoved")
                     collect (debase-listen
                              om signal
                              (apply-partially #'debase-objectmanager--changed this)))))))

(cl-defmethod debase-objectmanager-started? ((this debase-objectmanager))
  "Returns non-NIL if ObjectManager THIS is listening for changes."
  (and (slot-boundp this '-objectmanager-signals)
       (not (null (slot-value this '-objectmanager-signals)))))

(cl-defmethod debase-objectmanager-stop ((this debase-objectmanager))
  "Stop listening for updates to managed objects on THIS."
  (with-slots (-objectmanager-signals) this
    (mapc #'dbus-unregister-object -objectmanager-signals)
    (setf -objectmanager-signals nil)))

(provide 'debase-objectmanager)
;;; debase-objectmanager.el ends here