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

Revisions

View the latest version of libbirdfont/Preferences.vala.
Merge ../birdfont-2.x
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 void add_recent_files (string file) { 65 string escaped_string = file.replace ("\t", "\\t"); 66 StringBuilder recent = new StringBuilder (); 67 68 foreach (string f in get_recent_files ()) { 69 if (f != file) { 70 recent.append (f.replace ("\t", "\\t")); 71 recent.append ("\t"); 72 } 73 } 74 75 recent.append (escaped_string); 76 77 set ("recent_files", @"$(recent.str)"); 78 } 79 80 public static void set_window_size (int x, int y, int width, int height) { 81 set ("window_x", @"$x"); 82 set ("window_y", @"$y"); 83 set ("window_width", @"$width"); 84 set ("window_height", @"$height"); 85 } 86 87 public static int get_window_x () { 88 string wp = get ("window_x"); 89 int x = int.parse (wp); 90 return x; 91 } 92 93 public static int get_window_y () { 94 string wp = get ("window_y"); 95 int y = int.parse (wp); 96 return y; 97 } 98 99 public static int get_window_width() { 100 string wp = get ("window_width"); 101 int w = int.parse (wp); 102 return (w == 0) ? 860 : w; 103 } 104 105 public static int get_window_height() { 106 int h = int.parse (get ("window_height")); 107 return (h == 0) ? 500 : h; 108 } 109 110 public static void load () { 111 File config_dir; 112 File settings; 113 FileStream? settings_file; 114 unowned FileStream b; 115 string? l; 116 117 config_dir = BirdFont.get_settings_directory (); 118 settings = get_child (config_dir, "settings"); 119 120 data = new HashMap<string, string> (); 121 122 if (!settings.query_exists ()) { 123 return; 124 } 125 126 settings_file = FileStream.open ((!) settings.get_path (), "r"); 127 128 if (settings_file == null) { 129 stderr.printf ("Failed to load settings from file %s.\n", (!) settings.get_path ()); 130 return; 131 } 132 133 b = (!) settings_file; 134 l = b.read_line (); 135 while ((l = b.read_line ())!= null) { 136 string line; 137 138 line = (!) l; 139 140 if (line.get_char (0) == '#') { 141 continue; 142 } 143 144 int i = 0; 145 int s = 0; 146 147 i = line.index_of_char(' ', s); 148 string key = line.substring (s, i - s); 149 150 s = i + 1; 151 i = line.index_of_char('"', s); 152 s = i + 1; 153 i = line.index_of_char('"', s); 154 string val = line.substring (s, i - s); 155 156 data.set (key, val); 157 } 158 } 159 160 public static void save () { 161 try { 162 File config_dir = BirdFont.get_settings_directory (); 163 File settings = get_child (config_dir, "settings"); 164 165 return_if_fail (config_dir.query_exists ()); 166 167 if (settings.query_exists ()) { 168 settings.delete (); 169 } 170 171 DataOutputStream os = new DataOutputStream(settings.create(FileCreateFlags.REPLACE_DESTINATION)); 172 uint8[] d; 173 long written = 0; 174 175 StringBuilder sb = new StringBuilder (); 176 177 sb.append ("# BirdFont settings\n"); 178 sb.append ("# Version: 1.0\n"); 179 180 foreach (var k in data.keys) { 181 sb.append (k); 182 sb.append (" \""); 183 sb.append (data.get (k)); 184 sb.append ("\"\n"); 185 } 186 187 d = sb.str.data; 188 189 while (written < d.length) { 190 written += os.write (d[written:d.length]); 191 } 192 } catch (Error e) { 193 stderr.printf ("Can not save key settings. (%s)", e.message); 194 } 195 } 196 } 197 198 } 199