大量代码。
This commit is contained in:
68
ShadowsocksX-NG/ServerProfileManager.swift
Normal file
68
ShadowsocksX-NG/ServerProfileManager.swift
Normal file
@ -0,0 +1,68 @@
|
||||
//
|
||||
// ServerProfileManager.swift
|
||||
// ShadowsocksX-NG
|
||||
//
|
||||
// Created by 邱宇舟 on 16/6/6.
|
||||
// Copyright © 2016年 qiuyuzhou. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
class ServerProfileManager: NSObject {
|
||||
|
||||
var profiles:[ServerProfile]
|
||||
var activeProfileId: String?
|
||||
|
||||
override init() {
|
||||
profiles = [ServerProfile]()
|
||||
|
||||
let defaults = NSUserDefaults.standardUserDefaults()
|
||||
if let _profiles = defaults.arrayForKey("ServerProfiles") {
|
||||
for _profile in _profiles {
|
||||
let profile = ServerProfile.fromDictionary(_profile as! [String : AnyObject])
|
||||
profiles.append(profile)
|
||||
}
|
||||
}
|
||||
activeProfileId = defaults.stringForKey("ActiveServerProfileId")
|
||||
}
|
||||
|
||||
func setActiveProfiledId(id: String) {
|
||||
activeProfileId = id
|
||||
let defaults = NSUserDefaults.standardUserDefaults()
|
||||
defaults.setObject(id, forKey: "ActiveServerProfileId")
|
||||
}
|
||||
|
||||
func save() {
|
||||
let defaults = NSUserDefaults.standardUserDefaults()
|
||||
var _profiles = [AnyObject]()
|
||||
for profile in profiles {
|
||||
if profile.is_valid() {
|
||||
let _profile = profile.toDictionary()
|
||||
_profiles.append(_profile)
|
||||
}
|
||||
}
|
||||
defaults.setObject(_profiles, forKey: "ServerProfiles")
|
||||
|
||||
|
||||
// TODO
|
||||
if activeProfileId != nil {
|
||||
defaults.setObject(activeProfileId, forKey: "ActiveServerProfileId")
|
||||
writeSSLocalConfFile((getActiveProfile()?.toJsonConfig())!)
|
||||
} else {
|
||||
removeSSLocalConfFile()
|
||||
}
|
||||
}
|
||||
|
||||
func getActiveProfile() -> ServerProfile? {
|
||||
if let id = activeProfileId {
|
||||
for p in profiles {
|
||||
if p.uuid == id {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user