This repository was archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGrowlNotifier.m
197 lines (167 loc) · 6.16 KB
/
GrowlNotifier.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#import "GrowlNotifier.h"
@implementation GrowlNotifier
- (id) initWithAppName:(NSString *)appName
{
applicationName = appName;
if ( (self = [super init]) ) {
/* Tell growl we are going to use this class to hand growl notifications */
[GrowlApplicationBridge setGrowlDelegate:self];
// keeping track of connections and data in case there are concurrent downloads
connectionToInfoMapping = CFDictionaryCreateMutable(kCFAllocatorDefault,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
}
return self;
}
- (id) initWithAppName:(NSString *)appName iconURL:(NSString *)iconURL
{
applicationIcon = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:iconURL]];
return [self initWithAppName:appName];
}
/* Begin methods from GrowlApplicationBridgeDelegate */
- (NSDictionary *) registrationDictionaryForGrowl
{
NSArray *array = [NSArray arrayWithObjects:GN_NOTIFICATION_NAME, nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1],
GROWL_TICKET_VERSION,
array,
GROWL_NOTIFICATIONS_ALL,
array,
GROWL_NOTIFICATIONS_DEFAULT,
applicationName,
GROWL_APP_NAME,
nil];
return dict;
}
- (NSData *) applicationIconDataForGrowl
{
if (applicationIcon)
{
return [applicationIcon TIFFRepresentation];
}
return nil;
}
/* End methods from GrowlApplicationBridgeDelegate */
/*!
* @brief Returns state of growl installation
*
* @return BOOL growl installation state
*/
- (BOOL) isInstalled
{
return [GrowlApplicationBridge isGrowlInstalled];
}
/*!
* @brief Returns the running state of growl
*
* @return BOOL growl running state
*/
- (BOOL) isRunning
{
return [GrowlApplicationBridge isGrowlRunning];
}
/*!
* @brief Shows a growl alert with no icon
*/
-(void) notifyWithTitle:(NSString *)title description:(NSString *)description
{
[GrowlApplicationBridge notifyWithTitle:title
description:description
notificationName:GN_NOTIFICATION_NAME
iconData:nil
priority:0
isSticky:NO
clickContext:nil];
}
/*!
* @brief Shows a growl alert with an NSData icon
*/
-(void) notifyWithTitle:(NSString *)title description:(NSString *)description iconData:(NSData *)icon
{
//NSLog(@"title: %@ desc: %@", title, description);
[GrowlApplicationBridge notifyWithTitle:title
description:description
notificationName:GN_NOTIFICATION_NAME
iconData:icon
priority:0
isSticky:NO
clickContext:nil];
}
/*!
* @brief Shows a growl alert with an icon after downloading it
*
* This method take the url and starts downloading it and only displays
* the growl notification if it successfully downloads it.
* If there are any failures it displays the alert without an icon.
*/
-(void) notifyWithTitle:(NSString *)title description:(NSString *)description iconURL:(NSString *)url
{
//NSLog(@"title: %@ desc: %@ url: %@", title, description, url);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:3.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
{
CFDictionaryAddValue(connectionToInfoMapping,
connection,
[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSMutableData data],
@"receivedData",
title,
@"title",
description,
@"description",
nil]);
}
else
{
[self notifyWithTitle:title description:description];
}
}
/* Begin methods from NSURLConnection delegate */
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
const NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, connection);
[[connectionInfo objectForKey:@"receivedData"] setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
const NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, connection);
[[connectionInfo objectForKey:@"receivedData"] appendData:data];
}
/*!
* @brief Shows a growl alert with no icon on failure
*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
const NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, connection);
[[connectionInfo objectForKey:@"receivedData"] release];
[self notifyWithTitle:[connectionInfo objectForKey:@"title"]
description:[connectionInfo objectForKey:@"description"]];
}
/*!
* @brief Shows a growl alert with with the icon
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
const NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, connection);
[connection release];
NSImage *icon = [[NSImage alloc] initWithData:[connectionInfo objectForKey:@"receivedData"]];
[[connectionInfo objectForKey:@"receivedData"] release];
[self notifyWithTitle:[connectionInfo objectForKey:@"title"]
description:[connectionInfo objectForKey:@"description"]
iconData:[NSData dataWithData:[icon TIFFRepresentation]]];
[icon release];
}
/* End methods from NSURLConnection delegate */
/* Dealloc method */
- (void) dealloc
{
[applicationIcon release];
[super dealloc];
}
@end