| 1 | ### Copyright |
|---|
| 2 | # |
|---|
| 3 | # Copyright 2004 Thomas Kollbach <dev@bitfever.de> |
|---|
| 4 | # |
|---|
| 5 | # Released under the BSD license. |
|---|
| 6 | # |
|---|
| 7 | ### Description |
|---|
| 8 | # |
|---|
| 9 | # A ruby class that enables posting notifications to the Growl daemon. |
|---|
| 10 | # See <http://growl.info> for more information. |
|---|
| 11 | # |
|---|
| 12 | # Requires RubyCocoa (http://www.fobj.com/rubycocoa/) and Ruby 1.8 |
|---|
| 13 | # (http://ruby-lang.org). |
|---|
| 14 | # |
|---|
| 15 | ### Versions |
|---|
| 16 | # |
|---|
| 17 | # v0.1- 25.11.2004 - Initial version, this is less more then a ruby translation of |
|---|
| 18 | # the python bindings |
|---|
| 19 | # |
|---|
| 20 | # TODO: transform this into a ruby-module, so it is usable as a mixin |
|---|
| 21 | # for ruby-scripts |
|---|
| 22 | # |
|---|
| 23 | ### Usage |
|---|
| 24 | # |
|---|
| 25 | # Here is a short example how to use this in a script |
|---|
| 26 | # |
|---|
| 27 | # n = GrowlNotifier.new('bla',['Foo'],nil,OSX::NSWorkspace.sharedWorkspace().iconForFileType_('unknown')) |
|---|
| 28 | # n.register() |
|---|
| 29 | # |
|---|
| 30 | # n.notify('Foo', 'Test Notification', 'Blah blah blah') |
|---|
| 31 | # |
|---|
| 32 | ### |
|---|
| 33 | |
|---|
| 34 | require 'osx/cocoa' |
|---|
| 35 | |
|---|
| 36 | $priority = {"Very Low" => -2, |
|---|
| 37 | "Moderate" => -1, |
|---|
| 38 | "Normal" => 0, |
|---|
| 39 | "High" => 1, |
|---|
| 40 | "Emergency"=> 2 |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | class GrowlNotifier |
|---|
| 45 | # A class that abstracts the process of registering and posting |
|---|
| 46 | # notifications to the Growl daemon. |
|---|
| 47 | # |
|---|
| 48 | # `appName': The name of the application |
|---|
| 49 | # `notifications': an array of notifications - default is an empty array |
|---|
| 50 | # |
|---|
| 51 | # `defaultNotifications': optional - defaults to the value of |
|---|
| 52 | # `notifications' |
|---|
| 53 | # `appIcon' is also optional but defaults to a senseless icon so |
|---|
| 54 | # so you are higly encouraged to pass it along. |
|---|
| 55 | # |
|---|
| 56 | |
|---|
| 57 | def initialize(appName='GrowlNotifier', notifications=[], defaultNotifications=nil, appIcon=nil) |
|---|
| 58 | @appName = appName |
|---|
| 59 | @notifications = notifications |
|---|
| 60 | @defaultNotifications = defaultNotifications |
|---|
| 61 | @appIcon = appIcon |
|---|
| 62 | end #initialize |
|---|
| 63 | |
|---|
| 64 | def register |
|---|
| 65 | if @appIcon == nil then |
|---|
| 66 | @appIcon = OSX::NSWorkspace.sharedWorkspace().iconForFileType_("txt") |
|---|
| 67 | end |
|---|
| 68 | if @defaultNotifications == nil then |
|---|
| 69 | @defaultNotifications = @notifications |
|---|
| 70 | end |
|---|
| 71 | |
|---|
| 72 | regData = { |
|---|
| 73 | 'ApplicationName'=>@appName, |
|---|
| 74 | 'AllNotifications'=> OSX::NSArray.arrayWithArray(@notifications), |
|---|
| 75 | 'DefaultNotifications'=> OSX::NSArray.arrayWithArray(@defaultNotifications), |
|---|
| 76 | 'ApplicationIcon'=> @appIcon.TIFFRepresentation |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | dict = OSX::NSDictionary.dictionaryWithDictionary(regData) |
|---|
| 80 | notifyCenter = OSX::NSDistributedNotificationCenter.defaultCenter |
|---|
| 81 | |
|---|
| 82 | notifyCenter.postNotificationName_object_userInfo_deliverImmediately_("GrowlApplicationRegistrationNotification", nil, dict, true) |
|---|
| 83 | end #register |
|---|
| 84 | |
|---|
| 85 | def notify(noteType, title, description, icon=nil, appIcon=nil, sticky=false, priority=nil) |
|---|
| 86 | # Post a notification to the Growl daemon. |
|---|
| 87 | # |
|---|
| 88 | # `noteType' is the name of the notification that is being posted. |
|---|
| 89 | # `title' is the user-visible title for this notification. |
|---|
| 90 | # `description' is the user-visible description of this notification. |
|---|
| 91 | # `icon' is an optional icon for this notification. It defaults to |
|---|
| 92 | # `@applicationIcon'. |
|---|
| 93 | # `appIcon' is an optional icon for the sending application. |
|---|
| 94 | # `sticky' is a boolean controlling whether the notification is sticky. |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | @notifications << noteType |
|---|
| 98 | if icon == nil then icon = @appIcon end |
|---|
| 99 | |
|---|
| 100 | notification = {'NotificationName'=> noteType, |
|---|
| 101 | 'ApplicationName'=> @appName, |
|---|
| 102 | 'NotificationTitle'=> title, |
|---|
| 103 | 'NotificationDescription'=> description, |
|---|
| 104 | 'NotificationIcon'=> icon.TIFFRepresentation()} |
|---|
| 105 | |
|---|
| 106 | unless appIcon == nil |
|---|
| 107 | notification['NotificationAppIcon'] = appIcon.TIFFRepresentation |
|---|
| 108 | end |
|---|
| 109 | |
|---|
| 110 | if sticky |
|---|
| 111 | notification['NotificationSticky'] = OSX::NSNumber.numberWithBool_(true) |
|---|
| 112 | end |
|---|
| 113 | |
|---|
| 114 | unless priority == nil |
|---|
| 115 | notification['NotificationPriority'] = OSX::NSNumber.numberWithInt_(priority) |
|---|
| 116 | end |
|---|
| 117 | |
|---|
| 118 | d = OSX::NSDictionary.dictionaryWithDictionary_(notification) |
|---|
| 119 | |
|---|
| 120 | notCenter = OSX::NSDistributedNotificationCenter.defaultCenter() |
|---|
| 121 | notCenter.postNotificationName_object_userInfo_deliverImmediately_('GrowlNotification', nil, d, true) |
|---|
| 122 | |
|---|
| 123 | end #notify |
|---|
| 124 | end #class growlnotifier |
|---|
| 125 | |
|---|