class Libnotify::API

API for Libnotify

@see Libnotify

Attributes

icon_dirs[RW]

List of globs to icons

app_name[RW]
append[RW]
body[RW]
icon_path[R]
summary[RW]
timeout[R]
transient[RW]
urgency[RW]

Public Class Methods

new(options={}, &block) click to toggle source

Creates a notification object.

@see Libnotify.new

# File lib/libnotify/api.rb, line 27
def initialize(options={}, &block)
  set_defaults
  apply_options(options, &block)
end
show(options={}, &block) click to toggle source

Creates and shows a notification. It's a shortcut for +Libnotify.new(options).show!+.

@see Libnotify.show @see Libnotify.new

# File lib/libnotify/api.rb, line 124
def self.show(options={}, &block)
  new(options, &block).show!
end

Public Instance Methods

close() click to toggle source

Close a previously shown notification.

# File lib/libnotify/api.rb, line 80
def close
  notify_notification_close(@notification, nil) if @notification
end
icon_path=(path) click to toggle source

Sets icon path.

Path can be absolute, relative (will be resolved) or a symbol.

@todo document and refactor

# File lib/libnotify/api.rb, line 107
def icon_path=(path)
  case path
  when %r{^/} # absolute
    @icon_path = path
  when String
    @icon_path = icon_for(path)
  when Symbol
    self.icon_path = "#{path}.png"
  else
    @icon_path = nil
  end
end
show() click to toggle source

Shows an existing notification.

# File lib/libnotify/api.rb, line 59
def show
  notify_notification_set_urgency(@notification, urgency)
  notify_notification_set_timeout(@notification, timeout || -1)
  set_hints
  notify_notification_show(@notification, nil)
ensure
  clear_hints
end
show!() click to toggle source

Shows a new notification.

@see Libnotify.show

# File lib/libnotify/api.rb, line 51
def show!
  notify_init(app_name) or raise "notify_init failed"
  raw_ptr = notify_notification_new(summary, body, icon_path, nil)
  @notification = ::FFI::AutoPointer.new(raw_ptr, method(:g_object_unref))
  show
end
timeout=(timeout) click to toggle source

@todo Simplify timeout=

# File lib/libnotify/api.rb, line 85
def timeout=(timeout)
  @timeout = case timeout
  when Float
    (timeout * 1000).to_i
  when Integer
    if timeout >= 100 # assume miliseconds
      timeout
    else
      timeout * 1000
    end
  when NilClass, FalseClass
    nil
  else
    timeout.to_s.to_i
  end
end
update(options={}, &block) click to toggle source

Updates a previously shown notification or creates a new one.

# File lib/libnotify/api.rb, line 69
def update(options={}, &block)
  apply_options(options, &block)
  if @notification
    notify_notification_update(@notification, summary, body, icon_path, nil)
    show
  else
    show!
  end
end

Private Instance Methods

apply_options(options={}) { |self| ... } click to toggle source
# File lib/libnotify/api.rb, line 32
def apply_options(options={})
  options.each { |key, value| send("#{key}=", value) if respond_to?(key) }
  yield(self) if block_given?
end
clear_hints() click to toggle source
# File lib/libnotify/api.rb, line 140
def clear_hints
  notify_notification_clear_hints(@notification) if (append || transient)
end
icon_for(name) click to toggle source
# File lib/libnotify/api.rb, line 144
def icon_for(name)
  IconFinder.new(self.class.icon_dirs).icon_path(name) || name
end
set_defaults() click to toggle source
# File lib/libnotify/api.rb, line 38
def set_defaults
  self.app_name = self.class.to_s
  self.summary = self.body = ' '
  self.urgency = :normal
  self.timeout = nil
  self.append = true
  self.transient = false
end
set_hints() click to toggle source
# File lib/libnotify/api.rb, line 130
def set_hints
  if append
    notify_notification_set_hint_string(@notification, "x-canonical-append", "")
    notify_notification_set_hint_string(@notification, "append", "")
  end
  if transient
    notify_notification_set_hint_uint32(@notification, "transient", 1)
  end
end