]> git.saurik.com Git - apple/cf.git/blob - AppServices.subproj/CFUserNotification.c
CF-368.1.tar.gz
[apple/cf.git] / AppServices.subproj / CFUserNotification.c
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* CFUserNotification.c
24 Copyright 2000-2002, Apple, Inc. All rights reserved.
25 Responsibility: Doug Davidson
26 */
27
28 #include <CoreFoundation/CFUserNotification.h>
29 #include <CoreFoundation/CFPropertyList.h>
30 #include <CoreFoundation/CFNumber.h>
31 #include <CoreFoundation/CFMachPort.h>
32 #include <CoreFoundation/CFRunLoop.h>
33 #include "CFInternal.h"
34
35 #define __kCFLogUserNotification 20
36 #define CFUserNotificationLog(alertHeader, alertMessage) CFLog(__kCFLogUserNotification, CFSTR("%@: %@"), alertHeader, alertMessage);
37
38 enum {
39 kCFUserNotificationCancelFlag = (1 << 3),
40 kCFUserNotificationUpdateFlag = (1 << 4)
41 };
42
43 CONST_STRING_DECL(kCFUserNotificationTokenKey, "Token")
44 CONST_STRING_DECL(kCFUserNotificationTimeoutKey, "Timeout")
45 CONST_STRING_DECL(kCFUserNotificationFlagsKey, "Flags")
46 CONST_STRING_DECL(kCFUserNotificationIconPathKey, "IconPath")
47 CONST_STRING_DECL(kCFUserNotificationSoundPathKey, "SoundPath")
48 CONST_STRING_DECL(kCFUserNotificationLocalizationPathKey, "LocalizationPath")
49 CONST_STRING_DECL(kCFUserNotificationAlertSourceKey, "AlertSource")
50 CONST_STRING_DECL(kCFUserNotificationTextFieldLabelsKey, "TextFieldTitles")
51 CONST_STRING_DECL(kCFUserNotificationCheckBoxLabelsKey, "CheckBoxTitles")
52 CONST_STRING_DECL(kCFUserNotificationIconURLKey, "IconURL")
53 CONST_STRING_DECL(kCFUserNotificationSoundURLKey, "SoundURL")
54 CONST_STRING_DECL(kCFUserNotificationLocalizationURLKey, "LocalizationURL")
55 CONST_STRING_DECL(kCFUserNotificationAlertHeaderKey, "AlertHeader")
56 CONST_STRING_DECL(kCFUserNotificationAlertMessageKey, "AlertMessage")
57 CONST_STRING_DECL(kCFUserNotificationDefaultButtonTitleKey, "DefaultButtonTitle")
58 CONST_STRING_DECL(kCFUserNotificationAlternateButtonTitleKey, "AlternateButtonTitle")
59 CONST_STRING_DECL(kCFUserNotificationOtherButtonTitleKey, "OtherButtonTitle")
60 CONST_STRING_DECL(kCFUserNotificationProgressIndicatorValueKey, "ProgressIndicatorValue")
61 CONST_STRING_DECL(kCFUserNotificationSessionIDKey, "SessionID")
62 CONST_STRING_DECL(kCFUserNotificationPopUpTitlesKey, "PopUpTitles")
63 CONST_STRING_DECL(kCFUserNotificationTextFieldTitlesKey, "TextFieldTitles")
64 CONST_STRING_DECL(kCFUserNotificationCheckBoxTitlesKey, "CheckBoxTitles")
65 CONST_STRING_DECL(kCFUserNotificationTextFieldValuesKey, "TextFieldValues")
66 CONST_STRING_DECL(kCFUserNotificationPopUpSelectionKey, "PopUpSelection")
67
68 static CFTypeID __kCFUserNotificationTypeID = _kCFRuntimeNotATypeID;
69
70 struct __CFUserNotification {
71 CFRuntimeBase _base;
72 SInt32 _replyPort;
73 SInt32 _token;
74 CFTimeInterval _timeout;
75 CFOptionFlags _requestFlags;
76 CFOptionFlags _responseFlags;
77 CFStringRef _sessionID;
78 CFDictionaryRef _responseDictionary;
79 #if defined(__MACH__)
80 CFMachPortRef _machPort;
81 #endif
82 CFUserNotificationCallBack _callout;
83 };
84
85 static CFStringRef __CFUserNotificationCopyDescription(CFTypeRef cf) {
86 CFMutableStringRef result;
87 result = CFStringCreateMutable(CFGetAllocator(cf), 0);
88 CFStringAppendFormat(result, NULL, CFSTR("<CFUserNotification 0x%x>"), (UInt32)cf);
89 return result;
90 }
91
92 #if defined(__MACH__)
93
94 #include <stdlib.h>
95 #include <unistd.h>
96 #include <stdio.h>
97 #include <mach/mach.h>
98 #include <mach/error.h>
99 #include <servers/bootstrap.h>
100 #include <limits.h>
101 #include <errno.h>
102
103 #define MAX_STRING_LENGTH PATH_MAX
104 #define MAX_STRING_COUNT 16
105 #define MAX_PORT_NAME_LENGTH 63
106 #define NOTIFICATION_PORT_NAME "com.apple.UNCUserNotification"
107 #define NOTIFICATION_PORT_NAME_OLD "UNCUserNotification"
108 #define NOTIFICATION_PORT_NAME_SUFFIX ".session."
109 #define MESSAGE_TIMEOUT 100
110
111 #endif /* __MACH__ */
112
113 static void __CFUserNotificationDeallocate(CFTypeRef cf);
114
115 static const CFRuntimeClass __CFUserNotificationClass = {
116 0,
117 "CFUserNotification",
118 NULL, // init
119 NULL, // copy
120 __CFUserNotificationDeallocate,
121 NULL, // equal
122 NULL, // hash
123 NULL, //
124 __CFUserNotificationCopyDescription
125 };
126
127 __private_extern__ void __CFUserNotificationInitialize(void) {
128 __kCFUserNotificationTypeID = _CFRuntimeRegisterClass(&__CFUserNotificationClass);
129 }
130
131 CFTypeID CFUserNotificationGetTypeID(void) {
132 return __kCFUserNotificationTypeID;
133 }
134
135 #if defined(__MACH__)
136
137 static void __CFUserNotificationDeallocate(CFTypeRef cf) {
138 CFUserNotificationRef userNotification = (CFUserNotificationRef)cf;
139 if (userNotification->_machPort) {
140 CFMachPortInvalidate(userNotification->_machPort);
141 CFRelease(userNotification->_machPort);
142 } else if (MACH_PORT_NULL != userNotification->_replyPort) {
143 mach_port_destroy(mach_task_self(), userNotification->_replyPort);
144 }
145 if (userNotification->_sessionID) CFRelease(userNotification->_sessionID);
146 if (userNotification->_responseDictionary) CFRelease(userNotification->_responseDictionary);
147 }
148
149 static void _CFUserNotificationAddToDictionary(const void *key, const void *value, void *context) {
150 if (CFGetTypeID(key) == CFStringGetTypeID()) CFDictionarySetValue((CFMutableDictionaryRef)context, key, value);
151 }
152
153 static CFDictionaryRef _CFUserNotificationModifiedDictionary(CFAllocatorRef allocator, CFDictionaryRef dictionary, SInt32 token, SInt32 timeout, CFStringRef source) {
154 CFMutableDictionaryRef md = CFDictionaryCreateMutable(allocator, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
155 CFNumberRef tokenNumber = CFNumberCreate(allocator, kCFNumberSInt32Type, &token);
156 CFNumberRef timeoutNumber = CFNumberCreate(allocator, kCFNumberSInt32Type, &timeout);
157 CFURLRef url = NULL;
158 CFStringRef path = NULL;
159
160 if (dictionary) CFDictionaryApplyFunction(dictionary, _CFUserNotificationAddToDictionary, md);
161 if (source) CFDictionaryAddValue(md, kCFUserNotificationAlertSourceKey, source);
162 if (tokenNumber) {
163 CFDictionaryAddValue(md, kCFUserNotificationTokenKey, tokenNumber);
164 CFRelease(tokenNumber);
165 }
166 if (timeoutNumber) {
167 CFDictionaryAddValue(md, kCFUserNotificationTimeoutKey, timeoutNumber);
168 CFRelease(timeoutNumber);
169 }
170
171 url = CFDictionaryGetValue(md, kCFUserNotificationIconURLKey);
172 if (url && CFGetTypeID((CFTypeRef)url) == CFURLGetTypeID()) {
173 url = CFURLCopyAbsoluteURL(url);
174 CFDictionaryRemoveValue(md, kCFUserNotificationIconURLKey);
175 path = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
176 CFDictionaryAddValue(md, kCFUserNotificationIconPathKey, path);
177 CFRelease(url);
178 CFRelease(path);
179 }
180 url = CFDictionaryGetValue(md, kCFUserNotificationSoundURLKey);
181 if (url && CFGetTypeID((CFTypeRef)url) == CFURLGetTypeID()) {
182 url = CFURLCopyAbsoluteURL(url);
183 CFDictionaryRemoveValue(md, kCFUserNotificationSoundURLKey);
184 path = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
185 CFDictionaryAddValue(md, kCFUserNotificationSoundPathKey, path);
186 CFRelease(url);
187 CFRelease(path);
188 }
189 url = CFDictionaryGetValue(md, kCFUserNotificationLocalizationURLKey);
190 if (url && CFGetTypeID((CFTypeRef)url) == CFURLGetTypeID()) {
191 url = CFURLCopyAbsoluteURL(url);
192 CFDictionaryRemoveValue(md, kCFUserNotificationLocalizationURLKey);
193 path = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
194 CFDictionaryAddValue(md, kCFUserNotificationLocalizationPathKey, path);
195 CFRelease(url);
196 CFRelease(path);
197 }
198 return md;
199 }
200
201 static SInt32 _CFUserNotificationSendRequest(CFAllocatorRef allocator, CFStringRef sessionID, mach_port_t replyPort, SInt32 token, CFTimeInterval timeout, CFOptionFlags flags, CFDictionaryRef dictionary) {
202 CFDictionaryRef modifiedDictionary = NULL;
203 SInt32 retval = ERR_SUCCESS, itimeout = (timeout > 0.0 && timeout < INT_MAX) ? (SInt32)timeout : 0;
204 CFDataRef data;
205 mach_msg_base_t *msg = NULL;
206 mach_port_t bootstrapPort = MACH_PORT_NULL, serverPort = MACH_PORT_NULL;
207 CFIndex size;
208 char namebuffer[MAX_PORT_NAME_LENGTH + 1], oldnamebuffer[MAX_PORT_NAME_LENGTH + 1];
209 size_t namelen;
210
211 strcpy(namebuffer, NOTIFICATION_PORT_NAME);
212 strcpy(oldnamebuffer, NOTIFICATION_PORT_NAME_OLD);
213 if (sessionID) {
214 strcat(namebuffer, NOTIFICATION_PORT_NAME_SUFFIX);
215 namelen = strlen(namebuffer);
216 CFStringGetBytes(sessionID, CFRangeMake(0, CFStringGetLength(sessionID)), kCFStringEncodingUTF8, 0, false, namebuffer + namelen, MAX_PORT_NAME_LENGTH - namelen, &size);
217 namebuffer[namelen + size] = '\0';
218
219 strcat(oldnamebuffer, NOTIFICATION_PORT_NAME_SUFFIX);
220 namelen = strlen(oldnamebuffer);
221 CFStringGetBytes(sessionID, CFRangeMake(0, CFStringGetLength(sessionID)), kCFStringEncodingUTF8, 0, false, oldnamebuffer + namelen, MAX_PORT_NAME_LENGTH - namelen, &size);
222 oldnamebuffer[namelen + size] = '\0';
223 }
224
225 retval = task_get_bootstrap_port(mach_task_self(), &bootstrapPort);
226 if (ERR_SUCCESS == retval && MACH_PORT_NULL != bootstrapPort) retval = bootstrap_look_up(bootstrapPort, namebuffer, &serverPort);
227 if (ERR_SUCCESS != retval || MACH_PORT_NULL == serverPort) retval = bootstrap_look_up(bootstrapPort, oldnamebuffer, &serverPort);
228 if (ERR_SUCCESS == retval && MACH_PORT_NULL != serverPort) {
229 modifiedDictionary = _CFUserNotificationModifiedDictionary(allocator, dictionary, token, itimeout, _CFProcessNameString());
230 if (modifiedDictionary) {
231 data = CFPropertyListCreateXMLData(allocator, modifiedDictionary);
232 if (data) {
233 size = sizeof(mach_msg_base_t) + ((CFDataGetLength(data) + 3) & (~0x3));
234 msg = (mach_msg_base_t *)CFAllocatorAllocate(allocator, size, 0);
235 if (__CFOASafe) __CFSetLastAllocationEventName(msg, "CFUserNotification (temp)");
236 if (msg) {
237 memset(msg, 0, size);
238 msg->header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE);
239 msg->header.msgh_size = size;
240 msg->header.msgh_remote_port = serverPort;
241 msg->header.msgh_local_port = replyPort;
242 msg->header.msgh_id = flags;
243 msg->body.msgh_descriptor_count = 0;
244 CFDataGetBytes(data, CFRangeMake(0, CFDataGetLength(data)), (uint8_t *)msg + sizeof(mach_msg_base_t));
245 //CFShow(CFStringCreateWithBytes(NULL, (UInt8 *)msg + sizeof(mach_msg_base_t), CFDataGetLength(data), kCFStringEncodingUTF8, false));
246 retval = mach_msg((mach_msg_header_t *)msg, MACH_SEND_MSG|MACH_SEND_TIMEOUT, size, 0, MACH_PORT_NULL, MESSAGE_TIMEOUT, MACH_PORT_NULL);
247 CFAllocatorDeallocate(allocator, msg);
248 } else {
249 retval = unix_err(ENOMEM);
250 }
251 CFRelease(data);
252 } else {
253 retval = unix_err(ENOMEM);
254 }
255 CFRelease(modifiedDictionary);
256 } else {
257 retval = unix_err(ENOMEM);
258 }
259 }
260 return retval;
261 }
262
263 CFUserNotificationRef CFUserNotificationCreate(CFAllocatorRef allocator, CFTimeInterval timeout, CFOptionFlags flags, SInt32 *error, CFDictionaryRef dictionary) {
264 CFUserNotificationRef userNotification = NULL;
265 SInt32 retval = ERR_SUCCESS;
266 static uint16_t tokenCounter = 0;
267 SInt32 token = ((getpid()<<16) | (tokenCounter++));
268 CFStringRef sessionID = (dictionary ? CFDictionaryGetValue(dictionary, kCFUserNotificationSessionIDKey) : NULL);
269 mach_port_t replyPort = MACH_PORT_NULL;
270
271 if (!allocator) allocator = __CFGetDefaultAllocator();
272
273 retval = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &replyPort);
274 if (ERR_SUCCESS == retval && MACH_PORT_NULL != replyPort) retval = _CFUserNotificationSendRequest(allocator, sessionID, replyPort, token, timeout, flags, dictionary);
275 if (ERR_SUCCESS == retval) {
276 userNotification = (CFUserNotificationRef)_CFRuntimeCreateInstance(allocator, __kCFUserNotificationTypeID, sizeof(struct __CFUserNotification) - sizeof(CFRuntimeBase), NULL);
277 if (userNotification) {
278 userNotification->_replyPort = replyPort;
279 userNotification->_token = token;
280 userNotification->_timeout = timeout;
281 userNotification->_requestFlags = flags;
282 userNotification->_responseFlags = 0;
283 userNotification->_sessionID = NULL;
284 userNotification->_responseDictionary = NULL;
285 userNotification->_machPort = NULL;
286 userNotification->_callout = NULL;
287 if (sessionID) userNotification->_sessionID = CFStringCreateCopy(allocator, sessionID);
288 } else {
289 retval = unix_err(ENOMEM);
290 }
291 } else {
292 if (dictionary) CFUserNotificationLog(CFDictionaryGetValue(dictionary, kCFUserNotificationAlertHeaderKey), CFDictionaryGetValue(dictionary, kCFUserNotificationAlertMessageKey));
293 }
294 if (ERR_SUCCESS != retval && MACH_PORT_NULL != replyPort) mach_port_destroy(mach_task_self(), replyPort);
295 if (error) *error = retval;
296 return userNotification;
297 }
298
299 static void _CFUserNotificationMachPortCallBack(CFMachPortRef port, void *m, CFIndex size, void *info) {
300 CFUserNotificationRef userNotification = (CFUserNotificationRef)info;
301 mach_msg_base_t *msg = (mach_msg_base_t *)m;
302 CFOptionFlags responseFlags = msg->header.msgh_id;
303 if (msg->header.msgh_size > sizeof(mach_msg_base_t)) {
304 CFDataRef responseData = CFDataCreate(NULL, (uint8_t *)msg + sizeof(mach_msg_base_t), msg->header.msgh_size - sizeof(mach_msg_base_t));
305 if (responseData) {
306 userNotification->_responseDictionary = CFPropertyListCreateFromXMLData(NULL, responseData, kCFPropertyListImmutable, NULL);
307 CFRelease(responseData);
308 }
309 }
310 CFMachPortInvalidate(userNotification->_machPort);
311 CFRelease(userNotification->_machPort);
312 userNotification->_machPort = NULL;
313 mach_port_destroy(mach_task_self(), userNotification->_replyPort);
314 userNotification->_replyPort = MACH_PORT_NULL;
315 userNotification->_callout(userNotification, responseFlags);
316 }
317
318 SInt32 CFUserNotificationReceiveResponse(CFUserNotificationRef userNotification, CFTimeInterval timeout, CFOptionFlags *responseFlags) {
319 SInt32 retval = ERR_SUCCESS;
320 mach_msg_timeout_t msgtime = (timeout > 0.0 && 1000.0 * timeout < INT_MAX) ? (mach_msg_timeout_t)(1000.0 * timeout) : 0;
321 mach_msg_base_t *msg = NULL;
322 CFIndex size = MAX_STRING_COUNT * MAX_STRING_LENGTH;
323 CFDataRef responseData;
324
325 if (userNotification && MACH_PORT_NULL != userNotification->_replyPort) {
326 msg = (mach_msg_base_t *)CFAllocatorAllocate(CFGetAllocator(userNotification), size, 0);
327 if (__CFOASafe) __CFSetLastAllocationEventName(msg, "CFUserNotification (temp)");
328 if (msg) {
329 memset(msg, 0, size);
330 msg->header.msgh_size = size;
331 if (msgtime > 0) {
332 retval = mach_msg((mach_msg_header_t *)msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, size, userNotification->_replyPort, msgtime, MACH_PORT_NULL);
333 } else {
334 retval = mach_msg((mach_msg_header_t *)msg, MACH_RCV_MSG, 0, size, userNotification->_replyPort, 0, MACH_PORT_NULL);
335 }
336 if (ERR_SUCCESS == retval) {
337 if (responseFlags) *responseFlags = msg->header.msgh_id;
338 if (msg->header.msgh_size > sizeof(mach_msg_base_t)) {
339 responseData = CFDataCreate(NULL, (uint8_t *)msg + sizeof(mach_msg_base_t), msg->header.msgh_size - sizeof(mach_msg_base_t));
340 if (responseData) {
341 userNotification->_responseDictionary = CFPropertyListCreateFromXMLData(NULL, responseData, kCFPropertyListImmutable, NULL);
342 CFRelease(responseData);
343 }
344 }
345 if (userNotification->_machPort) {
346 CFMachPortInvalidate(userNotification->_machPort);
347 CFRelease(userNotification->_machPort);
348 userNotification->_machPort = NULL;
349 }
350 mach_port_destroy(mach_task_self(), userNotification->_replyPort);
351 userNotification->_replyPort = MACH_PORT_NULL;
352 }
353 CFAllocatorDeallocate(CFGetAllocator(userNotification), msg);
354 } else {
355 retval = unix_err(ENOMEM);
356 }
357 }
358 return retval;
359 }
360
361 CFStringRef CFUserNotificationGetResponseValue(CFUserNotificationRef userNotification, CFStringRef key, CFIndex idx) {
362 CFStringRef retval = NULL;
363 CFTypeRef value = NULL;
364 if (userNotification && userNotification->_responseDictionary) {
365 value = CFDictionaryGetValue(userNotification->_responseDictionary, key);
366 if (CFGetTypeID(value) == CFStringGetTypeID()) {
367 if (0 == idx) {
368 retval = (CFStringRef)value;
369 }
370 } else if (CFGetTypeID(value) == CFArrayGetTypeID()) {
371 if (0 <= idx && idx < CFArrayGetCount((CFArrayRef)value)) {
372 retval = (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)value, idx);
373 }
374 }
375 }
376 return retval;
377 }
378
379 CFDictionaryRef CFUserNotificationGetResponseDictionary(CFUserNotificationRef userNotification) {
380 return userNotification ? userNotification->_responseDictionary : NULL;
381 }
382
383 SInt32 CFUserNotificationUpdate(CFUserNotificationRef userNotification, CFTimeInterval timeout, CFOptionFlags flags, CFDictionaryRef dictionary) {
384 SInt32 retval = ERR_SUCCESS;
385 if (userNotification && MACH_PORT_NULL != userNotification->_replyPort) {
386 retval = _CFUserNotificationSendRequest(CFGetAllocator(userNotification), userNotification->_sessionID, userNotification->_replyPort, userNotification->_token, timeout, flags|kCFUserNotificationUpdateFlag, dictionary);
387 }
388 return retval;
389 }
390
391 SInt32 CFUserNotificationCancel(CFUserNotificationRef userNotification) {
392 SInt32 retval = ERR_SUCCESS;
393 if (userNotification && MACH_PORT_NULL != userNotification->_replyPort) {
394 retval = _CFUserNotificationSendRequest(CFGetAllocator(userNotification), userNotification->_sessionID, userNotification->_replyPort, userNotification->_token, 0, kCFUserNotificationCancelFlag, NULL);
395 }
396 return retval;
397 }
398
399 CFRunLoopSourceRef CFUserNotificationCreateRunLoopSource(CFAllocatorRef allocator, CFUserNotificationRef userNotification, CFUserNotificationCallBack callout, CFIndex order) {
400 CFRunLoopSourceRef source = NULL;
401 if (userNotification && callout && !userNotification->_machPort && MACH_PORT_NULL != userNotification->_replyPort) {
402 CFMachPortContext context = {0, userNotification, NULL, NULL, NULL};
403 userNotification->_machPort = CFMachPortCreateWithPort(CFGetAllocator(userNotification), (mach_port_t)userNotification->_replyPort, _CFUserNotificationMachPortCallBack, &context, false);
404 }
405 if (userNotification && userNotification->_machPort) {
406 source = CFMachPortCreateRunLoopSource(allocator, userNotification->_machPort, order);
407 userNotification->_callout = callout;
408 }
409 return source;
410 }
411
412 SInt32 CFUserNotificationDisplayNotice(CFTimeInterval timeout, CFOptionFlags flags, CFURLRef iconURL, CFURLRef soundURL, CFURLRef localizationURL, CFStringRef alertHeader, CFStringRef alertMessage, CFStringRef defaultButtonTitle) {
413 CFUserNotificationRef userNotification;
414 SInt32 retval = ERR_SUCCESS;
415 CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
416 if (iconURL) CFDictionaryAddValue(dict, kCFUserNotificationIconURLKey, iconURL);
417 if (soundURL) CFDictionaryAddValue(dict, kCFUserNotificationSoundURLKey, soundURL);
418 if (localizationURL) CFDictionaryAddValue(dict, kCFUserNotificationLocalizationURLKey, localizationURL);
419 if (alertHeader) CFDictionaryAddValue(dict, kCFUserNotificationAlertHeaderKey, alertHeader);
420 if (alertMessage) CFDictionaryAddValue(dict, kCFUserNotificationAlertMessageKey, alertMessage);
421 if (defaultButtonTitle) CFDictionaryAddValue(dict, kCFUserNotificationDefaultButtonTitleKey, defaultButtonTitle);
422 userNotification = CFUserNotificationCreate(NULL, timeout, flags, &retval, dict);
423 if (userNotification) CFRelease(userNotification);
424 CFRelease(dict);
425 return retval;
426 }
427
428 CF_EXPORT SInt32 CFUserNotificationDisplayAlert(CFTimeInterval timeout, CFOptionFlags flags, CFURLRef iconURL, CFURLRef soundURL, CFURLRef localizationURL, CFStringRef alertHeader, CFStringRef alertMessage, CFStringRef defaultButtonTitle, CFStringRef alternateButtonTitle, CFStringRef otherButtonTitle, CFOptionFlags *responseFlags) {
429 CFUserNotificationRef userNotification;
430 SInt32 retval = ERR_SUCCESS;
431 CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
432 if (iconURL) CFDictionaryAddValue(dict, kCFUserNotificationIconURLKey, iconURL);
433 if (soundURL) CFDictionaryAddValue(dict, kCFUserNotificationSoundURLKey, soundURL);
434 if (localizationURL) CFDictionaryAddValue(dict, kCFUserNotificationLocalizationURLKey, localizationURL);
435 if (alertHeader) CFDictionaryAddValue(dict, kCFUserNotificationAlertHeaderKey, alertHeader);
436 if (alertMessage) CFDictionaryAddValue(dict, kCFUserNotificationAlertMessageKey, alertMessage);
437 if (defaultButtonTitle) CFDictionaryAddValue(dict, kCFUserNotificationDefaultButtonTitleKey, defaultButtonTitle);
438 if (alternateButtonTitle) CFDictionaryAddValue(dict, kCFUserNotificationAlternateButtonTitleKey, alternateButtonTitle);
439 if (otherButtonTitle) CFDictionaryAddValue(dict, kCFUserNotificationOtherButtonTitleKey, otherButtonTitle);
440 userNotification = CFUserNotificationCreate(NULL, timeout, flags, &retval, dict);
441 if (userNotification) {
442 retval = CFUserNotificationReceiveResponse(userNotification, timeout, responseFlags);
443 if (MACH_RCV_TIMED_OUT == retval) {
444 retval = CFUserNotificationCancel(userNotification);
445 if (responseFlags) *responseFlags = kCFUserNotificationCancelResponse;
446 }
447 CFRelease(userNotification);
448 }
449 CFRelease(dict);
450 return retval;
451 }
452
453 #else /* __MACH__ */
454
455 #warning CFUserNotification functions not fully implemented
456
457 void __CFUserNotificationDeallocate(CFTypeRef cf) {
458 }
459
460 CFUserNotificationRef CFUserNotificationCreate(CFAllocatorRef allocator, CFTimeInterval timeout, CFOptionFlags flags, SInt32 *error, CFDictionaryRef dictionary) {
461 CFUserNotificationLog(CFDictionaryGetValue(dictionary, kCFUserNotificationAlertHeaderKey), CFDictionaryGetValue(dictionary, kCFUserNotificationAlertMessageKey));
462 return NULL;
463 }
464
465 SInt32 CFUserNotificationReceiveResponse(CFUserNotificationRef userNotification, CFTimeInterval timeout, CFOptionFlags *responseFlags) {
466 return -1;
467 }
468
469 CFDictionaryRef CFUserNotificationCopyResponseDictionary(CFUserNotificationRef userNotification) {
470 return NULL;
471 }
472
473 SInt32 CFUserNotificationUpdate(CFUserNotificationRef userNotification, CFTimeInterval timeout, CFOptionFlags flags, CFDictionaryRef dictionary) {
474 return -1;
475 }
476
477 SInt32 CFUserNotificationCancel(CFUserNotificationRef userNotification) {
478 return -1;
479 }
480
481 CFRunLoopSourceRef CFUserNotificationCreateRunLoopSource(CFAllocatorRef allocator, CFUserNotificationRef userNotification, CFUserNotificationCallBack callout, CFIndex order) {
482 return NULL;
483 }
484
485 SInt32 CFUserNotificationDisplayNotice(CFTimeInterval timeout, CFOptionFlags flags, CFURLRef iconURL, CFURLRef soundURL, CFURLRef localizationURL, CFStringRef alertHeader, CFStringRef alertMessage, CFStringRef defaultButtonTitle) {
486 CFUserNotificationLog(alertHeader, alertMessage);
487 return -1;
488 }
489
490 SInt32 CFUserNotificationDisplayAlert(CFTimeInterval timeout, CFOptionFlags flags, CFURLRef iconURL, CFURLRef soundURL, CFURLRef localizationURL, CFStringRef alertHeader, CFStringRef alertMessage, CFStringRef defaultButtonTitle, CFStringRef alternateButtonTitle, CFStringRef otherButtonTitle, CFOptionFlags *responseFlags) {
491 CFUserNotificationLog(alertHeader, alertMessage);
492 return -1;
493 }
494
495 #endif /* __MACH__ */
496
497 #undef __kCFLogUserNotification
498 #undef CFUserNotificationLog
499 #undef MAX_STRING_LENGTH
500 #undef MAX_STRING_COUNT
501 #undef NOTIFICATION_PORT_NAME
502 #undef NOTIFICATION_PORT_NAME_OLD
503 #undef MESSAGE_TIMEOUT
504