Class: Notifications::NotificationService

Inherits:
Object
  • Object
show all
Defined in:
lib/notifications.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNotificationService

Returns a new instance of NotificationService.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/notifications.rb', line 50

def initialize
  @bus = DBus::SessionBus.instance
  @service = @bus.service Notifications::NOTIF_SERVICE
  @service_obj = @service.object Notifications::NOTIF_OBJECT


  if not @service.exists? then
    raise NotificationDaemonError.new
  end


  @service_obj.define_singleton_method(:method_missing) do |name, *args, &block|
    if not self.introspected then
      raise ObjectIntrospectionError
    else
      raise CommandNotImplementedError name.to_s
    end
  end

  @supports = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

returns values of attributes mapped to @supports

Raises:

  • (NoMethodError)


82
83
84
85
86
87
# File 'lib/notifications.rb', line 82

def method_missing(name, *args)
  raise NoMethodError unless args.length == 0 and not block
  m_data = name.match /^supports_([a-zA-Z]+)\?$/
  raise NoMethodError unless m_data and m_data[1]
  ( val = @supports[m_data[1]] ) and Notifications::VALID_CAPABILITIES.include?(m_data[1]) ? val : false
end

Instance Attribute Details

#busObject (readonly)

Returns the value of attribute bus.



35
36
37
# File 'lib/notifications.rb', line 35

def bus
  @bus
end

#serviceObject (readonly)

Returns the value of attribute service.



35
36
37
# File 'lib/notifications.rb', line 35

def service
  @service
end

Instance Method Details

#capabilitiesObject



45
46
47
48
# File 'lib/notifications.rb', line 45

def capabilities
  @capabilities ||= try_to { @service_obj.GetCapabilities }
  @capabilities
end

#close_notification(notify_temp = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/notifications.rb', line 108

def close_notification(notify_temp=nil)
  if notify_temp then
    close = notify_temp
  elsif @last_notify
    close = @last_notify
  else
    return -1
  end

  try_to { @service_obj.CloseNotification(close.id)[0] }
end

#get_server_infoObject



120
121
122
# File 'lib/notifications.rb', line 120

def get_server_info
  try_to { @service_obj.GetServerInformation }
end

#introspected?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/notifications.rb', line 41

def introspected?
  @service_obj.introspected
end

#send_notification(notify_temp, replace_previous = false) ⇒ Object

returns notification after run



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/notifications.rb', line 90

def send_notification(notify_temp, replace_previous=false)
  ( replace_previous and notify_temp.id > 0 ) ? r_id = notify_temp.id : r_id = 0

  id = try_to {
    @service_obj.Notify( notify_temp.appname, r_id, notify_temp.icon, notify_temp.summary,
                         notify_temp.body, notify_temp.actions.pack, notify_temp.hints,
                         notify_temp.timeout )[0]
  }

  if not replace_previous then
    notify_temp = notify_temp.clone
  end

  notify_temp.id = id
  @last_notify = notify_temp
  notify_temp
end

#service_objectObject



37
38
39
# File 'lib/notifications.rb', line 37

def service_object
  @service_obj
end

#try_introspectObject



72
73
74
75
76
77
78
79
# File 'lib/notifications.rb', line 72

def try_introspect
  begin
    try_to { @service_obj.introspect }
    true
  rescue
    false
  end
end