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 pathViewControllerRsyncParameters.swift
343 lines (319 loc) · 15.5 KB
/
ViewControllerRsyncParameters.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
//
// ViewControllerRsyncParameters.swift
// Rsync
// The ViewController for rsync parameters.
//
// Created by Thomas Evensen on 13/02/16.
// Copyright © 2016 Thomas Evensen. All rights reserved.
//
// swiftlint:disable line_length function_body_length type_body_length cyclomatic_complexity
import Cocoa
import Foundation
// protocol for returning if userparams is updated or not
protocol RsyncUserParams: AnyObject {
func rsyncuserparamsupdated()
}
// Protocol for sending selected index in tableView
// The protocol is implemented in ViewControllertabMain
protocol GetSelecetedIndex: AnyObject {
func getindex() -> Int?
}
class ViewControllerRsyncParameters: NSViewController, SetConfigurations, Index {
var comboBoxValues = [String]()
var diddissappear: Bool = false
@IBOutlet var param1: NSTextField!
@IBOutlet var param2: NSTextField!
@IBOutlet var param3: NSTextField!
@IBOutlet var param4: NSTextField!
@IBOutlet var param5: NSTextField!
@IBOutlet var param8: NSTextField!
@IBOutlet var param9: NSTextField!
@IBOutlet var param10: NSTextField!
@IBOutlet var param11: NSTextField!
@IBOutlet var param12: NSTextField!
@IBOutlet var param13: NSTextField!
@IBOutlet var param14: NSTextField!
@IBOutlet var rsyncdaemon: NSButton!
@IBOutlet var sshport: NSTextField!
@IBOutlet var compressparameter: NSButton!
@IBOutlet var esshparameter: NSButton!
@IBOutlet var deleteparamater: NSButton!
@IBOutlet var sshkeypathandidentityfile: NSTextField!
@IBOutlet var combo8: NSComboBox!
@IBOutlet var combo9: NSComboBox!
@IBOutlet var combo10: NSComboBox!
@IBOutlet var combo11: NSComboBox!
@IBOutlet var combo12: NSComboBox!
@IBOutlet var combo13: NSComboBox!
@IBOutlet var combo14: NSComboBox!
@IBAction func close(_: NSButton) {
view.window?.close()
}
@IBAction func closeview(_: NSButton) {
view.window?.close()
}
@IBAction func togglersyncdaemon(_: NSButton) {
if let index = index() {
switch rsyncdaemon.state {
case .on:
configurations?.removeesshparameter(index: index, delete: true)
param5.stringValue = configurations?.getConfigurations()?[index].parameter5 ?? ""
esshparameter.state = .on
case .off:
configurations?.removeesshparameter(index: index, delete: false)
param5.stringValue = (configurations?.getConfigurations()?[index].parameter5 ?? "") + " ssh"
esshparameter.state = .off
default:
return
}
}
}
@IBAction func removecompressparameter(_: NSButton) {
if let index = index() {
switch compressparameter.state {
case .on:
configurations?.removecompressparameter(index: index, delete: true)
case .off:
configurations?.removecompressparameter(index: index, delete: false)
default:
break
}
param3.stringValue = configurations?.getConfigurations()?[index].parameter3 ?? ""
}
}
@IBAction func removeesshparameter(_: NSButton) {
if let index = index() {
switch esshparameter.state {
case .on:
configurations?.removeesshparameter(index: index, delete: true)
param5.stringValue = configurations?.getConfigurations()?[index].parameter5 ?? ""
case .off:
configurations?.removeesshparameter(index: index, delete: false)
param5.stringValue = (configurations?.getConfigurations()?[index].parameter5 ?? "") + " ssh"
default:
break
}
}
}
@IBAction func removedeleteparameter(_: NSButton) {
if let index = index() {
switch deleteparamater.state {
case .on:
configurations?.removeedeleteparameter(index: index, delete: true)
case .off:
configurations?.removeedeleteparameter(index: index, delete: false)
default:
break
}
param4.stringValue = configurations?.getConfigurations()?[index].parameter4 ?? ""
}
}
// Function for enabling backup of changed files in a backup catalog.
// Parameters are appended to last two parameters (12 and 13).
@IBAction func backup(_: NSButton) {
if let index = index() {
if let configurations: [Configuration] = configurations?.getConfigurations() {
let param = ComboboxRsyncParameters(config: configurations[index])
switch backupbutton.state {
case .on:
initcombox(combobox: combo12, index: param.indexandvaluersyncparameter(SuffixstringsRsyncParameters().backupstrings[0]).0)
param12.stringValue = param.indexandvaluersyncparameter(SuffixstringsRsyncParameters().backupstrings[0]).1
let hiddenID = self.configurations?.gethiddenID(index: (self.index())!)
guard (hiddenID ?? -1) > -1 else { return }
let localcatalog = self.configurations?.getResourceConfiguration(hiddenID ?? -1, resource: .localCatalog)
let localcatalogParts = (localcatalog as AnyObject).components(separatedBy: "/")
initcombox(combobox: combo13, index: param.indexandvaluersyncparameter(SuffixstringsRsyncParameters().backupstrings[1]).0)
param13.stringValue = "../backup" + "_" + localcatalogParts[localcatalogParts.count - 2]
case .off:
initcombox(combobox: combo12, index: 0)
param12.stringValue = ""
initcombox(combobox: combo13, index: 0)
param13.stringValue = ""
initcombox(combobox: combo14, index: 0)
param14.stringValue = ""
default: break
}
}
}
}
// Function for enabling suffix date + time changed files.
// Parameters are appended to last parameter (14).
@IBOutlet var suffixButton: NSButton!
@IBAction func suffix(_: NSButton) {
if let index = index() {
suffixButton2.state = .off
if let configurations: [Configuration] = configurations?.getConfigurations() {
let param = ComboboxRsyncParameters(config: configurations[index])
switch suffixButton.state {
case .on:
let suffix = SuffixstringsRsyncParameters().suffixstringfreebsd
initcombox(combobox: combo14, index: param.indexandvaluersyncparameter(suffix).0)
param14.stringValue = param.indexandvaluersyncparameter(suffix).1
case .off:
initcombox(combobox: combo14, index: 0)
param14.stringValue = ""
default:
break
}
}
}
}
@IBOutlet var suffixButton2: NSButton!
@IBAction func suffix2(_: NSButton) {
if let index = index() {
if let configurations: [Configuration] = configurations?.getConfigurations() {
let param = ComboboxRsyncParameters(config: configurations[index])
suffixButton.state = .off
switch suffixButton2.state {
case .on:
let suffix = SuffixstringsRsyncParameters().suffixstringlinux
initcombox(combobox: combo14, index: param.indexandvaluersyncparameter(suffix).0)
param14.stringValue = param.indexandvaluersyncparameter(suffix).1
case .off:
initcombox(combobox: combo14, index: 0)
param14.stringValue = ""
default:
break
}
}
}
}
@IBOutlet var backupbutton: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear() {
super.viewDidAppear()
// Check if there is another view open, if yes close it..
if let view = SharedReference.shared.getvcref(viewcontroller: .vcrsyncparameters) as? ViewControllerRsyncParameters {
weak var closeview: ViewControllerRsyncParameters?
closeview = view
closeview?.closeview()
}
SharedReference.shared.setvcref(viewcontroller: .vcrsyncparameters, nsviewcontroller: self)
guard diddissappear == false else { return }
if let index = index() {
if let configurations: [Configuration] = configurations?.getConfigurations() {
let param = ComboboxRsyncParameters(config: configurations[index])
comboBoxValues = param.getComboBoxValues()
backupbutton.state = .off
suffixButton.state = .off
suffixButton2.state = .off
param1.stringValue = configurations[index].parameter1
param2.stringValue = configurations[index].parameter2
param3.stringValue = configurations[index].parameter3
param4.stringValue = configurations[index].parameter4
if configurations[index].parameter5.isEmpty == false {
param5.stringValue = configurations[index].parameter5 + " " + configurations[index].parameter6
}
if configurations[index].parameter3.isEmpty == true {
compressparameter.state = .on
} else {
compressparameter.state = .off
}
if configurations[index].parameter4.isEmpty == true {
deleteparamater.state = .on
} else {
deleteparamater.state = .off
}
if configurations[index].parameter5.isEmpty == true {
esshparameter.state = .on
} else {
esshparameter.state = .off
}
let value8 = param.getParameter(rsyncparameternumber: 8).0
initcombox(combobox: combo8, index: value8)
param8.stringValue = param.getParameter(rsyncparameternumber: 8).1
let value9 = param.getParameter(rsyncparameternumber: 9).0
initcombox(combobox: combo9, index: value9)
param9.stringValue = param.getParameter(rsyncparameternumber: 9).1
let value10 = param.getParameter(rsyncparameternumber: 10).0
initcombox(combobox: combo10, index: value10)
param10.stringValue = param.getParameter(rsyncparameternumber: 10).1
let value11 = param.getParameter(rsyncparameternumber: 11).0
initcombox(combobox: combo11, index: value11)
param11.stringValue = param.getParameter(rsyncparameternumber: 11).1
let value12 = param.getParameter(rsyncparameternumber: 12).0
initcombox(combobox: combo12, index: value12)
param12.stringValue = param.getParameter(rsyncparameternumber: 12).1
let value13 = param.getParameter(rsyncparameternumber: 13).0
initcombox(combobox: combo13, index: value13)
param13.stringValue = param.getParameter(rsyncparameternumber: 13).1
let value14 = param.getParameter(rsyncparameternumber: 14).0
initcombox(combobox: combo14, index: value14)
param14.stringValue = param.getParameter(rsyncparameternumber: 14).1
if configurations[index].rsyncdaemon != nil {
rsyncdaemon.state = NSControl.StateValue(rawValue: configurations[index].rsyncdaemon!)
} else {
rsyncdaemon.state = .off
}
if configurations[index].sshport != nil {
sshport.stringValue = String(configurations[index].sshport!)
}
if (configurations[index].sshkeypathandidentityfile ?? "").isEmpty == false {
sshkeypathandidentityfile.stringValue = configurations[index].sshkeypathandidentityfile!
}
}
}
}
override func viewDidDisappear() {
super.viewDidDisappear()
diddissappear = true
SharedReference.shared.setvcref(viewcontroller: .vcrsyncparameters, nsviewcontroller: nil)
}
// Function for saving changed or new parameters for one configuration.
@IBAction func update(_: NSButton) {
if var configurations: [Configuration] = configurations?.getConfigurations() {
guard configurations.count > 0 else { return }
// Get the index of selected configuration
if let index = index() {
let param = SetrsyncParameter()
configurations[index].parameter8 = param.setrsyncparameter(indexComboBox:
combo8.indexOfSelectedItem, value: getValue(value: param8.stringValue))
configurations[index].parameter9 = param.setrsyncparameter(indexComboBox:
combo9.indexOfSelectedItem, value: getValue(value: param9.stringValue))
configurations[index].parameter10 = param.setrsyncparameter(indexComboBox:
combo10.indexOfSelectedItem, value: getValue(value: param10.stringValue))
configurations[index].parameter11 = param.setrsyncparameter(indexComboBox:
combo11.indexOfSelectedItem, value: getValue(value: param11.stringValue))
configurations[index].parameter12 = param.setrsyncparameter(indexComboBox:
combo12.indexOfSelectedItem, value: getValue(value: param12.stringValue))
configurations[index].parameter13 = param.setrsyncparameter(indexComboBox:
combo13.indexOfSelectedItem, value: getValue(value: param13.stringValue))
configurations[index].parameter14 = param.setrsyncparameter(indexComboBox:
combo14.indexOfSelectedItem, value: getValue(value: param14.stringValue))
configurations[index].rsyncdaemon = rsyncdaemon.state.rawValue
if let port = sshport {
configurations[index].sshport = Int(port.stringValue)
}
if let sshkeypathandidentityfile = sshkeypathandidentityfile {
if sshkeypathandidentityfile.stringValue.isEmpty == false {
configurations[index].sshkeypathandidentityfile = sshkeypathandidentityfile.stringValue
} else {
configurations[index].sshkeypathandidentityfile = nil
}
}
// Update configuration in memory before saving
self.configurations?.updateConfigurations(configurations[index], index: index)
}
}
view.window?.close()
}
// There are eight comboboxes, all eight are initalized during ViewDidLoad and the correct index is set.
private func initcombox(combobox: NSComboBox, index: Int) {
guard index > -1 else { return }
combobox.removeAllItems()
combobox.addItems(withObjectValues: comboBoxValues)
combobox.selectItem(at: index)
}
// Returns nil or value from stringvalue (rsync parameters)
private func getValue(value: String) -> String? {
guard value.isEmpty == false else { return nil }
return value
}
}
extension ViewControllerRsyncParameters {
func closeview() {
view.window?.close()
}
}