Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 1 | /* -*- Mode: objc; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame] | 2 | /** |
| 3 | * Copyright (c) 2013-2014, Regents of the University of California, |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 4 | * |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame] | 5 | * This file is part of NFD Control Center. See AUTHORS.md for complete list of NFD |
| 6 | * authors and contributors. |
| 7 | * |
| 8 | * NFD Control Center is free software: you can redistribute it and/or modify it under the |
| 9 | * terms of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NFD Control Center is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 14 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with NFD |
| 17 | * Control Center, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * \author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 20 | * \author Ilya Moiseenko <http://ilyamoiseenko.com/> |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | /* |
| 24 | Potentially useful System Configuration regex patterns: |
| 25 | |
| 26 | (backslash quoting below is only to protect the C comment) |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame] | 27 | State:/Network/Interface/.*\/Link |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 28 | State:/Network/Interface/.*\/IPv4 |
| 29 | State:/Network/Interface/.*\/IPv6 |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 31 | State:/Network/Global/DNS |
| 32 | State:/Network/Global/IPv4 |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 34 | Potentially useful notifications from Darwin Notify Center: |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 36 | com.apple.system.config.network_change |
| 37 | */ |
| 38 | |
| 39 | #import "system-events.h" |
| 40 | #import "menu-delegate.h" |
| 41 | |
| 42 | #include <CoreFoundation/CoreFoundation.h> |
| 43 | #include <SystemConfiguration/SystemConfiguration.h> |
| 44 | |
| 45 | @implementation SystemEvents |
| 46 | |
| 47 | -(void)scheduleDaemonRestart |
| 48 | { |
| 49 | [NSTimer scheduledTimerWithTimeInterval: 3.0 |
| 50 | target: (MenuDelegate*)[[NSApplication sharedApplication] delegate] |
| 51 | selector:@selector(restartDaemon:) |
| 52 | userInfo: nil |
| 53 | repeats:NO]; |
| 54 | } |
| 55 | |
| 56 | - (void)wakeUpNotification:(NSNotification*) note |
| 57 | { |
| 58 | [self scheduleDaemonRestart]; |
| 59 | } |
| 60 | |
| 61 | static void |
| 62 | NotificationCenterCallback(CFNotificationCenterRef center, |
| 63 | void *observer, |
| 64 | CFStringRef name, |
| 65 | const void *object, |
| 66 | CFDictionaryRef userInfo) |
| 67 | { |
| 68 | [(__bridge SystemEvents*)observer scheduleDaemonRestart]; |
| 69 | } |
| 70 | |
| 71 | -(id)init |
| 72 | { |
| 73 | if (![super init]) { |
| 74 | return nil; |
| 75 | } |
| 76 | |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame] | 77 | [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self |
| 78 | selector: @selector(wakeUpNotification:) |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 79 | name: NSWorkspaceDidWakeNotification object: NULL]; |
| 80 | |
| 81 | CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 82 | (__bridge void*)self, |
| 83 | NotificationCenterCallback, |
| 84 | CFSTR ("com.apple.system.config.network_change"), // name of notification |
| 85 | NULL, // object to observe |
| 86 | NSNotificationSuspensionBehaviorDeliverImmediately); |
Alexander Afanasyev | b6392e3 | 2014-05-12 23:43:50 -0700 | [diff] [blame] | 87 | |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 88 | return self; |
| 89 | } |
| 90 | |
| 91 | -(void)disable |
| 92 | { |
| 93 | [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; |
| 94 | |
| 95 | CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 96 | (__bridge void*)self); |
| 97 | } |
| 98 | |
| 99 | @end |