Sha256: 7e3d0af6b8c24aa44aec037e9d5ab5acbd59c379f603113cb2473945da37871b

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require 'libnotify'

module Snapsync
  # This class abstracts notifying each user on the system
  # NOTE: Not multi-thread safe!
  class Notification
    # @return [Hash{Integer => Libnotify::API}]
    attr_reader :notified_users

    def initialize(config)
      @notified_users = {}


      Dir.each_child('/run/user') do |user_id|
        # seteuid _must_ be set for session dbus to accept the connection
        Process::Sys.seteuid(user_id.to_i)
        ENV['DBUS_SESSION_BUS_ADDRESS'] = "unix:path=/run/user/#{user_id}/bus"

        n = Libnotify.new(config)
        @notified_users[user_id.to_i] = n
        n.show!
      end
      # set euid back, so all commands works normally
      Process::Sys.seteuid Process::Sys.getuid
    end

    def update(config)
      each_raw_notification do |n|
        n.update(config)
      end
    end

    def close
      each_raw_notification do |n|
        n.close
      end
    end

    # @yieldparam [Libnotify::API] n
    private def each_raw_notification
      notified_users.each do |user_id, n|
        # seteuid _must_ be set for session dbus to accept the connection
        Process::Sys.seteuid(user_id)
        ENV['DBUS_SESSION_BUS_ADDRESS'] = "unix:path=/run/user/#{user_id}/bus"

        yield n
      end
      # set euid back, so all commands works normally
      Process::Sys.seteuid Process::Sys.getuid
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
snapsync-0.5.0 lib/snapsync/Notification.rb