Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 1 | /* -*- Mode: objc; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * @copyright See LICENCE for copyright and license information. |
| 4 | * |
| 5 | * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 6 | * @author Ilya Moiseenko <iliamo@ucla.edu> |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | Potentially useful System Configuration regex patterns: |
| 11 | |
| 12 | (backslash quoting below is only to protect the C comment) |
| 13 | State:/Network/Interface/.*\/Link |
| 14 | State:/Network/Interface/.*\/IPv4 |
| 15 | State:/Network/Interface/.*\/IPv6 |
| 16 | |
| 17 | State:/Network/Global/DNS |
| 18 | State:/Network/Global/IPv4 |
| 19 | |
| 20 | Potentially useful notifications from Darwin Notify Center: |
| 21 | |
| 22 | com.apple.system.config.network_change |
| 23 | */ |
| 24 | |
| 25 | #import "system-events.h" |
| 26 | #import "menu-delegate.h" |
| 27 | |
| 28 | #include <CoreFoundation/CoreFoundation.h> |
| 29 | #include <SystemConfiguration/SystemConfiguration.h> |
| 30 | |
| 31 | @implementation SystemEvents |
| 32 | |
| 33 | -(void)scheduleDaemonRestart |
| 34 | { |
| 35 | [NSTimer scheduledTimerWithTimeInterval: 3.0 |
| 36 | target: (MenuDelegate*)[[NSApplication sharedApplication] delegate] |
| 37 | selector:@selector(restartDaemon:) |
| 38 | userInfo: nil |
| 39 | repeats:NO]; |
| 40 | } |
| 41 | |
| 42 | - (void)wakeUpNotification:(NSNotification*) note |
| 43 | { |
| 44 | [self scheduleDaemonRestart]; |
| 45 | } |
| 46 | |
| 47 | static void |
| 48 | NotificationCenterCallback(CFNotificationCenterRef center, |
| 49 | void *observer, |
| 50 | CFStringRef name, |
| 51 | const void *object, |
| 52 | CFDictionaryRef userInfo) |
| 53 | { |
| 54 | [(__bridge SystemEvents*)observer scheduleDaemonRestart]; |
| 55 | } |
| 56 | |
| 57 | -(id)init |
| 58 | { |
| 59 | if (![super init]) { |
| 60 | return nil; |
| 61 | } |
| 62 | |
| 63 | [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self |
| 64 | selector: @selector(wakeUpNotification:) |
| 65 | name: NSWorkspaceDidWakeNotification object: NULL]; |
| 66 | |
| 67 | CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 68 | (__bridge void*)self, |
| 69 | NotificationCenterCallback, |
| 70 | CFSTR ("com.apple.system.config.network_change"), // name of notification |
| 71 | NULL, // object to observe |
| 72 | NSNotificationSuspensionBehaviorDeliverImmediately); |
| 73 | |
| 74 | return self; |
| 75 | } |
| 76 | |
| 77 | -(void)disable |
| 78 | { |
| 79 | [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; |
| 80 | |
| 81 | CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 82 | (__bridge void*)self); |
| 83 | } |
| 84 | |
| 85 | @end |