The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

RecentFiles.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
Fix translation heads/master
1 /* 2 Copyright (C) 2015 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 using Cairo; 16 using Math; 17 18 namespace BirdFont { 19 20 /** A list of recently edited fonts. */ 21 public class RecentFiles : Table { 22 Gee.ArrayList<Row> rows = new Gee.ArrayList<Row> (); 23 24 const int NEW_FONT = -5; 25 const int CURRENT_FONT = -4; 26 const int RECENT_FONT = -3; 27 const int BACKUP = -2; 28 29 public RecentFiles () { 30 } 31 32 public override Gee.ArrayList<Row> get_rows () { 33 return rows; 34 } 35 36 public override void selected_row (Row row, int column, bool delete_button) { 37 Font f; 38 39 if (row.get_index () == NEW_FONT) { 40 MenuTab.new_file (); 41 MenuTab.select_overview (); 42 } else if (row.get_index () == RECENT_FONT) { 43 return_if_fail (row.get_row_data () is Font); 44 f = (Font) row.get_row_data (); 45 MainWindow.scrollbar.set_size (0); 46 GlyphCanvas.redraw (); 47 load_font (f.get_path ()); 48 } else if (row.get_index () == BACKUP) { 49 return_if_fail (row.get_row_data () is Font); 50 f = (Font) row.get_row_data (); 51 delete_backup (f.get_file_name ()); 52 } 53 54 GlyphCanvas.redraw (); 55 } 56 57 public override void update_rows () { 58 Row row; 59 Gee.ArrayList<Font> recent_fonts = get_recent_font_files (); 60 Gee.ArrayList<Font> backups = get_backups (); 61 Font current_font = BirdFont.get_current_font (); 62 63 rows.clear (); 64 65 if (recent_fonts.size == 0) { 66 row = new Row.headline (t_("No fonts created yet")); 67 rows.add (row); 68 69 row = new Row.columns_1 (t_("Create a New Font"), NEW_FONT, false); 70 rows.add (row); 71 } 72 73 if (current_font.font_file != null) { 74 row = new Row.headline (current_font.get_file_name ()); 75 rows.add (row); 76 77 row = new Row.columns_1 (t_("Folder") + ": " + (!) current_font.get_folder ().get_path (), CURRENT_FONT, false); 78 rows.add (row); 79 80 row = new Row.columns_1 (t_("Glyphs") + @": $(current_font.length ())", CURRENT_FONT, false); 81 rows.add (row); 82 } 83 84 if (recent_fonts.size > 0) { 85 row = new Row.headline (t_("Recent Files")); 86 rows.add (row); 87 } 88 89 foreach (Font font in recent_fonts) { 90 row = new Row.columns_1 (font.get_file_name (), RECENT_FONT, false); 91 row.set_row_data (font); 92 rows.add (row); 93 } 94 95 if (backups.size > 0) { 96 row = new Row.headline (t_("Backups")); 97 rows.add (row); 98 } 99 100 foreach (Font font in backups) { 101 row = new Row.columns_1 (font.get_file_name (), BACKUP, true); 102 row.set_row_data (font); 103 rows.add (row); 104 } 105 106 GlyphCanvas.redraw (); 107 } 108 109 public override string get_label () { 110 return t_("Files"); 111 } 112 113 public override string get_name () { 114 return "Files"; 115 } 116 117 public Gee.ArrayList<Font> get_recent_font_files () { 118 File file; 119 Font font; 120 bool unique; 121 Gee.ArrayList<Font> fonts = new Gee.ArrayList<Font> (); 122 123 foreach (string f in Preferences.get_recent_files ()) { 124 if (f == "") { 125 continue; 126 } 127 128 file = File.new_for_path (f); 129 130 font = new Font (); 131 font.set_font_file (f); 132 133 unique = true; 134 foreach (Font recent_font in fonts) { 135 if (recent_font.get_path () == f) { 136 unique = false; 137 } 138 } 139 140 if (unique && file.query_exists ()) { 141 fonts.insert (0, font); 142 } 143 } 144 145 return fonts; 146 } 147 148 public Gee.ArrayList<Font> get_backups () { 149 FileEnumerator enumerator; 150 string file_name; 151 FileInfo? file_info; 152 Gee.ArrayList<Font> backups = new Gee.ArrayList<Font> (); 153 File dir = BirdFont.get_backup_directory (); 154 Font font = BirdFont.get_current_font (); 155 Font backup_font; 156 157 try { 158 enumerator = dir.enumerate_children (FileAttribute.STANDARD_NAME, 0); 159 while ((file_info = enumerator.next_file ()) != null) { 160 file_name = ((!) file_info).get_name (); 161 162 // ignore old backup files 163 if (file_name.has_prefix ("current_font_")) { 164 continue; 165 } 166 167 backup_font = new Font (); 168 backup_font.set_font_file ((!) get_child (dir, file_name).get_path ()); 169 backups.insert (0, backup_font); 170 } 171 } catch (Error e) { 172 warning (e.message); 173 } 174 175 return backups; 176 } 177 178 public void delete_backup (string file_name) { 179 File backup_file; 180 181 try { 182 backup_file = BirdFont.get_backup_directory (); 183 backup_file = get_child (backup_file, file_name); 184 if (backup_file.query_exists ()) { 185 backup_file.delete (); 186 } 187 } catch (GLib.Error e) { 188 warning (e.message); 189 } 190 191 selected_canvas (); 192 } 193 194 public override void draw (WidgetAllocation allocation, Context cr) { 195 MainWindow.get_overview ().allocation = allocation; 196 base.draw (allocation, cr); 197 } 198 199 public static void load_font (string fn) { 200 Font font; 201 SaveDialogListener dialog = new SaveDialogListener (); 202 203 if (MenuTab.has_suppress_event ()) { 204 return; 205 } 206 207 font = BirdFont.get_current_font (); 208 209 MenuTab.load_callback = new LoadCallback (); 210 MenuTab.load_callback.file_loaded.connect (() => { 211 Font f; 212 213 if (MenuTab.has_suppress_event ()) { 214 warning ("Load font event suppressed."); 215 return; 216 } 217 218 f = BirdFont.get_current_font (); 219 220 MainWindow.get_drawing_tools ().remove_all_grid_buttons (); 221 foreach (string v in f.grid_width) { 222 MainWindow.get_drawing_tools ().parse_grid (v); 223 } 224 225 DrawingTools.background_scale.set_value (f.background_scale); 226 KerningTools.update_kerning_classes (); 227 MenuTab.apply_font_setting (f); 228 229 MenuTab.show_default_characters (); 230 MainWindow.get_overview ().selected_canvas (); 231 }); 232 233 dialog.signal_discard.connect (() => { 234 Font f; 235 236 if (MenuTab.has_suppress_event ()) { 237 return; 238 } 239 240 f = BirdFont.new_font (); 241 242 MainWindow.close_all_tabs (); 243 244 f.set_file (fn); 245 Preferences.add_recent_files (fn); 246 247 MainWindow.native_window.load (); // background thread 248 }); 249 250 dialog.signal_save.connect (() => { 251 if (MenuTab.has_suppress_event ()) { 252 warn_if_test ("Event suppressed."); 253 return; 254 } 255 256 MenuTab.set_save_callback (new SaveCallback ()); 257 MenuTab.save_callback.file_saved.connect (() => { 258 dialog.signal_discard (); 259 }); 260 MenuTab.save_callback.save (); // background thread 261 }); 262 263 dialog.signal_cancel.connect (() => { 264 MainWindow.hide_dialog (); 265 }); 266 267 if (!font.is_modified ()) { 268 dialog.signal_discard (); 269 } else { 270 MainWindow.show_dialog (new SaveDialog (dialog)); 271 } 272 } 273 } 274 275 } 276