| 1 | /* Copyright (C) 2008-2009 Peter Speck |
|---|
| 2 | * |
|---|
| 3 | * This program is free software: you can redistribute it and/or modify |
|---|
| 4 | * it under the terms of the GNU General Public License as published by |
|---|
| 5 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 6 | * (at your option) any later version. |
|---|
| 7 | * |
|---|
| 8 | * This program is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * GNU General Public License for more details. |
|---|
| 12 | * |
|---|
| 13 | * You should have received a copy of the GNU General Public License |
|---|
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #import "NetworkSettingsUpdaterUI.h" |
|---|
| 18 | #import "GBDebug.h" |
|---|
| 19 | #import "ServerCommunication.h" |
|---|
| 20 | #import "AlertHelper.h" |
|---|
| 21 | #import "Prefs.h" |
|---|
| 22 | |
|---|
| 23 | @interface NetworkSettingsUpdaterUI() |
|---|
| 24 | |
|---|
| 25 | - (void)confirmUpdateAlertDidEndWithReturnCode:(NSInteger)returnCode; |
|---|
| 26 | |
|---|
| 27 | @end |
|---|
| 28 | |
|---|
| 29 | @implementation NetworkSettingsUpdaterUI |
|---|
| 30 | |
|---|
| 31 | - (id)initWithServerCommunication:(ServerCommunication*)comm |
|---|
| 32 | withWantEnable:(BOOL)wantEnable; |
|---|
| 33 | { |
|---|
| 34 | if ((self = [super initWithProxyPort:[comm getPortNumber] withWantEnable:wantEnable])) { |
|---|
| 35 | _prefs = [[comm prefs] retain]; |
|---|
| 36 | _alertHelper = [[_prefs alertHelper] retain]; |
|---|
| 37 | } |
|---|
| 38 | return self; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | - (void)dealloc |
|---|
| 42 | { |
|---|
| 43 | [_alertHelper release]; |
|---|
| 44 | [_prefs release]; |
|---|
| 45 | [super dealloc]; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | - (SCPreferencesRef)createSCPreferencesRef:(BOOL)commit |
|---|
| 49 | { |
|---|
| 50 | CFStringRef appName = CFSTR("GlimmerBlocker"); |
|---|
| 51 | if (commit) |
|---|
| 52 | return SCPreferencesCreateWithAuthorization(NULL, appName, NULL, [_prefs authRef]); |
|---|
| 53 | else |
|---|
| 54 | return SCPreferencesCreate(NULL, appName, NULL); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | - (void)updateSettingsWithConfirm:(BOOL)confirm withFeedbackIfUnchanged:(BOOL)feedbackIfUnchanged |
|---|
| 58 | { |
|---|
| 59 | if (![_prefs hasGlimmerAuth]) { |
|---|
| 60 | NSLog(@"Can't change network settings without auth. Should never happend."); |
|---|
| 61 | return; |
|---|
| 62 | } |
|---|
| 63 | BOOL ok = [self updateSettingsWithCommit:NO]; |
|---|
| 64 | if (ok) { |
|---|
| 65 | DebugNSLog(@"Network settings don't need update."); |
|---|
| 66 | if (feedbackIfUnchanged) { |
|---|
| 67 | [_alertHelper prepareInformativeAlertWithTitle:@"No changes required to proxy settings" |
|---|
| 68 | withInformativeText:NULL]; |
|---|
| 69 | [_alertHelper showPreparedAlert]; |
|---|
| 70 | } |
|---|
| 71 | return; |
|---|
| 72 | } |
|---|
| 73 | NSMutableString* sb = [NSMutableString stringWithCapacity:50]; |
|---|
| 74 | for (NSUInteger i = 0; i < [_actionDescription count]; i++) { |
|---|
| 75 | NSString* s = [_actionDescription objectAtIndex:i]; |
|---|
| 76 | if ([sb length]) |
|---|
| 77 | [sb appendString:@"\n"]; |
|---|
| 78 | [sb appendString:s]; |
|---|
| 79 | DebugNSLog(@"%@", s); |
|---|
| 80 | } |
|---|
| 81 | if (confirm) { |
|---|
| 82 | [_alertHelper prepareWarningAlertWithTitle:@"Update proxy settings in Network?" |
|---|
| 83 | withInformativeText:sb]; |
|---|
| 84 | [_alertHelper setDelegate:self |
|---|
| 85 | withSelector:@selector(confirmUpdateAlertDidEndWithReturnCode:)]; |
|---|
| 86 | [_alertHelper addButtonWithTitle:@"Yes"]; |
|---|
| 87 | [_alertHelper addButtonWithTitle:@"No"]; |
|---|
| 88 | [_alertHelper showPreparedAlert]; |
|---|
| 89 | return; |
|---|
| 90 | } |
|---|
| 91 | ok = [self updateSettingsWithCommit:YES]; |
|---|
| 92 | if (!ok) |
|---|
| 93 | return; |
|---|
| 94 | ok = [self updateSettingsWithCommit:NO]; // check the settings was fully updated. |
|---|
| 95 | if (!ok) |
|---|
| 96 | NSLog(@"GlimmerBlocker failed updating network settings."); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | - (void)confirmUpdateAlertDidEndWithReturnCode:(NSInteger)returnCode |
|---|
| 100 | { |
|---|
| 101 | DebugNSLog(@"alertDidEnd, self-retain = %ld, returnCode = %ld", (long)[self retainCount], (long)returnCode); |
|---|
| 102 | if (returnCode == NSAlertFirstButtonReturn) |
|---|
| 103 | [self updateSettingsWithConfirm:NO withFeedbackIfUnchanged:NO]; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | @end |
|---|