Initialize Tizen 2.3
[apps/home/minicontrol.git] / src / minicontrol-viewer.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *  http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <Elementary.h>
18 #include <Ecore_Evas.h>
19
20 #include "minicontrol-internal.h"
21 #include "minicontrol-type.h"
22 #include "minicontrol-viewer.h"
23 #include "minicontrol-log.h"
24
25 #define MINICTRL_PLUG_DATA_KEY "__minictrl_plug_name"
26
27 static void _minictrl_plug_server_del(Ecore_Evas *ee)
28 {
29         char *svr_name = NULL;
30
31         svr_name = ecore_evas_data_get(ee, MINICTRL_PLUG_DATA_KEY);
32         if (!svr_name) {
33                 ERR("fail to get svr_name");
34                 return;
35         }
36
37         INFO("server - %s is deleted", svr_name);
38
39         /* send message to remve plug */
40         _minictrl_provider_message_send(MINICTRL_DBUS_SIG_STOP,
41                                         svr_name, 0, 0,
42                                         MINICONTROL_PRIORITY_LOW);
43         _minictrl_provider_proc_send(MINICONTROL_DBUS_PROC_INCLUDE);
44 }
45
46 static void _minictrl_plug_del(void *data, Evas *e,
47                         Evas_Object *obj, void *event_info)
48 {
49         Ecore_Evas *ee = NULL;
50         char *svr_name = NULL;
51
52         ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
53         if (!ee)
54                 return;
55
56         svr_name = ecore_evas_data_get(ee, MINICTRL_PLUG_DATA_KEY);
57         if (svr_name)
58                 free(svr_name);
59
60         ecore_evas_data_set(ee, MINICTRL_PLUG_DATA_KEY, NULL);
61 }
62
63 EXPORT_API
64 Evas_Object *minicontrol_viewer_image_object_get(const Evas_Object *obj)
65 {
66         return elm_plug_image_object_get(obj);
67 }
68
69 EXPORT_API Evas_Object *minicontrol_viewer_add(Evas_Object *parent,
70                                                 const char *svr_name)
71 {
72         Evas_Object *plug = NULL;
73         Evas_Object *plug_img = NULL;
74         Ecore_Evas *ee = NULL;
75
76         plug = elm_plug_add(parent);
77         if (!plug) {
78                 ERR("fail to create plug");
79                 return NULL;
80         }
81
82         if (!elm_plug_connect(plug, svr_name, 0, EINA_TRUE)) {
83                 ERR("Cannot connect plug[%s]", svr_name);
84                 evas_object_del(plug);
85                 return NULL;
86         }
87
88         plug_img = elm_plug_image_object_get(plug);
89
90         ee = ecore_evas_object_ecore_evas_get(plug_img);
91         ecore_evas_data_set(ee, MINICTRL_PLUG_DATA_KEY, strdup(svr_name));
92         ecore_evas_callback_delete_request_set(ee, _minictrl_plug_server_del);
93
94         evas_object_event_callback_add(plug, EVAS_CALLBACK_DEL,
95                                         _minictrl_plug_del, plug);
96
97         return plug;
98 }
99
100 EXPORT_API minicontrol_error_e minicontrol_viewer_request(const char *appid, minicontrol_request_e request, int value)
101 {
102         int ret = MINICONTROL_ERROR_NONE;
103
104         if (appid == NULL) {
105                 ERR("appid is NULL, invaild parameter");
106                 return MINICONTROL_ERROR_INVALID_PARAMETER;
107         }
108         if (request != MINICONTROL_REQ_ROTATE_PROVIDER
109                 && request != MINICONTROL_REQ_PROVIDER_PAUSE
110                 && request != MINICONTROL_REQ_PROVIDER_RESUME) {
111                 return MINICONTROL_ERROR_INVALID_PARAMETER;
112         }
113
114         ret = _minictrl_provider_message_send(MINICTRL_DBUS_SIG_REQUEST,
115                         appid, request, value, MINICONTROL_PRIORITY_MIDDLE);
116
117         return ret;
118 }
119