Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 3437ee4

Browse files
committed
copy profiles for configwindow, so that cancel' is real cancel'
1 parent 6546d9c commit 3437ee4

File tree

5 files changed

+101
-53
lines changed

5 files changed

+101
-53
lines changed

V2RayX/ConfigWindow.xib

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
5454
</textFieldCell>
5555
<connections>
56-
<binding destination="-2" name="value" keyPath="self.appDelegate.localPort" id="Qy6-eZ-dnd">
56+
<binding destination="-2" name="value" keyPath="self.localPort" id="ywZ-oA-aHA">
5757
<dictionary key="options">
5858
<bool key="NSContinuouslyUpdatesValue" value="YES"/>
5959
<bool key="NSValidatesImmediately" value="YES"/>
@@ -71,7 +71,7 @@
7171
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
7272
</textFieldCell>
7373
<connections>
74-
<binding destination="-2" name="value" keyPath="self.appDelegate.httpPort" id="MP8-et-l3C">
74+
<binding destination="-2" name="value" keyPath="self.httpPort" id="axL-oS-ub4">
7575
<dictionary key="options">
7676
<bool key="NSContinuouslyUpdatesValue" value="YES"/>
7777
<bool key="NSValidatesImmediately" value="YES"/>
@@ -87,7 +87,7 @@
8787
<font key="font" metaFont="system"/>
8888
</buttonCell>
8989
<connections>
90-
<binding destination="-2" name="value" keyPath="self.appDelegate.udpSupport" id="2C0-Jq-CWl"/>
90+
<binding destination="-2" name="value" keyPath="self.udpSupport" id="FMo-rg-Qyh"/>
9191
</connections>
9292
</button>
9393
<textField horizontalHuggingPriority="251" verticalHuggingPriority="749" fixedFrame="YES" allowsCharacterPickerTouchBarItem="YES" translatesAutoresizingMaskIntoConstraints="NO" id="reb-n0-jKg">
@@ -117,7 +117,7 @@
117117
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
118118
</textFieldCell>
119119
<connections>
120-
<binding destination="-2" name="value" keyPath="self.appDelegate.dnsString" id="xHX-QN-6DE"/>
120+
<binding destination="-2" name="value" keyPath="self.dnsString" id="VQy-DX-fDA"/>
121121
</connections>
122122
</textField>
123123
<textField horizontalHuggingPriority="251" verticalHuggingPriority="749" fixedFrame="YES" allowsCharacterPickerTouchBarItem="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ecy-ho-llN">
@@ -448,7 +448,7 @@
448448
<font key="font" metaFont="system"/>
449449
</buttonCell>
450450
<connections>
451-
<binding destination="-2" name="value" keyPath="self.appDelegate.shareOverLan" id="bIN-YN-4rS"/>
451+
<binding destination="-2" name="value" keyPath="self.shareOverLan" id="rkH-wq-0DJ"/>
452452
</connections>
453453
</button>
454454
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Jbe-gs-AvT">
@@ -480,6 +480,9 @@
480480
</items>
481481
</menu>
482482
</popUpButtonCell>
483+
<connections>
484+
<binding destination="-2" name="selectedIndex" keyPath="self.logLevel" id="W2U-Kc-adG"/>
485+
</connections>
483486
</popUpButton>
484487
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="FaF-dM-uPs">
485488
<rect key="frame" x="18" y="20" width="62" height="17"/>
@@ -560,7 +563,7 @@ Gw
560563
</segmentedControl>
561564
</subviews>
562565
</view>
563-
<point key="canvasLocation" x="162.5" y="319.5"/>
566+
<point key="canvasLocation" x="175" y="182"/>
564567
</window>
565568
<userDefaultsController representsSharedInstance="YES" id="I7n-bF-Cph"/>
566569
<menuItem title="Item" id="tLs-gB-gFx">

V2RayX/ConfigWindowController.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,18 @@
6767
@property (weak) IBOutlet NSTextField *checkLabel;
6868

6969

70+
@property AppDelegate* appDelegate;
7071
@property (nonatomic) ServerProfile* selectedProfile;
72+
@property (nonatomic) NSInteger logLevel;
7173
@property (nonatomic) NSInteger selectedServerIndex;
7274
@property (nonatomic) NSInteger selectedCusServerIndex;
75+
@property (nonatomic) NSInteger localPort;
76+
@property (nonatomic) NSInteger httpPort;
77+
@property (nonatomic) BOOL udpSupport;
78+
@property (nonatomic) BOOL shareOverLan;
79+
@property (nonatomic) NSString* dnsString;
80+
@property (nonatomic) NSMutableArray *profiles;
81+
@property (nonatomic) NSMutableArray *cusProfiles;
7382

74-
@property AppDelegate* appDelegate;
7583

7684
@end

V2RayX/ConfigWindowController.m

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
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

V2RayX/ServerProfile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ typedef enum NetWorkType : NSUInteger {
2727
- (NSMutableDictionary*)outboundProfile;
2828
+ (ServerProfile* _Nullable )readFromAnOutboundDic:(NSDictionary*)outDict;
2929
+ (NSArray*)profilesFromJson:(NSDictionary*)outboundJson;
30+
-(ServerProfile*)deepCopy;
3031
@property (nonatomic) NSString* address;
3132
@property (nonatomic) NSUInteger port;
3233
@property (nonatomic) NSString* userId;

V2RayX/ServerProfile.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ + (ServerProfile* _Nullable )readFromAnOutboundDic:(NSDictionary*)outDict {
114114
}
115115
}
116116

117+
-(ServerProfile*)deepCopy {
118+
ServerProfile* aCopy = [[ServerProfile alloc] init];
119+
aCopy.address = [NSString stringWithString:self.address];
120+
aCopy.port = self.port;
121+
aCopy.userId = [NSString stringWithString:self.userId];
122+
aCopy.alterId = self.alterId;
123+
aCopy.level = self.level;
124+
aCopy.remark = [NSString stringWithString:self.remark];
125+
aCopy.security = self.security;
126+
aCopy.network = self.network;
127+
aCopy.sendThrough = [NSString stringWithString:self.sendThrough];
128+
aCopy.streamSettings = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self.streamSettings]];
129+
aCopy.muxSettings = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self.muxSettings]];
130+
return aCopy;
131+
}
132+
117133
- (NSMutableDictionary*)outboundProfile {
118134
NSMutableDictionary* fullStreamSettings = [NSMutableDictionary dictionaryWithDictionary:streamSettings];
119135
fullStreamSettings[@"network"] = @[@"tcp",@"kcp", @"ws", @"http"][network];

0 commit comments

Comments
 (0)