growl_notifier 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/History.txt +1 -0
- data/LICENSE +2 -0
- data/Manifest.txt +11 -0
- data/README.txt +71 -0
- data/Rakefile +20 -0
- data/TODO +1 -0
- data/VERSION +1 -0
- data/init.rb +2 -0
- data/lib/growl_notifier.rb +170 -0
- metadata +83 -0
data/.gitignore
ADDED
data/History.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
See git-log.
|
data/LICENSE
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
= Growl Notifier
|
2
|
+
|
3
|
+
== Overview
|
4
|
+
|
5
|
+
Growl::Notifier is a class that allows your application to post notifications to the Growl daemon.
|
6
|
+
It can also receive clicked and timeout notifications as well as take blocks for clicked callback handlers.
|
7
|
+
|
8
|
+
This is an extraction and cleanup of the Growl code from LimeChat (http://github.com/psychs/limechat/tree/master) and later on extended for WebApp (http://github.com/alloy/webapp-app/tree/master).
|
9
|
+
|
10
|
+
== Requirements
|
11
|
+
|
12
|
+
* Mac OS X 10.4 or 10.5
|
13
|
+
* Ruby 1.8 (http://ruby-lang.org/)
|
14
|
+
* RubyCocoa (http://rubycocoa.sourceforge.net/)
|
15
|
+
|
16
|
+
== How to use Growl Notifier
|
17
|
+
|
18
|
+
A simple example:
|
19
|
+
|
20
|
+
require 'growl'
|
21
|
+
|
22
|
+
g = Growl::Notifier.sharedInstance
|
23
|
+
g.register('test_app', ['message_type'])
|
24
|
+
g.notify('message_type', 'title', 'desc')
|
25
|
+
|
26
|
+
How to receive clicked and timeout notifications in your application:
|
27
|
+
|
28
|
+
require 'rubygems'
|
29
|
+
require 'growl'
|
30
|
+
|
31
|
+
class GrowlController < OSX::NSObject
|
32
|
+
def init
|
33
|
+
if super_init
|
34
|
+
@g = Growl::Notifier.sharedInstance
|
35
|
+
@g.delegate = self
|
36
|
+
@g.register('test_app', ['message_type'])
|
37
|
+
@g.notify('message_type', 'title', 'desc')
|
38
|
+
self
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def growlNotifierClicked_context(sender, context)
|
43
|
+
puts context
|
44
|
+
end
|
45
|
+
|
46
|
+
def growlNotifierTimedOut_context(sender, context)
|
47
|
+
puts context
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
Include the Growl module into your class to get access to a few convenience methods:
|
52
|
+
|
53
|
+
require 'rubygems'
|
54
|
+
require 'growl'
|
55
|
+
|
56
|
+
class GrowlController < OSX::NSObject
|
57
|
+
include Growl
|
58
|
+
Growl::Notifier.sharedInstance.register('test_app', ['message_type'])
|
59
|
+
|
60
|
+
def init
|
61
|
+
if super_init
|
62
|
+
growl('message_type', 'title', 'desc')
|
63
|
+
self
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
== License
|
69
|
+
|
70
|
+
Copyright (c) 2007-2008 Satoshi Nakagawa <psychs@limechat.net>, Eloy Duran <e.duran@superalloy.nl>
|
71
|
+
You can redistribute it and/or modify it under the same terms as Ruby.
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "growl_notifier"
|
8
|
+
gem.description = %Q{A ruby library which allows you to send Growl notifications.}
|
9
|
+
gem.email = ["psychs@limechat.net", "e.duran@superalloy.nl","jphastings@gmail.com"]
|
10
|
+
gem.homepage = "http://github.com/jphastings/growl_notifier"
|
11
|
+
gem.summary = "Growl::Notifier is a OSX RubyCocoa class that allows your application to post notifications to the Growl daemon."
|
12
|
+
gem.authors = ["Satoshi Nakagawa", "Eloy Duran","JP Hastings-Spital"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
task :default => :build
|
data/TODO
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
- Maybe define a initWithDelegate initializer? But this might not be necessary because of the sharedInstance.
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.1.0
|
data/init.rb
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'osx/cocoa'
|
2
|
+
|
3
|
+
module Growl
|
4
|
+
class Notifier < OSX::NSObject
|
5
|
+
VERSION = '1.0.2'
|
6
|
+
|
7
|
+
GROWL_IS_READY = "Lend Me Some Sugar; I Am Your Neighbor!"
|
8
|
+
GROWL_NOTIFICATION_CLICKED = "GrowlClicked!"
|
9
|
+
GROWL_NOTIFICATION_TIMED_OUT = "GrowlTimedOut!"
|
10
|
+
GROWL_KEY_CLICKED_CONTEXT = "ClickedContext"
|
11
|
+
|
12
|
+
PRIORITIES = {
|
13
|
+
:emergency => 2,
|
14
|
+
:high => 1,
|
15
|
+
:normal => 0,
|
16
|
+
:moderate => -1,
|
17
|
+
:very_low => -2,
|
18
|
+
}
|
19
|
+
|
20
|
+
class << self
|
21
|
+
# Returns the singleton instance of Growl::Notifier with which you register and send your Growl notifications.
|
22
|
+
def sharedInstance
|
23
|
+
@sharedInstance ||= alloc.init
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_reader :application_name, :application_icon, :notifications, :default_notifications
|
28
|
+
attr_accessor :delegate
|
29
|
+
|
30
|
+
# Set to +true+ if you want to receive delegate callback messages,
|
31
|
+
# <tt>growlNotifierClicked_context</tt> & <tt>growlNotifierTimedOut_context</tt>,
|
32
|
+
# without the need to specify a <tt>:click_context</tt>.
|
33
|
+
#
|
34
|
+
# The default is +false+, which means your application won't receive any delegate
|
35
|
+
# callback messages if the <tt>:click_context</tt> is omitted.
|
36
|
+
attr_accessor :always_callback
|
37
|
+
|
38
|
+
# Registers the applications metadata and the notifications, that your application might send, to Growl.
|
39
|
+
# The +default_notifications+ are notifications that will be enabled by default, the regular +notifications+ are
|
40
|
+
# optional and should be enabled by the user in the Growl system preferences.
|
41
|
+
#
|
42
|
+
# Register the applications name and the notifications that will be used.
|
43
|
+
# * +default_notifications+ defaults to the regular +notifications+.
|
44
|
+
# * +application_icon+ defaults to OSX::NSApplication.sharedApplication.applicationIconImage.
|
45
|
+
#
|
46
|
+
# Growl::Notifier.sharedInstance.register 'FoodApp', ['YourHamburgerIsReady', 'OhSomeoneElseAteIt']
|
47
|
+
#
|
48
|
+
# Register the applications name, the notifications plus the default notifications that will be used and the icon that's to be used in the Growl notifications.
|
49
|
+
#
|
50
|
+
# Growl::Notifier.sharedInstance.register 'FoodApp', ['YourHamburgerIsReady', 'OhSomeoneElseAteIt'], ['DefaultNotification], OSX::NSImage.imageNamed('GreasyHamburger')
|
51
|
+
def register(application_name, notifications, default_notifications = nil, application_icon = nil)
|
52
|
+
@application_name, @application_icon = application_name, (application_icon || OSX::NSApplication.sharedApplication.applicationIconImage)
|
53
|
+
@notifications, @default_notifications = notifications, (default_notifications || notifications)
|
54
|
+
@callbacks = {}
|
55
|
+
send_registration!
|
56
|
+
end
|
57
|
+
|
58
|
+
# Sends a Growl notification.
|
59
|
+
#
|
60
|
+
# * +notification_name+ : the name of one of the notifcations that your apllication registered with Growl. See register for more info.
|
61
|
+
# * +title+ : the title that should be used in the Growl notification.
|
62
|
+
# * +description+ : the body of the Grow notification.
|
63
|
+
# * +options+ : specifies a few optional options:
|
64
|
+
# * <tt>:sticky</tt> : indicates if the Grow notification should "stick" to the screen. Defaults to +false+.
|
65
|
+
# * <tt>:priority</tt> : sets the priority level of the Growl notification. Defaults to 0.
|
66
|
+
# * <tt>:click_context</tt> : a string describing the context of the notification. This is send back to the delegate so you can check what kind of notification it was. If omitted, no delegate messages will be send. You can disable this behaviour by setting always_callback to +true+.
|
67
|
+
# * <tt>:icon</tt> : specifies the icon to be used in the Growl notification. Defaults to the registered +application_icon+, see register for more info.
|
68
|
+
#
|
69
|
+
# Simple example:
|
70
|
+
#
|
71
|
+
# name = 'YourHamburgerIsReady'
|
72
|
+
# title = 'Your hamburger is ready for consumption!'
|
73
|
+
# description = 'Please pick it up at isle 4.'
|
74
|
+
#
|
75
|
+
# Growl::Notifier.sharedInstance.notify(name, title, description)
|
76
|
+
#
|
77
|
+
# Example with optional options:
|
78
|
+
#
|
79
|
+
# Growl::Notifier.sharedInstance.notify(name, title, description, :sticky => true, :priority => 1, :icon => OSX::NSImage.imageNamed('SuperBigHamburger'))
|
80
|
+
#
|
81
|
+
# When you pass notify a block, that block will be used as the callback handler if the Growl notification was clicked. Eg:
|
82
|
+
#
|
83
|
+
# Growl::Notifier.sharedInstance.notify(name, title, description, :sticky => true) do
|
84
|
+
# user_clicked_notification_so_do_something!
|
85
|
+
# end
|
86
|
+
def notify(notification_name, title, description, options = {}, &callback)
|
87
|
+
dict = {
|
88
|
+
:ApplicationName => @application_name,
|
89
|
+
:ApplicationPID => pid,
|
90
|
+
:NotificationName => notification_name,
|
91
|
+
:NotificationTitle => title,
|
92
|
+
:NotificationDescription => description,
|
93
|
+
:NotificationPriority => PRIORITIES[options[:priority]] || options[:priority] || 0
|
94
|
+
}
|
95
|
+
dict[:NotificationIcon] = options[:icon].TIFFRepresentation if options[:icon]
|
96
|
+
dict[:NotificationSticky] = 1 if options[:sticky]
|
97
|
+
|
98
|
+
context = {}
|
99
|
+
context[:user_click_context] = options[:click_context] if options[:click_context]
|
100
|
+
if block_given?
|
101
|
+
@callbacks[callback.object_id] = callback
|
102
|
+
context[:callback_object_id] = callback.object_id.to_s
|
103
|
+
end
|
104
|
+
dict[:NotificationClickContext] = context if always_callback || !context.empty?
|
105
|
+
|
106
|
+
notification_center.postNotificationName_object_userInfo_deliverImmediately(:GrowlNotification, nil, dict, true)
|
107
|
+
end
|
108
|
+
|
109
|
+
def onReady(notification)
|
110
|
+
send_registration!
|
111
|
+
end
|
112
|
+
|
113
|
+
def onClicked(notification)
|
114
|
+
user_context = nil
|
115
|
+
if context = notification.userInfo[GROWL_KEY_CLICKED_CONTEXT]
|
116
|
+
user_context = context[:user_click_context]
|
117
|
+
if callback_object_id = context[:callback_object_id]
|
118
|
+
@callbacks.delete(callback_object_id.to_i).call
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
@delegate.growlNotifierClicked_context(self, user_context) if @delegate && @delegate.respond_to?(:growlNotifierClicked_context)
|
123
|
+
end
|
124
|
+
|
125
|
+
def onTimeout(notification)
|
126
|
+
user_context = nil
|
127
|
+
if context = notification.userInfo[GROWL_KEY_CLICKED_CONTEXT]
|
128
|
+
@callbacks.delete(context[:callback_object_id].to_i) if context[:callback_object_id]
|
129
|
+
user_context = context[:user_click_context]
|
130
|
+
end
|
131
|
+
|
132
|
+
@delegate.growlNotifierTimedOut_context(self, user_context) if @delegate && @delegate.respond_to?(:growlNotifierTimedOut_context)
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
def pid
|
138
|
+
OSX::NSProcessInfo.processInfo.processIdentifier.to_i
|
139
|
+
end
|
140
|
+
|
141
|
+
def notification_center
|
142
|
+
OSX::NSDistributedNotificationCenter.defaultCenter
|
143
|
+
end
|
144
|
+
|
145
|
+
def send_registration!
|
146
|
+
add_observer 'onReady:', GROWL_IS_READY, false
|
147
|
+
add_observer 'onClicked:', GROWL_NOTIFICATION_CLICKED, true
|
148
|
+
add_observer 'onTimeout:', GROWL_NOTIFICATION_TIMED_OUT, true
|
149
|
+
|
150
|
+
dict = {
|
151
|
+
:ApplicationName => @application_name,
|
152
|
+
:ApplicationIcon => application_icon.TIFFRepresentation,
|
153
|
+
:AllNotifications => @notifications,
|
154
|
+
:DefaultNotifications => @default_notifications
|
155
|
+
}
|
156
|
+
|
157
|
+
notification_center.objc_send(
|
158
|
+
:postNotificationName, :GrowlApplicationRegistrationNotification,
|
159
|
+
:object, nil,
|
160
|
+
:userInfo, dict,
|
161
|
+
:deliverImmediately, true
|
162
|
+
)
|
163
|
+
end
|
164
|
+
|
165
|
+
def add_observer(selector, name, prepend_name_and_pid)
|
166
|
+
name = "#{@application_name}-#{pid}-#{name}" if prepend_name_and_pid
|
167
|
+
notification_center.addObserver_selector_name_object self, selector, name, nil
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: growl_notifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Satoshi Nakagawa
|
14
|
+
- Eloy Duran
|
15
|
+
- JP Hastings-Spital
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2010-08-02 00:00:00 +01:00
|
21
|
+
default_executable:
|
22
|
+
dependencies: []
|
23
|
+
|
24
|
+
description: A ruby library which allows you to send Growl notifications.
|
25
|
+
email:
|
26
|
+
- psychs@limechat.net
|
27
|
+
- e.duran@superalloy.nl
|
28
|
+
- jphastings@gmail.com
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files:
|
34
|
+
- LICENSE
|
35
|
+
- README.txt
|
36
|
+
- TODO
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- History.txt
|
40
|
+
- LICENSE
|
41
|
+
- Manifest.txt
|
42
|
+
- README.txt
|
43
|
+
- Rakefile
|
44
|
+
- TODO
|
45
|
+
- VERSION
|
46
|
+
- init.rb
|
47
|
+
- lib/growl_notifier.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/jphastings/growl_notifier
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.3.7
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Growl::Notifier is a OSX RubyCocoa class that allows your application to post notifications to the Growl daemon.
|
82
|
+
test_files: []
|
83
|
+
|