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 pathViewControllerMain.swift
372 lines (341 loc) · 13.8 KB
/
ViewControllerMain.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Created by Thomas Evensen on 19/08/2016.
// Copyright © 2016 Thomas Evensen. All rights reserved.
//
// swiftlint:disable type_body_length line_length
import Cocoa
import Foundation
protocol SetProfileinfo: AnyObject {
func setprofile(profile: String?)
}
class ViewControllerMain: NSViewController, ReloadTable, Deselect, VcMain, Delay, Setcolor, Checkforrsync, Help, Connected {
// Main tableview
@IBOutlet var mainTableView: NSTableView!
// Progressbar indicating work
@IBOutlet var working: NSProgressIndicator!
@IBOutlet var info: NSTextField!
@IBOutlet var profilepopupbutton: NSPopUpButton!
// Reference to Configurations
var configurations: Configurations?
// Reference to the taskobjects
var singletask: SingleTask?
var executetasknow: ExecuteTaskNow?
// Index to selected row, index is set when row is selected
var localindex: Int?
var lastindex: Int?
// Indexes, multiple selection
var indexset: IndexSet?
var multipeselection: Bool = false
// Getting output from rsync
var outputprocess: OutputfromProcess?
// Send messages to the sidebar
weak var sidebaractionsDelegate: Sidebaractions?
// Toolbar - Find tasks and Execute backup
@IBAction func automaticbackup(_: NSButton) {
guard checkforrsync() == false else { return }
guard SharedReference.shared.process == nil else { return }
presentAsSheet(viewControllerEstimating!)
}
// Toolbar - Abort button
@IBAction func abort(_: NSButton) {
globalMainQueue.async { () in
self.abortOperations()
}
}
// Toolbar - Userconfiguration button
@IBAction func userconfiguration(_: NSButton) {
guard SharedReference.shared.process == nil else { return }
presentAsModalWindow(viewControllerUserconfiguration!)
}
// Toolbar - Estimate and Quickbackup
@IBAction func totinfo(_: NSButton) {
guard checkforrsync() == false else { return }
multipeselection = false
globalMainQueue.async { () in
self.presentAsSheet(self.viewControllerRemoteInfo!)
}
}
// Toolbar - Multiple select and execute
// Execute multipleselected tasks, only from main view
@IBAction func executemultipleselectedindexes(_: NSButton) {
guard checkforrsync() == false else { return }
guard SharedReference.shared.process == nil else { return }
guard indexset != nil else {
info.stringValue = Infoexecute().info(num: 6)
return
}
multipeselection = true
globalMainQueue.async { () in
self.presentAsSheet(self.viewControllerRemoteInfo!)
}
}
@IBAction func executetasknow(_: NSButton) {
guard SharedReference.shared.process == nil else { return }
guard checkforrsync() == false else { return }
guard localindex != nil else {
info.stringValue = Infoexecute().info(num: 1)
return
}
if let index = localindex {
executetask(index: index)
}
}
@IBAction func infoonetask(_: NSButton) {
guard SharedReference.shared.process == nil else { return }
guard localindex != nil else {
info.stringValue = Infoexecute().info(num: 1)
return
}
guard checkforrsync() == false else { return }
if let index = localindex {
if let task = configurations?.getConfigurations()?[index].task {
guard SharedReference.shared.synctasks.contains(task) else {
info.stringValue = Infoexecute().info(num: 7)
return
}
presentAsSheet(viewControllerInformationLocalRemote!)
}
}
}
// Presenting Information from Rsync
@IBAction func information(_: NSButton) {
globalMainQueue.async { () in
self.presentAsSheet(self.viewControllerInformation!)
}
}
// Selecting profiles
@IBAction func profiles(_: NSButton) {
guard SharedReference.shared.process == nil else { return }
presentAsModalWindow(viewControllerProfile!)
}
// Selecting About
@IBAction func about(_: NSButton) {
guard SharedReference.shared.process == nil else { return }
presentAsModalWindow(viewControllerAbout!)
}
// All ouput
@IBAction func alloutput(_: NSButton) {
guard SharedReference.shared.process == nil else { return }
presentAsModalWindow(viewControllerAllOutput!)
}
@IBAction func showHelp(_: AnyObject?) {
help()
}
@IBAction func delete(_: NSButton) {
guard SharedReference.shared.process == nil else { return }
delete()
}
func executetask(index: Int?) {
if let index = index {
if let task = configurations?.getConfigurations()?[index].task {
guard SharedReference.shared.synctasks.contains(task) else { return }
if let config = configurations?.getConfigurations()?[index] {
info.stringValue = Infoexecute().info(num: 11)
info.textColor = setcolor(nsviewcontroller: self, color: .green)
if PreandPostTasks(config: config).executepretask || PreandPostTasks(config: config).executeposttask {
executetasknow = ExecuteTaskNowShellOut(index: index)
} else {
executetasknow = ExecuteTaskNow(index: index)
}
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Create base profile catalog
CatalogProfile().createrootprofilecatalog()
// Check on Apple Silicon
let silicon = ProcessInfo().machineHardwareName?.contains("arm64") ?? false
if silicon {
SharedReference.shared.macosarm = true
} else {
SharedReference.shared.macosarm = false
}
// Must read userconfig when loading main view, view only load once
// ReadUserConfigurationPLIST()
ReadUserConfigurationJSON()
mainTableView.delegate = self
mainTableView.dataSource = self
mainTableView.allowsMultipleSelection = true
working.usesThreadedAnimation = true
SharedReference.shared.setvcref(viewcontroller: .vctabmain, nsviewcontroller: self)
mainTableView.target = self
mainTableView.doubleAction = #selector(ViewControllerMain.tableViewDoubleClick(sender:))
// configurations
configurations = Configurations(profile: nil)
initpopupbutton()
}
override func viewDidAppear() {
super.viewDidAppear()
// For sending messages to the sidebar
sidebaractionsDelegate = SharedReference.shared.getvcref(viewcontroller: .vcsidebar) as? ViewControllerSideBar
sidebaractionsDelegate?.sidebaractions(action: .mainviewbuttons)
if SharedReference.shared.initialstart == 0 {
view.window?.center()
SharedReference.shared.initialstart = 1
Task {
await CheckfornewversionofRsyncOSX().getversionsofrsyncosx()
}
}
if (configurations?.configurations?.count ?? 0) > 0 {
if localindex == nil {
globalMainQueue.async { () in
self.mainTableView.reloadData()
}
}
}
displayProfile()
// Display first time use
if SharedReference.shared.firsttime {
let question: String = NSLocalizedString("Welcome to RsyncOSX", comment: "Main")
let text: String = NSLocalizedString("There are some important info about the first time use of RsyncOSX", comment: "Main")
let dialog: String = NSLocalizedString("Open", comment: "Main")
let answer = Alerts.dialogOrCancel(question: question, text: text, dialog: dialog)
if answer {
NSWorkspace.shared.open(URL(string: Resources().getResource(resource: .firsttimeuse))!)
}
}
}
override func viewDidDisappear() {
super.viewDidDisappear()
multipeselection = false
}
func reset() {
info.stringValue = ""
// Close edit and parameters view if open
if let view = SharedReference.shared.getvcref(viewcontroller: .vcrsyncparameters) as? ViewControllerRsyncParameters {
weak var closeview: ViewControllerRsyncParameters?
closeview = view
closeview?.closeview()
}
if let view = SharedReference.shared.getvcref(viewcontroller: .vcedit) as? ViewControllerEdit {
weak var closeview: ViewControllerEdit?
closeview = view
closeview?.closeview()
}
}
// Execute tasks by double click in table
@objc(tableViewDoubleClick:) func tableViewDoubleClick(sender _: AnyObject) {
executeSingleTask()
}
// Single task can be activated by double click from table
func executeSingleTask() {
guard checkforrsync() == false else { return }
if let index = localindex {
if let task = configurations?.getConfigurations()?[index].task {
guard SharedReference.shared.synctasks.contains(task) else {
info.stringValue = Infoexecute().info(num: 6)
return
}
guard singletask != nil else {
// Dry run
info.stringValue = Infoexecute().info(num: 10)
info.textColor = setcolor(nsviewcontroller: self, color: .green)
singletask = SingleTask(index: index)
singletask?.synchronizesingletask()
return
}
// Real run
info.stringValue = Infoexecute().info(num: 11)
info.textColor = setcolor(nsviewcontroller: self, color: .green)
singletask?.synchronizesingletask()
}
}
}
// Function for setting profile
func displayProfile() {
weak var localprofileinfo: SetProfileinfo?
localprofileinfo = SharedReference.shared.getvcref(viewcontroller: .vcsidebar) as? ViewControllerSideBar
if let profile = configurations?.getProfile() {
localprofileinfo?.setprofile(profile: profile)
} else {
localprofileinfo?.setprofile(profile: nil)
}
}
func createandreloadconfigurations(profile: String?) {
if let profile = profile {
configurations = nil
configurations = Configurations(profile: profile)
} else {
configurations = nil
configurations = Configurations(profile: nil)
}
globalMainQueue.async { () in
self.mainTableView.reloadData()
}
}
func initpopupbutton() {
var profilestrings: [String]?
profilestrings = CatalogProfile().getcatalogsasstringnames()
profilestrings?.insert(NSLocalizedString("Default profile", comment: "default profile"), at: 0)
profilepopupbutton.removeAllItems()
profilepopupbutton.addItems(withTitles: profilestrings ?? [])
profilepopupbutton.selectItem(at: 0)
}
@IBAction func selectprofile(_: NSButton) {
var profile = profilepopupbutton.titleOfSelectedItem
let selectedindex = profilepopupbutton.indexOfSelectedItem
if profile == NSLocalizedString("Default profile", comment: "default profile") {
profile = nil
}
profilepopupbutton.selectItem(at: selectedindex)
if let profile = profile {
configurations = nil
configurations = Configurations(profile: profile)
} else {
configurations = nil
configurations = Configurations(profile: nil)
}
singletask = nil
localindex = nil
info.stringValue = ""
globalMainQueue.async { () in
self.mainTableView.reloadData()
}
displayProfile()
}
@IBAction func checksynchronizedfiles(_: NSButton) {
if let index = localindex {
if let config = configurations?.getConfigurations()?[index] {
guard config.task != SharedReference.shared.syncremote else {
info.stringValue = NSLocalizedString("Cannot verify a syncremote task...", comment: "Verify")
return
}
guard connected(config: config) == true else {
info.stringValue = NSLocalizedString("Seems not to be connected...", comment: "Verify")
return
}
let check = Checksynchronizedfiles(index: index)
check.checksynchronizedfiles()
}
}
}
func delete() {
guard SharedReference.shared.process == nil else { return }
guard localindex != nil else {
info.stringValue = Infoexecute().info(num: 1)
return
}
if let index = localindex {
deleterow(index: index)
}
}
func deleterow(index: Int?) {
if let index = index {
if let hiddenID = configurations?.gethiddenID(index: index) {
let question: String = NSLocalizedString("Delete selected task?", comment: "Execute")
let text: String = NSLocalizedString("Cancel or Delete", comment: "Execute")
let dialog: String = NSLocalizedString("Delete", comment: "Execute")
let answer = Alerts.dialogOrCancel(question: question, text: text, dialog: dialog)
if answer {
// Delete Configurations and Schedules by hiddenID
configurations?.deleteConfigurationsByhiddenID(hiddenID: hiddenID)
deselect()
reloadtabledata()
}
}
reset()
singletask = nil
}
}
}