88#import " ConfigWindowController.h"
99#import " AppDelegate.h"
1010
11- @interface ConfigWindowController () {
12- NSMutableArray *profiles;
13- NSMutableArray *cusProfiles;
14- }
11+ @interface ConfigWindowController ()
1512
1613@end
1714
@@ -26,52 +23,68 @@ - (void)windowDidLoad {
2623 [_alterIdField setFormatter: formatter];
2724 [_localPortField setFormatter: formatter];
2825 [_httpPortField setFormatter: formatter];
29- profiles = [appDelegate profiles ];
30- cusProfiles = [appDelegate cusProfiles ];
26+ [_addRemoveButton setMenu: _importFromJsonMenu forSegment: 2 ];
27+
28+ // copy data
29+ _profiles = [[NSMutableArray alloc ] init ];
30+ for (ServerProfile *p in appDelegate.profiles ) {
31+ [_profiles addObject: [p deepCopy ]];
32+ }
33+ _cusProfiles = [[NSMutableArray alloc ] init ];
34+ for (NSString * p in appDelegate.cusProfiles ) {
35+ [_cusProfiles addObject: [NSString stringWithString: p]];
36+ }
37+ //
3138 [_profileTable reloadData ];
3239 [self setSelectedServerIndex: appDelegate.selectedServerIndex];// must be put after reloadData!
33- [_profileTable selectRowIndexes: [NSIndexSet indexSetWithIndex: _selectedServerIndex] byExtendingSelection: NO ];
34- NSLog (@" %ld " , (long )[_profileTable selectedRow ]);
40+ self.selectedCusServerIndex = appDelegate.selectedCusServerIndex ;
41+ self.httpPort = appDelegate.httpPort ;
42+ self.localPort = appDelegate.localPort ;
43+ self.udpSupport = appDelegate.udpSupport ;
44+ self.shareOverLan = appDelegate.shareOverLan ;
45+ self.dnsString = appDelegate.dnsString ;
3546 NSDictionary *logLevelDic = @{
3647 @" debug" : @4 ,
3748 @" info" : @3 ,
3849 @" warning" : @2 ,
3950 @" error" :@1 ,
4051 @" none" :@0
4152 };
42- [_logLevelButton selectItemAtIndex: [logLevelDic[[appDelegate logLevel ]] integerValue ]];
43- [_addRemoveButton setMenu: _importFromJsonMenu forSegment: 2 ];
53+ self.logLevel = [logLevelDic[appDelegate.logLevel] integerValue ];
54+
55+ [_profileTable selectRowIndexes: [NSIndexSet indexSetWithIndex: self .selectedServerIndex] byExtendingSelection: NO ];
56+ [[self window ] makeFirstResponder: _profileTable];
4457}
4558
4659// set controller as profilesTable and cusProfileTable's datasource
4760- (NSInteger )numberOfRowsInTableView : (NSTableView *)tableView {
4861 if (tableView == _profileTable) {
49- return [profiles count ];
62+ return [_profiles count ];
5063 }
5164 if (tableView == _cusProfileTable) {
52- return [cusProfiles count ];
65+ return [_cusProfiles count ];
5366 }
5467 return 0 ;
5568}
5669
5770- (void )tableView : (NSTableView *)tableView setObjectValue : (id )object forTableColumn : (NSTableColumn *)tableColumn row : (NSInteger )row {
5871 if (tableView == _cusProfileTable) {
59- [cusProfiles setObject: object atIndexedSubscript: row];
72+ [_cusProfiles setObject: object atIndexedSubscript: row];
6073 }
6174}
6275
6376- (id ) tableView : (NSTableView *)tableView objectValueForTableColumn : (NSTableColumn *)tableColumn row : (NSInteger )row {
6477 if (tableView == _profileTable) {
65- if ([profiles count ] > 0 ) {
66- ServerProfile* p = [profiles objectAtIndex: row];
78+ if ([_profiles count ] > 0 ) {
79+ ServerProfile* p = [_profiles objectAtIndex: row];
6780 return [[p remark ] length ] > 0 ? [p remark ] : [NSString stringWithFormat: @" %@ :%ld " , [p address ], [p port ]];
6881 } else {
6982 return nil ;
7083 }
7184 }
7285 if (tableView == _cusProfileTable) {
73- if ([cusProfiles count ] > 0 ) {
74- return cusProfiles [row];
86+ if ([_cusProfiles count ] > 0 ) {
87+ return _cusProfiles [row];
7588 } else {
7689 return nil ;
7790 }
@@ -81,13 +94,13 @@ - (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColu
8194
8295-(void )tableViewSelectionDidChange : (NSNotification *)notification {
8396 if ([notification object ] == _profileTable) {
84- if ([profiles count ] > 0 ) {
97+ if ([_profiles count ] > 0 ) {
8598 [self setSelectedServerIndex: [_profileTable selectedRow ]];
86- [self setSelectedProfile: profiles [_selectedServerIndex]];
99+ [self setSelectedProfile: _profiles [_selectedServerIndex]];
87100 }
88101 }
89102 if ([notification object ] == _cusProfileTable) {
90- if ([cusProfiles count ] > 0 ) {
103+ if ([_cusProfiles count ] > 0 ) {
91104 [self setSelectedCusServerIndex: [_cusProfileTable selectedRow ]];
92105 }
93106 }
@@ -100,7 +113,7 @@ - (IBAction)chooseNetwork:(NSPopUpButton *)sender {
100113
101114- (BOOL )checkTLSforHttp2 {
102115 if ([_networkButton indexOfSelectedItem ] == 3 ) { // selected http/2
103- BOOL tlsEnabled = [selectedProfile.streamSettings[@" security" ] isEqual: @" tls" ];
116+ BOOL tlsEnabled = [self . selectedProfile.streamSettings[@" security" ] isEqual: @" tls" ];
104117 if (!tlsEnabled) {
105118 NSAlert *httpTlsAlerm = [[NSAlert alloc ] init ];
106119 [httpTlsAlerm addButtonWithTitle: @" Close" ];
@@ -119,19 +132,19 @@ - (BOOL)checkTLSforHttp2 {
119132- (IBAction )addRemoveServer : (id )sender {
120133 if ([sender selectedSegment ] == 0 ) {
121134 ServerProfile* newProfile = [[ServerProfile alloc ] init ];
122- [profiles addObject: newProfile];
135+ [_profiles addObject: newProfile];
123136 [_profileTable reloadData ];
124- [_profileTable selectRowIndexes: [NSIndexSet indexSetWithIndex: ([profiles count ] - 1 )] byExtendingSelection: NO ];
125- } else if ([sender selectedSegment ] == 1 && [profiles count ] > 0 ) {
137+ [_profileTable selectRowIndexes: [NSIndexSet indexSetWithIndex: ([_profiles count ] - 1 )] byExtendingSelection: NO ];
138+ } else if ([sender selectedSegment ] == 1 && [_profiles count ] > 0 ) {
126139 NSInteger originalSelectedServerIndex = [_profileTable selectedRow ];
127- [profiles removeObjectAtIndex: originalSelectedServerIndex];
128- if ([profiles count ] > 0 ) {
129- if (originalSelectedServerIndex == [profiles count ]) {// deleted the last server
140+ [_profiles removeObjectAtIndex: originalSelectedServerIndex];
141+ if ([_profiles count ] > 0 ) {
142+ if (originalSelectedServerIndex == [_profiles count ]) {// deleted the last server
130143 // select the last server of the remains
131- [self setSelectedServerIndex: [profiles count ] - 1 ];
144+ [self setSelectedServerIndex: [_profiles count ] - 1 ];
132145 }
133146 [_profileTable selectRowIndexes: [NSIndexSet indexSetWithIndex: _selectedServerIndex] byExtendingSelection: NO ];
134- [self setSelectedProfile: profiles [_selectedServerIndex]];
147+ [self setSelectedProfile: _profiles [_selectedServerIndex]];
135148 } else { // all the profiles are deleted;
136149 [self setSelectedServerIndex: -1 ];
137150 [self setSelectedProfile: nil ];
@@ -154,27 +167,34 @@ - (IBAction)okSave:(id)sender {
154167 if ([dnsStr length ] == 0 ) {
155168 dnsStr = @" localhost" ;
156169 }
157- appDelegate.dnsString = dnsStr;
158170 appDelegate.logLevel = _logLevelButton.selectedItem .title ;
159- appDelegate.selectedServerIndex = _selectedServerIndex;
160- [appDelegate configurationDidChange ];
171+ appDelegate.selectedServerIndex = self.selectedServerIndex ;
172+ appDelegate.selectedCusServerIndex = self.selectedCusServerIndex ;
173+ appDelegate.localPort = [_localPortField integerValue ];
174+ appDelegate.httpPort = [_httpPortField integerValue ];
175+ appDelegate.udpSupport = self.udpSupport ;
176+ appDelegate.shareOverLan = self.shareOverLan ;
177+ appDelegate.dnsString = dnsStr;
178+ appDelegate.profiles = self.profiles ;
179+ appDelegate.cusProfiles = self.cusProfiles ;
161180
181+ [appDelegate configurationDidChange ];
162182 [[self window ] close ];
163183}
164184
165185- (IBAction )addRemoveCusProfile : (NSSegmentedControl *)sender {
166186 if ([sender selectedSegment ] == 0 ) {
167- [cusProfiles addObject: @" /path/to/your/config.json" ];
187+ [_cusProfiles addObject: @" /path/to/your/config.json" ];
168188 [_cusProfileTable reloadData ];
169- [_cusProfileTable selectRowIndexes: [NSIndexSet indexSetWithIndex: [cusProfiles count ] -1 ] byExtendingSelection: NO ];
170- [_cusProfileTable setFocusedColumn: [cusProfiles count ] - 1 ];
171- // [[_cusProfileTable viewAtColumn:0 row:[cusProfiles count]-1 makeIfNecessary:NO] becomeFirstResponder];
172- } else if ([sender selectedSegment ] == 1 && [cusProfiles count ] > 0 ) {
189+ [_cusProfileTable selectRowIndexes: [NSIndexSet indexSetWithIndex: [_cusProfiles count ] -1 ] byExtendingSelection: NO ];
190+ [_cusProfileTable setFocusedColumn: [_cusProfiles count ] - 1 ];
191+ // [[_cusProfileTable viewAtColumn:0 row:_cusProfiles count]-1 makeIfNecessary:NO] becomeFirstResponder];
192+ } else if ([sender selectedSegment ] == 1 && [_cusProfiles count ] > 0 ) {
173193 NSInteger originalSelected = [_cusProfileTable selectedRow ];
174- [cusProfiles removeObjectAtIndex: originalSelected];
175- if ([cusProfiles count ] > 0 ) {
176- if (originalSelected == [cusProfiles count ]) {
177- [self setSelectedCusServerIndex: [cusProfiles count ] - 1 ];
194+ [_cusProfiles removeObjectAtIndex: originalSelected];
195+ if ([_cusProfiles count ] > 0 ) {
196+ if (originalSelected == [_cusProfiles count ]) {
197+ [self setSelectedCusServerIndex: [_cusProfiles count ] - 1 ];
178198 }
179199 [_cusProfileTable selectRowIndexes: [NSIndexSet indexSetWithIndex: _selectedCusServerIndex] byExtendingSelection: NO ];
180200 } else {
@@ -197,7 +217,7 @@ - (IBAction)showCusConfigWindow:(NSButton *)sender {
197217- (IBAction )cFinish : (NSButton *)sender {
198218 [_checkLabel setHidden: NO ];
199219 NSString * v2rayBinPath = [NSString stringWithFormat: @" %@ /v2ray" , [[NSBundle mainBundle ] resourcePath ]];
200- for (NSString * filePath in cusProfiles ) {
220+ for (NSString * filePath in _cusProfiles ) {
201221 int returnCode = runCommandLine (v2rayBinPath, @[@" -test" , @" -config" , filePath]);
202222 if (returnCode != 0 ) {
203223 [_checkLabel setHidden: YES ];
@@ -230,7 +250,7 @@ - (IBAction)showTransportSettings:(id)sender {
230250 [_muxConcurrencyField setFormatter: formatter];
231251 [_proxyPortField setFormatter: formatter];
232252 // read settings
233- NSDictionary *transportSettings = [selectedProfile streamSettings ];
253+ NSDictionary *transportSettings = [self . selectedProfile streamSettings ];
234254 // kcp
235255 [_kcpMtuField setIntegerValue: [transportSettings[@" kcpSettings" ][@" mtu" ] integerValue ]];
236256 [_kcpTtiField setIntegerValue: [transportSettings[@" kcpSettings" ][@" tti" ] integerValue ]];
@@ -284,7 +304,7 @@ - (IBAction)showTransportSettings:(id)sender {
284304 }
285305 [self useTLS: nil ];
286306 // mux
287- NSDictionary *muxSettings = [selectedProfile muxSettings ];
307+ NSDictionary *muxSettings = [self . selectedProfile muxSettings ];
288308 [_muxEnableButton setState: [nilCoalescing (muxSettings[@" enabled" ], @NO ) boolValue ]];
289309 [_muxConcurrencyField setIntegerValue: [nilCoalescing (muxSettings[@" concurrency" ], @8 ) integerValue ]];
290310 // proxy
@@ -447,7 +467,7 @@ - (IBAction)importFromConfigJson:(id)sender {
447467 for (ServerProfile* s in servers) {
448468 [s setRemark: [NSString stringWithFormat: @" imported %@ " , s.remark]];
449469 }
450- [self ->profiles addObjectsFromArray: servers];
470+ [self ->_profiles addObjectsFromArray: servers];
451471 }
452472 }
453473 [self ->_profileTable reloadData ];
@@ -467,6 +487,6 @@ - (IBAction)showLog:(id)sender {
467487 [appDelegate viewLog: sender];
468488}
469489
470- @synthesize selectedProfile;
471490@synthesize appDelegate;
491+
472492@end
0 commit comments