-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathAddServer.swift
More file actions
95 lines (73 loc) · 3.04 KB
/
AddServer.swift
File metadata and controls
95 lines (73 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// AddServer.swift
// Postgres
//
// Created by Chris on 24/06/16.
// Copyright © 2016 postgresapp. All rights reserved.
//
import Cocoa
class AddServerViewController: NSViewController, MainWindowModelConsumer {
@objc dynamic var mainWindowModel: MainWindowModel!
@objc dynamic var name: String = "New Server"
@objc dynamic var port: UInt = 5432
@objc dynamic var varPath: String = ""
var availableBinaries: [PostgresBinary] = []
@objc dynamic var versions: [String] = []
@objc dynamic var selectedVersionIdx: Int = 0
override func viewDidLoad() {
loadVersions()
if availableBinaries.indices.contains(selectedVersionIdx) {
varPath = FileManager().applicationSupportDirectoryPath().appending("/var-\(availableBinaries[selectedVersionIdx].version)")
}
super.viewDidLoad()
}
@IBAction func versionChanged(_ sender: AnyObject?) {
if availableBinaries.indices.contains(selectedVersionIdx) {
let regex = try! NSRegularExpression(pattern: "\\d+(\\.\\d+)?$", options: .caseInsensitive)
varPath = regex.stringByReplacingMatches(in: varPath, options: [], range: NSRange(0..<varPath.utf16.count), withTemplate: NSRegularExpression.escapedTemplate(for: availableBinaries[selectedVersionIdx].version))
}
}
@IBAction func openChooseFolder(_ sender: AnyObject?) {
let openPanel = NSOpenPanel()
openPanel.allowsMultipleSelection = false
openPanel.canChooseFiles = false
openPanel.canChooseDirectories = true
openPanel.canCreateDirectories = true
openPanel.directoryURL = URL(fileURLWithPath: FileManager().applicationSupportDirectoryPath())
openPanel.beginSheetModal(for: self.view.window!) { (returnCode) in
if returnCode == NSApplication.ModalResponse.OK {
self.varPath = openPanel.url!.path
}
}
}
@IBAction func cancel(_ sender: AnyObject?) {
self.dismiss(nil)
}
@IBAction func createServer(_ sender: AnyObject?) {
guard self.view.window!.makeFirstResponder(nil) else { NSSound.beep(); return }
guard availableBinaries.indices.contains(selectedVersionIdx) else {
NSSound.beep()
return
}
for server in mainWindowModel.serverManager.servers {
if server.varPath == self.varPath {
let alert = NSAlert()
alert.messageText = "The Data Directory is already in use by server \"\(server.name)\"."
alert.informativeText = "Please choose a different location."
alert.addButton(withTitle: "OK")
alert.beginSheetModal(for: self.view.window!)
return
}
}
let server = Server(name: name, binPath: availableBinaries[selectedVersionIdx].binPath, port: port, varPath: varPath)
mainWindowModel.serverManager.servers.append(server)
mainWindowModel.selectedServerIndices = IndexSet(integer: mainWindowModel.serverManager.servers.indices.last!)
NotificationCenter.default.post(name: Server.PropertyChangedNotification, object: nil)
self.dismiss(nil)
}
private func loadVersions() {
availableBinaries = BinaryManager.shared.findAvailableBinaries()
versions = availableBinaries.map { $0.displayName }
selectedVersionIdx = versions.count-1
}
}