tizen 2.3.1 release
[apps/home/minicontrol.git] / src / minicontrol-provider.c
index 306f7bc..bda4b06 100755 (executable)
@@ -1,14 +1,14 @@
 /*
- * Copyright 2012  Samsung Electronics Co., Ltd
+ * Copyright (c)  2013-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
- * Licensed under the Flora License, Version 1.0 (the "License");
+ * Licensed under the Apache License, Version 2.0 (the License);
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *  http://www.tizenopensource.org/license
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
+ * distributed under the License is distributed on an AS IS BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
@@ -21,6 +21,7 @@
 #include "minicontrol-internal.h"
 #include "minicontrol-provider.h"
 #include "minicontrol-log.h"
+#include "minicontrol-handler.h"
 
 #define MINICTRL_PRIORITY_SUFFIX_TOP "__minicontrol_top"
 #define MINICTRL_PRIORITY_SUFFIX_LOW "__minicontrol_low"
@@ -37,6 +38,7 @@ struct _provider_data {
        minicontrol_priority_e priority;
        Evas_Object *obj;
        minictrl_sig_handle *sh;
+       minicontrol_h handler;
 };
 
 static void __provider_data_free(struct _provider_data *pd)
@@ -48,6 +50,9 @@ static void __provider_data_free(struct _provider_data *pd)
                if (pd->sh)
                        _minictrl_dbus_sig_handle_dettach(pd->sh);
 
+               if (pd->handler)
+                       _minictrl_handler_destroy(pd->handler);
+
                free(pd);
        }
 }
@@ -87,7 +92,7 @@ static void _running_req_cb(void *data, DBusMessage *msg)
                Evas_Coord h = 0;
                evas_object_geometry_get(pd->obj, NULL, NULL, &w, &h);
                _minictrl_provider_message_send(MINICTRL_DBUS_SIG_START,
-                                       pd->name, w, h, pd->priority);
+                                       pd->name, w, h, pd->priority, pd->handler);
        }
 }
 
@@ -118,10 +123,45 @@ static int minicontrol_win_start(Evas_Object *mincontrol)
                pd->state = MINICTRL_STATE_RUNNING;
 
                evas_object_geometry_get(mincontrol, NULL, NULL, &w, &h);
+               _minictrl_provider_proc_send(MINICONTROL_DBUS_PROC_EXCLUDE);
                ret = _minictrl_provider_message_send(MINICTRL_DBUS_SIG_START,
-                                       pd->name, w, h, pd->priority);
+                                       pd->name, w, h, pd->priority, pd->handler);
+       }
+
+       return ret;
+}
+
+static int minicontrol_win_realize(Evas_Object *mincontrol)
+{
+       struct _provider_data *pd;
+       int ret = MINICONTROL_ERROR_NONE;
+
+       if (!mincontrol) {
+               ERR("mincontrol is NULL, invaild parameter");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+
+       pd = evas_object_data_get(mincontrol, MINICTRL_DATA_KEY);
+       if (!pd) {
+               ERR("pd is NULL, invaild parameter");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!pd->name) {
+               ERR("pd name is NULL, invaild parameter");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+
+       Evas_Coord w = 0;
+       Evas_Coord h = 0;
+       if (pd->state != MINICTRL_STATE_RUNNING) {
+               pd->state = MINICTRL_STATE_RUNNING;
        }
 
+       evas_object_geometry_get(mincontrol, NULL, NULL, &w, &h);
+       ret = _minictrl_provider_message_send(MINICTRL_DBUS_SIG_REALIZE,
+                               pd->name, w, h, pd->priority, pd->handler);
+
        return ret;
 }
 
@@ -147,8 +187,9 @@ static int minicontrol_win_stop(Evas_Object *mincontrol)
        }
        if (pd->state != MINICTRL_STATE_READY) {
                pd->state = MINICTRL_STATE_READY;
+               _minictrl_provider_proc_send(MINICONTROL_DBUS_PROC_INCLUDE);
                ret = _minictrl_provider_message_send(MINICTRL_DBUS_SIG_STOP,
-                                       pd->name, 0, 0, pd->priority);
+                                       pd->name, 0, 0, pd->priority, pd->handler);
        }
 
        return ret;
@@ -179,6 +220,35 @@ static void _minictrl_win_show(void *data, Evas *e,
        minicontrol_win_start(obj);
 }
 
+static void _minictrl_win_realize(void *data, Evas *e,
+                       Evas_Object *obj, void *event_info)
+{
+       struct _provider_data *pd;
+
+       if (!data) {
+               ERR("data is NULL, invaild parameter");
+               return;
+       }
+       pd = data;
+
+       if (!pd->obj) {
+               ERR("minicontrol object is NULL, invalid parameter");
+               return;
+       }
+
+       Evas *win_e = evas_object_evas_get(pd->obj);
+       if (win_e != NULL) {
+               evas_event_callback_del(win_e, EVAS_CALLBACK_RENDER_POST,
+                                               _minictrl_win_realize);
+       }
+
+       minicontrol_win_realize(pd->obj);
+
+       if (pd->handler != NULL) {
+               _minictrl_handler_set_state(pd->handler, 1);
+       }
+}
+
 static void _minictrl_win_resize(void *data, Evas *e,
                        Evas_Object *obj, void *event_info)
 {
@@ -196,15 +266,12 @@ static void _minictrl_win_resize(void *data, Evas *e,
 
                evas_object_geometry_get(obj, NULL, NULL, &w, &h);
                _minictrl_provider_message_send(MINICTRL_DBUS_SIG_RESIZE,
-                                       pd->name, w, h, pd->priority);
+                                       pd->name, w, h, pd->priority, pd->handler);
        }
 }
 
 static char *_minictrl_create_name(const char *name)
 {
-       time_t now;
-       struct tm *now_tm;
-       char time_buf[20]; /* 18 chars to represent time */
        char *buf;
        int size = 0;
 
@@ -213,18 +280,14 @@ static char *_minictrl_create_name(const char *name)
                return NULL;
        }
 
-       now = time(NULL);
-       now_tm = localtime(&now);
-       strftime(time_buf, sizeof(time_buf), "%y-%m-%d-%H:%M:%S", now_tm);
-
-       size = snprintf(NULL, 0, "[%s]-[%s]", name, time_buf) + 1;
+       size = snprintf(NULL, 0, "[%s]", name) + 1;
        buf = (char *)malloc(sizeof(char) * size);
        if (!buf) {
               ERR("fail to alloc buf");
               return NULL;
        }
 
-       snprintf(buf, size, "[%s]-[%s]", name, time_buf);
+       snprintf(buf, size, "[%s]", name);
 
        return buf;
 }
@@ -243,35 +306,92 @@ static minicontrol_priority_e _minictrl_get_priroty_by_name(const char *name)
 
 EXPORT_API Evas_Object *minicontrol_win_add(const char *name)
 {
+       minicontrol_h handler = NULL;
+       Evas_Object *win = NULL;
+       
+       if (_minictrl_handler_create(&handler) == MINICONTROL_ERROR_NONE) {
+               if (_minictrl_handler_set_service_name(handler, name) != MINICONTROL_ERROR_NONE) {
+                       ERR("failed to service name");
+                       _minictrl_handler_destroy(handler);
+                       return NULL;
+               }
+               if (_minictrl_handler_set_timestamp(handler, time(NULL)) != MINICONTROL_ERROR_NONE) {
+                       ERR("failed to set timestamp");
+               }
+               if (_minictrl_handler_set_priority(handler, MINICONTROL_HDL_PRIORITY_LV3) != MINICONTROL_ERROR_NONE) {
+                       ERR("failed to set priority");
+               }
+       } else {
+               ERR("Failed to create minicontrol handler");
+               return NULL;
+       }
+
+       win = minicontrol_win_add_by_handler(handler);
+       _minictrl_handler_destroy(handler);
+
+       return win;
+}
+
+EXPORT_API Evas_Object *minicontrol_win_add_by_handler(minicontrol_h handler)
+{
        Evas_Object *win = NULL;
+       char *name = NULL;
        char *name_inter = NULL;
+       char *priority = NULL;
        struct _provider_data *pd;
+       time_t timestamp_value = 0;
 
-       if (!name)
+       if (_minictrl_handler_check_validation(handler) != MINICONTROL_ERROR_NONE) {
+               ERR("handler is invalid, invaild parameter");
                return NULL;
+       }
+
+       _minictrl_handler_get_service_name(handler, &name);
+       if (name == NULL) {
+               ERR("service name cannot be NULL");
+               return NULL;
+       }
+       _minictrl_handler_get_timestamp(handler, &timestamp_value);
+       if (timestamp_value == 0) {
+               if (_minictrl_handler_set_timestamp(handler, time(NULL)) != MINICONTROL_ERROR_NONE) {
+                       ERR("failed to set timestamp");
+               }
+       }
+       _minictrl_handler_get_priority(handler, &priority);
+       if (priority == NULL) {
+               if (_minictrl_handler_set_priority(handler, MINICONTROL_HDL_PRIORITY_LV3) != MINICONTROL_ERROR_NONE) {
+                       ERR("failed to set priority");
+               }
+       } else {
+               free(priority);
+       }
 
        win = elm_win_add(NULL, "minicontrol", ELM_WIN_SOCKET_IMAGE);
-       if (!win)
+       if (!win) {
+               free(name);
                return NULL;
+       }
 
        name_inter = _minictrl_create_name(name);
        if (!name_inter) {
-
                ERR("Fail to create name_inter for : %s", name);
+               free(name);
                evas_object_del(win);
                return NULL;
-
        }
 
-       if (!elm_win_socket_listen(win, name_inter, 0, EINA_FALSE)) {
+       if (!elm_win_socket_listen(win, name_inter, 0, EINA_TRUE)) {
                ERR("Fail to elm win socket listen");
+               free(name);
                evas_object_del(win);
+               free(name_inter);
                return NULL;
        }
 
        pd = malloc(sizeof(struct _provider_data));
        if (!pd) {
                ERR("Fail to alloc memory");
+               free(name);
                evas_object_del(win);
                free(name_inter);
                return NULL;
@@ -283,6 +403,15 @@ EXPORT_API Evas_Object *minicontrol_win_add(const char *name)
        pd->obj = win;
        pd->priority = _minictrl_get_priroty_by_name(name);
 
+       if (_minictrl_handler_clone(handler, &(pd->handler)) != MINICONTROL_ERROR_NONE) {
+               ERR("Fail to clone handler");
+               free(name);
+               evas_object_del(win);
+               free(name_inter);
+               free(pd);
+               return NULL;
+       }
+
        evas_object_data_set(win ,MINICTRL_DATA_KEY,pd);
 
        elm_win_autodel_set(win, EINA_TRUE);
@@ -299,12 +428,113 @@ EXPORT_API Evas_Object *minicontrol_win_add(const char *name)
        evas_object_event_callback_add(win, EVAS_CALLBACK_RESIZE,
                                        _minictrl_win_resize, pd);
 
+       Evas *win_e = evas_object_evas_get(win);
+       if (win_e != NULL) {
+               evas_event_callback_add(win_e, EVAS_CALLBACK_RENDER_POST,
+                                               _minictrl_win_realize, pd);
+       }
+
        pd->sh = _minictrl_dbus_sig_handle_attach(MINICTRL_DBUS_SIG_RUNNING_REQ,
                                        _running_req_cb, pd);
 
-       INFO("new minicontrol win[%p] created - %s, priority[%d]",
-                               win, pd->name, pd->priority);
+       INFO("new minicontrol created - %s", pd->name);
+
+       free(name);
 
        return win;
 }
 
+EXPORT_API minicontrol_error_e minicontrol_win_handler_get(Evas_Object *mincontrol, minicontrol_h *handler)
+{
+       if (!mincontrol) {
+               ERR("invaild mincontrol object");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+       if (!handler) {
+               ERR("invaild mincontrol handler");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+
+       struct _provider_data *pd = evas_object_data_get(mincontrol, MINICTRL_DATA_KEY);
+       if (pd != NULL) {
+               *handler = pd->handler;
+       } else {
+               return MINICONTROL_ERROR_NO_DATA;
+       }
+
+       return MINICONTROL_ERROR_NONE;
+}
+
+EXPORT_API minicontrol_error_e minicontrol_win_handler_update(Evas_Object *mincontrol, minicontrol_h handler)
+{
+       struct _provider_data *pd;
+       int ret = MINICONTROL_ERROR_NONE;
+
+       if (!mincontrol) {
+               ERR("mincontrol is NULL, invaild parameter");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+       if (_minictrl_handler_check_validation(handler) != MINICONTROL_ERROR_NONE) {
+               ERR("handler is invalid, invaild parameter");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+
+       pd = evas_object_data_get(mincontrol, MINICTRL_DATA_KEY);
+       if (!pd) {
+               ERR("pd is NULL, invaild parameter");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!pd->name) {
+               ERR("pd name is NULL, invaild parameter");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+
+       if (pd->handler != handler) {
+               if (pd->handler != NULL) {
+                       _minictrl_handler_destroy(pd->handler);
+                       pd->handler = NULL;
+               }
+               if (_minictrl_handler_clone(handler, &(pd->handler)) != MINICONTROL_ERROR_NONE) {
+                       ERR("failed to clone a minicontrol handler");
+                       return MINICONTROL_ERROR_OUT_OF_MEMORY;
+               }
+       }
+
+       if (pd->state == MINICTRL_STATE_RUNNING) {
+               ret = _minictrl_provider_message_send(MINICTRL_DBUS_SIG_REQUEST,
+                                       pd->name, MINICONTROL_REQ_UPDATE_HANDLER, MINICONTROL_REQ_UPDATE_HANDLER
+                                       , pd->priority, pd->handler);
+       }
+
+       return ret;
+}
+
+EXPORT_API minicontrol_error_e minicontrol_request(Evas_Object *mincontrol, minicontrol_request_e request)
+{
+       struct _provider_data *pd;
+       int ret = MINICONTROL_ERROR_NONE;
+
+       if (!mincontrol) {
+               ERR("mincontrol is NULL, invaild parameter");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+
+       pd = evas_object_data_get(mincontrol, MINICTRL_DATA_KEY);
+       if (!pd) {
+               ERR("pd is NULL, invaild parameter");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!pd->name) {
+               ERR("pd name is NULL, invaild parameter");
+               return MINICONTROL_ERROR_INVALID_PARAMETER;
+       }
+
+       if (pd->state == MINICTRL_STATE_RUNNING) {
+               ret = _minictrl_provider_message_send(MINICTRL_DBUS_SIG_REQUEST,
+                                       pd->name, request, request, pd->priority, pd->handler);
+       }
+
+       return ret;
+}