X-Git-Url: https://git.r.bdr.sh/rbdr/captura/blobdiff_plain/ba17de891507da74fb07423803fd636a4457354c..505c1e620497828ffb914e05dd76d9ab124f144a:/Captura/Data/CapturaURLDecoder.swift?ds=sidebyside diff --git a/Captura/Data/CapturaURLDecoder.swift b/Captura/Data/CapturaURLDecoder.swift index 3fa6412..a325dbe 100644 --- a/Captura/Data/CapturaURLDecoder.swift +++ b/Captura/Data/CapturaURLDecoder.swift @@ -1,5 +1,20 @@ -import Foundation +/* + Copyright (C) 2024 Rubén Beltrán del Río + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see https://captura.tranquil.systems. + */ +import Foundation protocol ConfigureActionProtocol { var action: String { get } @@ -12,7 +27,7 @@ protocol ConfigureActionProtocol { protocol RecordActionProtocol { var action: String { get } - + var x: Int? { get } var y: Int? { get } var width: Int? { get } @@ -25,6 +40,8 @@ protocol RecordActionProtocol { var backendOutput: OutputFormatSetting? { get } var keepLocalFiles: Bool? { get } var autoStart: Bool? { get } + var skipBackend: Bool { get } + var maxLength: Int? { get } } // The concrete implementations @@ -51,48 +68,52 @@ struct RecordAction: RecordActionProtocol { var backendOutput: OutputFormatSetting? var keepLocalFiles: Bool? var autoStart: Bool? + var skipBackend: Bool + var maxLength: Int? } enum CapturaAction { - case record(RecordAction) - case configure(ConfigureAction) + case record(RecordAction) + case configure(ConfigureAction) } struct CapturaURLDecoder { - + static func decodeParams(url: URL) -> CapturaAction? { - guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false), - let params = components.queryItems else { - return nil - } + guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false), + let params = components.queryItems + else { + return nil + } + + var paramsDict = [String: Any]() + + params.forEach { item in + paramsDict[item.name] = item.value + } - var paramsDict = [String: Any]() + guard let action = paramsDict["action"] as? String else { + return nil + } - params.forEach { item in - paramsDict[item.name] = item.value + switch action { + case "configure": + var fps = Int(paramsDict["fps"] as? String ?? "") + let backend = URL(string: paramsDict["backend"] as? String ?? "") + let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "") + let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "") + var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "") + + if fps != nil { + fps = min(10, max(4, fps!)) } - guard let action = paramsDict["action"] as? String else { - return nil + if backendOutput == .all { + backendOutput = .gifOnly } - switch action { - case "configure": - var fps = Int(paramsDict["fps"] as? String ?? "") - let backend = URL(string: paramsDict["backend"] as? String ?? "") - let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "") - let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "") - var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "") - - if fps != nil { - fps = min(10, max(4, fps!)) - } - - if backendOutput == .all { - backendOutput = .gifOnly - } - - return .configure(ConfigureAction( + return .configure( + ConfigureAction( action: action, fps: fps, outputs: outputs, @@ -101,28 +122,42 @@ struct CapturaURLDecoder { keepLocalFiles: keepLocalFiles )) - case "record": - let x = Int(paramsDict["x"] as? String ?? "") - let y = Int(paramsDict["y"] as? String ?? "") - let width = Int(paramsDict["width"] as? String ?? "") - let height = Int(paramsDict["height"] as? String ?? "") - let preventResize = Bool(paramsDict["prevent_resize"] as? String ?? "") - let preventMove = Bool(paramsDict["prevent_move"] as? String ?? "") - var fps = Int(paramsDict["fps"] as? String ?? "") - let backend = URL(string: paramsDict["backend"] as? String ?? "") - let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "") - let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "") - var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "") - - if fps != nil { - fps = min(10, max(4, fps!)) - } - - if backendOutput == .all { - backendOutput = .gifOnly + case "record": + let x = Int(paramsDict["x"] as? String ?? "") + let y = Int(paramsDict["y"] as? String ?? "") + let width = Int(paramsDict["width"] as? String ?? "") + let height = Int(paramsDict["height"] as? String ?? "") + let preventResize = Bool(paramsDict["prevent_resize"] as? String ?? "") + let preventMove = Bool(paramsDict["prevent_move"] as? String ?? "") + var fps = Int(paramsDict["fps"] as? String ?? "") + let backend = URL(string: paramsDict["backend"] as? String ?? "") + let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "") + let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "") + var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "") + let autoStart = Bool(paramsDict["auto_start"] as? String ?? "") + var maxLength = Int(paramsDict["max_length"] as? String ?? "") + + if fps != nil { + fps = min(10, max(4, fps!)) + } + + if maxLength != nil { + maxLength = min(300, max(1, fps!)) + } + + if backendOutput == .all { + backendOutput = .gifOnly + } + + var skipBackend = false + if let backendString = paramsDict["backend"] as? String { + if backendString == "" { + skipBackend = true } - - return .record(RecordAction( + } + + return .record( + RecordAction( action: action, x: x, y: y, @@ -134,11 +169,14 @@ struct CapturaURLDecoder { outputs: outputs, backend: backend, backendOutput: backendOutput, - keepLocalFiles: keepLocalFiles + keepLocalFiles: keepLocalFiles, + autoStart: autoStart, + skipBackend: skipBackend, + maxLength: maxLength )) - default: - return nil - } + default: + return nil + } } }