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 71
/
Copy pathViewControllerNewConfigurations.swift
354 lines (327 loc) · 13.9 KB
/
ViewControllerNewConfigurations.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//
// ViewControllerNewConfigurations.swift
// Rsync
//
// Created by Thomas Evensen on 13/02/16.
// Copyright © 2016 Thomas Evensen. All rights reserved.
//
// swiftlint:disable function_body_length cyclomatic_complexity type_body_length
import Cocoa
import Foundation
enum Typebackup {
case synchronize
case snapshots
case syncremote
}
class ViewControllerNewConfigurations: NSViewController, SetConfigurations, Delay, Index, VcMain, Checkforrsync, Help {
var tabledata: [NSMutableDictionary]?
let archive: String = "--archive"
let verbose: String = "--verbose"
let compress: String = "--compress"
let delete: String = "--delete"
let eparam: String = "-e"
let ssh: String = "ssh"
var outputprocess: OutputfromProcess?
// Reference to rsync parameters to use in combox
var comboBoxValues = [SharedReference.shared.synchronize,
SharedReference.shared.snapshot,
SharedReference.shared.syncremote]
var backuptypeselected: Typebackup = .synchronize
var diddissappear: Bool = false
weak var reloadtabledata: Reloadandrefresh?
@IBOutlet var viewParameter1: NSTextField!
@IBOutlet var viewParameter2: NSTextField!
@IBOutlet var viewParameter3: NSTextField!
@IBOutlet var viewParameter4: NSTextField!
@IBOutlet var viewParameter5: NSTextField!
@IBOutlet var localCatalog: NSTextField!
@IBOutlet var offsiteCatalog: NSTextField!
@IBOutlet var offsiteUsername: NSTextField!
@IBOutlet var offsiteServer: NSTextField!
@IBOutlet var backupID: NSTextField!
@IBOutlet var backuptype: NSComboBox!
@IBOutlet var addingtrailingbackslash: NSButton!
@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!
// Assist
@IBOutlet var comboremoteusers: NSComboBox!
@IBOutlet var comboremotecomputers: NSComboBox!
@IBOutlet var combocatalogs: NSComboBox!
@IBOutlet var combolocalhome: NSComboBox!
var assist: Assist?
// Sidebar Clear button
@IBAction func delete(_: NSButton) {
globalMainQueue.async { () in
self.resetinputfields()
}
}
private func changelabels() {
switch backuptype.indexOfSelectedItem {
case 2:
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")
}
}
@IBAction func setbackuptype(_: NSComboBox) {
switch backuptype.indexOfSelectedItem {
case 0:
backuptypeselected = .synchronize
case 1:
backuptypeselected = .snapshots
case 2:
backuptypeselected = .syncremote
default:
backuptypeselected = .synchronize
}
changelabels()
}
override func viewDidLoad() {
super.viewDidLoad()
localCatalog.toolTip = NSLocalizedString("By using Finder drag and drop filepaths.", comment: "Tooltip")
offsiteCatalog.toolTip = NSLocalizedString("By using Finder drag and drop filepaths.", comment: "Tooltip")
SharedReference.shared.setvcref(viewcontroller: .vcnewconfigurations, nsviewcontroller: self)
initcombox(combobox: backuptype, index: 0)
}
override func viewDidAppear() {
super.viewDidAppear()
// sidebaractionsDelegate?.sidebaractions(action: .addviewbuttons)
backuptypeselected = .synchronize
addingtrailingbackslash.state = .off
backuptype.selectItem(at: 0)
guard diddissappear == false else { return }
viewParameter1.stringValue = archive
viewParameter2.stringValue = verbose
viewParameter3.stringValue = compress
viewParameter4.stringValue = delete
viewParameter5.stringValue = eparam + " " + ssh
changelabels()
// Assist
initialize()
}
override func viewDidDisappear() {
super.viewDidDisappear()
diddissappear = true
}
private func initcombox(combobox: NSComboBox, index: Int) {
combobox.removeAllItems()
combobox.addItems(withObjectValues: comboBoxValues)
combobox.selectItem(at: index)
}
private func resetinputfields() {
localCatalog.stringValue = ""
offsiteCatalog.stringValue = ""
offsiteUsername.stringValue = ""
offsiteServer.stringValue = ""
backupID.stringValue = ""
pretask.stringValue = ""
executepretask.state = .off
posttask.stringValue = ""
executeposttask.state = .off
haltshelltasksonerror.state = .off
}
// Sidebar Add button
@IBAction func addtask(_: NSButton) {
if let newconfig = qualifynewconfig() {
configurations?.addNewConfigurations(newconfig)
resetinputfields()
reloadtabledata = SharedReference.shared.getvcref(viewcontroller: .vctabmain) as? ViewControllerMain
reloadtabledata?.reloadtabledata()
}
}
private func qualifynewconfig() -> Configuration? {
if localCatalog.stringValue.hasSuffix("/") == false, addingtrailingbackslash.state == .off {
localCatalog.stringValue += "/"
}
if offsiteCatalog.stringValue.hasSuffix("/") == false, addingtrailingbackslash.state == .off {
offsiteCatalog.stringValue += "/"
}
var newconfig = Configuration()
newconfig.task = SharedReference.shared.synchronize
newconfig.backupID = backupID.stringValue
newconfig.localCatalog = localCatalog.stringValue
newconfig.offsiteCatalog = offsiteCatalog.stringValue
newconfig.offsiteServer = offsiteServer.stringValue
newconfig.offsiteUsername = offsiteUsername.stringValue
newconfig.parameter1 = archive
newconfig.parameter2 = verbose
newconfig.parameter3 = compress
newconfig.parameter4 = delete
newconfig.parameter5 = eparam
newconfig.parameter6 = ssh
newconfig.dateRun = ""
if backuptypeselected == .snapshots {
newconfig.snapshotnum = 1
newconfig.task = SharedReference.shared.snapshot
// Must be connected to create base remote snapshot catalog
guard Validatenewconfigs(newconfig, true).validated == true else { return nil }
outputprocess = OutputfromProcess()
// If connected create base remote snapshotcatalog
snapshotcreateremotecatalog(newconfig, outputprocess)
} else if backuptypeselected == .syncremote {
guard offsiteServer.stringValue.isEmpty == false else { return nil }
newconfig.task = SharedReference.shared.syncremote
}
// Pre task
if pretask.stringValue.isEmpty == false {
if executepretask.state == .on {
newconfig.executepretask = 1
} else {
newconfig.executepretask = 0
}
newconfig.pretask = pretask.stringValue
} else {
newconfig.executepretask = 0
}
// Post task
if posttask.stringValue.isEmpty == false {
if executeposttask.state == .on {
newconfig.executeposttask = 1
} else {
newconfig.executeposttask = 0
}
newconfig.pretask = pretask.stringValue
} else {
newconfig.executeposttask = 0
}
// Haltpretast on error
if haltshelltasksonerror.state == .on {
newconfig.haltshelltasksonerror = 1
} else {
newconfig.haltshelltasksonerror = 0
}
guard Validatenewconfigs(newconfig, true).validated == true else { return nil }
return newconfig
}
func snapshotcreateremotecatalog(_ config: Configuration, _: OutputfromProcess?) {
guard config.offsiteServer.isEmpty == false else { return }
let args = SnapshotCreateCatalogArguments(config: config)
let updatecurrent = CommandProcess(command: args.getCommand(),
arguments: args.getArguments(),
processtermination: processtermination)
updatecurrent.executeProcess()
}
@IBAction func selectlocalcatalog(_: NSButton) {
let openPanel = NSOpenPanel()
openPanel.canChooseFiles = false
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = true
openPanel.canCreateDirectories = false
openPanel.title = NSLocalizedString("select catalog", comment: "")
openPanel.beginSheetModal(for: view.window!, completionHandler: { num in
if num == NSApplication.ModalResponse.OK {
let path = openPanel.url?.path ?? ""
self.localCatalog.stringValue = path
}
})
}
@IBAction func selectoffsiteCatalog(_: NSButton) {
let openPanel = NSOpenPanel()
openPanel.canChooseFiles = false
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = true
openPanel.canCreateDirectories = false
openPanel.title = NSLocalizedString("select catalog", comment: "")
openPanel.beginSheetModal(for: view.window!, completionHandler: { num in
if num == NSApplication.ModalResponse.OK {
let path = openPanel.url?.path ?? ""
self.offsiteCatalog.stringValue = path
}
})
}
@IBAction func addremote(_: NSButton) {
if let home = combolocalhome.objectValue as? String,
let catalog = combocatalogs.objectValue as? String,
let user = comboremoteusers.objectValue as? String,
let remotecomputer = comboremotecomputers.objectValue as? String
{
var transfer = [String]()
transfer.append(home + "/" + catalog)
transfer.append("~/" + catalog)
transfer.append(user)
transfer.append(remotecomputer)
assisttransfer(values: transfer)
}
}
@IBAction func addlocal(_: NSButton) {
if let home = combolocalhome.objectValue as? String,
let catalog = combocatalogs.objectValue as? String
{
var transfer = [String]()
transfer.append(home + "/" + catalog)
transfer.append("/mounted_Volume/" + catalog)
assisttransfer(values: transfer)
}
}
private func assisttransfer(values: [String]?) {
if let values = values {
switch values.count {
case 2:
if localCatalog.stringValue.isEmpty != true || offsiteCatalog.stringValue.isEmpty != true {
let question: String = NSLocalizedString("Overwrite data", comment: "Logg") + "?"
let text: String = NSLocalizedString("Cancel or Add", comment: "Add schedule")
let dialog: String = NSLocalizedString("Add", comment: "Add schedule")
let answer = Alerts.dialogOrCancel(question: question, text: text, dialog: dialog)
if answer {
localCatalog.stringValue = values[0]
offsiteCatalog.stringValue = values[1]
}
} else {
localCatalog.stringValue = values[0]
offsiteCatalog.stringValue = values[1]
}
case 4:
// remote
if localCatalog.stringValue.isEmpty != true || offsiteCatalog.stringValue.isEmpty != true ||
offsiteUsername.stringValue.isEmpty != true || offsiteServer.stringValue.isEmpty != true
{
let question: String = NSLocalizedString("Overwrite data", comment: "Logg") + "?"
let text: String = NSLocalizedString("Cancel or Add", comment: "Add schedule")
let dialog: String = NSLocalizedString("Add", comment: "Add schedule")
let answer = Alerts.dialogOrCancel(question: question, text: text, dialog: dialog)
if answer {
localCatalog.stringValue = values[0]
offsiteCatalog.stringValue = values[1]
offsiteUsername.stringValue = values[2]
offsiteServer.stringValue = values[3]
}
} else {
localCatalog.stringValue = values[0]
offsiteCatalog.stringValue = values[1]
offsiteUsername.stringValue = values[2]
offsiteServer.stringValue = values[3]
}
default:
return
}
}
}
private func initialize() {
assist = Assist(configurations: configurations?.getConfigurations())
if let assist = assist {
initcomboxes(combobox: comboremotecomputers, values: assist.remoteservers)
initcomboxes(combobox: comboremoteusers, values: assist.remoteusers)
initcomboxes(combobox: combocatalogs, values: assist.catalogs)
initcomboxes(combobox: combolocalhome, values: assist.localhome)
}
}
private func initcomboxes(combobox: NSComboBox, values: Set<String>?) {
combobox.removeAllItems()
combobox.addItems(withObjectValues: Array(values ?? []))
if values?.count ?? 0 > 0 {
combobox.selectItem(at: 0)
} else {
combobox.stringValue = ""
}
}
}
extension ViewControllerNewConfigurations {
func processtermination(_: [String]?) {}
}