This repository was archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathViewControllerEdit.swift
213 lines (200 loc) · 8.52 KB
/
ViewControllerEdit.swift
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// ViewControllerEdit.swift
// RsyncOSXver30
//
// Created by Thomas Evensen on 05/09/2016.
// Copyright © 2016 Thomas Evensen. All rights reserved.
//
// swiftlint:disable line_length function_body_length
import Cocoa
import Foundation
class ViewControllerEdit: NSViewController, SetConfigurations, Index, Delay {
@IBOutlet var localCatalog: NSTextField!
@IBOutlet var offsiteCatalog: NSTextField!
@IBOutlet var offsiteUsername: NSTextField!
@IBOutlet var offsiteServer: NSTextField!
@IBOutlet var backupID: NSTextField!
@IBOutlet var snapshotnum: NSTextField!
@IBOutlet var stringlocalcatalog: NSTextField!
@IBOutlet var stringremotecatalog: NSTextField!
@IBOutlet var pretask: NSTextField!
@IBOutlet var executepretask: NSButton!
@IBOutlet var posttask: NSTextField!
@IBOutlet var executeposttask: NSButton!
@IBOutlet var haltshelltasksonerror: NSButton!
weak var reloadtabledata: Reloadandrefresh?
var index: Int?
@IBAction func enabledisableresetsnapshotnum(_: NSButton) {
if let config: Configuration = configurations?.getConfigurations()?[index!] {
guard config.task == SharedReference.shared.snapshot else { return }
let info: String = NSLocalizedString("Dont change the snapshot num if you don´t know what you are doing...", comment: "Snapshots")
Alerts.showInfo(info: info)
if snapshotnum.isEnabled {
snapshotnum.isEnabled = false
} else {
snapshotnum.isEnabled = true
}
}
}
// Close and dismiss view
@IBAction func close(_: NSButton) {
view.window?.close()
}
// Update configuration, save and dismiss view
@IBAction func update(_: NSButton) {
if var config: [Configuration] = configurations?.getConfigurations() {
if let index = index() {
config[index].localCatalog = localCatalog.stringValue
config[index].offsiteCatalog = offsiteCatalog.stringValue
config[index].offsiteServer = offsiteServer.stringValue
config[index].offsiteUsername = offsiteUsername.stringValue
config[index].backupID = backupID.stringValue
if snapshotnum.stringValue.count > 0 {
config[index].snapshotnum = Int(snapshotnum.stringValue)
}
// Pre task
if pretask.stringValue.isEmpty == false {
if executepretask.state == .on {
config[index].executepretask = 1
} else {
config[index].executepretask = 0
}
config[index].pretask = pretask.stringValue
} else {
config[index].executepretask = nil
config[index].pretask = nil
}
// Post task
if posttask.stringValue.isEmpty == false {
if executeposttask.state == .on {
config[index].executeposttask = 1
} else {
config[index].executeposttask = 0
}
config[index].posttask = posttask.stringValue
} else {
config[index].executeposttask = nil
config[index].posttask = nil
}
// Halt on error
if haltshelltasksonerror.state == .on {
config[index].haltshelltasksonerror = 1
} else {
config[index].haltshelltasksonerror = 0
}
guard Validatenewconfigs(config[index], false).validated == true else { return }
configurations?.updateConfigurations(config[index], index: index)
reloadtabledata = SharedReference.shared.getvcref(viewcontroller: .vctabmain) as? ViewControllerMain
reloadtabledata?.reloadtabledata()
view.window?.close()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
snapshotnum.delegate = self
}
override func viewDidAppear() {
super.viewDidAppear()
// Check if there is another view open, if yes close it..
if let view = SharedReference.shared.getvcref(viewcontroller: .vcedit) as? ViewControllerEdit {
weak var closeview: ViewControllerEdit?
closeview = view
closeview?.closeview()
}
SharedReference.shared.setvcref(viewcontroller: .vcedit, nsviewcontroller: self)
localCatalog.stringValue = ""
offsiteCatalog.stringValue = ""
offsiteUsername.stringValue = ""
offsiteServer.stringValue = ""
backupID.stringValue = ""
pretask.stringValue = ""
executepretask.state = .off
posttask.stringValue = ""
executeposttask.state = .off
haltshelltasksonerror.state = .off
if let index = index() {
self.index = index
if let config: Configuration = configurations?.getConfigurations()?[index] {
localCatalog.stringValue = config.localCatalog
offsiteCatalog.stringValue = config.offsiteCatalog
offsiteUsername.stringValue = config.offsiteUsername
offsiteServer.stringValue = config.offsiteServer
backupID.stringValue = config.backupID
if let snapshotnum = config.snapshotnum {
self.snapshotnum.stringValue = String(snapshotnum)
}
pretask.stringValue = config.pretask ?? ""
posttask.stringValue = config.posttask ?? ""
if let executepretask = config.executepretask {
if executepretask == 1 {
self.executepretask.state = .on
} else {
self.executepretask.state = .off
}
} else {
executepretask.state = .off
}
if let executeposttask = config.executeposttask {
if executeposttask == 1 {
self.executeposttask.state = .on
} else {
self.executeposttask.state = .off
}
} else {
executeposttask.state = .off
}
if let haltshelltasksonerror = config.haltshelltasksonerror {
if haltshelltasksonerror == 1 {
self.haltshelltasksonerror.state = .on
} else {
self.haltshelltasksonerror.state = .off
}
}
changelabels()
}
}
}
override func viewDidDisappear() {
super.viewDidDisappear()
SharedReference.shared.setvcref(viewcontroller: .vcedit, nsviewcontroller: nil)
}
private func changelabels() {
if let index = index {
if let config = configurations?.getConfigurations()?[index] {
switch config.task {
case SharedReference.shared.syncremote:
stringlocalcatalog.stringValue = NSLocalizedString("Source catalog:", comment: "Tooltip")
stringremotecatalog.stringValue = NSLocalizedString("Destination catalog:", comment: "Tooltip")
default:
stringlocalcatalog.stringValue = NSLocalizedString("Local catalog:", comment: "Tooltip")
stringremotecatalog.stringValue = NSLocalizedString("Remote catalog:", comment: "Tooltip")
}
}
}
}
}
extension ViewControllerEdit: NSTextFieldDelegate {
func controlTextDidChange(_: Notification) {
delayWithSeconds(0.5) {
if let index = self.index {
if let config = self.configurations?.getConfigurations()?[index] {
if let num = Int(self.snapshotnum.stringValue) {
guard num < config.snapshotnum ?? 0, num > 0 else {
self.snapshotnum.stringValue = String(config.snapshotnum ?? 1)
return
}
} else {
self.snapshotnum.stringValue = String(config.snapshotnum ?? 1)
}
}
}
}
}
}
// Needed for automatically close view if another config is selected
extension ViewControllerEdit {
func closeview() {
view.window?.close()
}
}