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
Circle boundaries 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 // ignore backup of the current font 168 if (file_name == @"$(font.get_name ()).bf") { 169 continue; 170 } 171 172 backup_font = new Font (); 173 backup_font.set_font_file ((!) get_child (dir, file_name).get_path ()); 174 backups.insert (0, backup_font); 175 } 176 } catch (Error e) { 177 warning (e.message); 178 } 179 180 return backups; 181 } 182 183 public void delete_backup (string file_name) { 184 File backup_file; 185 186 try { 187 backup_file = BirdFont.get_backup_directory (); 188 backup_file = get_child (backup_file, file_name); 189 if (backup_file.query_exists ()) { 190 backup_file.delete (); 191 } 192 } catch (GLib.Error e) { 193 warning (e.message); 194 } 195 196 selected_canvas (); 197 } 198 199 public override void draw (WidgetAllocation allocation, Context cr) { 200 MainWindow.get_overview ().allocation = allocation; 201 base.draw (allocation, cr); 202 } 203 204 public static void load_font (string fn) { 205 Font font; 206 SaveDialogListener dialog = new SaveDialogListener (); 207 208 if (MenuTab.has_suppress_event ()) { 209 return; 210 } 211 212 font = BirdFont.get_current_font (); 213 214 MenuTab.load_callback = new LoadCallback (); 215 MenuTab.load_callback.file_loaded.connect (() => { 216 Font f; 217 218 if (MenuTab.has_suppress_event ()) { 219 warning ("Load font event suppressed."); 220 return; 221 } 222 223 f = BirdFont.get_current_font (); 224 225 MainWindow.get_drawing_tools ().remove_all_grid_buttons (); 226 foreach (string v in f.grid_width) { 227 MainWindow.get_drawing_tools ().parse_grid (v); 228 } 229 230 DrawingTools.background_scale.set_value (f.background_scale); 231 KerningTools.update_kerning_classes (); 232 MenuTab.apply_font_setting (f); 233 234 MenuTab.show_default_characters (); 235 MainWindow.get_overview ().selected_canvas (); 236 }); 237 238 dialog.signal_discard.connect (() => { 239 Font f; 240 241 if (MenuTab.has_suppress_event ()) { 242 return; 243 } 244 245 f = BirdFont.new_font (); 246 247 MainWindow.close_all_tabs (); 248 249 f.set_file (fn); 250 Preferences.add_recent_files (fn); 251 252 MainWindow.native_window.load (); // background thread 253 }); 254 255 dialog.signal_save.connect (() => { 256 if (MenuTab.has_suppress_event ()) { 257 warn_if_test ("Event suppressed."); 258 return; 259 } 260 261 MenuTab.set_save_callback (new SaveCallback ()); 262 MenuTab.save_callback.file_saved.connect (() => { 263 dialog.signal_discard (); 264 }); 265 MenuTab.save_callback.save (); // background thread 266 }); 267 268 dialog.signal_cancel.connect (() => { 269 MainWindow.hide_dialog (); 270 }); 271 272 if (!font.is_modified ()) { 273 dialog.signal_discard (); 274 } else { 275 MainWindow.show_dialog (new SaveDialog (dialog)); 276 } 277 } 278 } 279 280 } 281