-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSNotifications_ColloquyAdditions.m
88 lines (69 loc) · 4.3 KB
/
NSNotifications_ColloquyAdditions.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
//
// NSNotification_ColloquyAdditions.m
// Journler
//
// Created by Phil Dow on 1/19/07.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
// From the Colloquy Source Code: http://colloquy.info/
#import <SproutedUtilities/NSNotifications_ColloquyAdditions.h>
#import <pthread.h>
@implementation NSNotificationCenter (NSNotificationCenterAdditions)
- (void) postNotificationOnMainThread:(NSNotification *) notification {
if( pthread_main_np() ) return [self postNotification:notification];
[self postNotificationOnMainThread:notification waitUntilDone:NO];
}
- (void) postNotificationOnMainThread:(NSNotification *) notification waitUntilDone:(BOOL) wait {
if( pthread_main_np() ) return [self postNotification:notification];
[[self class] performSelectorOnMainThread:@selector( _postNotification: ) withObject:notification waitUntilDone:wait];
}
+ (void) _postNotification:(NSNotification *) notification {
[[self defaultCenter] postNotification:notification];
}
- (void) postNotificationOnMainThreadWithName:(NSString *) name object:(id) object {
if( pthread_main_np() ) return [self postNotificationName:name object:object userInfo:nil];
[self postNotificationOnMainThreadWithName:name object:object userInfo:nil waitUntilDone:NO];
}
- (void) postNotificationOnMainThreadWithName:(NSString *) name object:(id) object userInfo:(NSDictionary *) userInfo {
if( pthread_main_np() ) return [self postNotificationName:name object:object userInfo:userInfo];
[self postNotificationOnMainThreadWithName:name object:object userInfo:userInfo waitUntilDone:NO];
}
- (void) postNotificationOnMainThreadWithName:(NSString *) name object:(id) object userInfo:(NSDictionary *) userInfo waitUntilDone:(BOOL) wait {
if( pthread_main_np() ) return [self postNotificationName:name object:object userInfo:userInfo];
NSMutableDictionary *info = [[NSMutableDictionary allocWithZone:nil] initWithCapacity:3];
if( name ) [info setObject:name forKey:@"name"];
if( object ) [info setObject:object forKey:@"object"];
if( userInfo ) [info setObject:userInfo forKey:@"userInfo"];
[[self class] performSelectorOnMainThread:@selector( _postNotificationName: ) withObject:info waitUntilDone:wait];
}
+ (void) _postNotificationName:(NSDictionary *) info {
NSString *name = [info objectForKey:@"name"];
id object = [info objectForKey:@"object"];
NSDictionary *userInfo = [info objectForKey:@"userInfo"];
[[self defaultCenter] postNotificationName:name object:object userInfo:userInfo];
[info release];
}
@end
@implementation NSNotificationQueue (NSNotificationQueueAdditions)
- (void) enqueueNotificationOnMainThread:(NSNotification *) notification postingStyle:(NSPostingStyle) postingStyle {
if( pthread_main_np() ) return [self enqueueNotification:notification postingStyle:postingStyle coalesceMask:( NSNotificationCoalescingOnName | NSNotificationCoalescingOnSender ) forModes:nil];
[self enqueueNotificationOnMainThread:notification postingStyle:postingStyle coalesceMask:( NSNotificationCoalescingOnName | NSNotificationCoalescingOnSender ) forModes:nil];
}
- (void) enqueueNotificationOnMainThread:(NSNotification *) notification postingStyle:(NSPostingStyle) postingStyle coalesceMask:(unsigned) coalesceMask forModes:(NSArray *) modes {
if( pthread_main_np() ) return [self enqueueNotification:notification postingStyle:postingStyle coalesceMask:coalesceMask forModes:modes];
NSMutableDictionary *info = [[NSMutableDictionary allocWithZone:nil] initWithCapacity:4];
if( notification ) [info setObject:notification forKey:@"notification"];
[info setObject:[NSNumber numberWithUnsignedInt:postingStyle] forKey:@"postingStyle"];
[info setObject:[NSNumber numberWithUnsignedInt:coalesceMask] forKey:@"coalesceMask"];
if( modes ) [info setObject:modes forKey:@"modes"];
[[self class] performSelectorOnMainThread:@selector( _enqueueNotification: ) withObject:info waitUntilDone:NO];
}
+ (void) _enqueueNotification:(NSDictionary *) info {
NSNotification *notification = [info objectForKey:@"notification"];
NSPostingStyle postingStyle = [[info objectForKey:@"postingStyle"] unsignedIntValue];
unsigned coalesceMask = [[info objectForKey:@"coalesceMask"] unsignedIntValue];
NSArray *modes = [info objectForKey:@"modes"];
[[self defaultQueue] enqueueNotification:notification postingStyle:postingStyle coalesceMask:coalesceMask forModes:modes];
[info release];
}
@end