tizen 2.4 release
[apps/home/settings.git] / setting-applications / src / setting-applications.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <setting-applications.h>
23 #include <Ecore_X.h>
24 #include <utilX.h>
25
26 #include <Eina.h>
27
28 #include <setting-cfg.h>
29
30 #define SETTING_BRIGHTNESS_DEFAULT_LEVEL 60
31
32 #ifndef UG_MODULE_API
33 #define UG_MODULE_API __attribute__ ((visibility("default")))
34 #endif
35
36 /**
37  * view selection by service variable
38  *
39  * viewtype == 'brightness' --> setting_view_applications_brightness
40  * viewtype != NULL && viewtype --> setting_view_applications_main
41  */
42 setting_view *__get_applications_view_to_load(void *data, app_control_h service)
43 {
44         SETTING_TRACE_BEGIN;
45         setting_retvm_if((!data), NULL, "!data");
46
47         SettingApplicationsUG *applicationsUG = (SettingApplicationsUG *)data;
48         char *viewtype = NULL;
49         char *uri = NULL;
50
51         setting_view_node_table_register(&setting_view_applications_main, NULL);
52         setting_view_node_table_register(&setting_view_applications_defaultapp, &setting_view_applications_main);
53         return &setting_view_applications_main;
54 }
55
56 /**
57  * return view layout
58  *
59  * viewtype != 'wallpaper' --> ly_main
60  */
61 Evas_Object *__get_applications_layout_to_return(app_control_h service, void *priv)
62 {
63         SETTING_TRACE_BEGIN;
64         SettingApplicationsUG *applicationsUG = priv;
65         return applicationsUG->ly_main;
66
67 }
68
69
70 static void setting_applications_ug_cb_resize(void *data, Evas *e,
71                                               Evas_Object *obj, void *event_info)
72 {
73         SettingApplicationsUG *ad = (SettingApplicationsUG *) data;
74         setting_view_update(ad->view_to_load, ad);
75 }
76
77 static void *setting_applications_ug_on_create(ui_gadget_h ug,
78                                                enum ug_mode mode, app_control_h service,
79                                                void *priv)
80 {
81         setting_retvm_if((!priv), NULL, "!priv");
82         SETTING_TRACE_BEGIN;
83
84         SettingApplicationsUG *applicationsUG = priv;
85         applicationsUG->ug = ug;
86         setting_set_i18n(SETTING_PACKAGE, SETTING_LOCALEDIR);
87
88         applicationsUG->win_main_layout = (Evas_Object *) ug_get_parent_layout(ug);
89         applicationsUG->win_get = (Evas_Object *) ug_get_window();
90
91         /*evas_object_show(applicationsUG->win_main_layout); */
92         applicationsUG->evas = evas_object_evas_get(applicationsUG->win_main_layout);
93
94         setting_retvm_if(applicationsUG->win_main_layout == NULL, NULL,
95                          "cannot get main window ");
96
97         /* register view node table */
98         setting_view_node_table_intialize();
99
100         setting_create_Gendial_itc(SETTING_GENLIST_2LINE_STYLE, &(applicationsUG->itc_2text_2));
101         setting_create_Gendial_itc(SETTING_GENLIST_ICON_1LINE_STYLE, &(applicationsUG->itc_1text_1icon));
102
103         setting_create_Gendial_itc(SETTING_GENLIST_ICON_1LINE_STYLE, &(applicationsUG->itc_1icon_1text_sub));
104
105         setting_create_Gendial_itc(SETTING_GENLIST_ICON_1LINE_STYLE, &(applicationsUG->itc_1text));
106         setting_create_Gendial_itc(SETTING_GENLIST_ICON_1LINE_STYLE, &(applicationsUG->itc_2text_3));
107         setting_create_Gendial_itc(SETTING_GENLIST_ICON_1LINE_STYLE, &(applicationsUG->itc_1text_1icon_divider));
108
109         /*  creating a view. */
110         applicationsUG->view_to_load = __get_applications_view_to_load(applicationsUG, service);
111         setting_retvm_if(NULL == applicationsUG->view_to_load, NULL, "NULL == applicationsUG->view_to_load");
112
113         setting_view_node_set_cur_view(applicationsUG->view_to_load);
114         setting_view_create(applicationsUG->view_to_load, (void *)applicationsUG);
115         evas_object_event_callback_add(applicationsUG->win_main_layout,
116                                        EVAS_CALLBACK_RESIZE,
117                                        setting_applications_ug_cb_resize, applicationsUG);
118         return __get_applications_layout_to_return(service, applicationsUG);
119 }
120
121 static void setting_applications_ug_on_start(ui_gadget_h ug, app_control_h service,
122                                              void *priv)
123 {
124 }
125
126 static void setting_applications_ug_on_pause(ui_gadget_h ug, app_control_h service,
127                                              void *priv)
128 {
129         SETTING_TRACE_BEGIN;
130         setting_retm_if((!priv), "!priv");
131         SettingApplicationsUG *applicationsUG = priv;
132 }
133
134 static void setting_applications_ug_on_resume(ui_gadget_h ug, app_control_h service,
135                                               void *priv)
136 {
137         SETTING_TRACE_BEGIN;
138         setting_retvm_if((!priv), NULL, "!priv");
139
140         SettingApplicationsUG *applicationsUG = priv;
141
142 #if 0
143         if (applicationsUG->data_home) {
144                 char *appid = vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME);
145                 char *sub_desc = setting_application_get_defaultapp_name(appid);
146                 if (sub_desc)
147                         applicationsUG->data_home->sub_desc = (char *)strdup(sub_desc);
148                 elm_object_item_data_set(applicationsUG->data_home->item, applicationsUG->data_home);
149                 elm_genlist_item_update(applicationsUG->data_home->item);
150                 free(sub_desc);
151         }
152 #endif
153 }
154
155 static void setting_applications_ug_on_destroy(ui_gadget_h ug, app_control_h service,
156                                                void *priv)
157 {
158         SETTING_TRACE_BEGIN;
159         setting_retm_if((!priv), "!priv");
160         SettingApplicationsUG *applicationsUG = priv;
161
162         /* fix flash issue for gallery */
163         evas_object_event_callback_del(applicationsUG->win_main_layout, EVAS_CALLBACK_RESIZE, setting_applications_ug_cb_resize);
164         applicationsUG->ug = ug;
165
166         FREE(applicationsUG->uri);
167
168         /*  called when this shared gadget is terminated. similar with app_exit */
169         if (&setting_view_applications_main == applicationsUG->view_to_load) {
170                 setting_view_destroy(&setting_view_applications_main, applicationsUG);
171         } else {
172                 /* do nothing */
173         }
174
175         if (NULL != ug_get_layout(applicationsUG->ug)) {
176                 evas_object_hide((Evas_Object *) ug_get_layout(applicationsUG->ug));
177                 evas_object_del((Evas_Object *) ug_get_layout(applicationsUG->ug));
178         }
179
180         SETTING_TRACE_END;
181 }
182
183 static void setting_applications_ug_on_message(ui_gadget_h ug, app_control_h msg,
184                                                app_control_h service, void *priv)
185 {
186         SETTING_TRACE_BEGIN;
187 }
188
189 static void setting_applications_ug_on_event(ui_gadget_h ug,
190                                              enum ug_event event, app_control_h service,
191                                              void *priv)
192 {
193         SETTING_TRACE_BEGIN;
194         SettingApplicationsUG *ad = priv;
195         setting_retm_if(NULL == ad, "ad is NULL");
196         switch (event) {
197                 case UG_EVENT_LOW_MEMORY:
198                         break;
199                 case UG_EVENT_LOW_BATTERY:
200                         break;
201                 case UG_EVENT_LANG_CHANGE:
202                         setting_navi_items_update(ad->navi_bar);
203                         break;
204                 case UG_EVENT_ROTATE_PORTRAIT:
205                         break;
206                 case UG_EVENT_ROTATE_LANDSCAPE:
207                 case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
208                         break;
209                 case UG_EVENT_REGION_CHANGE:
210                         break;
211                 default:
212                         break;
213         }
214 }
215
216 static void setting_applications_ug_on_key_event(ui_gadget_h ug,
217                                                  enum ug_key_event event,
218                                                  app_control_h service, void *priv)
219 {
220         SETTING_TRACE_BEGIN;
221         SettingApplicationsUG *ad = (SettingApplicationsUG *) priv;
222
223         switch (event) {
224                 case UG_KEY_EVENT_END: {
225                                 if (elm_naviframe_top_item_get(ad->navi_bar) ==
226                                     elm_naviframe_bottom_item_get(ad->navi_bar)) {
227                                         ug_destroy_me(ug);
228                                 } else {
229                                         setting_view_cb_at_endKey(ad);
230                                 }
231                         }
232                         break;
233                 default:
234                         break;
235         }
236 }
237
238 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
239 {
240         SETTING_TRACE_BEGIN;
241         SettingApplicationsUG *applicationsUG = calloc(1, sizeof(SettingApplicationsUG));
242         setting_retvm_if(!applicationsUG, -1, "Create SettingApplicationsUG obj failed");
243
244         memset(applicationsUG, 0x00, sizeof(SettingApplicationsUG));
245
246         ops->create = setting_applications_ug_on_create;
247         ops->start = setting_applications_ug_on_start;
248         ops->pause = setting_applications_ug_on_pause;
249         ops->resume = setting_applications_ug_on_resume;
250         ops->destroy = setting_applications_ug_on_destroy;
251         ops->message = setting_applications_ug_on_message;
252         ops->event = setting_applications_ug_on_event;
253         ops->key_event = setting_applications_ug_on_key_event;
254         ops->priv = applicationsUG;
255         ops->opt = UG_OPT_INDICATOR_ENABLE;
256
257         return 0;
258 }
259
260 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
261 {
262         SETTING_TRACE_BEGIN;
263         struct SettingApplicationsUG *applicationsUG;
264         setting_retm_if(!ops, "ops == NULL");
265
266         applicationsUG = ops->priv;
267         if (applicationsUG) {
268                 FREE(applicationsUG);
269         }
270 }
271
272 /* ***************************************************
273  *
274  *general func
275  *
276  ***************************************************/
277
278 #define BUS_NAME                                "org.tizen.system.deviced"
279 #define INTERFACE_NAME                          BUS_NAME
280 #define OBJECT_PATH                             "/Org/Tizen/System/DeviceD"
281 #define DEVICED_PATH_DISPLAY        OBJECT_PATH"/Applications"
282 #define DEVICED_INTERFACE_DISPLAY   INTERFACE_NAME".applications"
283 #define DBUS_REPLY_TIMEOUT                      (120 * 1000)
284
285 static int __append_param(DBusMessage *msg, const char *sig, char *param[])
286 {
287         DBusMessageIter iter;
288         /*DBusMessageIter cont_iter; */
289         char *ch;
290         char *str_type;
291         int int_type;
292         int i;
293         if (sig == NULL || param == NULL) {
294                 return 0;
295         }
296
297         dbus_message_iter_init_append(msg, &iter);
298         for (ch = (char *)sig, i = 0; *ch != '\0'; ++i, ++ch) {
299                 SETTING_TRACE_DEBUG("sig : %c", *ch);
300                 switch (*ch) {
301                         case 'i':
302                                 int_type = atoi(param[i]);
303                                 SETTING_TRACE_DEBUG("param[%2d] : %d", i, int_type);
304                                 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &int_type);
305                                 break;
306                         case 's':
307                                 str_type = param[i];
308                                 SETTING_TRACE_DEBUG("param[%2d] : %s", i, str_type);
309                                 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_type);
310                                 break;
311                         default:
312                                 break;
313                 }
314         }
315         return 0;
316 }
317
318 static DBusMessage *__invoke_dbus_method(const char *dest, const char *path,
319                                          const char *interface, const char *method,
320                                          const char *sig, char *param[])
321 {
322         DBusError err;
323         DBusConnection *conn;
324         DBusMessage *reply;
325         DBusMessage *msg;
326         int r;
327
328         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
329         if (conn == NULL) {
330                 SETTING_TRACE_DEBUG("dbus_bus_get error");
331                 return NULL;
332         }
333         msg = dbus_message_new_method_call(dest, path, interface, method);
334         if (msg == NULL) {
335                 SETTING_TRACE_DEBUG("dbus_message_new_method_call error");
336                 return NULL;
337         }
338         r = __append_param(msg, sig, param);
339         if (r < 0) {
340                 SETTING_TRACE_DEBUG("append_param error");
341                 return NULL;
342         }
343         dbus_error_init(&err);
344         reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
345         dbus_message_unref(msg);
346         if (dbus_error_is_set(&err)) {
347                 SETTING_TRACE_DEBUG("dbus_connection_send_with_reply_and_block error");
348                 SETTING_TRACE_DEBUG("error [%s:%s]", err.name, err.message);
349                 dbus_error_free(&err);
350                 return NULL;
351         }
352         return reply;
353 }
354
355 int setting_applications_set_auto_adjust_tone(int value)
356 {
357         SETTING_TRACE_BEGIN;
358         if (value < 0 || value > 1) {
359                 SETTING_TRACE_DEBUG("value has invalid range. [0 or 1]");
360                 return SETTING_RETURN_FAIL;
361         }
362
363         int ret = 0;
364         int state;
365         DBusMessage *msg;
366         DBusError err;
367         char *arr[4] = {0,};
368         char temp[2] = {0,};
369
370         e_dbus_init();
371         dbus_error_init(&err);
372
373         snprintf(temp, 2, "%1d", value);
374         temp[1] = '\0';
375         SETTING_TRACE_DEBUG("set as [%s]", temp);
376         arr[0] = temp;
377         msg = __invoke_dbus_method(BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY, "setautotone", "i", arr);
378         if (msg) {
379                 if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID)) {
380                         SETTING_TRACE_DEBUG("no message : [%s:%s]", err.name, err.message);
381                         ret = SETTING_RETURN_FAIL;
382                 } else {
383                         SETTING_TRACE_DEBUG("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, "setautotone", state);
384                 }
385                 dbus_message_unref(msg);
386         }
387         e_dbus_shutdown();
388
389         return ret;
390 }
391
392 void setting_applications_layout_ug_cb(ui_gadget_h ug, enum ug_mode mode,
393                                        void *priv)
394 {
395         /*SettingApplicationsUG *ad = (SettingApplicationsUG *) priv; */
396         Evas_Object *base;
397
398         if (!priv) {
399                 return;
400         }
401         SETTING_TRACE_BEGIN;
402
403         base = (Evas_Object *) ug_get_layout(ug);
404         if (!base) {
405                 return;
406         }
407
408         switch (mode) {
409                 case UG_MODE_FULLVIEW:
410                         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
411                                                          EVAS_HINT_EXPAND);
412                         evas_object_show(base);
413                         break;
414                 default:
415                         break;
416         }
417
418         SETTING_TRACE_END;
419 }
420