File: object_manager.rb

package info (click to toggle)
ruby-dbus 0.25.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 776 kB
  • sloc: ruby: 6,584; xml: 225; sh: 38; makefile: 8
file content (61 lines) | stat: -rw-r--r-- 2,301 bytes parent folder | download
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
# frozen_string_literal: true

# This file is part of the ruby-dbus project
# Copyright (C) 2022 José Iván López González
# Copyright (C) 2022 Martin Vidner
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License, version 2.1 as published by the Free Software Foundation.
# See the file "COPYING" for the exact licensing terms.

module DBus
  # A mixin for {DBus::Object} implementing
  # {https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager
  # org.freedesktop.DBus.ObjectManager}.
  #
  # {ObjectServer#export} and {ObjectServer#unexport} will look for an ObjectManager
  # parent in the path hierarchy. If found, it will emit InterfacesAdded
  # or InterfacesRemoved, as appropriate.
  module ObjectManager
    OBJECT_MANAGER_INTERFACE = "org.freedesktop.DBus.ObjectManager"

    # Implements `the GetManagedObjects` method.
    # @return [Hash{ObjectPath => Hash{String => Hash{String => Data::Base}}}]
    #   object -> interface -> property -> value
    def managed_objects
      descendant_objects = object_server.descendants_for(path)
      descendant_objects.each_with_object({}) do |obj, hash|
        hash[obj.path] = obj.interfaces_and_properties
      end
    end

    # {ObjectServer#export} will call this for you to emit the `InterfacesAdded` signal.
    # @param object [DBus::Object]
    # @return [void]
    def object_added(object)
      InterfacesAdded(object.path, object.interfaces_and_properties)
    end

    # {ObjectServer#unexport} will call this for you to emit the `InterfacesRemoved` signal.
    # @param object [DBus::Object]
    # @return [void]
    def object_removed(object)
      InterfacesRemoved(object.path, object.intfs.keys)
    end

    # Module#included, a hook for `include ObjectManager`, declares its dbus_interface.
    def self.included(base)
      base.class_eval do
        dbus_interface OBJECT_MANAGER_INTERFACE do
          dbus_method :GetManagedObjects, "out res:a{oa{sa{sv}}}" do
            [managed_objects]
          end

          dbus_signal :InterfacesAdded, "object:o, interfaces_and_properties:a{sa{sv}}"
          dbus_signal :InterfacesRemoved, "object:o, interfaces:as"
        end
      end
    end
  end
end