The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Preferences.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
Add *nix as OS tag heads/master
1 /* 2 Copyright (C) 2012 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 using Gee; 18 19 public class Preferences { 20 21 static Gee.HashMap<string, string> data; 22 public static bool draw_boundaries = false; 23 24 public Preferences () { 25 data = new Gee.HashMap<string, string> (); 26 } 27 28 public static void set_last_file (string fn) { 29 set ("last_file", fn); 30 } 31 32 public static string @get (string k) { 33 string? s; 34 35 if (is_null (data)) { 36 data = new Gee.HashMap<string, string> (); 37 } 38 39 s = data.get (k); 40 41 return (s != null) ? (!) s : ""; 42 } 43 44 public static void @set (string k, string v) { 45 if (is_null (data)) { 46 data = new Gee.HashMap<string, string> (); 47 } 48 49 data.set (k, v); 50 save (); 51 } 52 53 public static string[] get_recent_files () { 54 string recent = get ("recent_files"); 55 string[] files = recent.split ("\t"); 56 57 for (uint i = 0; i < files.length; i++) { 58 files[i] = files[i].replace ("\\t", "\t"); 59 } 60 61 return files; 62 } 63 64 public static File get_backup_directory () { 65 File config_directory = BirdFont.get_settings_directory (); 66 File backup_directory = get_child (config_directory, "backup"); 67 68 if (!backup_directory.query_exists ()) { 69 int error = DirUtils.create ((!) backup_directory.get_path (), 0766); 70 71 if (error == -1) { 72 warning (@"Failed to create backup directory: $((!) backup_directory.get_path ())\n"); 73 } 74 } 75 76 return backup_directory; 77 } 78 79 public static File get_backup_directory_for_font (string bf_file_name) { 80 if (bf_file_name == "") { 81 warning ("no filename."); 82 } 83 84 if (bf_file_name.index_of ("/") > -1) { 85 warning ("Expecting a file and not a folder got: " + bf_file_name); 86 } 87 88 File backup_directory = get_backup_directory (); 89 string subdir_name = bf_file_name; 90 91 if (subdir_name.has_suffix (".bf")) { 92 subdir_name = subdir_name.substring (0, subdir_name.length - ".bf".length); 93 } 94 95 if (subdir_name.has_suffix (".birdfont")) { 96 subdir_name = subdir_name.substring (0, subdir_name.length - ".birdfont".length); 97 } 98 99 subdir_name += ".backup"; 100 101 File backup_subdir = get_child (backup_directory, subdir_name); 102 103 if (!backup_subdir.query_exists ()) { 104 int error = DirUtils.create ((!) backup_subdir.get_path (), 0766); 105 106 if (error == -1) { 107 warning (@"Failed to create backup directory: $((!) backup_subdir.get_path ())\n"); 108 } 109 } 110 111 return backup_subdir; 112 } 113 114 public static void add_recent_files (string file) { 115 string escaped_string = file.replace ("\t", "\\t"); 116 StringBuilder recent = new StringBuilder (); 117 118 foreach (string f in get_recent_files ()) { 119 if (f != file) { 120 recent.append (f.replace ("\t", "\\t")); 121 recent.append ("\t"); 122 } 123 } 124 125 recent.append (escaped_string); 126 127 set ("recent_files", @"$(recent.str)"); 128 } 129 130 public static void set_window_size (int x, int y, int width, int height) { 131 set ("window_x", @"$x"); 132 set ("window_y", @"$y"); 133 set ("window_width", @"$width"); 134 set ("window_height", @"$height"); 135 } 136 137 public static int get_window_x () { 138 string wp = get ("window_x"); 139 int x = int.parse (wp); 140 return x; 141 } 142 143 public static int get_window_y () { 144 string wp = get ("window_y"); 145 int y = int.parse (wp); 146 return y; 147 } 148 149 public static int get_window_width() { 150 string wp = get ("window_width"); 151 int w = int.parse (wp); 152 return (w == 0) ? 860 : w; 153 } 154 155 public static int get_window_height() { 156 int h = int.parse (get ("window_height")); 157 return (h == 0) ? 500 : h; 158 } 159 160 public static void load () { 161 File config_dir; 162 File settings; 163 FileStream? settings_file; 164 unowned FileStream b; 165 string? l; 166 167 config_dir = BirdFont.get_settings_directory (); 168 settings = get_child (config_dir, "settings"); 169 170 data = new HashMap<string, string> (); 171 172 if (!settings.query_exists ()) { 173 return; 174 } 175 176 settings_file = FileStream.open ((!) settings.get_path (), "r"); 177 178 if (settings_file == null) { 179 stderr.printf ("Failed to load settings from file %s.\n", (!) settings.get_path ()); 180 return; 181 } 182 183 b = (!) settings_file; 184 l = b.read_line (); 185 while ((l = b.read_line ())!= null) { 186 string line; 187 188 line = (!) l; 189 190 if (line.get_char (0) == '#') { 191 continue; 192 } 193 194 int i = 0; 195 int s = 0; 196 197 i = line.index_of_char(' ', s); 198 string key = line.substring (s, i - s); 199 200 s = i + 1; 201 i = line.index_of_char('"', s); 202 s = i + 1; 203 i = line.index_of_char('"', s); 204 string val = line.substring (s, i - s); 205 206 data.set (key, val); 207 } 208 } 209 210 public static void save () { 211 try { 212 File config_dir = BirdFont.get_settings_directory (); 213 File settings = get_child (config_dir, "settings"); 214 215 return_if_fail (config_dir.query_exists ()); 216 217 if (settings.query_exists ()) { 218 settings.delete (); 219 } 220 221 DataOutputStream os = new DataOutputStream(settings.create(FileCreateFlags.REPLACE_DESTINATION)); 222 uint8[] d; 223 long written = 0; 224 225 StringBuilder sb = new StringBuilder (); 226 227 sb.append ("# BirdFont settings\n"); 228 sb.append ("# Version: 1.0\n"); 229 230 foreach (var k in data.keys) { 231 sb.append (k); 232 sb.append (" \""); 233 sb.append (data.get (k)); 234 sb.append ("\"\n"); 235 } 236 237 d = sb.str.data; 238 239 while (written < d.length) { 240 written += os.write (d[written:d.length]); 241 } 242 } catch (Error e) { 243 stderr.printf ("Can not save key settings. (%s)", e.message); 244 } 245 } 246 } 247 248 } 249