tin 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +16 -8
  2. data/lib/sn.rb +166 -22
  3. metadata +3 -3
data/README.md CHANGED
@@ -11,21 +11,34 @@ tin provides a simple interface to libnotify.
11
11
  FEATURES/PROBLEMS:
12
12
  ------------------
13
13
 
14
- * Very simple
14
+ * Simple, one method notifications
15
+ * Notification class for more complicated needs
16
+ * Includes most (non-deprecated) libnotify features
17
+
15
18
 
16
19
  SYNOPSIS:
17
20
  ---------
18
21
 
19
22
  require 'sn'
20
23
 
24
+ # a simple demo
21
25
  summary = 'tin demo'
22
26
  message = 'Just a quick message from tin!'
23
- Sn::Notification.new(summary, message).show
27
+ Sn.notify(summary, message)
28
+
29
+ # a customized demo
30
+ notification = Sn::Notification.new(summary, message)
31
+ notification.timeout = :never
32
+ notification.show
33
+ sleep 6
34
+ notification.close
35
+
24
36
 
25
37
  REQUIREMENTS:
26
38
  -------------
27
39
 
28
40
  * Ruby 1.9
41
+ * libnotify (tested with version 0.7.5)
29
42
 
30
43
  INSTALL:
31
44
  --------
@@ -35,12 +48,7 @@ INSTALL:
35
48
  DEVELOPERS:
36
49
  -----------
37
50
 
38
- After checking out the source, run:
39
-
40
- $ rake newb
41
-
42
- This task will install any missing dependencies, run the tests/specs,
43
- and generate the RDoc.
51
+ There is nothing special to know...
44
52
 
45
53
  LICENSE:
46
54
  --------
data/lib/sn.rb CHANGED
@@ -1,46 +1,190 @@
1
1
  require 'fiddle'
2
2
 
3
+ =begin
4
+
5
+ Tin is a simple interface to libnotify using Fiddle.
6
+
7
+ == Example
8
+
9
+ require 'sn'
10
+
11
+ # a simple demo
12
+ summary = 'tin demo'
13
+ message = 'Just a quick message from tin!'
14
+ Sn.notify(summary, message)
15
+
16
+ # a customized demo
17
+ notification = Sn::Notification.new(summary, message)
18
+ notification.timeout = :never
19
+ notification.show
20
+ sleep 6
21
+ notification.close
22
+
23
+ == Notes
24
+
25
+ Deprecated functions are not included in the those available through Sn.
26
+
27
+ These function are also not included (for lack of a good implementation):
28
+ * void notify_notification_set_hint(NotifyNotification *notification,
29
+ const char *key, GVariant *value);
30
+ * void notify_notification_clear_hints(NotifyNotification *notification);
31
+ * void notify_notification_set_image_from_pixbuf(
32
+ NotifyNotification *notification, GdkPixbuf *pixbuf);
33
+ * void notify_notification_add_action(NotifyNotification *notification,
34
+ const char *action, const char *label, NotifyActionCallback callback,
35
+ gpointer user_data, GFreeFunc free_func);
36
+ * void notify_notification_clear_actions(NotifyNotification *notification);
37
+ =end
38
+
3
39
  module Sn
4
40
  include Fiddle
5
41
 
6
- def self.start(app_name)
7
- @dl = DL.dlopen 'libnotify.so'
8
- @init = Function.new(@dl['notify_init'], [TYPE_VOIDP], TYPE_INT)
9
- @uninit = Function.new(@dl['notify_uninit'], [], TYPE_VOID)
10
- @newnot = Function.new(@dl['notify_notification_new'],
11
- [TYPE_VOIDP, TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
12
- @shownot = Function.new(@dl['notify_notification_show'],
13
- [TYPE_VOIDP, TYPE_VOIDP], TYPE_INT)
14
- @init.call app_name
15
- end
42
+ # Mapping from keyword urgencies to the in values used by libnotify
43
+ URGENCIES = {:low => 0, :normal => 1, :critical => 2}
16
44
 
45
+ # Uninitialize libnotify
17
46
  def self.stop
18
- @uninit.call
47
+ @notify[:uninit].call
19
48
  end
20
49
 
21
- def self.create(summary, message="", icon="")
22
- @newnot.call summary, message, icon
50
+ # Setup the libnotify interface and initialize libnotify.
51
+ def self.start(app_name)
52
+ notify_cmd = {
53
+ :init => {:arg => [TYPE_VOIDP], :ret => TYPE_INT},
54
+ :uninit => {:arg => [], :ret => TYPE_INT}
55
+ }
56
+
57
+ notification_cmd = {
58
+ :new => {:arg => [TYPE_VOIDP, TYPE_VOIDP, TYPE_VOIDP],
59
+ :ret => TYPE_VOIDP},
60
+ :show => {:arg => [TYPE_VOIDP, TYPE_VOIDP], :ret => TYPE_INT},
61
+ :update => {:arg => [TYPE_VOIDP, TYPE_VOIDP, TYPE_VOIDP, TYPE_VOIDP],
62
+ :ret => TYPE_INT},
63
+ :set_urgency => {:arg => [TYPE_VOIDP, TYPE_INT], :ret => TYPE_VOID},
64
+ :close => {:arg => [TYPE_VOIDP, TYPE_VOIDP], :ret => TYPE_INT},
65
+ :get_closed_reason => {:arg => [TYPE_VOIDP], :ret => TYPE_INT},
66
+ :set_app_name => {:arg => [TYPE_VOIDP, TYPE_VOIDP], :ret => TYPE_VOID},
67
+ :set_timeout => {:arg => [TYPE_VOIDP, TYPE_INT], :ret => TYPE_VOID},
68
+ :set_category => {:arg => [TYPE_VOIDP, TYPE_VOIDP], :ret => TYPE_VOID},
69
+ }
70
+
71
+ dl = DL.dlopen 'libnotify.so'
72
+
73
+ @notify = {}
74
+ notify_cmd.each do |k, v|
75
+ name = "notify_#{k.to_s}"
76
+ @notify[k] = Function.new(dl[name], v[:arg], v[:ret])
77
+ end
78
+
79
+ @notification = {}
80
+ notification_cmd.each do |k, v|
81
+ name = "notify_notification_#{k.to_s}"
82
+ @notification[k] = Function.new(dl[name], v[:arg], v[:ret])
83
+ end
84
+
85
+ @app_name = app_name
86
+ @notify[:init].call app_name # 1 or 0
87
+
88
+ at_exit do
89
+ Sn.stop
90
+ end
23
91
  end
24
92
 
25
- def self.show(notification)
26
- @shownot.call notification, nil
93
+ start $0 # Run start immediatle so @notification and @app_name are available
94
+
95
+ # Provides access to the individual Fiddle::Function objects.
96
+ NOTIFICATION = @notification
97
+
98
+ # The application name used to initialize libnotify
99
+ APP_NAME = @app_name
100
+
101
+ # Create and display a notification using (mostly) default values.
102
+ def self.notify(summary, message="", icon="")
103
+ n = @notification[:new].call(summary, message, icon)
104
+ @notification[:show].call n, nil
27
105
  end
28
106
 
107
+ # This class manages an application notification.
29
108
  class Notification
109
+ attr_reader :app_name, :category, :summary, :message, :urgency, :icon
110
+
111
+ # Mapping from keyword timeouts to the int values libnotify uses
112
+ @@timeout_consts = {:default => -1, :never => 0}
113
+
114
+ # +summary+:: the summary line generally displayed at the top of the
115
+ # notification
116
+ #
117
+ # +message+:: the message text of the notification
118
+ #
119
+ # +icon+:: a theme name or file path for an icon to display with the
120
+ # notification (if this is supported?)
30
121
  def initialize(summary, message="", icon="")
31
- @notification = Sn.create summary, message, icon
122
+ @summary = summary
123
+ @message = message
124
+ @icon = icon
125
+ @urgency = :normal
126
+ @category = ""
127
+ @app_name = APP_NAME
128
+ @notification = NOTIFICATION[:new].call summary, message, icon
32
129
  end
33
130
 
131
+ # Send the notification to the system
34
132
  def show
35
- Sn.show @notification
133
+ NOTIFICATION[:show].call @notification, nil
134
+ end
135
+
136
+ # Close this notification.
137
+ def close
138
+ NOTIFICATION[:close].call @notification, nil
139
+ end
140
+
141
+ # Obtain the reason that this notification was closed.
142
+ def closed_reason
143
+ NOTIFICATION[:get_closed_reason].call @notification
36
144
  end
37
- end
38
- end
39
145
 
40
- Sn.start $0
146
+ # Register changed summary, body, and icon values.
147
+ # Each time any of these fields are assigned, they will be updated by
148
+ # calling this method automatically. Generally, it should not be necessary
149
+ # to call this method explicitly.
150
+ def update
151
+ NOTIFICATION[:update].call @notification, @summary, @body, @icon
152
+ end
153
+
154
+ def summary=(value)
155
+ @summary = value
156
+ update
157
+ end
41
158
 
42
- at_exit do
43
- Sn.stop
159
+ def message=(value)
160
+ @message = value
161
+ update
162
+ end
163
+
164
+ def icon=(value)
165
+ @icon = value
166
+ update
167
+ end
168
+
169
+ def category=(value)
170
+ @category = category
171
+ NOTIFICATION[:set_category].call @notification, @category
172
+ end
173
+
174
+ # Set the timeout that controls when the notification will be closed. The
175
+ # timeout can be specified as a number of milliseconds or using the keywords
176
+ # +:default+ or +:never+.
177
+ def timeout=(timeout=:default)
178
+ @timeout = @@timeout_consts[timeout] if @@timeout_consts.member?(timeout)
179
+ NOTIFICATION[:set_timeout].call @notification, @timeout
180
+ end
181
+
182
+ def urgency=(value)
183
+ @urgency = value
184
+ NOTIFICATION[:set_urgency].call @notification, URGENCIES[@urgency]
185
+ end
186
+ end
44
187
  end
45
188
 
46
189
 
190
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-10 00:00:00.000000000 Z
12
+ date: 2012-05-21 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: An incredibly simple interface to libnotify using Fiddle.
15
15
  email: baguthrie@sbcglobal.net
@@ -40,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  version: '0'
41
41
  requirements: []
42
42
  rubyforge_project: tin
43
- rubygems_version: 1.8.23
43
+ rubygems_version: 1.8.15
44
44
  signing_key:
45
45
  specification_version: 3
46
46
  summary: Very simple libnotify access