-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathGeneratorAutoSetterRootListController.m
60 lines (51 loc) · 2.09 KB
/
GeneratorAutoSetterRootListController.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
#import <Preferences/PSListController.h>
#import <Preferences/PSSpecifier.h>
UIAlertController *alert(NSString *alertTitle, NSString *alertMessage, NSString *actionTitle) {
UIAlertController *theAlert = [UIAlertController alertControllerWithTitle:alertTitle message:alertMessage preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
[theAlert addAction:defaultAction];
return theAlert;
}
bool vaildGenerator(NSString *generator) {
if ([generator length] != 18 || [generator characterAtIndex:0] != '0' || [generator characterAtIndex:1] != 'x') {
return false;
}
for (int i = 2; i <= 17; i++) {
if (!isxdigit([generator characterAtIndex:i])) {
return false;
}
}
return true;
}
@interface GeneratorAutoSetterRootListController : PSListController
@end
@implementation GeneratorAutoSetterRootListController
- (NSArray *)specifiers {
if (!_specifiers) {
_specifiers = [self loadSpecifiersFromPlistName:@"Root" target:self];
}
return _specifiers;
}
- (void)setPreferenceValue:(id)value specifier:(PSSpecifier*)specifier {
if ([[specifier propertyForKey:@"key"] isEqualToString:@"generator"]) {
if (!vaildGenerator(value)) {
[self presentViewController:alert(@"setgenerator", [NSString stringWithFormat:@"Wrong generator \"%@\":\nFormat error!", value], @"OK") animated:YES completion:nil];
return;
}
}
[super setPreferenceValue:value specifier:specifier];
}
-(void)setgenerator {
[self.view endEditing:YES];
NSMutableString *alertMessage = [[NSMutableString alloc] init];
FILE *fp = popen("setgenerator", "r");
char buffer = fgetc(fp);
while (!feof(fp)) {
[alertMessage appendFormat:@"%c", buffer];
buffer = fgetc(fp);
}
pclose(fp);
[alertMessage deleteCharactersInRange:NSMakeRange([alertMessage length] - 1, 1)];
[self presentViewController:alert(@"setgenerator", alertMessage, @"OK") animated:YES completion:nil];
}
@end