Skip to content

Commit 99c9336

Browse files
committed
add profile manager
1 parent 6f6662a commit 99c9336

File tree

9 files changed

+271
-0
lines changed

9 files changed

+271
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ build
33
shadowsocks.xcodeproj/project.xcworkspace/
44
shadowsocks.xcodeproj/xcuserdata/
55
.DS_STORE
6+
*.dmg

ShadowsocksX/Configuration.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Generated by json_to_model
2+
3+
#import <Foundation/Foundation.h>
4+
#import "Profile.h"
5+
6+
7+
@interface Configuration : NSObject
8+
9+
- (id)initWithJSONData:(NSData *)data;
10+
- (id)initWithJSONDictionary:(NSDictionary *)dictionary;
11+
- (NSDictionary *)JSONDictionary;
12+
- (NSData *)JSONData;
13+
14+
15+
@property (nonatomic, assign) NSInteger current;
16+
17+
@property (nonatomic, strong) NSArray * servers;
18+
19+
20+
@end
21+

ShadowsocksX/Configuration.m

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Generated by json_to_model
2+
3+
#import "Configuration.h"
4+
5+
@implementation Configuration {
6+
7+
}
8+
9+
- (id)initWithJSONDictionary:(NSDictionary *)dictionary {
10+
11+
self = [super init];
12+
if (![dictionary isKindOfClass:[NSDictionary class]])
13+
return nil;
14+
15+
if (self) {
16+
17+
self.current = (dictionary[@"current"] != [NSNull null]) ? [dictionary[@"current"] integerValue] : 0;
18+
19+
self.servers = [[NSMutableArray alloc] initWithCapacity:16];
20+
for (NSDictionary *_ in dictionary[@"servers"]) {
21+
22+
[((NSMutableArray *)self.servers) addObject:[[Profile alloc] initWithJSONDictionary:_]];
23+
24+
}
25+
26+
}
27+
return self;
28+
}
29+
30+
- (id)initWithJSONData:(NSData *)data {
31+
self = [super init];
32+
if (self) {
33+
NSError *error = nil;
34+
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
35+
if (result) {
36+
self = [self initWithJSONDictionary:result];
37+
} else {
38+
return nil;
39+
}
40+
}
41+
return self;
42+
}
43+
44+
- (NSDictionary *)JSONDictionary {
45+
46+
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
47+
48+
49+
dictionary[@"current"] = @(self.current);
50+
51+
{
52+
NSMutableArray *_ = [[NSMutableArray alloc] init];
53+
dictionary[@"servers"] = _;
54+
55+
for (Profile *__ in self.servers) {
56+
57+
[_ addObject:[__ JSONDictionary]];
58+
59+
}
60+
61+
}
62+
63+
return dictionary;
64+
}
65+
66+
67+
- (NSData *)JSONData {
68+
NSError *error = nil;
69+
NSData *data = [NSJSONSerialization dataWithJSONObject:[self JSONDictionary] options:0 error:&error];
70+
if (error) {
71+
@throw error;
72+
}
73+
return data;
74+
}
75+
76+
77+
@end

ShadowsocksX/Profile.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Generated by json_to_model
2+
3+
#import <Foundation/Foundation.h>
4+
5+
6+
@interface Profile : NSObject
7+
8+
- (id)initWithJSONData:(NSData *)data;
9+
- (id)initWithJSONDictionary:(NSDictionary *)dictionary;
10+
- (NSDictionary *)JSONDictionary;
11+
- (NSData *)JSONData;
12+
13+
14+
@property (nonatomic, copy) NSString * method;
15+
16+
@property (nonatomic, copy) NSString * password;
17+
18+
@property (nonatomic, assign) NSInteger serverPort;
19+
20+
@property (nonatomic, copy) NSString * server;
21+
22+
23+
@end
24+

ShadowsocksX/Profile.m

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Generated by json_to_model
2+
3+
#import "Profile.h"
4+
5+
@implementation Profile {
6+
7+
}
8+
9+
- (id)initWithJSONDictionary:(NSDictionary *)dictionary {
10+
11+
self = [super init];
12+
if (![dictionary isKindOfClass:[NSDictionary class]])
13+
return nil;
14+
15+
if (self) {
16+
17+
self.method = (dictionary[@"method"] != [NSNull null]) ? dictionary[@"method"] : nil;
18+
19+
self.password = (dictionary[@"password"] != [NSNull null]) ? dictionary[@"password"] : nil;
20+
21+
self.serverPort = (dictionary[@"server_port"] != [NSNull null]) ? [dictionary[@"server_port"] integerValue] : 0;
22+
23+
self.server = (dictionary[@"server"] != [NSNull null]) ? dictionary[@"server"] : nil;
24+
25+
}
26+
return self;
27+
}
28+
29+
- (id)initWithJSONData:(NSData *)data {
30+
self = [super init];
31+
if (self) {
32+
NSError *error = nil;
33+
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
34+
if (result) {
35+
self = [self initWithJSONDictionary:result];
36+
} else {
37+
return nil;
38+
}
39+
}
40+
return self;
41+
}
42+
43+
- (NSDictionary *)JSONDictionary {
44+
45+
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
46+
47+
48+
dictionary[@"method"] = (self.method != nil) ? self.method : [NSNull null];
49+
50+
dictionary[@"password"] = (self.password != nil) ? self.password : [NSNull null];
51+
52+
dictionary[@"server_port"] = @(self.serverPort);
53+
54+
dictionary[@"server"] = (self.server != nil) ? self.server : [NSNull null];
55+
56+
return dictionary;
57+
}
58+
59+
60+
- (NSData *)JSONData {
61+
NSError *error = nil;
62+
NSData *data = [NSJSONSerialization dataWithJSONObject:[self JSONDictionary] options:0 error:&error];
63+
if (error) {
64+
@throw error;
65+
}
66+
return data;
67+
}
68+
69+
70+
@end

ShadowsocksX/ProfileManager.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Created by clowwindy on 11/3/14.
3+
// Copyright (c) 2014 clowwindy. All rights reserved.
4+
//
5+
6+
#import <Foundation/Foundation.h>
7+
#import "Configuration.h"
8+
9+
@interface ProfileManager : NSObject
10+
11+
+ (Configuration *)configuration;
12+
+ (void)saveConfiguration:(Configuration *)configuration;
13+
14+
@end

ShadowsocksX/ProfileManager.m

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Created by clowwindy on 11/3/14.
3+
// Copyright (c) 2014 clowwindy. All rights reserved.
4+
//
5+
6+
#import "ProfileManager.h"
7+
8+
#define CONFIG_DATA_KEY @"config"
9+
10+
@implementation ProfileManager {
11+
12+
}
13+
14+
+ (Configuration *)configuration {
15+
NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:CONFIG_DATA_KEY];
16+
if (data == nil) {
17+
// TODO load data from old version
18+
}
19+
Configuration *configuration;
20+
if (data == nil) {
21+
// load default configuration
22+
configuration = [[Configuration alloc] init];
23+
// public server
24+
configuration.current = -1;
25+
} else {
26+
configuration = [[Configuration alloc] initWithJSONData:data];
27+
}
28+
return configuration;
29+
}
30+
31+
+ (void)saveConfiguration:(Configuration *)configuration {
32+
[[NSUserDefaults standardUserDefaults] setObject:[configuration JSONData] forKey:CONFIG_DATA_KEY];
33+
}
34+
35+
@end

ShadowsocksX/models/profile.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"__class__": "Configuration",
3+
"current": 0,
4+
"servers": [{
5+
"__class__": "Profile",
6+
"server":"1.2.3.4",
7+
"server_port":8388,
8+
"password":"barfoo!",
9+
"method":"aes-256-cfb"
10+
}]
11+
}

shadowsocks.xcodeproj/project.pbxproj

+18
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
628693EC16DA2706008B1A26 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 628693EA16DA26E2008B1A26 /* SystemConfiguration.framework */; };
8181
628693F816DA2817008B1A26 /* ProxySettingsTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 628693F416DA2817008B1A26 /* ProxySettingsTableViewController.m */; };
8282
628C3DDF18AD5AFA0090632A /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 628C3DE118AD5AFA0090632A /* Localizable.strings */; };
83+
628C91BD1A07AA5800BA1566 /* Profile.m in Sources */ = {isa = PBXBuildFile; fileRef = 628C91BC1A07AA5800BA1566 /* Profile.m */; };
84+
628C91C41A07AB4500BA1566 /* Configuration.m in Sources */ = {isa = PBXBuildFile; fileRef = 628C91C31A07AB4500BA1566 /* Configuration.m */; };
8385
629AC93318B49341001D2771 /* Application.xib in Resources */ = {isa = PBXBuildFile; fileRef = 629AC93218B49341001D2771 /* Application.xib */; };
8486
629AC93418B49676001D2771 /* GCDWebServer.m in Sources */ = {isa = PBXBuildFile; fileRef = 621FCBFA17603FFA00411E5F /* GCDWebServer.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
8587
629AC93518B49676001D2771 /* GCDWebServerConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 621FCBFC17603FFA00411E5F /* GCDWebServerConnection.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
@@ -131,6 +133,7 @@
131133
EB1891FD5CE9480608918422 /* SimpleTableViewSource.m in Sources */ = {isa = PBXBuildFile; fileRef = EB1897832237B71EF50B3DF4 /* SimpleTableViewSource.m */; };
132134
EB1896F6F5F2468484F67FAD /* ShadowsocksRunner.m in Sources */ = {isa = PBXBuildFile; fileRef = EB1897A89055A4C2CFD81AF8 /* ShadowsocksRunner.m */; };
133135
EB1897A04CF0382A754FE700 /* SWBConfigWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = EB1895348E32ABA4F4F86742 /* SWBConfigWindowController.m */; };
136+
EB1899E6974F61B38B2FFD2C /* ProfileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = EB1892B5027A6B1F1E2CDC12 /* ProfileManager.m */; };
134137
EB189A01F66776EC1714F32D /* 3gnet_enable.mobileconfig in Resources */ = {isa = PBXBuildFile; fileRef = EB1897A8F80969EC7477F89D /* 3gnet_enable.mobileconfig */; };
135138
EB189DBA13C945ADF838DF2C /* QRCodeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = EB1892CE8806A5E5632B5714 /* QRCodeViewController.m */; };
136139
EB189E4CAA0DEF020070DC84 /* SWBAboutController.m in Sources */ = {isa = PBXBuildFile; fileRef = EB189EE1CF6E8C6C431B87BA /* SWBAboutController.m */; };
@@ -318,6 +321,10 @@
318321
628693FD16DA2983008B1A26 /* ev.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ev.c; path = libev/ev.c; sourceTree = "<group>"; };
319322
628C3DE218AD5B100090632A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
320323
628C3DE318AD5B700090632A /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
324+
628C91BB1A07AA5800BA1566 /* Profile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Profile.h; sourceTree = "<group>"; };
325+
628C91BC1A07AA5800BA1566 /* Profile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Profile.m; sourceTree = "<group>"; };
326+
628C91C21A07AB4500BA1566 /* Configuration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Configuration.h; sourceTree = "<group>"; };
327+
628C91C31A07AB4500BA1566 /* Configuration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Configuration.m; sourceTree = "<group>"; };
321328
629AC93218B49341001D2771 /* Application.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = Application.xib; sourceTree = "<group>"; };
322329
629AC93918B49B56001D2771 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/Cocoa.framework; sourceTree = DEVELOPER_DIR; };
323330
629AC93B18B49B69001D2771 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/AppKit.framework; sourceTree = DEVELOPER_DIR; };
@@ -401,8 +408,10 @@
401408
62E27E5D18C1D1000086033D /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib/libz.dylib; sourceTree = DEVELOPER_DIR; };
402409
96D80C04176CE1D7006C8078 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
403410
EB189069EF341E65B5414D00 /* QRCodeViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QRCodeViewController.h; sourceTree = "<group>"; };
411+
EB189071407B3A2A9B4C83CB /* ProfileManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ProfileManager.h; sourceTree = "<group>"; };
404412
EB1891B68FCBD841C5E461B6 /* About.md */ = {isa = PBXFileReference; lastKnownFileType = file.md; path = About.md; sourceTree = SOURCE_ROOT; };
405413
EB1891FD08E70E9E9DC84AFD /* about.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = about.txt; sourceTree = "<group>"; };
414+
EB1892B5027A6B1F1E2CDC12 /* ProfileManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ProfileManager.m; sourceTree = "<group>"; };
406415
EB1892CE8806A5E5632B5714 /* QRCodeViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QRCodeViewController.m; sourceTree = "<group>"; };
407416
EB18942F05960F64CC059413 /* SimpleTableViewSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleTableViewSource.h; sourceTree = "<group>"; };
408417
EB1895348E32ABA4F4F86742 /* SWBConfigWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWBConfigWindowController.m; sourceTree = "<group>"; };
@@ -713,6 +722,12 @@
713722
EB189A194D9EB70D1C8E7123 /* SWBApplication.h */,
714723
625E5BBB19EA36D2007A5124 /* SWBQRCodeWindowController.h */,
715724
625E5BBC19EA36D2007A5124 /* SWBQRCodeWindowController.m */,
725+
628C91BB1A07AA5800BA1566 /* Profile.h */,
726+
628C91BC1A07AA5800BA1566 /* Profile.m */,
727+
628C91C21A07AB4500BA1566 /* Configuration.h */,
728+
628C91C31A07AB4500BA1566 /* Configuration.m */,
729+
EB1892B5027A6B1F1E2CDC12 /* ProfileManager.m */,
730+
EB189071407B3A2A9B4C83CB /* ProfileManager.h */,
716731
);
717732
path = ShadowsocksX;
718733
sourceTree = "<group>";
@@ -1188,19 +1203,22 @@
11881203
files = (
11891204
62E27E5A18C1D0CF0086033D /* GZIP.m in Sources */,
11901205
629AC93418B49676001D2771 /* GCDWebServer.m in Sources */,
1206+
628C91BD1A07AA5800BA1566 /* Profile.m in Sources */,
11911207
629AC93518B49676001D2771 /* GCDWebServerConnection.m in Sources */,
11921208
629AC93618B49676001D2771 /* GCDWebServerRequest.m in Sources */,
11931209
629AC93718B49676001D2771 /* GCDWebServerResponse.m in Sources */,
11941210
627A6E4118B47FAE00493BBC /* encrypt.c in Sources */,
11951211
627A6E4218B47FAE00493BBC /* table.m in Sources */,
11961212
627A6E4318B47FAE00493BBC /* local.m in Sources */,
11971213
627A6E4418B47FAE00493BBC /* ev.c in Sources */,
1214+
628C91C41A07AB4500BA1566 /* Configuration.m in Sources */,
11981215
627A6E0518B47E9300493BBC /* main.m in Sources */,
11991216
627A6E0C18B47E9300493BBC /* SWBAppDelegate.m in Sources */,
12001217
EB1897A04CF0382A754FE700 /* SWBConfigWindowController.m in Sources */,
12011218
EB18906527B59E4F6A70A14A /* ShadowsocksRunner.m in Sources */,
12021219
625E5BBD19EA36D2007A5124 /* SWBQRCodeWindowController.m in Sources */,
12031220
EB189FE30A2E13F4D10D59ED /* SWBApplication.m in Sources */,
1221+
EB1899E6974F61B38B2FFD2C /* ProfileManager.m in Sources */,
12041222
);
12051223
runOnlyForDeploymentPostprocessing = 0;
12061224
};

0 commit comments

Comments
 (0)