blob: 812ebf4709155adb980559a71415dc17db8c0d27 [file] [log] [blame]
Alexander Afanasyev17922872013-10-15 11:45:58 +03001/* -*- Mode: objc; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevb6392e32014-05-12 23:43:50 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California,
Alexander Afanasyev17922872013-10-15 11:45:58 +03004 *
Alexander Afanasyevb6392e32014-05-12 23:43:50 -07005 * 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 Afanasyev17922872013-10-15 11:45:58 +030021 */
22
23/*
24 Potentially useful System Configuration regex patterns:
25
26 (backslash quoting below is only to protect the C comment)
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070027 State:/Network/Interface/.*\/Link
Alexander Afanasyev17922872013-10-15 11:45:58 +030028 State:/Network/Interface/.*\/IPv4
29 State:/Network/Interface/.*\/IPv6
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070030
Alexander Afanasyev17922872013-10-15 11:45:58 +030031 State:/Network/Global/DNS
32 State:/Network/Global/IPv4
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070033
Alexander Afanasyev17922872013-10-15 11:45:58 +030034 Potentially useful notifications from Darwin Notify Center:
Alexander Afanasyevb6392e32014-05-12 23:43:50 -070035
Alexander Afanasyev17922872013-10-15 11:45:58 +030036 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
61static void
62NotificationCenterCallback(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 Afanasyevb6392e32014-05-12 23:43:50 -070077 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
78 selector: @selector(wakeUpNotification:)
Alexander Afanasyev17922872013-10-15 11:45:58 +030079 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 Afanasyevb6392e32014-05-12 23:43:50 -070087
Alexander Afanasyev17922872013-10-15 11:45:58 +030088 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