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 int get_window_width() { 81 string wp = get ("window_width"); 82 int w = int.parse (wp); 83 return (w == 0) ? 860 : w; 84 } 85 86 public static int get_window_height() { 87 int h = int.parse (get ("window_height")); 88 return (h == 0) ? 500 : h; 89 } 90 91 public static void load () { 92 File config_dir; 93 File settings; 94 FileStream? settings_file; 95 unowned FileStream b; 96 string? l; 97 98 config_dir = BirdFont.get_settings_directory (); 99 settings = get_child (config_dir, "settings"); 100 101 data = new HashMap<string, string> (); 102 103 if (!settings.query_exists ()) { 104 return; 105 } 106 107 settings_file = FileStream.open ((!) settings.get_path (), "r"); 108 109 if (settings_file == null) { 110 stderr.printf ("Failed to load settings from file %s.\n", (!) settings.get_path ()); 111 return; 112 } 113 114 b = (!) settings_file; 115 l = b.read_line (); 116 while ((l = b.read_line ())!= null) { 117 string line; 118 119 line = (!) l; 120 121 if (line.get_char (0) == '#') { 122 continue; 123 } 124 125 int i = 0; 126 int s = 0; 127 128 i = line.index_of_char(' ', s); 129 string key = line.substring (s, i - s); 130 131 s = i + 1; 132 i = line.index_of_char('"', s); 133 s = i + 1; 134 i = line.index_of_char('"', s); 135 string val = line.substring (s, i - s); 136 137 data.set (key, val); 138 } 139 } 140 141 public static void save () { 142 try { 143 File config_dir = BirdFont.get_settings_directory (); 144 File settings = get_child (config_dir, "settings"); 145 146 return_if_fail (config_dir.query_exists ()); 147 148 if (settings.query_exists ()) { 149 settings.delete (); 150 } 151 152 DataOutputStream os = new DataOutputStream(settings.create(FileCreateFlags.REPLACE_DESTINATION)); 153 uint8[] d; 154 long written = 0; 155 156 StringBuilder sb = new StringBuilder (); 157 158 sb.append ("# BirdFont settings\n"); 159 sb.append ("# Version: 1.0\n"); 160 161 foreach (var k in data.keys) { 162 sb.append (k); 163 sb.append (" \""); 164 sb.append (data.get (k)); 165 sb.append ("\"\n"); 166 } 167 168 d = sb.str.data; 169 170 while (written < d.length) { 171 written += os.write (d[written:d.length]); 172 } 173 } catch (Error e) { 174 stderr.printf ("Can not save key settings. (%s)", e.message); 175 } 176 } 177 } 178 179 } 180