ludusavi 0.18.0

Game save backup tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use std::collections::HashSet;

use iced::{Alignment, Length};

use crate::{
    cloud::{Remote, RemoteChoice},
    gui::{
        badge::Badge,
        button,
        common::{BrowseFileSubject, BrowseSubject, Message, Operation, Screen, ScrollSubject, UndoSubject},
        editor,
        game_list::GameList,
        icon::Icon,
        shortcuts::TextHistories,
        style,
        widget::{number_input, Button, Checkbox, Column, Container, Element, IcedParentExt, PickList, Row, Text},
    },
    lang::{Language, TRANSLATOR},
    prelude::{AVAILABLE_PARALELLISM, STEAM_DECK},
    resource::{
        cache::Cache,
        config::{BackupFormat, Config, SortKey, Theme, ZipCompression},
        manifest::Manifest,
    },
    scan::{DuplicateDetector, Duplication, OperationStatus},
};

const RCLONE_URL: &str = "https://rclone.org/downloads";

fn template(content: Column) -> Element {
    Container::new(content.spacing(15).align_items(Alignment::Center))
        .height(Length::Fill)
        .width(Length::Fill)
        .padding([0, 5, 5, 5])
        .center_x()
        .into()
}

fn make_status_row<'a>(status: &OperationStatus, duplication: Duplication) -> Row<'a> {
    Row::new()
        .padding([0, 20, 0, 20])
        .align_items(Alignment::Center)
        .spacing(15)
        .push(Text::new(TRANSLATOR.processed_games(status)).size(35))
        .push_if(
            || status.changed_games.new > 0,
            || Badge::new_entry_with_count(status.changed_games.new).view(),
        )
        .push_if(
            || status.changed_games.different > 0,
            || Badge::changed_entry_with_count(status.changed_games.different).view(),
        )
        .push(Text::new("|").size(35))
        .push(Text::new(TRANSLATOR.processed_bytes(status)).size(35))
        .push_if(
            || !duplication.resolved(),
            || Badge::new(&TRANSLATOR.badge_duplicates()).view(),
        )
}

#[derive(Default)]
pub struct Backup {
    pub log: GameList,
    pub previewed_games: HashSet<String>,
    pub duplicate_detector: DuplicateDetector,
    pub show_settings: bool,
}

impl Backup {
    pub fn new(config: &Config, cache: &Cache) -> Self {
        Self {
            log: GameList::with_recent_games(false, config, cache),
            ..Default::default()
        }
    }

    pub fn view(
        &self,
        config: &Config,
        manifest: &Manifest,
        operation: &Operation,
        histories: &TextHistories,
    ) -> Element {
        let screen = Screen::Backup;
        let sort = &config.backup.sort;

        let content = Column::new()
            .push(
                Row::new()
                    .padding([0, 20, 0, 20])
                    .spacing(20)
                    .align_items(Alignment::Center)
                    .push(button::backup_preview(operation))
                    .push(button::backup(operation))
                    .push(button::toggle_all_scanned_games(
                        self.log.all_entries_selected(config, false),
                    ))
                    .push(button::filter(Screen::Backup, self.log.search.show))
                    .push(button::settings(self.show_settings)),
            )
            .push(make_status_row(
                &self.log.compute_operation_status(config, false),
                self.duplicate_detector.overall(),
            ))
            .push(
                Row::new()
                    .padding([0, 20, 0, 20])
                    .spacing(20)
                    .align_items(Alignment::Center)
                    .push(Text::new(TRANSLATOR.backup_target_label()))
                    .push(histories.input(UndoSubject::BackupTarget))
                    .push(button::choose_folder(BrowseSubject::BackupTarget))
                    .push("|")
                    .push(Text::new(TRANSLATOR.sort_label()))
                    .push(
                        PickList::new(SortKey::ALL, Some(sort.key), move |value| Message::EditedSortKey {
                            screen,
                            value,
                        })
                        .style(style::PickList::Primary),
                    )
                    .push(button::sort_order(screen, sort.reversed)),
            )
            .push_if(
                || self.show_settings,
                || {
                    Row::new()
                        .padding([0, 20, 0, 20])
                        .spacing(20)
                        .height(30)
                        .align_items(Alignment::Center)
                        .push({
                            number_input(
                                config.backup.retention.full as i32,
                                TRANSLATOR.full_retention(),
                                1..=255,
                                |x| Message::EditedFullRetention(x as u8),
                            )
                        })
                        .push({
                            number_input(
                                config.backup.retention.differential as i32,
                                TRANSLATOR.differential_retention(),
                                0..=255,
                                |x| Message::EditedDiffRetention(x as u8),
                            )
                        })
                },
            )
            .push_if(
                || self.show_settings,
                || {
                    Row::new()
                        .padding([0, 20, 0, 20])
                        .spacing(20)
                        .align_items(Alignment::Center)
                        .push(
                            Row::new()
                                .spacing(5)
                                .align_items(Alignment::Center)
                                .push(Text::new(TRANSLATOR.backup_format_field()))
                                .push(
                                    PickList::new(
                                        BackupFormat::ALL,
                                        Some(config.backup.format.chosen),
                                        Message::SelectedBackupFormat,
                                    )
                                    .style(style::PickList::Primary),
                                ),
                        )
                        .push_if(
                            || config.backup.format.chosen == BackupFormat::Zip,
                            || {
                                Row::new()
                                    .spacing(5)
                                    .align_items(Alignment::Center)
                                    .push(Text::new(TRANSLATOR.backup_compression_field()))
                                    .push(
                                        PickList::new(
                                            ZipCompression::ALL,
                                            Some(config.backup.format.zip.compression),
                                            Message::SelectedBackupCompression,
                                        )
                                        .style(style::PickList::Primary),
                                    )
                            },
                        )
                        .push_some(|| match (config.backup.format.level(), config.backup.format.range()) {
                            (Some(level), Some(range)) => Some(number_input(
                                level,
                                TRANSLATOR.backup_compression_level_field(),
                                range,
                                Message::EditedCompressionLevel,
                            )),
                            _ => None,
                        })
                },
            )
            .push(
                self.log
                    .view(false, config, manifest, &self.duplicate_detector, operation, histories),
            );

        template(content)
    }
}

#[derive(Default)]
pub struct Restore {
    pub log: GameList,
    pub duplicate_detector: DuplicateDetector,
}

impl Restore {
    pub fn new(config: &Config, cache: &Cache) -> Self {
        Self {
            log: GameList::with_recent_games(true, config, cache),
            ..Default::default()
        }
    }

    pub fn view(
        &self,
        config: &Config,
        manifest: &Manifest,
        operation: &Operation,
        histories: &TextHistories,
    ) -> Element {
        let screen = Screen::Restore;
        let sort = &config.restore.sort;

        let content = Column::new()
            .push(
                Row::new()
                    .padding([0, 20, 0, 20])
                    .spacing(20)
                    .align_items(Alignment::Center)
                    .push(button::restore_preview(operation))
                    .push(button::restore(operation))
                    .push(button::toggle_all_scanned_games(
                        self.log.all_entries_selected(config, true),
                    ))
                    .push(button::filter(Screen::Restore, self.log.search.show)),
            )
            .push(make_status_row(
                &self.log.compute_operation_status(config, true),
                self.duplicate_detector.overall(),
            ))
            .push(
                Row::new()
                    .padding([0, 20, 0, 20])
                    .spacing(20)
                    .align_items(Alignment::Center)
                    .push(Text::new(TRANSLATOR.restore_source_label()))
                    .push(histories.input(UndoSubject::RestoreSource))
                    .push(button::choose_folder(BrowseSubject::RestoreSource))
                    .push("|")
                    .push(Text::new(TRANSLATOR.sort_label()))
                    .push(
                        PickList::new(SortKey::ALL, Some(sort.key), move |value| Message::EditedSortKey {
                            screen,
                            value,
                        })
                        .style(style::PickList::Primary),
                    )
                    .push(button::sort_order(screen, sort.reversed)),
            )
            .push(
                self.log
                    .view(true, config, manifest, &self.duplicate_detector, operation, histories),
            );

        template(content)
    }
}

pub fn custom_games<'a>(config: &Config, operating: bool, histories: &TextHistories) -> Element<'a> {
    let content = Column::new()
        .push(
            Row::new()
                .padding([0, 20, 0, 20])
                .spacing(20)
                .align_items(Alignment::Center)
                .push(button::add_game())
                .push(button::toggle_all_custom_games(config.are_all_custom_games_enabled())),
        )
        .push(editor::custom_games(config, operating, histories));

    template(content)
}

pub fn other<'a>(
    updating_manifest: bool,
    config: &'a Config,
    cache: &'a Cache,
    operation: &Operation,
    histories: &'a TextHistories,
) -> Element<'a> {
    let is_rclone_valid = config.apps.rclone.is_valid();
    let is_cloud_configured = config.cloud.remote.is_some();
    let is_cloud_path_valid = crate::cloud::validate_cloud_path(&config.cloud.path).is_ok();

    let content = Column::new()
        .push_if(
            || *STEAM_DECK,
            || {
                Row::new()
                    .padding([0, 20, 0, 20])
                    .spacing(20)
                    .align_items(iced::Alignment::Center)
                    .push(
                        Button::new(
                            Text::new(TRANSLATOR.exit_button())
                                .horizontal_alignment(iced::alignment::Horizontal::Center),
                        )
                        .on_press(Message::Exit { user: true })
                        .width(125)
                        .style(style::Button::Negative),
                    )
            },
        )
        .push({
            let content = Column::new()
                .spacing(20)
                .padding([0, 15, 5, 15])
                .width(Length::Fill)
                .push(
                    Row::new()
                        .align_items(iced::Alignment::Center)
                        .spacing(20)
                        .push(Text::new(TRANSLATOR.field_language()))
                        .push(
                            PickList::new(Language::ALL, Some(config.language), Message::SelectedLanguage)
                                .style(style::PickList::Primary),
                        ),
                )
                .push(
                    Row::new()
                        .align_items(iced::Alignment::Center)
                        .spacing(20)
                        .push(Text::new(TRANSLATOR.field_theme()))
                        .push(
                            PickList::new(Theme::ALL, Some(config.theme), Message::SelectedTheme)
                                .style(style::PickList::Primary),
                        ),
                )
                .push(
                    Column::new().spacing(5).push(Text::new(TRANSLATOR.scan_field())).push(
                        Container::new(
                            Column::new()
                                .padding(5)
                                .spacing(10)
                                .push_some(|| {
                                    AVAILABLE_PARALELLISM.map(|max_threads| {
                                        Column::new()
                                            .spacing(5)
                                            .push(Checkbox::new(
                                                TRANSLATOR.override_max_threads(),
                                                config.runtime.threads.is_some(),
                                                Message::OverrideMaxThreads,
                                            ))
                                            .push_some(|| {
                                                config.runtime.threads.map(|threads| {
                                                    Container::new(number_input(
                                                        threads.get() as i32,
                                                        TRANSLATOR.threads_label(),
                                                        1..=(max_threads.get() as i32),
                                                        |x| Message::EditedMaxThreads(x as usize),
                                                    ))
                                                    .padding([0, 0, 0, 35])
                                                })
                                            })
                                    })
                                })
                                .push(
                                    Checkbox::new(
                                        TRANSLATOR.explanation_for_exclude_store_screenshots(),
                                        config.backup.filter.exclude_store_screenshots,
                                        Message::EditedExcludeStoreScreenshots,
                                    )
                                    .style(style::Checkbox),
                                )
                                .push(Checkbox::new(
                                    TRANSLATOR.show_deselected_games(),
                                    config.scan.show_deselected_games,
                                    Message::SetShowDeselectedGames,
                                ))
                                .push(Checkbox::new(
                                    TRANSLATOR.show_unchanged_games(),
                                    config.scan.show_unchanged_games,
                                    Message::SetShowUnchangedGames,
                                ))
                                .push(Checkbox::new(
                                    TRANSLATOR.show_unscanned_games(),
                                    config.scan.show_unscanned_games,
                                    Message::SetShowUnscannedGames,
                                )),
                        )
                        .style(style::Container::GameListEntry),
                    ),
                )
                .push(
                    Column::new()
                        .spacing(5)
                        .push(
                            Row::new()
                                .align_items(iced::Alignment::Center)
                                .push(Text::new(TRANSLATOR.manifest_label()).width(100))
                                .push(button::refresh(Message::UpdateManifest, updating_manifest)),
                        )
                        .push_some(|| {
                            let cached = cache.manifests.get(&config.manifest.url)?;
                            let checked = match cached.checked {
                                Some(x) => chrono::DateTime::<chrono::Local>::from(x)
                                    .format("%Y-%m-%dT%H:%M:%S")
                                    .to_string(),
                                None => "?".to_string(),
                            };
                            let updated = match cached.updated {
                                Some(x) => chrono::DateTime::<chrono::Local>::from(x)
                                    .format("%Y-%m-%dT%H:%M:%S")
                                    .to_string(),
                                None => "?".to_string(),
                            };
                            Some(
                                Container::new(
                                    Column::new()
                                        .padding(5)
                                        .spacing(4)
                                        .push(
                                            Row::new()
                                                .align_items(iced::Alignment::Center)
                                                .push(Container::new(Text::new(TRANSLATOR.checked_label())).width(100))
                                                .push(Container::new(Text::new(checked))),
                                        )
                                        .push(
                                            Row::new()
                                                .align_items(iced::Alignment::Center)
                                                .push(Container::new(Text::new(TRANSLATOR.updated_label())).width(100))
                                                .push(Container::new(Text::new(updated))),
                                        ),
                                )
                                .style(style::Container::GameListEntry),
                            )
                        }),
                )
                .push(
                    Column::new()
                        .spacing(5)
                        .push(
                            Row::new()
                                .align_items(iced::Alignment::Center)
                                .push(Text::new(TRANSLATOR.cloud_field()).width(100)),
                        )
                        .push(
                            Container::new({
                                let mut column = Column::new().spacing(5).push(
                                    Row::new()
                                        .spacing(20)
                                        .align_items(Alignment::Center)
                                        .push(Text::new(TRANSLATOR.rclone_label()).width(70))
                                        .push(histories.input(UndoSubject::RcloneExecutable))
                                        .push_if(
                                            || !is_rclone_valid,
                                            || Icon::Error.as_text().width(Length::Shrink).style(style::Text::Failure),
                                        )
                                        .push(button::choose_file(BrowseFileSubject::RcloneExecutable))
                                        .push(histories.input(UndoSubject::RcloneArguments)),
                                );

                                if is_rclone_valid {
                                    let choice: RemoteChoice = config.cloud.remote.as_ref().into();
                                    column = column
                                        .push({
                                            let mut row = Row::new()
                                                .spacing(20)
                                                .align_items(Alignment::Center)
                                                .push(Text::new(TRANSLATOR.remote_label()).width(70))
                                                .push_if(
                                                    || !operation.idle(),
                                                    || {
                                                        Text::new(choice.to_string())
                                                            .height(30)
                                                            .vertical_alignment(iced::alignment::Vertical::Center)
                                                    },
                                                )
                                                .push_if(
                                                    || operation.idle(),
                                                    || {
                                                        PickList::new(
                                                            RemoteChoice::ALL,
                                                            Some(choice),
                                                            Message::EditedCloudRemote,
                                                        )
                                                    },
                                                );

                                            if let Some(Remote::Custom { .. }) = &config.cloud.remote {
                                                row = row
                                                    .push(Text::new(TRANSLATOR.remote_name_label()))
                                                    .push(histories.input(UndoSubject::CloudRemoteId));
                                            }

                                            if let Some(description) =
                                                config.cloud.remote.as_ref().and_then(|x| x.description())
                                            {
                                                row = row.push(Text::new(description));
                                            }

                                            row
                                        })
                                        .push_if(
                                            || choice != RemoteChoice::None,
                                            || {
                                                Row::new()
                                                    .spacing(20)
                                                    .align_items(Alignment::Center)
                                                    .push(Text::new(TRANSLATOR.folder_label()).width(70))
                                                    .push(histories.input(UndoSubject::CloudPath))
                                                    .push_if(
                                                        || !is_cloud_path_valid,
                                                        || {
                                                            Icon::Error
                                                                .as_text()
                                                                .width(Length::Shrink)
                                                                .style(style::Text::Failure)
                                                        },
                                                    )
                                            },
                                        )
                                        .push_if(
                                            || is_cloud_configured && is_cloud_path_valid,
                                            || {
                                                Row::new()
                                                    .spacing(20)
                                                    .align_items(Alignment::Center)
                                                    .push(button::upload(operation))
                                                    .push(button::download(operation))
                                                    .push(Checkbox::new(
                                                        TRANSLATOR.synchronize_automatically(),
                                                        config.cloud.synchronize,
                                                        |_| Message::ToggleCloudSynchronize,
                                                    ))
                                            },
                                        )
                                        .push_if(
                                            || !is_cloud_configured,
                                            || Text::new(TRANSLATOR.cloud_not_configured()),
                                        )
                                        .push_if(
                                            || !is_cloud_path_valid,
                                            || {
                                                Text::new(TRANSLATOR.prefix_warning(&TRANSLATOR.cloud_path_invalid()))
                                                    .style(style::Text::Failure)
                                            },
                                        );
                                } else {
                                    column = column
                                        .push(
                                            Text::new(TRANSLATOR.prefix_warning(&TRANSLATOR.rclone_unavailable()))
                                                .style(style::Text::Failure),
                                        )
                                        .push(button::open_url(TRANSLATOR.get_rclone_button(), RCLONE_URL.to_string()));
                                }

                                column
                            })
                            .padding(5)
                            .style(style::Container::GameListEntry),
                        ),
                )
                .push(
                    Column::new().spacing(5).push(Text::new(TRANSLATOR.roots_label())).push(
                        Container::new(
                            Column::new()
                                .padding(5)
                                .spacing(4)
                                .push(editor::root(config, histories)),
                        )
                        .style(style::Container::GameListEntry),
                    ),
                )
                .push(
                    Column::new()
                        .push(Text::new(TRANSLATOR.ignored_items_label()))
                        .push(editor::ignored_items(config, histories).padding([10, 0, 0, 0])),
                )
                .push(
                    Column::new()
                        .push(Text::new(TRANSLATOR.redirects_label()))
                        .push(editor::redirect(config, histories).padding([10, 0, 0, 0])),
                );
            ScrollSubject::Other.into_widget(content)
        });

    template(content)
}