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 pathextensionVCMain.swift
371 lines (327 loc) · 9.74 KB
/
extensionVCMain.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
//
// extensionVCMain.swift
// RsyncOSX
//
// Created by Thomas Evensen on 31.05.2018.
// Copyright © 2018 Thomas Evensen. All rights reserved.
//
import Cocoa
import Foundation
// Get output from rsync command
extension ViewControllerMain: GetOutput {
// Get information from rsync output.
func getoutput() -> [String] {
return TrimTwo(outputprocess?.getOutput() ?? []).trimmeddata
}
}
// Scheduled task are changed, read schedule again og redraw table
extension ViewControllerMain: Reloadandrefresh {
// Refresh tableView in main
func reloadtabledata() {
globalMainQueue.async { () in
self.mainTableView.reloadData()
}
}
}
// Get index of selected row
extension ViewControllerMain: GetSelecetedIndex {
func getindex() -> Int? {
return localindex
}
}
// New profile is loaded.
extension ViewControllerMain: NewProfile {
// Function is called from profiles when new or default profiles is seleceted
func newprofile(profile: String?, selectedindex: Int?) {
if let index = selectedindex {
profilepopupbutton.selectItem(at: index)
} else {
initpopupbutton()
}
reset()
singletask = nil
deselect()
// Read configurations
configurations = createconfigurationsobject(profile: profile)
// Make sure loading profile
displayProfile()
reloadtabledata()
}
func reloadprofilepopupbutton() {
globalMainQueue.async { () in
self.displayProfile()
}
}
func createconfigurationsobject(profile: String?) -> Configurations? {
configurations = nil
configurations = Configurations(profile: profile)
return configurations
}
}
// Check for remote connections, reload table when completed.
extension ViewControllerMain: Connections {
func displayConnections() {
globalMainQueue.async { () in
self.mainTableView.reloadData()
}
}
}
extension ViewControllerMain: NewVersionDiscovered {
func notifyNewVersion() {
globalMainQueue.async { () in
self.info.stringValue = Infoexecute().info(num: 9)
}
}
}
extension ViewControllerMain: DismissViewController {
func dismiss_view(viewcontroller: NSViewController) {
dismiss(viewcontroller)
globalMainQueue.async { () in
self.mainTableView.reloadData()
self.displayProfile()
}
}
}
// Deselect a row
extension ViewControllerMain: DeselectRowTable {
// deselect a row after row is deleted
func deselect() {
if let index = localindex {
SharedReference.shared.process = nil
localindex = nil
mainTableView.deselectRow(index)
}
}
}
// If rsync throws any error
extension ViewControllerMain: RsyncError {
func rsyncerror() {
// Set on or off in user configuration
globalMainQueue.async { () in
self.info.stringValue = "Rsync error, see logfile..."
self.info.textColor = self.setcolor(nsviewcontroller: self, color: .red)
self.info.isHidden = false
guard SharedReference.shared.haltonerror == true else { return }
self.deselect()
_ = InterruptProcess()
self.singletask?.error()
}
}
}
// If, for any reason, handling files or directory throws an error
extension ViewControllerMain: ErrorMessage {
func errormessage(errorstr: String, error errortype: RsyncOSXTypeErrors) {
globalMainQueue.async { () in
if errortype == .logfilesize {
self.info.stringValue = "Reduce size logfile, filesize is: " + errorstr
self.info.textColor = self.setcolor(nsviewcontroller: self, color: .red)
self.info.isHidden = false
} else {
self.outputprocess?.addlinefromoutput(str: errorstr + "\n" + errorstr)
self.info.stringValue = "Some error: see logfile."
self.info.textColor = self.setcolor(nsviewcontroller: self, color: .red)
self.info.isHidden = false
}
var message = [String]()
message.append(errorstr)
_ = Logfile(message, error: true)
}
}
}
// Abort task from progressview
extension ViewControllerMain: Abort {
// Abort the task
func abortOperations() {
_ = InterruptProcess()
working.stopAnimation(nil)
localindex = nil
info.stringValue = ""
}
}
// Extensions from here are used in newSingleTask
extension ViewControllerMain: StartStopProgressIndicatorSingleTask {
func startIndicatorExecuteTaskNow() {
working.startAnimation(nil)
}
func startIndicator() {
working.startAnimation(nil)
}
func stopIndicator() {
working.stopAnimation(nil)
}
}
extension ViewControllerMain: GetConfigurationsObject {
func getconfigurationsobject() -> Configurations? {
guard configurations != nil else { return nil }
return configurations
}
}
extension ViewControllerMain: ErrorOutput {
func erroroutput() {
info.stringValue = Infoexecute().info(num: 2)
}
}
extension ViewControllerMain: SendOutputProcessreference {
func sendoutputprocessreference(outputprocess: OutputfromProcess?) {
self.outputprocess = outputprocess
}
}
extension ViewControllerMain: OpenQuickBackup {
func openquickbackup() {
globalMainQueue.async { () in
self.presentAsSheet(self.viewControllerQuickBackup!)
}
}
}
extension ViewControllerMain: Count {
func maxCount() -> Int {
return TrimTwo(outputprocess?.getOutput() ?? []).maxnumber
}
func inprogressCount() -> Int {
return outputprocess?.getOutput()?.count ?? 0
}
}
extension ViewControllerMain: ViewOutputDetails {
func getalloutput() -> [String] {
return outputprocess?.getOutput() ?? []
}
func reloadtable() {
weak var localreloadDelegate: Reloadandrefresh?
localreloadDelegate = SharedReference.shared.getvcref(viewcontroller: .vcalloutput) as? ViewControllerAllOutput
localreloadDelegate?.reloadtabledata()
}
func appendnow() -> Bool {
if SharedReference.shared.getvcref(viewcontroller: .vcalloutput) != nil {
return true
} else {
return false
}
}
func outputfromrsync(data: [String]?) {
if outputprocess == nil {
outputprocess = OutputfromProcess()
}
if let data = data {
for i in 0 ..< data.count {
outputprocess?.addlinefromoutput(str: data[i])
}
} else {
outputprocess?.output = []
}
}
}
extension ViewControllerMain: OpenOutputfromrsync {
func openoutputfromrsync() {
presentAsModalWindow(viewControllerAllOutput!)
}
}
enum Color {
case red
case white
case green
case black
}
protocol Setcolor: AnyObject {
func setcolor(nsviewcontroller: NSViewController, color: Color) -> NSColor
}
extension Setcolor {
private func isDarkMode(view: NSView) -> Bool {
return view.effectiveAppearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
}
func setcolor(nsviewcontroller: NSViewController, color: Color) -> NSColor {
let darkmode = isDarkMode(view: nsviewcontroller.view)
switch color {
case .red:
return .red
case .white:
if darkmode {
return .white
} else {
return .black
}
case .green:
if darkmode {
return .green
} else {
return .blue
}
case .black:
if darkmode {
return .white
} else {
return .black
}
}
}
}
protocol Checkforrsync: AnyObject {
func checkforrsync() -> Bool
}
extension Checkforrsync {
func checkforrsync() -> Bool {
if SharedReference.shared.norsync == true {
_ = Norsync()
return true
} else {
return false
}
}
}
// Protocol for start,stop, complete progressviewindicator
protocol StartStopProgressIndicator: AnyObject {
func start()
func stop()
}
// Protocol for either completion of work or update progress when Process discovers a
// process termination and when filehandler discover data
protocol UpdateProgress: AnyObject {
func processTermination()
func fileHandler()
}
protocol ViewOutputDetails: AnyObject {
func reloadtable()
func appendnow() -> Bool
func getalloutput() -> [String]
func outputfromrsync(data: [String]?)
}
// Get multiple selected indexes
protocol GetMultipleSelectedIndexes: AnyObject {
func getindexes() -> [Int]
func multipleselection() -> Bool
}
extension ViewControllerMain: GetMultipleSelectedIndexes {
func multipleselection() -> Bool {
return multipeselection
}
func getindexes() -> [Int] {
if let indexes = indexset {
return indexes.map { $0 }
} else {
return []
}
}
}
extension ViewControllerMain: DeinitExecuteTaskNow {
func deinitexecutetasknow() {
executetasknow = nil
info.stringValue = Infoexecute().info(num: 0)
}
}
extension ViewControllerMain: DisableEnablePopupSelectProfile {
func enableselectpopupprofile() {
profilepopupbutton.isEnabled = true
}
func disableselectpopupprofile() {
profilepopupbutton.isEnabled = false
}
}
extension ViewControllerMain: Sidebarbuttonactions {
func sidebarbuttonactions(action: Sidebaractionsmessages) {
switch action {
case .Delete:
delete()
default:
return
}
}
}