[Tizen 3.0] 2.4 source code merge
[apps/core/preloaded/quickpanel.git] / daemon / settings / settings_ipc.c
1 /*
2  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
18 #include <Elementary.h>
19 #include <E_DBus.h>
20 #include <glib.h>
21
22 #include <tzsh.h>
23 #include <tzsh_quickpanel_service.h>
24 #include <E_DBus.h>
25
26 #include "quickpanel-ui.h"
27 #include "quickpanel_def.h"
28 #include "common.h"
29 #include "modules.h"
30 #include "settings.h"
31 #include "setting_utils.h"
32 #include "settings_view_all.h"
33 #include "settings_view_featured.h"
34
35 static struct _info {
36         E_DBus_Signal_Handler *hdl_activity;
37         E_DBus_Signal_Handler *hdl_editing;
38 } s_info = {
39         .hdl_activity = NULL,
40         .hdl_editing = NULL,
41 };
42
43 static void _handler_activity(void *data, DBusMessage *msg)
44 {
45         int ret = 0;
46         DBusError err;
47         char *module = NULL;
48         char *command = NULL;
49         QP_Module_Setting *mod = NULL;
50         retif(data == NULL || msg == NULL, , "Invalid parameter!");
51
52         dbus_error_init(&err);
53         ret = dbus_message_get_args(msg, &err,
54                         DBUS_TYPE_STRING, &module,
55                         DBUS_TYPE_STRING, &command,
56                         DBUS_TYPE_INVALID);
57         retif(ret == 0, , "dbus_message_get_args error");
58         retif(module == NULL, , "Failed to get module");
59         retif(command == NULL, , "Failed to get command");
60
61         if (dbus_error_is_set(&err)) {
62                 ERR("Dbus err: %s", err.message);
63                 dbus_error_free(&err);
64                 return;
65         }
66
67         mod = quickpanel_settings_module_get_by_name(module);
68         if (mod != NULL) {
69                 DBG("module:%s, command:%s", module, command);
70                 if (mod->handler_ipc != NULL) {
71                         if (mod->is_loaded == EINA_TRUE && mod->loader != NULL) {
72                                 mod->handler_ipc(command, mod);
73                         } else {
74                                 ERR("module:%s isn't loaded");
75                         }
76                 } else {
77                         ERR("module:%s don't have IPC handler");
78                 }
79         } else {
80                 ERR("failed to lookup module:%s", module);
81         }
82 }
83
84 static void _handler_editing(void *data, DBusMessage *msg)
85 {
86         int i = 0;
87         int ret = 0, is_error = 0;
88         DBusError err;
89         char *key = NULL;
90         char *order = NULL;
91         int num_featured = 0;
92         int order_count = 0;
93         gchar **order_split = NULL;
94         Eina_List *list_active = NULL;
95         QP_Module_Setting *mod = NULL;
96         retif(data == NULL || msg == NULL, , "Invalid parameter!");
97
98         dbus_error_init(&err);
99         ret = dbus_message_get_args(msg, &err,
100                         DBUS_TYPE_STRING, &key,
101                         DBUS_TYPE_STRING, &order,
102                         DBUS_TYPE_INT32, &num_featured,
103                         DBUS_TYPE_INVALID);
104         retif(ret == 0, , "dbus_message_get_args error");
105         retif(key == NULL, , "Failed to get key");
106         retif(order == NULL, , "Failed to get value");
107
108         if (dbus_error_is_set(&err)) {
109                 ERR("dbus err: %s", err.message);
110                 dbus_error_free(&err);
111                 return;
112         }
113
114         if (strcmp(key, "quicksetting_order") == 0) {
115                 DBG("order:%s %d", order, num_featured);
116                 if (quickpanel_settings_featured_list_validation_check(order) == 1) {
117                         order_split = g_strsplit(order, ",", 0);
118                         if (order_split != NULL) {
119                                 order_count = g_strv_length(order_split);
120                                 DBG("count of quicksettings:%d", order_count);
121
122                                 for (i = 0; i < order_count; i++) {
123                                         mod = quickpanel_settings_module_get_by_name(order_split[i]);
124                                         if (mod != NULL && mod->init != NULL) {
125                                                 list_active = eina_list_append (list_active, mod);
126                                         } else {
127                                                 ERR("failed to get quicksetting:%s", order_split[i]);
128                                                 is_error = 1;
129                                         }
130                                 }
131
132                                 if (is_error == 0) {
133                                         if (list_active != NULL) {
134                                                 quickpanel_setting_view_featured_reload(list_active, num_featured);
135                                                 quickpanel_setting_view_all_reload(list_active);
136                                                 quickpanel_setting_save_list_to_file(list_active, num_featured);
137                                                 eina_list_free(list_active);
138                                         }
139                                 }
140                                 g_strfreev(order_split);
141                         }
142                 } else {
143                         ERR("setting order validation check failed, igonore this signal");
144                 }
145         }
146 }
147
148 static void _settings_ipc_init(void *data)
149 {
150         struct appdata *ad = data;
151         retif(ad == NULL, , "Invalid parameter!");
152         retif(ad->dbus_connection == NULL, , "Invalid parameter!");
153
154         s_info.hdl_activity =
155                 e_dbus_signal_handler_add(ad->dbus_connection, NULL,
156                                 QP_DBUS_PATH,
157                                 QP_DBUS_NAME,
158                                 QS_DBUS_SIG_ACTIVITY,
159                                 _handler_activity, ad);
160         msgif(s_info.hdl_activity == NULL, "fail to add size signal");
161
162         s_info.hdl_editing =
163                 e_dbus_signal_handler_add(ad->dbus_connection, NULL,
164                                 QP_DBUS_PATH,
165                                 QP_DBUS_NAME,
166                                 QS_DBUS_SIG_EDITING,
167                                 _handler_editing, ad);
168         msgif(s_info.hdl_editing == NULL, "fail to add size signal");
169 }
170
171 static void _settings_ipc_fini(void *data)
172 {
173         struct appdata *ad = data;
174         retif(ad == NULL, , "Invalid parameter!");
175         retif(ad->dbus_connection == NULL, , "Invalid parameter!");
176
177         if (s_info.hdl_activity != NULL) {
178                 e_dbus_signal_handler_del(ad->dbus_connection,
179                                 s_info.hdl_activity);
180                 s_info.hdl_activity = NULL;
181         }
182
183         if (s_info.hdl_editing != NULL) {
184                 e_dbus_signal_handler_del(ad->dbus_connection,
185                                 s_info.hdl_editing);
186                 s_info.hdl_editing = NULL;
187         }
188 }
189
190 /*****************************************************************************
191  *
192  * Util functions
193  *
194  *****************************************************************************/
195 HAPI int quickpanel_settings_ipc_init(void *data)
196 {
197         _settings_ipc_init(data);
198
199         return QP_OK;
200 }
201
202 HAPI int quickpanel_settings_ipc_fini(void *data)
203 {
204         _settings_ipc_fini(data);
205
206         return QP_OK;
207 }