.
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 /** Look for files in default folders on different operating systems. */
18 public class SearchPaths {
19
20 private static string resources_folder = "";
21
22 public static File find_file (string? dir, string name) {
23 File f = search_file (dir, name);
24
25 if (!f.query_exists ()) {
26 warning (@"Did not find file $name");
27 }
28
29 return f;
30 }
31
32 public static File search_file (string? dir, string name) {
33 File f;
34 string d = (dir == null) ? "" : (!) dir;
35 string resources;
36
37 resources = (is_null (resources_folder)) ? "" : resources_folder;
38
39 f = get_file (resources + "/" + d + "/", name);
40 if (likely (f.query_exists ())) return f;
41
42 f = get_file (resources + "/", name);
43 if (likely (f.query_exists ())) return f;
44
45 f = get_file ("resources/", name);
46 if (likely (f.query_exists ())) return f;
47
48 f = get_file (resources + "/", name + "/");
49 if (likely (f.query_exists ())) return f;
50
51 f = get_file (BirdFont.exec_path + "/" + d + "/", name);
52 if (likely (f.query_exists ())) return f;
53
54 f = get_file (BirdFont.exec_path + "/", name + "/");
55 if (likely (f.query_exists ())) return f;
56
57 f = get_file (BirdFont.exec_path + "\\" + d + "\\", name);
58 if (likely (f.query_exists ())) return f;
59
60 f = get_file (BirdFont.exec_path + "\\", name + "\\");
61 if (likely (f.query_exists ())) return f;
62
63 f = get_file (BirdFont.bundle_path + "/Contents/Resources/", d + "/" + name);
64 if (likely (f.query_exists ())) return f;
65
66 f = get_file (BirdFont.bundle_path + "/Contents/Resources/", name + "/");
67 if (likely (f.query_exists ())) return f;
68
69 f = get_file (BirdFont.bundle_path + "/Contents/MacOS/", name);
70 if (likely (f.query_exists ())) return f;
71
72 f = get_file ("./" + d + "/", name);
73 if (likely (f.query_exists ())) return f;
74
75 f = get_file ("../" + d + "/", name);
76 if (likely (f.query_exists ())) return f;
77
78 f = get_file (".\\" + d + "\\", name);
79 if (likely (f.query_exists ())) return f;
80
81 f = get_file ("", name);
82 if (likely (f.query_exists ())) return f;
83
84 f = get_file (d + "\\", name);
85 if (likely (f.query_exists ())) return f;
86
87 f = get_file (@"$PREFIX/share/birdfont/" + d + "/", name);
88 if (likely (f.query_exists ())) return f;
89
90 f = get_file (@"/usr/local/share/birdfont/" + d + "/", name);
91 if (likely (f.query_exists ())) return f;
92
93 f = get_file (@"/usr/share/birdfont/" + d + "/", name);
94 if (likely (f.query_exists ())) return f;
95
96 return f;
97 }
98
99 public static string get_locale_directory () {
100 string f = "";
101 string resources;
102
103 resources = (is_null (resources_folder)) ? "" : resources_folder;
104
105 f = resources + "\\locale\\sv\\LC_MESSAGES\\birdfont.mo";
106 if (exists (f)) {
107 return resources + "\\locale";
108 }
109
110 if (!is_null (BirdFont.exec_path)) {
111 f = BirdFont.exec_path + "/Contents/Resources/locale/sv/LC_MESSAGES/birdfont.mo";
112 if (exists (f)) {
113 return BirdFont.exec_path + "/Contents/Resources/locale";
114 }
115
116 f = BirdFont.exec_path + "\\locale\\sv\\LC_MESSAGES\\birdfont.mo";
117 if (exists (f)) {
118 return BirdFont.exec_path + "\\locale";
119 }
120 }
121
122 f = "./build/locale/sv/LC_MESSAGES/birdfont.mo";
123 if (exists (f)) {
124 return "./build/locale";
125 }
126
127 f = ".\\locale\\sv\\LC_MESSAGES\\birdfont.mo";
128 if (exists (f)) {
129 return ".\\locale";
130 }
131
132 f = PREFIX + "/share/locale/sv/LC_MESSAGES/birdfont.mo";
133 if (exists (f)) {
134 return PREFIX + "/share/locale/";
135 }
136
137 f = "/usr/share/locale/sv/LC_MESSAGES/birdfont.mo";
138 if (exists (f)) {
139 return "/usr/share/locale";
140 }
141
142 f = BirdFont.exec_path + "/Contents/Resources/locale";
143 if (exists (f)) {
144 return BirdFont.exec_path + "/Contents/Resources/locale";
145 }
146
147 f = BirdFont.bundle_path + "/Contents/Resources/locale";
148 if (exists (f)) {
149 return BirdFont.bundle_path + "/Contents/Resources/locale";
150 }
151
152 warning ("translations not found");
153 return "/usr/share/locale";
154 }
155
156 public static File get_char_database () {
157 File f;
158
159 f = (!) File.new_for_path (PREFIX + "/share/unicode/NamesList.txt");
160 if (f.query_exists ()) {
161 return f;
162 }
163
164 f = (!) File.new_for_path (PREFIX + "/share/unicode/ucd/NamesList.txt");
165 if (f.query_exists ()) {
166 return f;
167 }
168
169 f = (!) File.new_for_path (".\\NamesList.txt");
170 if (f.query_exists ()) {
171 return f;
172 }
173
174 f = (!) File.new_for_path ("/usr/share/unicode/NamesList.txt");
175 if (f.query_exists ()) {
176 return f;
177 }
178
179 f = (!) File.new_for_path (BirdFont.exec_path + "/Contents/Resources/NamesList.txt");
180 if (f.query_exists ()) {
181 return f;
182 }
183
184 f = (!) File.new_for_path ("/usr/share/unicode/ucd/NamesList.txt");
185 if (f.query_exists ()) {
186 return f;
187 }
188
189 warning ("ucd not found");
190
191 return f;
192 }
193
194 static File get_file (string? path, string name) {
195 StringBuilder fn = new StringBuilder ();
196 string p = (path == null) ? "" : (!) path;
197 fn.append (p);
198 fn.append ((!) name);
199
200 return File.new_for_path (fn.str);
201 }
202
203 static bool exists (string file) {
204 File f = File.new_for_path (file);
205 return f.query_exists ();
206 }
207
208 public static void set_resources_folder (string res) {
209 resources_folder = res;
210 }
211 }
212
213 }
214