2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
35 #include "properties.h"
36 #include <ucx/string.h>
37 #include <ucx/buffer.h>
38 #include <ucx/properties.h>
40 static UiProperties *application_properties;
41 static UiProperties *language;
45 static char *locales_dir;
46 static char *pixmaps_dir;
50 char* ui_getappdir() {
51 if(ui_appname() == NULL) {
55 return ui_configfile(NULL);
58 char* ui_configfile(char *name) {
59 char *appname = ui_appname();
64 UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
67 char *homeenv = getenv("HOME");
72 sstr_t home = sstr(homeenv);
74 ucx_buffer_write(home.ptr, 1, home.length, buf);
75 if(home.ptr[home.length-1] != '/') {
76 ucx_buffer_putc(buf, '/');
80 // on OS X the app dir is $HOME/Library/Application Support/$APPNAME/
81 ucx_buffer_puts(buf, "Library/Application Support/");
83 // app dir is $HOME/.$APPNAME/
84 ucx_buffer_putc(buf, '.');
86 ucx_buffer_puts(buf, appname);
87 ucx_buffer_putc(buf, '/');
91 ucx_buffer_puts(buf, name);
94 char *path = buf->space;
99 static int ui_mkdir(char *path) {
103 return mkdir(path, S_IRWXU);
107 void uic_load_app_properties() {
108 application_properties = ucx_map_new(128);
111 // applications without name cannot load app properties
115 char *dir = ui_configfile(NULL);
120 if(errno != EEXIST) {
121 fprintf(stderr, "Ui Error: Cannot create directory %s\n", dir);
128 char *path = ui_configfile("application.properties");
133 FILE *file = fopen(path, "r");
139 if(ucx_properties_load(application_properties, file)) {
140 fprintf(stderr, "Ui Error: Cannot load application properties.\n");
147 void uic_store_app_properties() {
148 char *path = ui_configfile("application.properties");
153 FILE *file = fopen(path, "w");
155 fprintf(stderr, "Ui Error: Cannot open properties file: %s\n", path);
160 if(ucx_properties_store(application_properties, file)) {
161 fprintf(stderr, "Ui Error: Cannot store application properties.\n");
169 char* ui_get_property(char *name) {
170 return ucx_map_cstr_get(application_properties, name);
173 void ui_set_property(char *name, char *value) {
174 ucx_map_cstr_put(application_properties, name, value);
177 void ui_set_default_property(char *name, char *value) {
178 char *v = ucx_map_cstr_get(application_properties, name);
180 ucx_map_cstr_put(application_properties, name, value);
186 static char* uic_concat_path(const char *base, const char *p, const char *ext) {
187 size_t baselen = strlen(base);
189 UcxBuffer *buf = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
191 ucx_buffer_write(base, 1, baselen, buf);
192 if(base[baselen - 1] != '/') {
193 ucx_buffer_putc(buf, '/');
196 ucx_buffer_write(p, 1, strlen(p), buf);
198 ucx_buffer_write(ext, 1, strlen(ext), buf);
201 char *str = buf->space;
208 void ui_locales_dir(char *path) {
212 void ui_pixmaps_dir(char *path) {
216 char* uic_get_image_path(const char *imgfilename) {
218 return uic_concat_path(pixmaps_dir, imgfilename, NULL);
224 void ui_load_lang(char *locale) {
229 char *path = uic_concat_path(locales_dir, locale, ".properties");
231 uic_load_language_file(path);
235 void ui_load_lang_def(char *locale, char *default_locale) {
238 char *lang = getenv("LANG");
239 if(lang && strlen(lang) >= 5) {
240 memcpy(tmp, lang, 5);
244 locale = default_locale;
248 char *path = uic_concat_path(locales_dir, locale, ".properties");
250 if(uic_load_language_file(path)) {
252 ui_load_lang_def(default_locale, NULL);
254 // cannot find any language file
255 fprintf(stderr, "Ui Error: Cannot load language.\n");
265 int uic_load_language_file(const char *path) {
266 UcxMap *lang = ucx_map_new(256);
268 FILE *file = fopen(path, "r");
273 if(ucx_properties_load(lang, file)) {
274 fprintf(stderr, "Ui Error: Cannot parse language file: %s.\n", path);
279 ucx_map_rehash(lang);
286 char* uistr(char *name) {
287 char *value = uistr_n(name);
288 return value ? value : "missing string";
291 char* uistr_n(char *name) {
295 return ucx_map_cstr_get(language, name);