[BUG] Show import/export panel as modal panel.

This approach would ensure the panel show in most front and in center of screen.
This commit is contained in:
Charlie Qiu
2017-01-17 02:07:33 +08:00
parent e5c8e965d4
commit f730c5cba2

View File

@ -79,49 +79,48 @@ class ServerProfileManager: NSObject {
openPanel.canCreateDirectories = false
openPanel.canChooseFiles = true
openPanel.becomeKey()
openPanel.begin { (result) -> Void in
if (result == NSFileHandlingPanelOKButton && (openPanel.url) != nil) {
let fileManager = FileManager.default
let filePath:String = (openPanel.url?.path)!
if (fileManager.fileExists(atPath: filePath) && filePath.hasSuffix("json")) {
let data = fileManager.contents(atPath: filePath)
let readString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
let readStringData = readString.data(using: String.Encoding.utf8.rawValue)
let result = openPanel.runModal()
if (result == NSFileHandlingPanelOKButton && (openPanel.url) != nil) {
let fileManager = FileManager.default
let filePath:String = (openPanel.url?.path)!
if (fileManager.fileExists(atPath: filePath) && filePath.hasSuffix("json")) {
let data = fileManager.contents(atPath: filePath)
let readString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
let readStringData = readString.data(using: String.Encoding.utf8.rawValue)
let jsonArr1 = try! JSONSerialization.jsonObject(with: readStringData!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
for item in jsonArr1.object(forKey: "configs") as! [[String: AnyObject]]{
let profile = ServerProfile()
profile.serverHost = item["server"] as! String
profile.serverPort = UInt16((item["server_port"]?.integerValue)!)
profile.method = item["method"] as! String
profile.password = item["password"] as! String
profile.remark = item["remarks"] as! String
let jsonArr1 = try! JSONSerialization.jsonObject(with: readStringData!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
for item in jsonArr1.object(forKey: "configs") as! [[String: AnyObject]]{
let profile = ServerProfile()
profile.serverHost = item["server"] as! String
profile.serverPort = UInt16((item["server_port"]?.integerValue)!)
profile.method = item["method"] as! String
profile.password = item["password"] as! String
profile.remark = item["remarks"] as! String
// Kcptun
profile.enabledKcptun = item["enabled_kcptun"]?.boolValue ?? false
if let kcptun = item["kcptun"] {
profile.kcptunProfile = KcptunProfile.fromDictionary(kcptun as! [String : Any?])
}
self.profiles.append(profile)
self.save()
// Kcptun
profile.enabledKcptun = item["enabled_kcptun"]?.boolValue ?? false
if let kcptun = item["kcptun"] {
profile.kcptunProfile = KcptunProfile.fromDictionary(kcptun as! [String : Any?])
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NOTIFY_SERVER_PROFILES_CHANGED), object: nil)
let configsCount = (jsonArr1.object(forKey: "configs") as! [[String: AnyObject]]).count
let notification = NSUserNotification()
notification.title = "Import Server Profile succeed!".localized
notification.informativeText = "Successful import \(configsCount) items".localized
NSUserNotificationCenter.default
.deliver(notification)
}else{
let notification = NSUserNotification()
notification.title = "Import Server Profile failed!".localized
notification.informativeText = "Invalid config file!".localized
NSUserNotificationCenter.default
.deliver(notification)
return
self.profiles.append(profile)
self.save()
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NOTIFY_SERVER_PROFILES_CHANGED), object: nil)
let configsCount = (jsonArr1.object(forKey: "configs") as! [[String: AnyObject]]).count
let notification = NSUserNotification()
notification.title = "Import Server Profile succeed!".localized
notification.informativeText = "Successful import \(configsCount) items".localized
NSUserNotificationCenter.default
.deliver(notification)
}else{
let notification = NSUserNotification()
notification.title = "Import Server Profile failed!".localized
notification.informativeText = "Invalid config file!".localized
NSUserNotificationCenter.default
.deliver(notification)
return
}
}
}
@ -164,17 +163,16 @@ class ServerProfileManager: NSObject {
savePanel.allowedFileTypes = ["json"]
savePanel.nameFieldStringValue = "export.json"
savePanel.becomeKey()
savePanel.begin { (result) -> Void in
if (result == NSFileHandlingPanelOKButton && (savePanel.url) != nil) {
//write jsonArr1 back to file
try! jsonString.write(toFile: (savePanel.url?.path)!, atomically: true, encoding: String.Encoding.utf8)
NSWorkspace.shared().selectFile((savePanel.url?.path)!, inFileViewerRootedAtPath: (savePanel.directoryURL?.path)!)
let notification = NSUserNotification()
notification.title = "Export Server Profile succeed!".localized
notification.informativeText = "Successful Export \(self.profiles.count) items".localized
NSUserNotificationCenter.default
.deliver(notification)
}
let result = savePanel.runModal()
if (result == NSFileHandlingPanelOKButton && (savePanel.url) != nil) {
//write jsonArr1 back to file
try! jsonString.write(toFile: (savePanel.url?.path)!, atomically: true, encoding: String.Encoding.utf8)
NSWorkspace.shared().selectFile((savePanel.url?.path)!, inFileViewerRootedAtPath: (savePanel.directoryURL?.path)!)
let notification = NSUserNotification()
notification.title = "Export Server Profile succeed!".localized
notification.informativeText = "Successful Export \(self.profiles.count) items".localized
NSUserNotificationCenter.default
.deliver(notification)
}
}