lib/libnotify.rb in libnotify-0.4.0 vs lib/libnotify.rb in libnotify-0.5.0
- old
+ new
@@ -11,18 +11,22 @@
# Creates a notification.
#
# @example Block syntax
# n = Libnotify.new do |notify|
- # notify.summary = "world"
- # notify.body = "hello"
- # notify.timeout = 1.5 # 1.5 (s), 1000 (ms), "2", nil, false
- # notify.urgency = :critical # :low, :normal, :critical
- # notify.icon_path = "/usr/share/icons/gnome/scalable/emblems/emblem-default.svg"
+ # notify.summary = "hello"
+ # notify.body = "world"
+ # notify.timeout = 1.5 # 1.5 (s), 1000 (ms), "2", nil, false
+ # notify.urgency = :critical # :low, :normal, :critical
+ # notify.append = false # default true - append onto existing notification
+ # notify.icon_path = "/usr/share/icons/gnome/scalable/emblems/emblem-default.svg"
# end
# n.show!
#
+ # @example Hash syntax
+ # Libnotify.show(:body => "hello", :summary => "world", :timeout => 2.5)
+ #
# @example Mixed syntax
# Libnotify.new(options) do |n|
# n.timeout = 1.5 # overrides :timeout in options
# n.show!
# end
@@ -44,22 +48,10 @@
API.new(options, &block)
end
# Shows a notification. It takes the same +options+ as Libnotify.new.
#
- # @example Block syntax
- # Libnotify.show do |notify|
- # notify.summary = "world"
- # notify.body = "hello"
- # notify.timeout = 1.5 # 1.5 (s), 1000 (ms), "2", nil, false
- # notify.urgency = :critical # :low, :normal, :critical
- # notify.icon_path = "/usr/share/icons/gnome/scalable/emblems/emblem-default.svg"
- # end
- #
- # @example Hash syntax
- # Libnotify.show(:body => "hello", :summary => "world", :timeout => 2.5)
- #
# @see Libnotify.new
def self.show(options={}, &block)
API.show(options, &block)
end
@@ -83,13 +75,24 @@
end
class API
include FFI
- attr_reader :timeout
- attr_accessor :summary, :body, :icon_path, :urgency, :append
+ attr_reader :timeout, :icon_path
+ attr_accessor :summary, :body, :urgency, :append
+ class << self
+ # List of globs to icons
+ attr_accessor :icon_dirs
+ end
+
+ self.icon_dirs = [
+ "/usr/share/icons/gnome/48x48/emblems",
+ "/usr/share/icons/gnome/256x256/emblems",
+ "/usr/share/icons/gnome/*/emblems"
+ ]
+
# Creates a notification object.
#
# @see Libnotify.new
def initialize(options={}, &block)
set_defaults
@@ -136,9 +139,35 @@
end
when NilClass, FalseClass
nil
else
timeout.to_s.to_i
+ end
+ end
+
+ # Sets icon path.
+ #
+ # Path can be absolute, relative (will be resolved) or an symbol.
+ #
+ # @todo document and refactor
+ def icon_path=(path)
+ case path
+ when /^\// # absolute
+ @icon_path = path
+ when String
+ # TODO refactor!
+ self.class.icon_dirs.map { |d| Dir[d] }.flatten.uniq.each do |dir|
+ full_path = File.join(dir, path)
+ if File.exist?(full_path)
+ @icon_path = full_path
+ return
+ end
+ end
+ @icon_path = path
+ when Symbol
+ self.icon_path = "#{path}.png"
+ else
+ @icon_path = nil
end
end
# Creates and shows a notification. It's a shortcut for +Libnotify.new(options).show!+.
#