| 1 |
/* SaveChangesDialog.vala |
| 2 |
* |
| 3 |
* Copyright 2023 Diego Iván <[email protected]> |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
[GtkTemplate (ui = "/io/github/diegoivan/pdf_metadata_editor/gtk/save-changes-dialog.ui")] |
| 9 |
public class PaperClip.SaveChangesDialog : Adw.MessageDialog { |
| 10 |
[GtkChild] |
| 11 |
private unowned Gtk.ListBox documents_listbox; |
| 12 |
|
| 13 |
public ListModel selected_documents { |
| 14 |
owned get { |
| 15 |
var list_store = new ListStore (typeof (Services.DocumentManager)); |
| 16 |
for (unowned Gtk.Widget? row = documents_listbox.get_first_child (); |
| 17 |
row != null; |
| 18 |
row = row.get_next_sibling ()) { |
| 19 |
var document_row = (DocumentRow) row; |
| 20 |
|
| 21 |
if (document_row.selected) { |
| 22 |
list_store.append (document_row.document_manager); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
return list_store; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
public async SaveChangesResponse ask ( |
| 31 |
Services.DocumentManager[] document_managers, |
| 32 |
Cancellable? cancellable = null |
| 33 |
) { |
| 34 |
foreach (Services.DocumentManager manager in document_managers) { |
| 35 |
if (!manager.changed) { |
| 36 |
continue; |
| 37 |
} |
| 38 |
|
| 39 |
var document_row = new DocumentRow () { |
| 40 |
document_manager = manager |
| 41 |
}; |
| 42 |
documents_listbox.append (document_row); |
| 43 |
} |
| 44 |
|
| 45 |
unowned string dialog_response = yield choose (cancellable); |
| 46 |
switch (dialog_response) { |
| 47 |
case "save": |
| 48 |
return SAVE; |
| 49 |
case "discard": |
| 50 |
return DISCARD; |
| 51 |
case "cancel": |
| 52 |
default: |
| 53 |
return CANCEL; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public class PaperClip.DocumentRow : Adw.ActionRow { |
| 59 |
private Services.DocumentManager _document; |
| 60 |
public Services.DocumentManager document_manager { |
| 61 |
get { |
| 62 |
return _document; |
| 63 |
} |
| 64 |
set { |
| 65 |
_document = value; |
| 66 |
|
| 67 |
title = document_manager.document.title; |
| 68 |
subtitle = document_manager.document.original_file.get_basename () ?? ""; |
| 69 |
check_button.active = true; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public bool selected { get; set; } |
| 74 |
|
| 75 |
private Gtk.CheckButton check_button = new Gtk.CheckButton (); |
| 76 |
|
| 77 |
construct { |
| 78 |
check_button.valign = CENTER; |
| 79 |
check_button.bind_property ("active", this, "selected", SYNC_CREATE | BIDIRECTIONAL); |
| 80 |
add_prefix (check_button); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public enum PaperClip.SaveChangesResponse { |
| 85 |
CANCEL, |
| 86 |
DISCARD, |
| 87 |
SAVE; |
| 88 |
} |