.
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 (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 is_done = true;
44
45 if (fn != null) {
46 f = (!) fn;
47
48 #if MAC
49 font_file_path = f;
50 save ();
51 #else
52 if (!f.has_suffix (".bf") && !f.has_suffix (".birdfont")) {
53 f = @"$f.birdfont";
54 }
55
56 file_name = @"$(f)";
57 file = File.new_for_path (file_name);
58 font_file_path = (!) file.get_path ();
59 if (!file.query_exists ()) {
60 save ();
61 } else {
62 dialog = new OverwriteBfFile (this);
63 MainWindow.show_dialog (dialog);
64 }
65 #endif
66 }
67 });
68
69 fc.add_extension ("bf");
70 MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE);
71 }
72
73 public void save () {
74 Font f;
75 string fn;
76
77 if (MenuTab.has_suppress_event ()) {
78 warn_if_test ("Event suppressed");
79 return;
80 }
81
82 f = BirdFont.get_current_font ();
83
84 if (font_file_path != "") {
85 f.font_file = font_file_path;
86 }
87
88 #if !MAC
89 Preferences.add_recent_files (f.get_path ());
90 #endif
91
92 if (f.is_bfp ()) {
93 MainWindow.native_window.save ();
94 } else {
95 f.delete_backup ();
96 fn = f.get_path ();
97
98 if (f.font_file != null
99 && (fn.has_suffix (".bf") || fn.has_suffix (".birdfont"))) {
100
101 f.set_font_file (fn);
102 MainWindow.native_window.save ();
103 } else {
104 save_as ();
105 }
106 }
107 }
108 }
109
110 }
111