-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSCNotificationManager.m
66 lines (50 loc) · 1.65 KB
/
SCNotificationManager.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
/*
* Written by Theo Hultberg ([email protected]) 2004-03-09 with help from Boaz Stuller.
* This code is in the public domain, provided that this notice remains.
*/
#import "SCNotificationManager.h"
void _SCNotificationCallback(SCDynamicStoreRef store, CFArrayRef changedKeys, void *info)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSEnumerator *keysE = [(NSArray *)changedKeys objectEnumerator];
NSString *key = nil;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
while(key = [keysE nextObject])
{
[nc postNotificationName:key
object:(id)info
userInfo:[(NSDictionary *)SCDynamicStoreCopyValue(store, (CFStringRef) key) autorelease]];
}
[pool release];
}
@implementation SCNotificationManager
- (id)init
{
if(self = [super init])
{
SCDynamicStoreContext context = { 0, (void *)self, NULL, NULL, NULL };
dynStore = SCDynamicStoreCreate(
NULL,
(CFStringRef) [[NSBundle mainBundle] bundleIdentifier],
_SCNotificationCallback,
&context
);
// Add to runloop to receive automatic callbacks to our SCNotificationCallback method
rlSrc = SCDynamicStoreCreateRunLoopSource(NULL, dynStore, 0);
CFRunLoopAddSource([[NSRunLoop currentRunLoop] getCFRunLoop], rlSrc, kCFRunLoopCommonModes);
SCDynamicStoreSetNotificationKeys(
dynStore,
(CFArrayRef) [(NSArray *)SCDynamicStoreCopyKeyList(dynStore, CFSTR(".*")) autorelease],
NULL
);
}
return self;
}
- (void)dealloc
{
CFRunLoopRemoveSource([[NSRunLoop currentRunLoop] getCFRunLoop], rlSrc, kCFRunLoopCommonModes);
if(rlSrc) CFRelease(rlSrc);
if(dynStore) CFRelease(dynStore);
[super dealloc];
}
@end