]> git.r.bdr.sh - rbdr/captura/blame_incremental - Captura/Data/CapturaURLDecoder.swift
Format the code
[rbdr/captura] / Captura / Data / CapturaURLDecoder.swift
... / ...
CommitLineData
1/*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
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.
8
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.
13
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.
16 */
17import Foundation
18
19protocol ConfigureActionProtocol {
20 var action: String { get }
21 var fps: Int? { get }
22 var outputs: OutputFormatSetting? { get }
23 var backend: URL? { get }
24 var backendOutput: OutputFormatSetting? { get }
25 var keepLocalFiles: Bool? { get }
26}
27
28protocol RecordActionProtocol {
29 var action: String { get }
30
31 var x: Int? { get }
32 var y: Int? { get }
33 var width: Int? { get }
34 var height: Int? { get }
35 var preventResize: Bool? { get }
36 var preventMove: Bool? { get }
37 var fps: Int? { 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 }
45}
46
47// The concrete implementations
48struct ConfigureAction: ConfigureActionProtocol {
49 let action: String
50 var fps: Int?
51 var outputs: OutputFormatSetting?
52 var backend: URL?
53 var backendOutput: OutputFormatSetting?
54 var keepLocalFiles: Bool?
55}
56
57struct RecordAction: RecordActionProtocol {
58 let action: String
59 var x: Int?
60 var y: Int?
61 var width: Int?
62 var height: Int?
63 var preventResize: Bool?
64 var preventMove: Bool?
65 var fps: Int?
66 var outputs: OutputFormatSetting?
67 var backend: URL?
68 var backendOutput: OutputFormatSetting?
69 var keepLocalFiles: Bool?
70 var autoStart: Bool?
71 var skipBackend: Bool
72 var maxLength: Int?
73}
74
75enum CapturaAction {
76 case record(RecordAction)
77 case configure(ConfigureAction)
78}
79
80struct CapturaURLDecoder {
81
82 static func decodeParams(url: URL) -> CapturaAction? {
83 guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
84 let params = components.queryItems
85 else {
86 return nil
87 }
88
89 var paramsDict = [String: Any]()
90
91 params.forEach { item in
92 paramsDict[item.name] = item.value
93 }
94
95 guard let action = paramsDict["action"] as? String else {
96 return nil
97 }
98
99 switch action {
100 case "configure":
101 var fps = Int(paramsDict["fps"] as? String ?? "")
102 let backend = URL(string: paramsDict["backend"] as? String ?? "")
103 let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
104 let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
105 var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
106
107 if fps != nil {
108 fps = min(10, max(4, fps!))
109 }
110
111 if backendOutput == .all {
112 backendOutput = .gifOnly
113 }
114
115 return .configure(
116 ConfigureAction(
117 action: action,
118 fps: fps,
119 outputs: outputs,
120 backend: backend,
121 backendOutput: backendOutput,
122 keepLocalFiles: keepLocalFiles
123 ))
124
125 case "record":
126 let x = Int(paramsDict["x"] as? String ?? "")
127 let y = Int(paramsDict["y"] as? String ?? "")
128 let width = Int(paramsDict["width"] as? String ?? "")
129 let height = Int(paramsDict["height"] as? String ?? "")
130 let preventResize = Bool(paramsDict["prevent_resize"] as? String ?? "")
131 let preventMove = Bool(paramsDict["prevent_move"] as? String ?? "")
132 var fps = Int(paramsDict["fps"] as? String ?? "")
133 let backend = URL(string: paramsDict["backend"] as? String ?? "")
134 let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
135 let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
136 var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
137 let autoStart = Bool(paramsDict["auto_start"] as? String ?? "")
138 var maxLength = Int(paramsDict["max_length"] as? String ?? "")
139
140 if fps != nil {
141 fps = min(10, max(4, fps!))
142 }
143
144 if maxLength != nil {
145 maxLength = min(300, max(1, fps!))
146 }
147
148 if backendOutput == .all {
149 backendOutput = .gifOnly
150 }
151
152 var skipBackend = false
153 if let backendString = paramsDict["backend"] as? String {
154 if backendString == "" {
155 skipBackend = true
156 }
157 }
158
159 return .record(
160 RecordAction(
161 action: action,
162 x: x,
163 y: y,
164 width: width,
165 height: height,
166 preventResize: preventResize,
167 preventMove: preventMove,
168 fps: fps,
169 outputs: outputs,
170 backend: backend,
171 backendOutput: backendOutput,
172 keepLocalFiles: keepLocalFiles,
173 autoStart: autoStart,
174 skipBackend: skipBackend,
175 maxLength: maxLength
176 ))
177
178 default:
179 return nil
180 }
181 }
182}