<commit_before>//
//  ViewControllerVerify.swift
//  RsyncOSX
//
//  Created by <NAME> on 26.07.2018.
//  Copyright © 2018 <NAME>. All rights reserved.
//
// swiftlint:disable line_length type_body_length function_body_length

import Foundation
import Cocoa

class ViewControllerVerify: NSViewController, SetConfigurations, Index, VcMain, Connected, Setcolor, Checkforrsync {

    var outputprocess: OutputProcess?
    var index: Int?
    var lastindex: Int?
    var estimatedindex: Int?
    var gotremoteinfo: Bool = false
    private var complete: Bool = false
    private var processRefererence: ProcessCmd?
    let lastdate: String = NSLocalizedString("Date last backup:", comment: "Verify")
    let dayssince: String = NSLocalizedString("Days since last backup:", comment: "Verify")

    @IBOutlet weak var outputtable: NSTableView!
    @IBOutlet weak var working: NSProgressIndicator!
    @IBOutlet weak var verifybutton: NSButton!
    @IBOutlet weak var changedbutton: NSButton!

    @IBOutlet weak var transferredNumber: NSTextField!
    @IBOutlet weak var transferredNumberSizebytes: NSTextField!
    @IBOutlet weak var newfiles: NSTextField!
    @IBOutlet weak var deletefiles: NSTextField!
    @IBOutlet weak var totalNumber: NSTextField!
    @IBOutlet weak var totalDirs: NSTextField!
    @IBOutlet weak var totalNumberSizebytes: NSTextField!
    @IBOutlet weak var localtotalNumber: NSTextField!
    @IBOutlet weak var localtotalDirs: NSTextField!
    @IBOutlet weak var localtotalNumberSizebytes: NSTextField!
    @IBOutlet weak var gotit: NSTextField!
    @IBOutlet weak var datelastbackup: NSTextField!
    @IBOutlet weak var dayslastbackup: NSTextField!
    @IBOutlet weak var rsynccommanddisplay: NSTextField!
    @IBOutlet weak var verifyradiobutton: NSButton!
    @IBOutlet weak var changedradiobutton: NSButton!

    @IBOutlet weak var localcatalog: NSTextField!
    @IBOutlet weak var remotecatalog: NSTextField!
    @IBOutlet weak var remoteserver: NSTextField!

    @IBAction func totinfo(_ sender: NSButton) {
        guard self.checkforrsync() == false else { return }
        globalMainQueue.async(execute: { () -> Void in
            self.presentAsSheet(self.viewControllerRemoteInfo!)
        })
    }

    @IBAction func quickbackup(_ sender: NSButton) {
        guard self.checkforrsync() == false else { return }
        self.openquickbackup()
    }

    @IBAction func automaticbackup(_ sender: NSButton) {
        self.presentAsSheet(self.viewControllerEstimating!)
    }

    // Selecting profiles
    @IBAction func profiles(_ sender: NSButton) {
        globalMainQueue.async(execute: { () -> Void in
            self.presentAsSheet(self.viewControllerProfile!)
        })
    }

    // Userconfiguration button
    @IBAction func userconfiguration(_ sender: NSButton) {
        globalMainQueue.async(execute: { () -> Void in
            self.presentAsSheet(self.viewControllerUserconfiguration!)
        })
    }

    @IBAction func verify(_ sender: NSButton) {
        guard self.index != nil else { return }
        self.rsynccommanddisplay.stringValue = Displayrsyncpath(index: self.index!, display: .verify).displayrsyncpath ?? ""
        self.verifyradiobutton.state = .on
        self.changedradiobutton.state = .off
        self.gotit.textColor = setcolor(nsviewcontroller: self, color: .white)
        let gotit: String = NSLocalizedString("Verifying, please wait...", comment: "Verify")
        self.gotit.stringValue = gotit
        self.enabledisablebuttons(enable: false)
        self.working.startAnimation(nil)
        if let arguments = self.configurations?.arguments4verify(index: self.index!) {
            self.outputprocess = OutputProcess()
            self.outputprocess?.addlinefromoutput(str: "*** Verify ***")
            self.verifyandchanged(arguments: arguments)
        }
    }

    @IBAction func changed(_ sender: NSButton) {
        guard self.index != nil else { return }
        self.rsynccommanddisplay.stringValue = Displayrsyncpath(index: self.index!, display: .restore).displayrsyncpath ?? ""
        self.changedradiobutton.state = .on
        self.verifyradiobutton.state = .off
        self.gotit.textColor = setcolor(nsviewcontroller: self, color: .white)
        let gotit: String = NSLocalizedString("Computing changed, please wait...", comment: "Verify")
        self.gotit.stringValue = gotit
        self.enabledisablebuttons(enable: false)
        self.working.startAnimation(nil)
        if let arguments = self.configurations?.arguments4restore(index: self.index!, argtype: .argdryRun) {
            self.outputprocess = OutputProcess()
            self.outputprocess?.addlinefromoutput(str: "*** Changed ***")
            self.verifyandchanged(arguments: arguments)
        }
    }

    private func verifyandchanged(arguments: [String]) {
        let verifytask = ProcessCmd(command: nil, arguments: arguments)
        verifytask.setupdateDelegate(object: self)
        verifytask.executeProcess(outputprocess: self.outputprocess)
        self.processRefererence = verifytask
    }

    @IBAction func info(_ sender: NSButton) {
        let resources = Resources()
        NSWorkspace.shared.open(URL(string: resources.getResource(resource: .verify))!)
    }

    @IBAction func displayrsynccommand(_ sender: NSButton) {
        guard self.index != nil else {
            self.rsynccommanddisplay.stringValue = ""
            return
        }
        if self.verifyradiobutton.state == .on {
            self.rsynccommanddisplay.stringValue = Displayrsyncpath(index: self.index!, display: .verify).displayrsyncpath ?? ""
        } else {
            self.rsynccommanddisplay.stringValue = Displayrsyncpath(index: self.index!, display: .restore).displayrsyncpath ?? ""
        }
    }

    // Abort button
    @IBAction func abort(_ sender: NSButton) {
        self.lastindex = self.index
        guard self.processRefererence != nil else { return }
        self.processRefererence!.abortProcess()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        ViewControllerReference.shared.setvcref(viewcontroller: .vcverify, nsviewcontroller: self)
        self.outputtable.delegate = self
        self.outputtable.dataSource = self
        self.working.usesThreadedAnimation = true
    }

    override func viewDidAppear() {
        super.viewDidAppear()
        self.index = self.index()
        if let index = self.index {
            let config = self.configurations!.getConfigurations()[index]
            guard config.task != ViewControllerReference.shared.syncremote else {
                self.gotit.stringValue = ""
                self.verifybutton.isEnabled = false
                self.changedbutton.isEnabled = false
                self.resetinfo()
                return
            }
            guard self.connected(config: config) == true else {
                self.gotit.textColor = setcolor(nsviewcontroller: self, color: .red)
                let dontgotit: String = NSLocalizedString("Seems not to be connected...", comment: "Verify")
                self.gotit.stringValue = dontgotit
                self.verifybutton.isEnabled = false
                self.changedbutton.isEnabled = false
                self.resetinfo()
                return
            }
            guard index != self.lastindex ?? -1 else { return }
            guard self.estimatedindex ?? -1 != index else { return }
            self.resetinfo()
            self.setinfo()
            self.enabledisablebuttons(enable: false)
            self.estimatedindex = index
            let gotit: String = NSLocalizedString("Getting information, please wait ...", comment: "Verify")
            self.gotit.textColor = setcolor(nsviewcontroller: self, color: .green)
            self.gotit.stringValue = gotit
            self.gotremoteinfo = false
            self.complete = false
            let datelastbackup = self.configurations?.getConfigurations()[index].dateRun ?? ""
            if datelastbackup.isEmpty == false {
                let date = datelastbackup.en_us_date_from_string()
                self.datelastbackup.stringValue = NSLocalizedString("Date last backup:", comment: "Remote Info")
                    + " " + date.localized_string_from_date()
            } else {
                self.datelastbackup.stringValue = NSLocalizedString("Date last backup:", comment: "Remote Info")
            }
            let numberlastbackup = self.configurations?.getConfigurations()[index].dayssincelastbackup ?? ""
            self.dayslastbackup.stringValue = self.dayssince +  " " + numberlastbackup
            self.estimateremoteinfo(index: index, local: true)
        } else {
            self.gotit.textColor = setcolor(nsviewcontroller: self, color: .red)
            let task: String = NSLocalizedString("Please select a task in Execute ...", comment: "Verify")
            self.gotit.stringValue = task
            self.outputprocess = nil
            globalMainQueue.async(execute: { () -> Void in
                self.resetinfo()
                self.outputtable.reloadData()
            })
        }
    }

    override func viewDidDisappear() {
        super.viewDidDisappear()
        self.lastindex = self.index
    }

    private func enabledisablebuttons(enable: Bool) {
        self.verifybutton.isEnabled = enable
        self.changedbutton.isEnabled = enable
    }

    private func estimateremoteinfo(index: Int, local: Bool) {<commit_msg>Update ViewControllerVerify.swift<commit_after>//
//  ViewControllerVerify.swift
//  RsyncOSX
//
//  Created by <NAME> on 26.07.2018.
//  Copyright © 2018 <NAME>. All rights reserved.
//
// swiftlint:disable line_length type_body_length function_body_length

import Foundation
import Cocoa

class ViewControllerVerify: NSViewController, SetConfigurations, Index, VcMain, Connected, Setcolor, Checkforrsync {

    var outputprocess: OutputProcess?
    var index: Int?
    var lastindex: Int?
    var estimatedindex: Int?
    var gotremoteinfo: Bool = false
    private var complete: Bool = false
    private var processRefererence: ProcessCmd?
    let lastdate: String = NSLocalizedString("Date last backup:", comment: "Verify")
    let dayssince: String = NSLocalizedString("Days since last backup:", comment: "Verify")

    @IBOutlet weak var outputtable: NSTableView!
    @IBOutlet weak var working: NSProgressIndicator!
    @IBOutlet weak var verifybutton: NSButton!
    @IBOutlet weak var changedbutton: NSButton!

    @IBOutlet weak var transferredNumber: NSTextField!
    @IBOutlet weak var transferredNumberSizebytes: NSTextField!
    @IBOutlet weak var newfiles: NSTextField!
    @IBOutlet weak var deletefiles: NSTextField!
    @IBOutlet weak var totalNumber: NSTextField!
    @IBOutlet weak var totalDirs: NSTextField!
    @IBOutlet weak var totalNumberSizebytes: NSTextField!
    @IBOutlet weak var localtotalNumber: NSTextField!
    @IBOutlet weak var localtotalDirs: NSTextField!
    @IBOutlet weak var localtotalNumberSizebytes: NSTextField!
    @IBOutlet weak var gotit: NSTextField!
    @IBOutlet weak var datelastbackup: NSTextField!
    @IBOutlet weak var dayslastbackup: NSTextField!
    @IBOutlet weak var rsynccommanddisplay: NSTextField!
    @IBOutlet weak var verifyradiobutton: NSButton!
    @IBOutlet weak var changedradiobutton: NSButton!

    @IBOutlet weak var localcatalog: NSTextField!
    @IBOutlet weak var remotecatalog: NSTextField!
    @IBOutlet weak var remoteserver: NSTextField!

    @IBAction func totinfo(_ sender: NSButton) {
        guard self.checkforrsync() == false else { return }
        globalMainQueue.async(execute: { () -> Void in
            self.presentAsSheet(self.viewControllerRemoteInfo!)
        })
    }

    @IBAction func quickbackup(_ sender: NSButton) {
        guard self.checkforrsync() == false else { return }
        self.openquickbackup()
    }

    @IBAction func automaticbackup(_ sender: NSButton) {
        self.presentAsSheet(self.viewControllerEstimating!)
    }

    // Selecting profiles
    @IBAction func profiles(_ sender: NSButton) {
        globalMainQueue.async(execute: { () -> Void in
            self.presentAsSheet(self.viewControllerProfile!)
        })
    }

    // Userconfiguration button
    @IBAction func userconfiguration(_ sender: NSButton) {
        globalMainQueue.async(execute: { () -> Void in
            self.presentAsSheet(self.viewControllerUserconfiguration!)
        })
    }

    @IBAction func verify(_ sender: NSButton) {
        guard self.index != nil else { return }
        self.rsynccommanddisplay.stringValue = Displayrsyncpath(index: self.index!, display: .verify).displayrsyncpath ?? ""
        self.verifyradiobutton.state = .on
        self.changedradiobutton.state = .off
        self.gotit.textColor = setcolor(nsviewcontroller: self, color: .white)
        let gotit: String = NSLocalizedString("Verifying, please wait...", comment: "Verify")
        self.gotit.stringValue = gotit
        self.enabledisablebuttons(enable: false)
        self.working.startAnimation(nil)
        if let arguments = self.configurations?.arguments4verify(index: self.index!) {
            self.outputprocess = OutputProcess()
            self.outputprocess?.addlinefromoutput(str: "*** Verify ***")
            self.verifyandchanged(arguments: arguments)
        }
    }

    @IBAction func changed(_ sender: NSButton) {
        guard self.index != nil else { return }
        self.rsynccommanddisplay.stringValue = Displayrsyncpath(index: self.index!, display: .restore).displayrsyncpath ?? ""
        self.changedradiobutton.state = .on
        self.verifyradiobutton.state = .off
        self.gotit.textColor = setcolor(nsviewcontroller: self, color: .white)
        let gotit: String = NSLocalizedString("Computing changed, please wait...", comment: "Verify")
        self.gotit.stringValue = gotit
        self.enabledisablebuttons(enable: false)
        self.working.startAnimation(nil)
        if let arguments = self.configurations?.arguments4restore(index: self.index!, argtype: .argdryRun) {
            self.outputprocess = OutputProcess()
            self.outputprocess?.addlinefromoutput(str: "*** Changed ***")
            self.verifyandchanged(arguments: arguments)
        }
    }

    private func verifyandchanged(arguments: [String]) {
        let verifytask = ProcessCmd(command: nil, arguments: arguments)
        verifytask.setupdateDelegate(object: self)
        verifytask.executeProcess(outputprocess: self.outputprocess)
        self.processRefererence = verifytask
    }

    @IBAction func info(_ sender: NSButton) {
        let resources = Resources()
        NSWorkspace.shared.open(URL(string: resources.getResource(resource: .verify))!)
    }

    @IBAction func displayrsynccommand(_ sender: NSButton) {
        guard self.index != nil else {
            self.rsynccommanddisplay.stringValue = ""
            return
        }
        if self.verifyradiobutton.state == .on {
            self.rsynccommanddisplay.stringValue = Displayrsyncpath(index: self.index!, display: .verify).displayrsyncpath ?? ""
        } else {
            self.rsynccommanddisplay.stringValue = Displayrsyncpath(index: self.index!, display: .restore).displayrsyncpath ?? ""
        }
    }

    // Abort button
    @IBAction func abort(_ sender: NSButton) {
        self.lastindex = self.index
        guard self.processRefererence != nil else { return }
        self.processRefererence!.abortProcess()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        ViewControllerReference.shared.setvcref(viewcontroller: .vcverify, nsviewcontroller: self)
        self.outputtable.delegate = self
        self.outputtable.dataSource = self
        self.working.usesThreadedAnimation = true
    }

    override func viewDidAppear() {
        super.viewDidAppear()
        self.index = self.index()
        if let index = self.index {
            let config = self.configurations!.getConfigurations()[index]
            guard config.task != ViewControllerReference.shared.syncremote else {
                self.gotit.stringValue = ""
                self.verifybutton.isEnabled = false
                self.changedbutton.isEnabled = false
                self.resetinfo()
                return
            }
            guard self.connected(config: config) == true else {
                self.gotit.textColor = setcolor(nsviewcontroller: self, color: .red)
                let dontgotit: String = NSLocalizedString("Seems not to be connected...", comment: "Verify")
                self.gotit.stringValue = dontgotit
                self.verifybutton.isEnabled = false
                self.changedbutton.isEnabled = false
                self.resetinfo()
                return
            }
            guard index != self.lastindex ?? -1 else { return }
            guard self.estimatedindex ?? -1 != index else { return }
            self.resetinfo()
            self.setinfo()
            self.enabledisablebuttons(enable: false)
            self.estimatedindex = index
            let gotit: String = NSLocalizedString("Getting information, please wait ...", comment: "Verify")
            self.gotit.textColor = setcolor(nsviewcontroller: self, color: .green)
            self.gotit.stringValue = gotit
            self.gotremoteinfo = false
            self.complete = false
            let datelastbackup = self.configurations?.getConfigurations()[index].dateRun ?? ""
            if datelastbackup.isEmpty == false {
                let date = datelastbackup.en_us_date_from_string()
                self.datelastbackup.stringValue = NSLocalizedString("Date last backup:", comment: "Remote Info")
                    + " " + date.localized_string_from_date()
            } else {
                self.datelastbackup.stringValue = NSLocalizedString("Date last backup:", comment: "Remote Info")
            }
            let numberlastbackup = self.configurations?.getConfigurations()[index].dayssincelastbackup ?? ""
            self.dayslastbackup.stringValue = self.dayssince +  " " + numberlastbackup
            self.estimateremoteinfo(index: index, local: true)
        } else {
            self.gotit.textColor = setcolor(nsviewcontroller: self, color: .green)
            let task: String = NSLocalizedString("Please select a task in Execute ...", comment: "Verify")
            self.gotit.stringValue = task
            self.outputprocess = nil
            globalMainQueue.async(execute: { () -> Void in
                self.resetinfo()
                self.outputtable.reloadData()
            })
        }
    }

    override func viewDidDisappear() {
        super.viewDidDisappear()
        self.lastindex = self.index
    }

    private func enabledisablebuttons(enable: Bool) {
        self.verifybutton.isEnabled = enable
        self.changedbutton.isEnabled = enable
    }

    private func estimateremoteinfo(index: Int, local: Bool) {
{'fraction_non_alphanumeric': 0.09500949567419287, 'fraction_numerical': 0.0016881198565098122, 'mean_word_length': 2.6631884057971016, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1990271', 'n_tokens_mistral': 5269, 'n_tokens_neox': 5132, 'n_words': 1187}