|
| 1 | +/* |
| 2 | +# Copyright (c) 2016 Jeremy Kister |
| 3 | +# http://jeremy.kister.net./ |
| 4 | +# Released under the Artistic License 2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +#import <Preferences/Preferences.h> |
| 8 | +#import <Preferences/PSSpecifier.h> |
| 9 | + |
| 10 | +#define DEBUG 0 |
| 11 | +#define BITCOIN @"bitcoin://17nWouHyEueDiRf2vXA17Dpa9kMCXsmNVg" |
| 12 | +#define COINBASE @"https://www.coinbase.com/checkouts/56e137721a33709b4d0efceb335f35d9" |
| 13 | +# define PAYPAL @"https://www.paypal.com/myaccount/transfer/send/[email protected]&amount=¤cyCode=USD&payment_type=Gift" |
| 14 | + |
| 15 | +#define TIMEOUT_FILE @"/etc/apt/apt.conf.d/99APTTimeout" |
| 16 | + |
| 17 | +#if DEBUG |
| 18 | +# define debug(fmt, ...) NSLog((@"%s @ %d " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) |
| 19 | +#else |
| 20 | +# define debug(...) |
| 21 | +#endif |
| 22 | + |
| 23 | +@interface APTTimeoutListController : PSListController |
| 24 | +-(id)readPreferenceValue:(PSSpecifier*)specifier; |
| 25 | +-(void)setPreferenceValue:(id)value specifier:(PSSpecifier*)specifier; |
| 26 | +@end |
| 27 | + |
| 28 | +@interface AuthorListController : PSListController |
| 29 | +@end |
| 30 | + |
| 31 | +@implementation AuthorListController |
| 32 | +@end |
| 33 | + |
| 34 | +@interface iPopup:NSObject |
| 35 | +-(void) title:(NSString *)UITitle message:(NSString *)UIMessage; |
| 36 | +@end |
| 37 | + |
| 38 | +@implementation iPopup |
| 39 | +-(void) title:(NSString *)UITitle message:(NSString *)UIMessage { |
| 40 | + UIAlertView *alert = [ |
| 41 | + [UIAlertView alloc] |
| 42 | + initWithTitle:UITitle |
| 43 | + message:UIMessage |
| 44 | + delegate:self |
| 45 | + cancelButtonTitle:@"OK" |
| 46 | + otherButtonTitles:nil |
| 47 | + ]; |
| 48 | + [alert show]; |
| 49 | + [alert release]; |
| 50 | +} |
| 51 | +@end |
| 52 | + |
| 53 | +@implementation APTTimeoutListController |
| 54 | +- (NSArray *)specifiers { |
| 55 | + if (! _specifiers) { |
| 56 | + _specifiers = [[self loadSpecifiersFromPlistName:@"APTTimeout" target:self] retain]; |
| 57 | + } |
| 58 | + return _specifiers; |
| 59 | +} |
| 60 | + |
| 61 | +- (id)readPreferenceValue:(PSSpecifier*)specifier { |
| 62 | + NSString *path = [NSString stringWithFormat:@"/User/Library/Preferences/%@.plist", specifier.properties[@"defaults"]]; |
| 63 | + debug(@"path is: %@", path); |
| 64 | + NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:path]; |
| 65 | + NSString *value = (settings[specifier.properties[@"key"]]) ?: |
| 66 | + specifier.properties[@"default"] ; |
| 67 | + debug(@"returning: %@", value); |
| 68 | + return value; |
| 69 | +} |
| 70 | + |
| 71 | +- (void)setPreferenceValue:(id)value specifier:(PSSpecifier*)specifier { |
| 72 | + NSString *path = [NSString stringWithFormat:@"/User/Library/Preferences/%@.plist", specifier.properties[@"defaults"]]; |
| 73 | + NSMutableDictionary *defaults = [NSMutableDictionary dictionary]; |
| 74 | + [defaults addEntriesFromDictionary:[NSDictionary dictionaryWithContentsOfFile:path]]; |
| 75 | + debug(@"%@", specifier.properties[@"key"]); |
| 76 | + [defaults setObject:value forKey:specifier.properties[@"key"]]; |
| 77 | + [defaults writeToFile:path atomically:YES]; |
| 78 | + CFStringRef notificationName = (CFStringRef)specifier.properties[@"PostNotification"]; |
| 79 | + if (notificationName) { |
| 80 | + CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), notificationName, NULL, NULL, YES); |
| 81 | + } |
| 82 | + |
| 83 | +#if DEBUG |
| 84 | +# NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:path]; |
| 85 | +# debug(@"Settings: %@", settings); |
| 86 | +#endif |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | +-(void)repo { |
| 91 | + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"cydia://url/https://cydia.saurik.com/api/share#?source=http://kister.net/cydia/"]]; |
| 92 | +} |
| 93 | + |
| 94 | +-(void)twitter { |
| 95 | + if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"twitter://user?screen_name=jkister"]]) { |
| 96 | + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"twitter://user?screen_name=jkister"]]; |
| 97 | + }else{ |
| 98 | + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://twitter.com/jkister"]]; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +-(void)reddit { |
| 103 | + if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"alienblue://r/apttimeout"]]) { |
| 104 | + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"alienblue://r/apttimeout"]]; |
| 105 | + }else{ |
| 106 | + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://reddit.com/r/apttimeout"]]; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +-(void)paypal { |
| 111 | + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:PAYPAL]]; |
| 112 | +} |
| 113 | + |
| 114 | +-(void)bitcoin { |
| 115 | + if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:BITCOIN]]) { |
| 116 | + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:BITCOIN]]; |
| 117 | + }else{ |
| 118 | + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:COINBASE]]; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +-(void)apply { |
| 123 | + |
| 124 | + /* this should eventually be a darwin hook |
| 125 | + https://github.com/angelXwind/PreferenceOrganizer2 */ |
| 126 | + |
| 127 | + NSString *http = nil; |
| 128 | + NSString *ftp = nil; |
| 129 | + NSMutableArray *specifiers = [[NSMutableArray alloc] initWithArray:((PSListController *)self).specifiers]; |
| 130 | + for (int i=0; i < [specifiers count]; i++ ){ |
| 131 | + PSSpecifier *item = [specifiers objectAtIndex:i]; |
| 132 | + |
| 133 | + if ([item.identifier isEqualToString:@"HTTP Timeout"]){ |
| 134 | + http = [self readPreferenceValue:item]; |
| 135 | + } else if ([item.identifier isEqualToString:@"FTP Timeout"]){ |
| 136 | + ftp = [self readPreferenceValue:item]; |
| 137 | + } |
| 138 | + |
| 139 | + if ( http != nil && ftp != nil ){ |
| 140 | + break; |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + iPopup *popup = [[iPopup alloc] init]; |
| 146 | + |
| 147 | + debug(@"HTTP: %@ - FTP: %@", http, ftp); |
| 148 | + if( http == nil || ftp == nil ){ |
| 149 | + [popup title:@"Error" message:@"Failure getting timeout values."]; |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + NSFileHandle *fh = [NSFileHandle fileHandleForUpdatingAtPath:TIMEOUT_FILE]; |
| 154 | + if (fh == nil){ |
| 155 | + debug(@"Could not open timeout file"); |
| 156 | + [popup title:@"Error" message:@"Couldnt open timeout file"]; |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + [fh truncateFileAtOffset: 0]; |
| 161 | + NSString *hline = [NSString stringWithFormat:@"Acquire::http::Timeout \"%@\";\n", http]; |
| 162 | + NSString *fline = [NSString stringWithFormat:@"Acquire::ftp::Timeout \"%@\";\n", ftp]; |
| 163 | + |
| 164 | + [fh writeData:[hline dataUsingEncoding:NSASCIIStringEncoding]]; |
| 165 | + [fh writeData:[fline dataUsingEncoding:NSASCIIStringEncoding]]; |
| 166 | + [fh closeFile]; |
| 167 | + |
| 168 | + debug(@"Success HTTP %@ -- FTP %@", http, ftp); |
| 169 | + [popup title:@"Success" message:@"Timeouts updated."]; |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | +@end |
0 commit comments