2 Copyright (C) 2024 Rubén Beltrán del Río
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://captura.tranquil.systems.
19 protocol ConfigureActionProtocol {
20 var action: String { get }
22 var outputs: OutputFormatSetting? { get }
23 var backend: URL? { get }
24 var backendOutput: OutputFormatSetting? { get }
25 var keepLocalFiles: Bool? { get }
28 protocol RecordActionProtocol {
29 var action: String { get }
33 var width: Int? { get }
34 var height: Int? { get }
35 var preventResize: Bool? { get }
36 var preventMove: Bool? { get }
38 var backend: URL? { get }
39 var outputs: OutputFormatSetting? { get }
40 var backendOutput: OutputFormatSetting? { get }
41 var keepLocalFiles: Bool? { get }
42 var autoStart: Bool? { get }
43 var skipBackend: Bool { get }
44 var maxLength: Int? { get }
47 // The concrete implementations
48 struct ConfigureAction: ConfigureActionProtocol {
51 var outputs: OutputFormatSetting?
53 var backendOutput: OutputFormatSetting?
54 var keepLocalFiles: Bool?
57 struct RecordAction: RecordActionProtocol {
63 var preventResize: Bool?
64 var preventMove: Bool?
66 var outputs: OutputFormatSetting?
68 var backendOutput: OutputFormatSetting?
69 var keepLocalFiles: Bool?
76 case record(RecordAction)
77 case configure(ConfigureAction)
80 struct CapturaURLDecoder {
82 static func decodeParams(url: URL) -> CapturaAction? {
83 guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
84 let params = components.queryItems else {
88 var paramsDict = [String: Any]()
90 params.forEach { item in
91 paramsDict[item.name] = item.value
94 guard let action = paramsDict["action"] as? String else {
100 var fps = Int(paramsDict["fps"] as? String ?? "")
101 let backend = URL(string: paramsDict["backend"] as? String ?? "")
102 let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
103 let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
104 var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
107 fps = min(10, max(4, fps!))
110 if backendOutput == .all {
111 backendOutput = .gifOnly
114 return .configure(ConfigureAction(
119 backendOutput: backendOutput,
120 keepLocalFiles: keepLocalFiles
124 let x = Int(paramsDict["x"] as? String ?? "")
125 let y = Int(paramsDict["y"] as? String ?? "")
126 let width = Int(paramsDict["width"] as? String ?? "")
127 let height = Int(paramsDict["height"] as? String ?? "")
128 let preventResize = Bool(paramsDict["prevent_resize"] as? String ?? "")
129 let preventMove = Bool(paramsDict["prevent_move"] as? String ?? "")
130 var fps = Int(paramsDict["fps"] as? String ?? "")
131 let backend = URL(string: paramsDict["backend"] as? String ?? "")
132 let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
133 let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
134 var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
135 let autoStart = Bool(paramsDict["auto_start"] as? String ?? "")
136 var maxLength = Int(paramsDict["max_length"] as? String ?? "")
139 fps = min(10, max(4, fps!))
142 if maxLength != nil {
143 maxLength = min(300, max(1, fps!))
146 if backendOutput == .all {
147 backendOutput = .gifOnly
150 var skipBackend = false
151 if let backendString = paramsDict["backend"] as? String {
152 if backendString == "" {
157 return .record(RecordAction(
163 preventResize: preventResize,
164 preventMove: preventMove,
168 backendOutput: backendOutput,
169 keepLocalFiles: keepLocalFiles,
170 autoStart: autoStart,
171 skipBackend: skipBackend,