added application properties

Fri, 04 Apr 2014 17:45:36 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 04 Apr 2014 17:45:36 +0200
changeset 24
06bceda81a03
parent 23
decc6bf584aa
child 25
78ae3efe463f

added application properties

ui/cocoa/toolkit.m file | annotate | diff | comparison | revisions
ui/common/objs.mk file | annotate | diff | comparison | revisions
ui/common/properties.c file | annotate | diff | comparison | revisions
ui/common/properties.h file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
ui/motif/toolkit.c file | annotate | diff | comparison | revisions
ui/ui/properties.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
ui/ui/ui.h file | annotate | diff | comparison | revisions
--- a/ui/cocoa/toolkit.m	Fri Apr 04 11:29:17 2014 +0200
+++ b/ui/cocoa/toolkit.m	Fri Apr 04 17:45:36 2014 +0200
@@ -33,6 +33,7 @@
 #import <errno.h>
 
 #import "../common/document.h"
+#import "../common/properties.h"
 
 #import "toolkit.h"
 #import "window.h"
@@ -57,6 +58,8 @@
     ui_menu_init();
     ui_toolbar_init();
     ui_stock_init();
+    
+    uic_load_app_properties();
 }
 
 void ui_show(UiObject *obj) {
--- a/ui/common/objs.mk	Fri Apr 04 11:29:17 2014 +0200
+++ b/ui/common/objs.mk	Fri Apr 04 17:45:36 2014 +0200
@@ -33,9 +33,7 @@
 COMMON_OBJ += document.o
 COMMON_OBJ += object.o
 COMMON_OBJ += types.o
-#COMMON_OBJ = observer.o
-#COMMON_OBJ += list.o
-
+COMMON_OBJ += properties.o
 
 TOOLKITOBJS += $(COMMON_OBJ:%=$(COMMON_OBJPRE)%)
 TOOLKITSOURCE += $(COMMON_OBJ:%.o=common/%.c)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/common/properties.c	Fri Apr 04 17:45:36 2014 +0200
@@ -0,0 +1,174 @@
+/*
+ * 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);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/common/properties.h	Fri Apr 04 17:45:36 2014 +0200
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+#ifndef UIC_PROPERTIES_H
+#define	UIC_PROPERTIES_H
+
+#include "../ui/properties.h"
+#include "../../ucx/map.h"
+#include "../../ucx/list.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+#ifdef _WIN32
+#define UI_HOME "USERPROFILE"
+#else
+#define UI_HOME "HOME"
+#endif
+
+void uic_load_app_properties();
+void uic_store_app_properties();
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* UIC_PROPERTIES_H */
+
--- a/ui/gtk/toolkit.c	Fri Apr 04 11:29:17 2014 +0200
+++ b/ui/gtk/toolkit.c	Fri Apr 04 17:45:36 2014 +0200
@@ -32,6 +32,7 @@
 #include "toolkit.h"
 #include "toolbar.h"
 #include "../common/document.h"
+#include "../common/properties.h"
 
 static char *application_name;
 
@@ -47,6 +48,8 @@
     
     // init custom types
     //ui_list_init();
+    
+    uic_load_app_properties();
 }
 
 void ui_exitfunc(ui_callback f, void *udata) {
@@ -54,11 +57,16 @@
     appclose_udata = udata;
 }
 
+char* ui_appname() {
+    return application_name;
+}
+
 void ui_main() {
     gtk_main();
     if(appclose_fnc) {
         appclose_fnc(NULL, appclose_udata);
     }
+    uic_store_app_properties();
 }
 
 void ui_show(UiObject *obj) {
--- a/ui/gtk/window.c	Fri Apr 04 11:29:17 2014 +0200
+++ b/ui/gtk/window.c	Fri Apr 04 17:45:36 2014 +0200
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 
 #include "../ui/window.h"
+#include "../ui/properties.h"
 #include "../common/context.h"
 
 #include "menu.h"
@@ -58,16 +59,26 @@
 UiObject* ui_window(char *title, void *window_data) {
     UcxMempool *mp = ucx_mempool_new(256);
     UiObject *obj = ucx_mempool_calloc(mp, 1, sizeof(UiObject));  
-    obj->widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+    obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     obj->ctx = uic_context(obj, mp);
     
     if(title != NULL) {
         gtk_window_set_title(GTK_WINDOW(obj->widget), title);
     }
-    gtk_window_set_default_size(
-            GTK_WINDOW(obj->widget),
-            window_default_width,
-            window_default_height);
+    
+    char *width = ui_get_property("ui.window.width");
+    char *height = ui_get_property("ui.window.height");
+    if(width && height) {
+        gtk_window_set_default_size(
+                GTK_WINDOW(obj->widget),
+                atoi(width),
+                atoi(height));
+    } else {
+        gtk_window_set_default_size(
+                GTK_WINDOW(obj->widget),
+                window_default_width,
+                window_default_height);
+    }
     
     g_signal_connect(
             obj->widget,
--- a/ui/motif/toolkit.c	Fri Apr 04 11:29:17 2014 +0200
+++ b/ui/motif/toolkit.c	Fri Apr 04 17:45:36 2014 +0200
@@ -33,6 +33,7 @@
 #include "toolbar.h"
 #include "stock.h"
 #include "../common/document.h"
+#include "../common/properties.h"
 
 static XtAppContext app;
 static Display *display;
@@ -60,6 +61,8 @@
     uic_docmgr_init();
     ui_toolbar_init();
     ui_stock_init();
+    
+    uic_load_app_properties();
 }
 
 Display* ui_get_display() {
@@ -76,6 +79,7 @@
     if(appclose_fnc) {
         appclose_fnc(NULL, appclose_udata);
     }
+    uic_store_app_properties();
 }
 
 void ui_show(UiObject *obj) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/ui/properties.h	Fri Apr 04 17:45:36 2014 +0200
@@ -0,0 +1,53 @@
+/*
+ * 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.
+ */
+
+#ifndef UI_PROPERTIES_H
+#define	UI_PROPERTIES_H
+
+#include "toolkit.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct UcxMap UiProperties;
+    
+char* ui_getappdir();
+char* ui_configfile(char *name);
+
+char* ui_get_property(char *name);
+void  ui_set_property(char *name, char *value);
+
+void ui_set_default_property(char *name, char *value);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* UI_PROPERTIES_H */
+
--- a/ui/ui/toolkit.h	Fri Apr 04 11:29:17 2014 +0200
+++ b/ui/ui/toolkit.h	Fri Apr 04 17:45:36 2014 +0200
@@ -169,9 +169,7 @@
 
 void ui_init(char *appname, int argc, char **argv);
 void ui_exitfunc(ui_callback f, void *udata);
-
-char* ui_getappdir();
-char* ui_configfile(char *name);
+char* ui_appname();
 
 void ui_main();
 void ui_show(UiObject *obj);
--- a/ui/ui/ui.h	Fri Apr 04 11:29:17 2014 +0200
+++ b/ui/ui/ui.h	Fri Apr 04 17:45:36 2014 +0200
@@ -36,6 +36,7 @@
 #include "stock.h"
 #include "button.h"
 #include "text.h"
+#include "properties.h"
 
 #endif	/* UI_H */
 

mercurial