-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQSBasicEventTriggers.m
92 lines (85 loc) · 3.41 KB
/
QSBasicEventTriggers.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
//
// QSBasicEventTriggers.m
// QSEventTriggersPlugIn
//
// Created by Nicholas Jitkoff on 1/22/05.
//
#import "QSBasicEventTriggers.h"
@implementation QSBasicEventTriggers
-(id)init{
if (self=[super init]){
wsmap = @{
@"QSWorkspaceWillSleepEvent": NSWorkspaceWillSleepNotification,
@"QSWorkspaceDidWakeEvent": NSWorkspaceDidWakeNotification,
@"QSWorkspaceWillPowerOffEvent": NSWorkspaceWillPowerOffNotification,
@"QSWorkspaceSessionDidResignActiveEvent": NSWorkspaceSessionDidResignActiveNotification,
@"QSWorkspaceSessionDidBecomeActiveEvent": NSWorkspaceSessionDidBecomeActiveNotification,
@"QSActiveSpaceChangedEvent": NSWorkspaceActiveSpaceDidChangeNotification,
@"QSWorkspaceDidMountEvent": NSWorkspaceDidMountNotification,
@"QSWorkspaceWillUnmountEvent": NSWorkspaceWillUnmountNotification,
@"QSWorkspaceDidUnmountEvent": NSWorkspaceDidUnmountNotification,
};
distmap = @{
@"QSEthernetConfigurationChanged": @"com.apple.internetconfignotification",
@"QSScreensaverStartedEvent": @"com.apple.screensaver.didstart",
@"QSScreensaverStoppedEvent": @"com.apple.screensaver.didstop",
@"QSExternalDisplayChanged": @"com.apple.BezelServices.BMDisplayHWReconfiguredEvent",
};
}
return self;
}
- (void)addObserverForEvent:(NSString *)event trigger:(QSTrigger *)trigger
{
if ([wsmap objectForKey:event]) {
NSNotificationCenter *wc = [[NSWorkspace sharedWorkspace] notificationCenter];
[wc addObserver:self selector:@selector(handleWorkspaceNotification:) name:[wsmap objectForKey:event] object:nil];
}
if ([distmap objectForKey:event]) {
NSDistributedNotificationCenter *dc = [NSDistributedNotificationCenter defaultCenter];
[dc addObserver:self selector:@selector(handleSystemNotification:) name:[distmap objectForKey:event] object:nil];
}
}
- (void)removeObserverForEvent:(NSString *)event trigger:(QSTrigger *)trigger
{
if ([wsmap objectForKey:event]) {
NSNotificationCenter *wc = [[NSWorkspace sharedWorkspace] notificationCenter];
[wc removeObserver:self name:[wsmap objectForKey:event] object:nil];
}
if ([distmap objectForKey:event]) {
NSDistributedNotificationCenter *dc = [NSDistributedNotificationCenter defaultCenter];
[dc removeObserver:self name:[distmap objectForKey:event] object:nil];
}
}
-(void)handleWorkspaceNotification:(NSNotification *)notif{
NSString *name = [self nameForEvent:[notif name]];
if (!name) {
return;
}
id argument = nil;
NSString *path = [[notif userInfo] objectForKey:@"NSDevicePath"];
if (path) {
if ([path isEqualToString:@"/Network"]) {
return;
}
argument = [QSObject fileObjectWithPath:path];
}
[[QSEventTriggerManager sharedInstance] handleTriggerEvent:name withObject:argument];
}
- (void)handleSystemNotification:(NSNotification *)notif
{
NSString *name = [self nameForEvent:[notif name]];
if (!name) {
return;
}
[[QSEventTriggerManager sharedInstance] handleTriggerEvent:name withObject:nil];
}
- (NSString *)nameForEvent:(NSString *)inName
{
NSArray *keyList = [[distmap allKeysForObject:inName] arrayByAddingObjectsFromArray:[wsmap allKeysForObject:inName]];
if ([keyList count]) {
// should only be one
return [keyList objectAtIndex:0];
}
return nil;
}
@end