ui/common/properties.c

Sun, 06 Apr 2014 13:21:37 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 06 Apr 2014 13:21:37 +0200
changeset 28
794a5c91c479
parent 24
06bceda81a03
child 29
c96169444d88
permissions
-rw-r--r--

added open/save dialogs

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2014 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>

#include "properties.h"
#include "../../ucx/string.h"
#include "../../ucx/buffer.h"
#include "../../ucx/properties.h"

static UiProperties *application_properties;

char* ui_getappdir() {
    if(ui_appname() == NULL) {
        return NULL;
    }
    
    return ui_configfile(NULL);
}

char* ui_configfile(char *name) {
    char *appname = ui_appname();
    if(!appname) {
        return NULL;
    }
    
    UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
    
    // add base dir
    char *homeenv = getenv("HOME");
    if(homeenv == NULL) {
        ucx_buffer_free(buf);
        return NULL;
    }
    sstr_t home = sstr(homeenv);
    
    ucx_buffer_write(home.ptr, 1, home.length, buf);
    if(home.ptr[home.length-1] != '/') {
        ucx_buffer_putc(buf, '/');
    }
    
#ifdef UI_COCOA
    // on OS X the app dir is $HOME/Library/Application Support/$APPNAME/
    ucx_buffer_puts(buf, "Library/Application Support/");
#else
    // app dir is $HOME/.$APPNAME/
    ucx_buffer_putc(buf, '.');
#endif
    ucx_buffer_puts(buf, appname);
    ucx_buffer_putc(buf, '/');
    
    // add file name
    if(name) {
        ucx_buffer_puts(buf, name);
    }
    
    char *path = buf->space;
    free(buf);
    return path;  
}

static int ui_mkdir(char *path) {
#ifdef _WIN32
    return mkdir(path);
#else
    return mkdir(path, S_IRWXU);
#endif
} 

void uic_load_app_properties() {
    application_properties = ucx_map_new(128);
    
    if(!ui_appname()) {
        // applications without name cannot load app properties
        return;
    }
    
    char *dir = ui_configfile(NULL);
    if(!dir) {
        return;
    }
    if(ui_mkdir(dir)) {
        if(errno != EEXIST) {
            fprintf(stderr, "Ui Error: Cannot create directory %s\n", dir);
            free(dir);
            return;
        }
    }
    free(dir);
    
    char *path = ui_configfile("application.properties");
    if(!path) {
        return;
    }
    
    FILE *file = fopen(path, "r");
    if(!file) {
        free(path);
        return;
    }
    
    if(ucx_properties_load(application_properties, file)) {
        fprintf(stderr, "Ui Error: Cannot load application properties.\n");
    }
    
    fclose(file);
    free(path);
}

void uic_store_app_properties() {
    char *path = ui_configfile("application.properties");
    if(!path) {
        return;
    }
    
    FILE *file = fopen(path, "w");
    if(!file) {
        fprintf(stderr, "Ui Error: Cannot open properties file: %s\n", path);
        free(path);
        return;
    }
    
    if(ucx_properties_store(application_properties, file)) {
        fprintf(stderr, "Ui Error: Cannot store application properties.\n");
    }
    
    fclose(file);
    free(path);
}


char* ui_get_property(char *name) {
    return ucx_map_cstr_get(application_properties, name);
}

void ui_set_property(char *name, char *value) {
    ucx_map_cstr_put(application_properties, name, value);
}

void ui_set_default_property(char *name, char *value) {
    char *v = ucx_map_cstr_get(application_properties, name);
    if(!v) {
        ucx_map_cstr_put(application_properties, name, value);
    }
}

mercurial