1 /*
2 Copyright (C) 2014 Johan Mattsson
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 3 of the
7 License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 */
14
15 namespace BirdFont {
16
17 public class SaveCallback : GLib.Object {
18
19 public signal void file_saved ();
20 public bool is_done = false;
21
22 public string font_file_path = "";
23
24 public SaveCallback () {
25 file_saved.connect (() => {
26 is_done = true;
27 });
28 }
29
30 public void save_as () {
31 if (unlikely (MenuTab.has_suppress_event ())) {
32 warn_if_test ("Event suppressed");
33 return;
34 }
35
36 FileChooser fc = new FileChooser ();
37 fc.file_selected.connect ((fn) => {
38 string f;
39 File file;
40 OverwriteBfFile dialog;
41 string file_name;
42
43 if (fn != null) {
44 f = (!) fn;
45
46 #if MAC
47 font_file_path = f;
48 save ();
49 #else
50 if (!f.has_suffix (".bf")) {
51 f = @"$f.bf";
52 }
53
54 file_name = @"$(f)";
55 file = File.new_for_path (file_name);
56 font_file_path = (!) file.get_path ();
57
58 if (!file.query_exists ()) {
59 save ();
60 } else {
61 dialog = new OverwriteBfFile (this);
62 MainWindow.show_dialog (dialog);
63 }
64 #endif
65 }
66 });
67
68 fc.add_extension ("bf");
69 MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE);
70 }
71
72 public void save () {
73 Font f;
74 string fn;
75
76 if (MenuTab.has_suppress_event ()) {
77 warn_if_test ("Event suppressed");
78 return;
79 }
80
81 f = BirdFont.get_current_font ();
82
83 if (font_file_path != "") {
84 f.font_file = font_file_path;
85 }
86
87 #if !MAC
88 Preferences.add_recent_files (f.get_path ());
89 #endif
90
91 if (f.is_bfp ()) {
92 MainWindow.native_window.save ();
93 } else {
94 f.delete_backup ();
95 fn = f.get_path ();
96
97 if (f.font_file != null && fn.has_suffix (".bf")) {
98 f.set_font_file (fn);
99 MainWindow.native_window.save ();
100 } else {
101 save_as ();
102 }
103 }
104 }
105 }
106
107 }
108