Add new function for setting the configuration value via argument
[platform/framework/web/livebox-viewer.git] / src / livebox.c
index 27d1290..a139f7a 100644 (file)
@@ -19,6 +19,7 @@
 #include <stdlib.h> /* malloc */
 #include <string.h> /* strdup */
 #include <math.h>
+#include <unistd.h>
 
 #include <aul.h>
 #include <dlog.h>
@@ -620,6 +621,44 @@ static int send_mouse_event(struct livebox *handler, const char *event, int x, i
        return master_rpc_request_only(handler, packet);
 }
 
+static void initialize_livebox(void *disp)
+{
+#if defined(FLOG)
+       char filename[BUFSIZ];
+       snprintf(filename, sizeof(filename), "/tmp/%d.box.log", getpid());
+       __file_log_fp = fopen(filename, "w+t");
+       if (!__file_log_fp) {
+               __file_log_fp = fdopen(1, "w+t");
+       }
+#endif
+       critical_log_init("viewer");
+       livebox_service_init();
+       fb_init(disp);
+
+       client_init();
+
+       s_info.init_count++;
+}
+
+EAPI int livebox_init_with_options(void *disp, int prevent_overwrite, double event_filter)
+{
+       if (s_info.init_count > 0) {
+               s_info.init_count++;
+               return LB_STATUS_SUCCESS;
+       }
+
+       /*!
+        * \note
+        * Some application doesn't want to use the environment value.
+        * So set them using arguments.
+        */
+       s_info.prevent_overwrite = prevent_overwrite;
+       MINIMUM_EVENT = event_filter;
+
+       initialize_livebox(disp);
+       return LB_STATUS_SUCCESS;
+}
+
 EAPI int livebox_init(void *disp)
 {
        const char *env;
@@ -628,6 +667,7 @@ EAPI int livebox_init(void *disp)
                s_info.init_count++;
                return LB_STATUS_SUCCESS;
        }
+
        env = getenv("PROVIDER_DISABLE_PREVENT_OVERWRITE");
        if (env && !strcasecmp(env, "true")) {
                s_info.prevent_overwrite = 1;
@@ -638,21 +678,7 @@ EAPI int livebox_init(void *disp)
                sscanf(env, "%lf", &MINIMUM_EVENT);
        }
 
-#if defined(FLOG)
-       char filename[BUFSIZ];
-       snprintf(filename, sizeof(filename), "/tmp/%d.box.log", getpid());
-       __file_log_fp = fopen(filename, "w+t");
-       if (!__file_log_fp) {
-               __file_log_fp = fdopen(1, "w+t");
-       }
-#endif
-       critical_log_init("viewer");
-       livebox_service_init();
-       fb_init(disp);
-
-       client_init();
-
-       s_info.init_count++;
+       initialize_livebox(disp);
        return LB_STATUS_SUCCESS;
 }
 
@@ -736,7 +762,7 @@ EAPI struct livebox *livebox_add_with_size(const char *pkgname, const char *cont
                return NULL;
        }
 
-       if (content) {
+       if (content && strlen(content)) {
                handler->content = strdup(content);
                if (!handler->content) {
                        ErrPrint("Error: %s\n", strerror(errno));
@@ -2678,49 +2704,6 @@ struct livebox *lb_find_livebox_by_timestamp(double timestamp)
        return NULL;
 }
 
-static inline char *get_file_kept_in_safe(const char *id)
-{
-       const char *path;
-       char *new_path;
-       int len;
-       int base_idx;
-
-       path = util_uri_to_path(id);
-       if (!path) {
-               ErrPrint("Invalid URI(%s)\n", id);
-               return NULL;
-       }
-
-       /*!
-        * \TODO: REMOVE ME
-        */
-       if (s_info.prevent_overwrite) {
-               new_path = strdup(path);
-               if (!new_path) {
-                       ErrPrint("Heap: %s\n", strerror(errno));
-               }
-
-               return new_path;
-       }
-
-
-       len = strlen(path);
-       base_idx = len - 1;
-
-       while (base_idx > 0 && path[base_idx] != '/') base_idx--;
-       base_idx += (path[base_idx] == '/');
-
-       new_path = malloc(len + 10);
-       if (!new_path) {
-               ErrPrint("Heap: %s\n", strerror(errno));
-               return NULL;
-       }
-
-       strncpy(new_path, path, base_idx);
-       snprintf(new_path + base_idx, len + 10 - base_idx, "reader/%s", path + base_idx);
-       return new_path;
-}
-
 struct livebox *lb_new_livebox(const char *pkgname, const char *id, double timestamp)
 {
        struct livebox *handler;
@@ -2746,14 +2729,6 @@ struct livebox *lb_new_livebox(const char *pkgname, const char *id, double times
                return NULL;
        }
 
-       handler->filename = get_file_kept_in_safe(id);
-       if (!handler->filename) {
-               handler->filename = strdup(util_uri_to_path(id));
-               if (!handler->filename) {
-                       ErrPrint("Error: %s\n", strerror(errno));
-               }
-       }
-
        handler->timestamp = timestamp;
        handler->lb.type = _LB_TYPE_FILE;
        handler->pd.type = _PD_TYPE_SCRIPT;
@@ -2847,17 +2822,22 @@ void lb_set_id(struct livebox *handler, const char *id)
        if (!handler->id) {
                ErrPrint("Error: %s\n", strerror(errno));
        }
+}
 
+void lb_set_filename(struct livebox *handler, const char *filename)
+{
        if (handler->filename) {
+               if (unlink(handler->filename) < 0) {
+                       ErrPrint("unlink: %s\n", strerror(errno));
+               }
+
                free(handler->filename);
        }
 
-       handler->filename = get_file_kept_in_safe(id);
+       handler->filename = strdup(filename);
        if (!handler->filename) {
-               handler->filename = strdup(util_uri_to_path(id));
-               if (!handler->filename) {
-                       ErrPrint("Error: %s\n", strerror(errno));
-               }
+               ErrPrint("Heap: %s\n", strerror(errno));
+               return;
        }
 }
 
@@ -3063,7 +3043,7 @@ struct livebox *lb_unref(struct livebox *handler)
        }
 
        if (handler->filename) {
-               util_unlink(handler->filename);
+               (void)util_unlink(handler->filename);
        }
 
        dlist_remove_data(s_info.livebox_list, handler);