]> git.saurik.com Git - apple/cf.git/blob - CFUserNotification.c
CF-476.17.tar.gz
[apple/cf.git] / CFUserNotification.c
1 /*
2 * Copyright (c) 2008 Apple 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 (c) 2000-2007 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/CFRunLoop.h>
32 #include "CFInternal.h"
33 #include <CoreFoundation/CFMachPort.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <stdio.h>
37 #include <mach/mach.h>
38 #include <mach/error.h>
39 #include <bootstrap_priv.h>
40 #include <limits.h>
41 #include <errno.h>
42 #include <pthread.h>
43
44
45 #define CFUserNotificationLog(alertHeader, alertMessage) CFLog(3, CFSTR("%@: %@"), alertHeader, alertMessage);
46
47 enum {
48 kCFUserNotificationCancelFlag = (1 << 3),
49 kCFUserNotificationUpdateFlag = (1 << 4)
50 };
51
52 CONST_STRING_DECL(kCFUserNotificationTokenKey, "Token")
53 CONST_STRING_DECL(kCFUserNotificationTimeoutKey, "Timeout")
54 CONST_STRING_DECL(kCFUserNotificationFlagsKey, "Flags")
55 CONST_STRING_DECL(kCFUserNotificationIconPathKey, "IconPath")
56 CONST_STRING_DECL(kCFUserNotificationSoundPathKey, "SoundPath")
57 CONST_STRING_DECL(kCFUserNotificationLocalizationPathKey, "LocalizationPath")
58 CONST_STRING_DECL(kCFUserNotificationAlertSourceKey, "AlertSource")
59 CONST_STRING_DECL(kCFUserNotificationTextFieldLabelsKey, "TextFieldTitles")
60 CONST_STRING_DECL(kCFUserNotificationCheckBoxLabelsKey, "CheckBoxTitles")
61 CONST_STRING_DECL(kCFUserNotificationIconURLKey, "IconURL")
62 CONST_STRING_DECL(kCFUserNotificationSoundURLKey, "SoundURL")
63 CONST_STRING_DECL(kCFUserNotificationLocalizationURLKey, "LocalizationURL")
64 CONST_STRING_DECL(kCFUserNotificationAlertHeaderKey, "AlertHeader")
65 CONST_STRING_DECL(kCFUserNotificationAlertMessageKey, "AlertMessage")
66 CONST_STRING_DECL(kCFUserNotificationDefaultButtonTitleKey, "DefaultButtonTitle")
67 CONST_STRING_DECL(kCFUserNotificationAlternateButtonTitleKey, "AlternateButtonTitle")
68 CONST_STRING_DECL(kCFUserNotificationOtherButtonTitleKey, "OtherButtonTitle")
69 CONST_STRING_DECL(kCFUserNotificationProgressIndicatorValueKey, "ProgressIndicatorValue")
70 CONST_STRING_DECL(kCFUserNotificationSessionIDKey, "SessionID")
71 CONST_STRING_DECL(kCFUserNotificationPopUpTitlesKey, "PopUpTitles")
72 CONST_STRING_DECL(kCFUserNotificationTextFieldTitlesKey, "TextFieldTitles")
73 CONST_STRING_DECL(kCFUserNotificationCheckBoxTitlesKey, "CheckBoxTitles")
74 CONST_STRING_DECL(kCFUserNotificationTextFieldValuesKey, "TextFieldValues")
75 CONST_STRING_DECL(kCFUserNotificationPopUpSelectionKey, "PopUpSelection")
76 CONST_STRING_DECL(kCFUserNotificationKeyboardTypesKey, "KeyboardTypes")
77
78 static CFTypeID __kCFUserNotificationTypeID = _kCFRuntimeNotATypeID;
79
80 struct __CFUserNotification {
81 CFRuntimeBase _base;
82 SInt32 _replyPort;
83 SInt32 _token;
84 CFTimeInterval _timeout;
85 CFOptionFlags _requestFlags;
86 CFOptionFlags _responseFlags;
87 CFStringRef _sessionID;
88 CFDictionaryRef _responseDictionary;
89 CFMachPortRef _machPort;
90 CFUserNotificationCallBack _callout;
91 };
92
93 static CFStringRef __CFUserNotificationCopyDescription(CFTypeRef cf) {
94 CFMutableStringRef result;
95 result = CFStringCreateMutable(CFGetAllocator(cf), 0);
96 CFStringAppendFormat(result, NULL, CFSTR("<CFUserNotification %p>"), cf);
97 return result;
98 }
99
100 #define MAX_STRING_LENGTH PATH_MAX
101 #define MAX_STRING_COUNT 16
102 #define MAX_PORT_NAME_LENGTH 63
103 #define NOTIFICATION_PORT_NAME_SUFFIX ".session."
104 #define MESSAGE_TIMEOUT 100
105 #if DEPLOYMENT_TARGET_MACOSX
106 #define NOTIFICATION_PORT_NAME "com.apple.UNCUserNotification"
107 #elif 0
108 #define NOTIFICATION_PORT_NAME "com.apple.SBUserNotification"
109 #else
110 #error Unknown or unspecified DEPLOYMENT_TARGET
111 #endif
112
113
114 static void __CFUserNotificationDeallocate(CFTypeRef cf);
115
116 static const CFRuntimeClass __CFUserNotificationClass = {
117 0,
118 "CFUserNotification",
119 NULL, // init
120 NULL, // copy
121 __CFUserNotificationDeallocate,
122 NULL, // equal
123 NULL, // hash
124 NULL, //
125 __CFUserNotificationCopyDescription
126 };
127
128 __private_extern__ void __CFUserNotificationInitialize(void) {
129 __kCFUserNotificationTypeID = _CFRuntimeRegisterClass(&__CFUserNotificationClass);
130 }
131
132 CFTypeID CFUserNotificationGetTypeID(void) {
133 if (_kCFRuntimeNotATypeID == __kCFUserNotificationTypeID) __CFUserNotificationInitialize();
134 return __kCFUserNotificationTypeID;
135 }
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];
209 strlcpy(namebuffer, NOTIFICATION_PORT_NAME, sizeof(namebuffer));
210 if (sessionID) {
211 char sessionid[MAX_PORT_NAME_LENGTH + 1];
212 CFIndex len = MAX_PORT_NAME_LENGTH - sizeof(NOTIFICATION_PORT_NAME) - sizeof(NOTIFICATION_PORT_NAME_SUFFIX);
213 CFStringGetBytes(sessionID, CFRangeMake(0, CFStringGetLength(sessionID)), kCFStringEncodingUTF8, 0, false, (uint8_t *)sessionid, len, &size);
214 sessionid[len - 1] = '\0';
215 strlcat(namebuffer, NOTIFICATION_PORT_NAME_SUFFIX, sizeof(namebuffer));
216 strlcat(namebuffer, sessionid, sizeof(namebuffer));
217 }
218
219 retval = task_get_bootstrap_port(mach_task_self(), &bootstrapPort);
220 if (ERR_SUCCESS == retval && MACH_PORT_NULL != bootstrapPort) retval = bootstrap_look_up2(bootstrapPort, namebuffer, &serverPort, 0, 0);
221 if (ERR_SUCCESS == retval && MACH_PORT_NULL != serverPort) {
222 modifiedDictionary = _CFUserNotificationModifiedDictionary(allocator, dictionary, token, itimeout, _CFProcessNameString());
223 if (modifiedDictionary) {
224 data = CFPropertyListCreateXMLData(allocator, modifiedDictionary);
225 if (data) {
226 size = sizeof(mach_msg_base_t) + ((CFDataGetLength(data) + 3) & (~0x3));
227 msg = (mach_msg_base_t *)CFAllocatorAllocate(allocator, size, 0);
228 if (__CFOASafe) __CFSetLastAllocationEventName(msg, "CFUserNotification (temp)");
229 if (msg) {
230 memset(msg, 0, size);
231 msg->header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE);
232 msg->header.msgh_size = size;
233 msg->header.msgh_remote_port = serverPort;
234 msg->header.msgh_local_port = replyPort;
235 msg->header.msgh_id = flags;
236 msg->body.msgh_descriptor_count = 0;
237 CFDataGetBytes(data, CFRangeMake(0, CFDataGetLength(data)), (uint8_t *)msg + sizeof(mach_msg_base_t));
238 //CFShow(CFStringCreateWithBytes(kCFAllocatorSystemDefault, (UInt8 *)msg + sizeof(mach_msg_base_t), CFDataGetLength(data), kCFStringEncodingUTF8, false));
239 retval = mach_msg((mach_msg_header_t *)msg, MACH_SEND_MSG|MACH_SEND_TIMEOUT, size, 0, MACH_PORT_NULL, MESSAGE_TIMEOUT, MACH_PORT_NULL);
240 CFAllocatorDeallocate(allocator, msg);
241 } else {
242 retval = unix_err(ENOMEM);
243 }
244 CFRelease(data);
245 } else {
246 retval = unix_err(ENOMEM);
247 }
248 CFRelease(modifiedDictionary);
249 } else {
250 retval = unix_err(ENOMEM);
251 }
252 }
253 return retval;
254 }
255
256 CFUserNotificationRef CFUserNotificationCreate(CFAllocatorRef allocator, CFTimeInterval timeout, CFOptionFlags flags, SInt32 *error, CFDictionaryRef dictionary) {
257 CHECK_FOR_FORK();
258 CFUserNotificationRef userNotification = NULL;
259 SInt32 retval = ERR_SUCCESS;
260 static uint16_t tokenCounter = 0;
261 SInt32 token = ((getpid()<<16) | (tokenCounter++));
262 CFStringRef sessionID = (dictionary ? CFDictionaryGetValue(dictionary, kCFUserNotificationSessionIDKey) : NULL);
263 mach_port_t replyPort = MACH_PORT_NULL;
264
265 if (!allocator) allocator = __CFGetDefaultAllocator();
266
267 retval = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &replyPort);
268 if (ERR_SUCCESS == retval && MACH_PORT_NULL != replyPort) retval = _CFUserNotificationSendRequest(allocator, sessionID, replyPort, token, timeout, flags, dictionary);
269 if (ERR_SUCCESS == retval) {
270 userNotification = (CFUserNotificationRef)_CFRuntimeCreateInstance(allocator, CFUserNotificationGetTypeID(), sizeof(struct __CFUserNotification) - sizeof(CFRuntimeBase), NULL);
271 if (userNotification) {
272 userNotification->_replyPort = replyPort;
273 userNotification->_token = token;
274 userNotification->_timeout = timeout;
275 userNotification->_requestFlags = flags;
276 userNotification->_responseFlags = 0;
277 userNotification->_sessionID = NULL;
278 userNotification->_responseDictionary = NULL;
279 userNotification->_machPort = NULL;
280 userNotification->_callout = NULL;
281 if (sessionID) userNotification->_sessionID = CFStringCreateCopy(allocator, sessionID);
282 } else {
283 retval = unix_err(ENOMEM);
284 }
285 } else {
286 if (dictionary) CFUserNotificationLog(CFDictionaryGetValue(dictionary, kCFUserNotificationAlertHeaderKey), CFDictionaryGetValue(dictionary, kCFUserNotificationAlertMessageKey));
287 }
288 if (ERR_SUCCESS != retval && MACH_PORT_NULL != replyPort) mach_port_destroy(mach_task_self(), replyPort);
289 if (error) *error = retval;
290 return userNotification;
291 }
292
293 static void _CFUserNotificationMachPortCallBack(CFMachPortRef port, void *m, CFIndex size, void *info) {
294 CFUserNotificationRef userNotification = (CFUserNotificationRef)info;
295 mach_msg_base_t *msg = (mach_msg_base_t *)m;
296 CFOptionFlags responseFlags = msg->header.msgh_id;
297 if (msg->header.msgh_size > sizeof(mach_msg_base_t)) {
298 CFDataRef responseData = CFDataCreate(kCFAllocatorSystemDefault, (uint8_t *)msg + sizeof(mach_msg_base_t), msg->header.msgh_size - sizeof(mach_msg_base_t));
299 if (responseData) {
300 userNotification->_responseDictionary = CFPropertyListCreateFromXMLData(kCFAllocatorSystemDefault, responseData, kCFPropertyListImmutable, NULL);
301 CFRelease(responseData);
302 }
303 }
304 CFMachPortInvalidate(userNotification->_machPort);
305 CFRelease(userNotification->_machPort);
306 userNotification->_machPort = NULL;
307 mach_port_destroy(mach_task_self(), userNotification->_replyPort);
308 userNotification->_replyPort = MACH_PORT_NULL;
309 userNotification->_callout(userNotification, responseFlags);
310 }
311
312 SInt32 CFUserNotificationReceiveResponse(CFUserNotificationRef userNotification, CFTimeInterval timeout, CFOptionFlags *responseFlags) {
313 CHECK_FOR_FORK();
314 SInt32 retval = ERR_SUCCESS;
315 mach_msg_timeout_t msgtime = (timeout > 0.0 && 1000.0 * timeout < INT_MAX) ? (mach_msg_timeout_t)(1000.0 * timeout) : 0;
316 mach_msg_base_t *msg = NULL;
317 CFIndex size = MAX_STRING_COUNT * MAX_STRING_LENGTH;
318 CFDataRef responseData;
319
320 if (userNotification && MACH_PORT_NULL != userNotification->_replyPort) {
321 msg = (mach_msg_base_t *)CFAllocatorAllocate(CFGetAllocator(userNotification), size, 0);
322 if (__CFOASafe) __CFSetLastAllocationEventName(msg, "CFUserNotification (temp)");
323 if (msg) {
324 memset(msg, 0, size);
325 msg->header.msgh_size = size;
326 if (msgtime > 0) {
327 retval = mach_msg((mach_msg_header_t *)msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, size, userNotification->_replyPort, msgtime, MACH_PORT_NULL);
328 } else {
329 retval = mach_msg((mach_msg_header_t *)msg, MACH_RCV_MSG, 0, size, userNotification->_replyPort, 0, MACH_PORT_NULL);
330 }
331 if (ERR_SUCCESS == retval) {
332 if (responseFlags) *responseFlags = msg->header.msgh_id;
333 if (msg->header.msgh_size > sizeof(mach_msg_base_t)) {
334 responseData = CFDataCreate(kCFAllocatorSystemDefault, (uint8_t *)msg + sizeof(mach_msg_base_t), msg->header.msgh_size - sizeof(mach_msg_base_t));
335 if (responseData) {
336 userNotification->_responseDictionary = CFPropertyListCreateFromXMLData(kCFAllocatorSystemDefault, responseData, kCFPropertyListImmutable, NULL);
337 CFRelease(responseData);
338 }
339 }
340 if (userNotification->_machPort) {
341 CFMachPortInvalidate(userNotification->_machPort);
342 CFRelease(userNotification->_machPort);
343 userNotification->_machPort = NULL;
344 }
345 mach_port_destroy(mach_task_self(), userNotification->_replyPort);
346 userNotification->_replyPort = MACH_PORT_NULL;
347 }
348 CFAllocatorDeallocate(CFGetAllocator(userNotification), msg);
349 } else {
350 retval = unix_err(ENOMEM);
351 }
352 }
353 return retval;
354 }
355
356 CFStringRef CFUserNotificationGetResponseValue(CFUserNotificationRef userNotification, CFStringRef key, CFIndex idx) {
357 CHECK_FOR_FORK();
358 CFStringRef retval = NULL;
359 CFTypeRef value = NULL;
360 if (userNotification && userNotification->_responseDictionary) {
361 value = CFDictionaryGetValue(userNotification->_responseDictionary, key);
362 if (CFGetTypeID(value) == CFStringGetTypeID()) {
363 if (0 == idx) {
364 retval = (CFStringRef)value;
365 }
366 } else if (CFGetTypeID(value) == CFArrayGetTypeID()) {
367 if (0 <= idx && idx < CFArrayGetCount((CFArrayRef)value)) {
368 retval = (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)value, idx);
369 }
370 }
371 }
372 return retval;
373 }
374
375 CFDictionaryRef CFUserNotificationGetResponseDictionary(CFUserNotificationRef userNotification) {
376 CHECK_FOR_FORK();
377 return userNotification ? userNotification->_responseDictionary : NULL;
378 }
379
380 SInt32 CFUserNotificationUpdate(CFUserNotificationRef userNotification, CFTimeInterval timeout, CFOptionFlags flags, CFDictionaryRef dictionary) {
381 CHECK_FOR_FORK();
382 SInt32 retval = ERR_SUCCESS;
383 if (userNotification && MACH_PORT_NULL != userNotification->_replyPort) {
384 retval = _CFUserNotificationSendRequest(CFGetAllocator(userNotification), userNotification->_sessionID, userNotification->_replyPort, userNotification->_token, timeout, flags|kCFUserNotificationUpdateFlag, dictionary);
385 }
386 return retval;
387 }
388
389 SInt32 CFUserNotificationCancel(CFUserNotificationRef userNotification) {
390 CHECK_FOR_FORK();
391 SInt32 retval = ERR_SUCCESS;
392 if (userNotification && MACH_PORT_NULL != userNotification->_replyPort) {
393 retval = _CFUserNotificationSendRequest(CFGetAllocator(userNotification), userNotification->_sessionID, userNotification->_replyPort, userNotification->_token, 0, kCFUserNotificationCancelFlag, NULL);
394 }
395 return retval;
396 }
397
398 CFRunLoopSourceRef CFUserNotificationCreateRunLoopSource(CFAllocatorRef allocator, CFUserNotificationRef userNotification, CFUserNotificationCallBack callout, CFIndex order) {
399 CHECK_FOR_FORK();
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 CHECK_FOR_FORK();
414 CFUserNotificationRef userNotification;
415 SInt32 retval = ERR_SUCCESS;
416 CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
417 if (iconURL) CFDictionaryAddValue(dict, kCFUserNotificationIconURLKey, iconURL);
418 if (soundURL) CFDictionaryAddValue(dict, kCFUserNotificationSoundURLKey, soundURL);
419 if (localizationURL) CFDictionaryAddValue(dict, kCFUserNotificationLocalizationURLKey, localizationURL);
420 if (alertHeader) CFDictionaryAddValue(dict, kCFUserNotificationAlertHeaderKey, alertHeader);
421 if (alertMessage) CFDictionaryAddValue(dict, kCFUserNotificationAlertMessageKey, alertMessage);
422 if (defaultButtonTitle) CFDictionaryAddValue(dict, kCFUserNotificationDefaultButtonTitleKey, defaultButtonTitle);
423 userNotification = CFUserNotificationCreate(kCFAllocatorSystemDefault, timeout, flags, &retval, dict);
424 if (userNotification) CFRelease(userNotification);
425 CFRelease(dict);
426 return retval;
427 }
428
429
430 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) {
431 CHECK_FOR_FORK();
432 CFUserNotificationRef userNotification;
433 SInt32 retval = ERR_SUCCESS;
434 CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
435 if (iconURL) CFDictionaryAddValue(dict, kCFUserNotificationIconURLKey, iconURL);
436 if (soundURL) CFDictionaryAddValue(dict, kCFUserNotificationSoundURLKey, soundURL);
437 if (localizationURL) CFDictionaryAddValue(dict, kCFUserNotificationLocalizationURLKey, localizationURL);
438 if (alertHeader) CFDictionaryAddValue(dict, kCFUserNotificationAlertHeaderKey, alertHeader);
439 if (alertMessage) CFDictionaryAddValue(dict, kCFUserNotificationAlertMessageKey, alertMessage);
440 if (defaultButtonTitle) CFDictionaryAddValue(dict, kCFUserNotificationDefaultButtonTitleKey, defaultButtonTitle);
441 if (alternateButtonTitle) CFDictionaryAddValue(dict, kCFUserNotificationAlternateButtonTitleKey, alternateButtonTitle);
442 if (otherButtonTitle) CFDictionaryAddValue(dict, kCFUserNotificationOtherButtonTitleKey, otherButtonTitle);
443 userNotification = CFUserNotificationCreate(kCFAllocatorSystemDefault, timeout, flags, &retval, dict);
444 if (userNotification) {
445 retval = CFUserNotificationReceiveResponse(userNotification, timeout, responseFlags);
446 if (MACH_RCV_TIMED_OUT == retval) {
447 retval = CFUserNotificationCancel(userNotification);
448 if (responseFlags) *responseFlags = kCFUserNotificationCancelResponse;
449 }
450 CFRelease(userNotification);
451 }
452 CFRelease(dict);
453 return retval;
454 }
455
456 #undef MAX_STRING_LENGTH
457 #undef MAX_STRING_COUNT
458 #undef NOTIFICATION_PORT_NAME
459 #undef MESSAGE_TIMEOUT
460 #undef MAX_PORT_NAME_LENGTH
461 #undef NOTIFICATION_PORT_NAME_SUFFIX
462