forked from dustinrue/ControlPlane
/
AttachedPowerAdapterEvidenceSource.m
143 lines (112 loc) · 4.67 KB
/
AttachedPowerAdapterEvidenceSource.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// AttachedPowerAdapterEvidenceSource.m
// ControlPlane
//
// Created by Dustin Rue on 8/27/12.
//
// IMPORTANT: This code is intended to be compiled for the ARC mode
//
#import <IOKit/ps/IOPowerSources.h>
#import <IOKit/ps/IOPSKeys.h>
#import "AttachedPowerAdapterEvidenceSource.h"
#import "DSLogger.h"
#import "CPSystemInfo.h"
#import "CPNotifications.h"
@interface AttachedPowerAdapterEvidenceSource ()
@property (strong,atomic) NSNumber *attachedPowerAdapter;
@end
@implementation AttachedPowerAdapterEvidenceSource
- (NSString *)description {
return NSLocalizedString(@"Create a rules based on what power adapter"
" is currently connected to your portable mac based on its serial number", @"");
}
- (void)doFullUpdate:(NSNotification *)notification {
NSNumber *serialNumber = nil;
CFDictionaryRef powerAdapterInfo = IOPSCopyExternalPowerAdapterDetails();
if (powerAdapterInfo) {
CFNumberRef serialNumberRef = CFDictionaryGetValue(powerAdapterInfo, CFSTR(kIOPSPowerAdapterSerialNumberKey));
serialNumber = [((__bridge NSNumber *) serialNumberRef) copy];
if (serialNumber == nil) {
DSLog(@"WARNING: Power adapter details are available, but don't contain the adapter's serial number.");
}
CFRelease(powerAdapterInfo);
}
else {
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
if (blob) {
NSString *powerSourceType = (__bridge NSString *) IOPSGetProvidingPowerSourceType(blob);
if ([powerSourceType isEqualToString:@kIOPSACPowerValue]) {
DSLog(@"WARNING: System is on AC power, but cannot provide power adapter details"
" due to an internal error.");
NSString *title = NSLocalizedString(@"Cannot get power adapter S/N",
@"Title to warn users on failed attempts to get power adapter S/N");
NSString *msg = NSLocalizedString(@"It is an internal system error."
" Try to re-plug the MagSafe connector.",
@"Shown when CP cannot get details about the attached power adapter");
[CPNotifications postUserNotification:title withMessage:msg];
}
CFRelease(blob);
}
else {
DSLog(@"WARNING: Failed to get a copy of power sources info.");
}
}
NSNumber *attachedPowerAdapter = self.attachedPowerAdapter;
if (!attachedPowerAdapter || ![serialNumber isEqualToNumber:attachedPowerAdapter]) {
self.attachedPowerAdapter = serialNumber;
[self setDataCollected:(serialNumber != nil)];
if (notification) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"evidenceSourceDataDidChange" object:nil];
}
}
}
- (void)start {
if (running) {
return;
}
running = YES;
[self doFullUpdate:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doFullUpdate:)
name:@"powerAdapterDidChangeNotification"
object:nil];
}
- (void)stop {
if (!running) {
return;
}
// remove notifications
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"powerAdapterDidChangeNotification"
object:nil];
self.attachedPowerAdapter = nil;
[self setDataCollected:NO];
running = NO;
}
- (NSString *)name {
return @"AttachedPowerAdapter";
}
- (BOOL)doesRuleMatch:(NSDictionary *)rule {
NSNumber *attachedPowerAdapter = self.attachedPowerAdapter;
return (attachedPowerAdapter && [((NSNumber *) rule[@"parameter"]) isEqualToNumber:attachedPowerAdapter]);
}
- (NSString *)getSuggestionLeadText:(NSString *)type {
return NSLocalizedString(@"The following application is active", @"In rule-adding dialog");
}
- (NSArray *)getSuggestions {
[self doFullUpdate:nil];
NSNumber *serialNum = self.attachedPowerAdapter;
NSString *descr = [NSString stringWithFormat:NSLocalizedString(@"Power adapter with serial: %@", @""), serialNum];
NSArray *array = @[ @{ @"type": @"AttachedPowerAdapter", @"parameter": serialNum, @"description": descr } ];
#ifdef DEBUG_MODE
DSLog(@"stuff %@", array);
#endif
return array;
}
- (NSString *)friendlyName {
return NSLocalizedString(@"Attached Power Adapter", @"");
}
+ (BOOL)isEvidenceSourceApplicableToSystem {
return [CPSystemInfo isPortable];
}
@end