team-ljyzipelo

공개
411줄16 KB
1use crate::archive_ui::*;
2use eframe::egui;
3use std::path::PathBuf;
4
5pub struct ArchiveApp {
6 pub model: Model,
7 factory: Box<dyn Factory>,
8 open_text: String,
9 destination: String,
10 gzip_name: String,
11 sources: String,
12 zip_output: String,
13 password: String,
14 password_challenge: Option<u64>,
15 aes256: bool,
16 create: bool,
17 open: bool,
18 selected_only: bool,
19 method: Method,
20 fixture: bool,
21}
22impl ArchiveApp {
23 pub fn new(factory: Box<dyn Factory>, startup: Option<PathBuf>, fixture: bool) -> Self {
24 let mut app = Self {
25 model: Model::default(),
26 factory,
27 open_text: String::new(),
28 destination: String::new(),
29 gzip_name: String::new(),
30 sources: String::new(),
31 zip_output: String::new(),
32 password: String::new(),
33 password_challenge: None,
34 aes256: false,
35 create: false,
36 open: false,
37 selected_only: false,
38 method: Method::Deflate,
39 fixture,
40 };
41 if let Some(path) = startup {
42 app.open_path(path);
43 }
44 app
45 }
46 fn clear_password(&mut self) {
47 let mut bytes = std::mem::take(&mut self.password).into_bytes();
48 bytes.fill(0);
49 }
50 pub fn open_path(&mut self, archive: PathBuf) {
51 self.clear_password();
52 self.model
53 .begin(Start::List { archive }, self.factory.as_mut());
54 }
55 fn on_drop(&mut self, paths: Vec<PathBuf>) {
56 if self.model.busy() || paths.is_empty() {
57 return;
58 }
59 if paths.len() == 1 && !self.create {
60 self.open_path(paths[0].clone());
61 } else if paths.iter().all(|p| p.to_str().is_some()) {
62 self.sources = paths
63 .iter()
64 .map(|p| p.to_str().unwrap())
65 .collect::<Vec<_>>()
66 .join("\n");
67 self.create = true;
68 } else {
69 self.model.input_error = Some("ZIP 생성 경로는 현재 UTF-8 입력만 지원합니다".into());
70 }
71 }
72}
73impl ArchiveApp {
74 pub fn show(&mut self, root: &mut egui::Ui) {
75 self.model.poll();
76 let challenge = self.model.challenge.map(|(id, _, _)| id);
77 let new_challenge = challenge != self.password_challenge;
78 if new_challenge || challenge.is_none() {
79 self.clear_password();
80 }
81 self.password_challenge = challenge;
82 let ctx = root.ctx().clone();
83 let dropped = ctx.input(|i| {
84 i.raw
85 .dropped_files
86 .iter()
87 .map(|f| f.path().to_path_buf())
88 .collect::<Vec<_>>()
89 });
90 self.on_drop(dropped);
91 if ctx.input(|i| i.viewport().close_requested()) {
92 self.model.request_close();
93 if self.model.close != Close::Allowed {
94 ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
95 }
96 }
97 if self.model.close == Close::Allowed {
98 ctx.send_viewport_cmd(egui::ViewportCommand::Close);
99 }
100 egui::Panel::top("archive-actions").show(root, |ui| {
101 ui.horizontal_wrapped(|ui| {
102 ui.heading("Zipelo");
103 ui.add_enabled_ui(
104 !self.model.busy() && self.model.close == Close::Open,
105 |ui| {
106 if ui.button("파일 열기").clicked() {
107 self.open = true;
108 }
109 if ui.button("ZIP 만들기").clicked() {
110 self.create = true;
111 }
112 },
113 );
114 if ui
115 .add_enabled(self.model.busy(), egui::Button::new("취소 요청"))
116 .clicked()
117 {
118 self.clear_password();
119 self.model.cancel();
120 }
121 });
122 if self.fixture {
123 ui.colored_label(
124 egui::Color32::from_rgb(143, 69, 0),
125 "화면 시험 모드 / 파일을 읽거나 저장하지 않습니다",
126 );
127 }
128 });
129 egui::Panel::bottom("archive-status").show(root, |ui| {
130 ui.label(self.model.status());
131 if let Some((stage, done, total, entries)) =
132 self.model.progress.filter(|_| self.model.busy())
133 {
134 ui.label(format!(
135 "{} / {done}바이트 / {entries}개 처리",
136 stage_label(stage)
137 ));
138 if let Some(total) = total.filter(|n| *n > 0) {
139 ui.add(
140 egui::ProgressBar::new((done as f64 / total as f64).clamp(0.0, 1.0) as f32)
141 .show_percentage(),
142 );
143 } else if self.model.busy() {
144 ui.spinner();
145 }
146 }
147 if !self.model.busy() {
148 if let Some(t) = &self.model.terminal {
149 for line in terminal_lines(t) {
150 ui.label(line);
151 }
152 }
153 }
154 });
155 egui::CentralPanel::default_margins().show(root, |ui| {
156 if let Some(path) = &self.model.archive {
157 ui.heading(
158 path.file_name()
159 .unwrap_or(path.as_os_str())
160 .to_string_lossy(),
161 );
162 ui.label(path.display().to_string());
163 } else {
164 ui.heading("압축파일을 여기로 끌어 놓으세요");
165 }
166 if self.model.list_id.is_some() {
167 ui.add_enabled_ui(self.model.can_extract(), |ui| {
168 ui.horizontal_wrapped(|ui| {
169 ui.label("출력 폴더");
170 ui.text_edit_singleline(&mut self.destination);
171 });
172 if self
173 .model
174 .list_id
175 .as_ref()
176 .is_some_and(|id| id.format == Format::Gzip)
177 {
178 ui.horizontal(|ui| {
179 ui.label("출력 파일명");
180 ui.text_edit_singleline(&mut self.gzip_name);
181 });
182 }
183 ui.horizontal_wrapped(|ui| {
184 ui.checkbox(&mut self.selected_only, "선택한 항목만 풀기");
185 if ui.button("풀기").clicked() {
186 self.model.extract(
187 PathBuf::from(&self.destination),
188 self.selected_only,
189 self.model
190 .list_id
191 .as_ref()
192 .filter(|id| id.format == Format::Gzip)
193 .map(|_| self.gzip_name.clone()),
194 self.factory.as_mut(),
195 );
196 }
197 });
198 });
199 }
200 ui.separator();
201 egui::ScrollArea::both().show(ui, |ui| {
202 egui::Grid::new("archive-list")
203 .striped(true)
204 .min_col_width(90.0)
205 .show(ui, |ui| {
206 ui.label("선택");
207 ui.label("이름");
208 ui.label("크기");
209 ui.label("형식");
210 ui.label("암호");
211 ui.end_row();
212 for entry in &self.model.entries {
213 let mut selected = self.model.selected.contains(&entry.id);
214 if ui
215 .add_enabled(
216 !self.model.busy()
217 && matches!(entry.kind, Kind::File | Kind::Directory),
218 egui::Checkbox::without_text(&mut selected),
219 )
220 .on_hover_text(format!("{} 선택", entry.name))
221 .changed()
222 {
223 if selected {
224 self.model.selected.insert(entry.id);
225 } else {
226 self.model.selected.remove(&entry.id);
227 }
228 }
229 ui.label(&entry.name);
230 ui.label(
231 entry
232 .bytes
233 .map_or("알 수 없음".into(), |n| format!("{n} B")),
234 );
235 ui.label(match entry.kind {
236 Kind::File => "파일",
237 Kind::Directory => "폴더",
238 Kind::Link => "링크",
239 Kind::Other => "기타",
240 });
241 ui.label(if entry.encrypted { "있음" } else { "없음" });
242 ui.end_row();
243 }
244 });
245 if self.model.entries.is_empty() && self.model.list_id.is_some() {
246 ui.label("압축파일에 항목이 없습니다");
247 }
248 for (name, state) in &self.model.outputs {
249 ui.label(format!("{name}: {}", disposition_label(*state)));
250 }
251 });
252 });
253 if self.open {
254 egui::Window::new("파일 열기")
255 .collapsible(false)
256 .show(&ctx, |ui| {
257 ui.label("압축파일 절대 경로");
258 ui.text_edit_singleline(&mut self.open_text);
259 if ui.button("열기").clicked() {
260 self.open_path(self.open_text.clone().into());
261 self.open = false;
262 }
263 if ui.button("닫기").clicked() {
264 self.open = false;
265 }
266 });
267 }
268 if self.create {
269 egui::Window::new("ZIP 만들기")
270 .collapsible(false)
271 .show(&ctx, |ui| {
272 ui.add_enabled_ui(!self.model.busy(), |ui| {
273 ui.label("원본 파일 절대 경로 / 한 줄에 하나");
274 ui.add(egui::TextEdit::multiline(&mut self.sources).desired_rows(4));
275 ui.label("저장할 ZIP 절대 경로");
276 ui.text_edit_singleline(&mut self.zip_output);
277 ui.horizontal(|ui| {
278 ui.selectable_value(&mut self.method, Method::Deflate, "압축");
279 ui.selectable_value(&mut self.method, Method::Store, "묶기");
280 });
281 ui.checkbox(&mut self.aes256, "암호로 보호");
282 if ui.button("ZIP 만들기").clicked() {
283 let sources = self
284 .sources
285 .lines()
286 .map(|s| {
287 let path = PathBuf::from(s);
288 let name = path
289 .file_name()
290 .and_then(|n| n.to_str())
291 .unwrap_or("")
292 .to_owned();
293 (path, name)
294 })
295 .collect();
296 if self.model.begin(
297 Start::CreateZip {
298 sources,
299 output_file: self.zip_output.clone().into(),
300 method: self.method,
301 aes256: self.aes256,
302 },
303 self.factory.as_mut(),
304 ) {
305 self.create = false;
306 }
307 }
308 });
309 if ui.button("닫기").clicked() {
310 self.create = false;
311 }
312 });
313 }
314 if let Some((_, attempt, encrypt)) = self.model.challenge {
315 egui::Window::new(if encrypt {
316 "ZIP 암호 설정"
317 } else {
318 "압축파일 암호"
319 })
320 .collapsible(false)
321 .show(&ctx, |ui| {
322 ui.label(if encrypt {
323 "사용할 암호를 입력하세요"
324 } else if attempt > 1 {
325 "암호가 맞지 않습니다. 다시 입력하세요"
326 } else {
327 "암호를 입력하세요"
328 });
329 ui.label("암호");
330 let edit = ui.add(
331 egui::TextEdit::singleline(&mut self.password)
332 .password(true)
333 .id(egui::Id::new("archive-password-input"))
334 .char_limit(1024),
335 );
336 if new_challenge {
337 edit.request_focus();
338 }
339 if ui.button("암호 전달").clicked()
340 || (edit.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)))
341 {
342 let text = std::mem::take(&mut self.password);
343 self.model.password(text);
344 }
345 if ui.button("작업 취소").clicked() {
346 self.clear_password();
347 self.model.cancel();
348 }
349 });
350 }
351 match self.model.close {
352 Close::Confirm => {
353 egui::Window::new("작업 중 닫기")
354 .collapsible(false)
355 .show(&ctx, |ui| {
356 ui.label("취소를 요청하고 작업자 종료를 기다릴까요?");
357 if ui.button("계속 열기").clicked() {
358 self.model.keep_open();
359 }
360 if ui.button("취소 후 기다리기").clicked() {
361 self.model.confirm_close();
362 }
363 });
364 }
365 Close::Waiting => {
366 egui::Window::new("종료 확인 중")
367 .collapsible(false)
368 .show(&ctx, |ui| {
369 ui.label("취소 결과와 프로세스 종료를 기다립니다");
370 if ui.button("닫기 철회").clicked() {
371 self.model.keep_open();
372 }
373 });
374 }
375 Close::Review => {
376 egui::Window::new("결과 확인 후 닫기")
377 .collapsible(false)
378 .show(&ctx, |ui| {
379 ui.label(self.model.status());
380 if let Some(t) = &self.model.terminal {
381 for line in terminal_lines(t) {
382 ui.label(line);
383 }
384 }
385 if ui.button("계속 열기").clicked() {
386 self.model.keep_open();
387 }
388 if ui.button("닫기").clicked() {
389 self.model.acknowledge_close();
390 }
391 });
392 }
393 _ => {}
394 }
395 if self.model.busy() {
396 ctx.request_repaint_after(std::time::Duration::from_millis(30));
397 }
398 }
399}
400
401impl eframe::App for ArchiveApp {
402 fn ui(&mut self, root: &mut egui::Ui, _: &mut eframe::Frame) {
403 self.show(root);
404 }
405}
406
407impl Drop for ArchiveApp {
408 fn drop(&mut self) {
409 self.clear_password();
410 }
411}