d99cde7d1908a08deb3e329fc078ed517d51c0b3
[apps/home/message-app.git] / composer / src / gadget / msg-ui-composer-gadget.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://floralicense.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
18 #include "msg-ui-composer-data.h"
19 #include "msg-ui-composer-main.h"
20 #include "msg-ui-composer-util.h"
21 #include "msg-ui-composer-common.h"
22 #include "msg-ui-composer-recipient.h"
23 #include <Ecore_X.h>
24 #include <system_info.h>
25
26 #ifndef UG_MODULE_API
27 #define UG_MODULE_API   __attribute__ ((visibility("default")))
28 #endif
29
30 int ref_count;
31
32 int msg_ui_composer_get_ref_count(void)
33 {
34         D_MSG("ref_count = %d", ref_count);
35         return ref_count;
36 }
37
38 void msg_ui_composer_increase_ref_count(void)
39 {
40         ++ref_count;
41         D_MSG("ref_count = %d", ref_count);
42 }
43
44 void msg_ui_composer_decrease_ref_count(void)
45 {
46         --ref_count;
47         D_MSG("ref_count = %d", ref_count);
48
49         if (ref_count < 0)
50                 ref_count = 0;
51 }
52
53 static void __base_layout_delete_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
54 {
55         D_ENTER;
56
57         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)data;
58
59         if (!cd)
60                 return;
61
62         msg_ui_composer_destroy(cd);
63
64         D_LEAVE;
65 }
66
67 static void *__msg_ui_composer_on_create(ui_gadget_h ug, enum ug_mode mode, service_h data, void *priv)
68 {
69         D_ENTER;
70         MSG_COMPOSER_VIEW_DATA_S *cd;
71         int win_w = 0;
72         int win_h = 0;
73
74         if (ug == NULL || priv == NULL) {
75                 D_EMSG("ug = %p, priv = %p", ug, priv);
76                 return NULL;
77         }
78
79         bindtextdomain("message", LOCALEDIR);
80
81         cd = (MSG_COMPOSER_VIEW_DATA_S *)priv;
82         cd->ug = ug;
83
84         cd->main_window = ug_get_window();
85
86         if (cd->main_window == NULL) {
87                 D_EMSG("main window is NULL");
88                 return NULL;
89         }
90
91         ecore_x_window_size_get(ecore_x_window_root_first_get(), &win_w, &win_h);
92         cd->window_w = win_w;
93         cd->window_h = win_h;
94         D_MSG("screen width = %d, screen_height = %d", win_w, win_h);
95
96         cd->xdisplay = ecore_x_display_get();
97         if (cd->xdisplay == NULL)
98                 D_EMSG("ecore_x_display_get() is failed");
99
100         cd->indicator_mode = elm_win_indicator_mode_get(cd->main_window);
101         D_MSG("cd->indicator_mode = %d", cd->indicator_mode);
102
103         elm_win_indicator_mode_set(cd->main_window, ELM_WIN_INDICATOR_SHOW);
104
105         bool show_indicator = true;
106         char *indicator = NULL;
107
108         service_get_extra_data(data, MSG_BUNDLE_KEY_INDICATOR_MODE, &indicator);
109         if (indicator && g_strcmp0(indicator, MSG_BUNDLE_VALUE_NO_INDICATOR) == 0)
110                 show_indicator = false;
111
112         cd->base = msg_ui_composer_layout_create(cd->main_window, show_indicator);
113         if (cd->base == NULL) {
114                 D_EMSG("cd->base is NULL");
115                 return NULL;
116         }
117
118         char *from = NULL;
119         char *thread_id = NULL;
120         char *msg_id = NULL;
121         char *composer_mode = NULL;
122         char *operation = NULL;
123
124         cd->bg = msg_ui_composer_bg_create(cd->base);
125         elm_object_part_content_set(cd->base, "elm.swallow.bg", cd->bg);
126         elm_object_style_set(cd->bg, "edit_mode");
127
128         char *cvalue = NULL;
129         int ret = SYSTEM_INFO_ERROR_NONE;
130
131         ret = system_info_get_value_string(SYSTEM_INFO_KEY_MODEL, &cvalue);
132         if (ret == SYSTEM_INFO_ERROR_NONE && cvalue != NULL) {
133                 if (g_strcmp0(cvalue, "Emulator") == 0)
134                         cd->isEmulator = true;
135         }
136
137         if (cvalue) {
138                 g_free(cvalue);
139                 cvalue = NULL;
140         }
141
142         if (service_get_operation(data, &operation) == SERVICE_ERROR_NONE && operation)
143                 cd->isAppControl = true;
144         else
145                 cd->isAppControl = false;
146
147         service_get_extra_data(data, MSG_BUNDLE_KEY_FROM, &from);
148
149         if (from && g_strcmp0(from, MSG_BUNDLE_VALUE_INTERNAL) == 0)
150                 cd->isMsgInternal = true;
151         else
152                 cd->isMsgInternal = false;
153
154         service_get_extra_data(data, MSG_BUNDLE_KEY_THREAD_ID, &thread_id);
155         service_get_extra_data(data, MSG_BUNDLE_KEY_MSG_ID, &msg_id);
156         service_get_extra_data(data, MSG_BUNDLE_KEY_MODE, &composer_mode);
157
158         if (composer_mode) {
159                 if (g_strcmp0(composer_mode, MSG_BUNDLE_VALUE_EDIT) == 0)
160                         cd->composer_mode = MSG_COMPOSER_MODE_EDIT;
161                 else if (g_strcmp0(composer_mode, MSG_BUNDLE_VALUE_FORWARD) == 0)
162                         cd->composer_mode = MSG_COMPOSER_MODE_FORWARD;
163                 else
164                         cd->composer_mode = MSG_COMPOSER_MODE_NORMAL;
165         } else {
166                 cd->composer_mode = MSG_COMPOSER_MODE_NORMAL;
167         }
168
169         if ((thread_id || msg_id) && (cd->composer_mode == MSG_COMPOSER_MODE_NORMAL)) {
170                 cd->msg_ug_mode = MSG_UG_MODE_BUBBLE_COMPOSER;
171         } else {
172                 if (cd->isMsgInternal == true) {
173                         cd->msg_ug_mode = MSG_UG_MODE_FULL_COMPOSER;
174                 } else {
175                         cd->msg_ug_mode = MSG_UG_MODE_ONLY_COMPOSER;
176                 }
177         }
178
179         D_MSG("UG MODE %d[Internal = %d], composer mode = %d", cd->msg_ug_mode, cd->isMsgInternal, cd->composer_mode);
180
181         cd->isFullview = true;
182
183         evas_object_event_callback_add(cd->base, EVAS_CALLBACK_DEL, __base_layout_delete_cb, cd);
184
185         if (msg_ui_composer_init(cd) != COMPOSER_RETURN_SUCCESS) {
186                 D_EMSG("msg_ui_composer_init error");
187                 evas_object_del(cd->base);
188
189                 return NULL;
190         }
191
192         if (msg_ui_composer_create(cd) != COMPOSER_RETURN_SUCCESS) {
193                 D_EMSG("creating main layout failed");
194                 evas_object_del(cd->base);
195
196                 return NULL;
197         }
198
199         if (cd->base)
200                 msg_ui_composer_increase_ref_count();
201
202         return cd->base;
203 }
204
205 static void __msg_ui_composer_on_start(ui_gadget_h ug, service_h data, void *priv)
206 {
207         D_ENTER;
208
209         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)priv;
210
211         msg_ui_composer_start(cd, data);
212
213         msg_common_apply_font_size("bubble", cd->content_layout);
214         msg_ui_composer_apply_font_size(cd, true);
215
216         D_LEAVE;
217 }
218
219 static void __msg_ui_composer_on_pause(ui_gadget_h ug, service_h data, void *priv)
220 {
221         D_ENTER;
222         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)priv;
223         D_MSG_RETM_IF(cd == NULL, "Composer data is NULL");
224         D_MSG_RETM_IF(cd->state == COMPOSER_STATE_TYPE_DESTROY, "Composer state is already Destroy");
225
226         cd->state = COMPOSER_STATE_TYPE_PAUSE;
227         /* discoonect volume key handler */
228         msg_ui_composer_disconnect_handler(cd);
229
230         if (cd->bubble_data) {
231                 cd->bubble_data->ug_state = BUBBLE_UG_PAUSE;
232         }
233
234         D_LEAVE;
235 }
236
237 static void __msg_ui_composer_on_resume(ui_gadget_h ug, service_h data, void *priv)
238 {
239         D_ENTER;
240         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)priv;
241         D_MSG_RETM_IF(cd == NULL, "Composer data is NULL");
242         D_MSG_RETM_IF(cd->state == COMPOSER_STATE_TYPE_DESTROY, "Composer state is already Destroy");
243
244         if (!cd->loaded_ug)
245                 msg_common_apply_font_size("bubble", cd->content_layout);
246
247         msg_ui_composer_apply_font_size(cd, true);
248
249         /* coonect volume key handler */
250         msg_ui_composer_connect_handler(cd);
251
252         cd->state = COMPOSER_STATE_TYPE_RUNNING;
253         cd->loaded_aul = FALSE;
254
255         if (cd->bubble_data) {
256                 cd->bubble_data->ug_state = BUBBLE_UG_START;
257
258                 msg_ui_bubble_list_update_read_status(cd->bubble_data);
259         }
260
261         if (cd->recipient)
262                 msg_ui_composer_recipient_update(cd->recipient);
263
264         D_LEAVE;
265 }
266
267 static void __msg_ui_composer_on_destroy(ui_gadget_h ug, service_h data, void *priv)
268 {
269         D_ENTER;
270
271         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)priv;
272         D_MSG_RETM_IF(cd == NULL, "Composer data is NULL");
273         D_MSG_RETM_IF(cd->state == COMPOSER_STATE_TYPE_DESTROY, "Composer state is already Destroy");
274
275         cd->state = COMPOSER_STATE_TYPE_DESTROY;
276         cd->isclosed = true;
277
278         if (cd->bubble_data)
279                 cd->bubble_data->ug_state = BUBBLE_UG_DESTROY;
280
281         msg_ui_bubble_deinit_bubble_data(cd->bubble_data);
282
283         if (cd->base) {
284                 evas_object_del(cd->base);
285                 cd->base = NULL;
286         }
287
288         D_LEAVE;
289 }
290
291 static void __msg_ui_composer_on_message(ui_gadget_h ug, service_h msg, service_h data, void *priv)
292 {
293         D_ENTER;
294
295         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)priv;
296         if (msg)
297                 msg_ui_composer_reset_request(cd, msg);
298
299         D_LEAVE;
300 }
301
302
303 static void __msg_ui_composer_on_event(ui_gadget_h ug, enum ug_event event, service_h data, void *priv)
304 {
305         D_ENTER;
306
307         switch (event) {
308         case UG_EVENT_LOW_MEMORY:
309                 break;
310         case UG_EVENT_LOW_BATTERY:
311                 break;
312         case UG_EVENT_LANG_CHANGE:
313                 msg_ui_composer_change_language(priv);
314                 break;
315         case UG_EVENT_ROTATE_PORTRAIT:
316                 msg_ui_composer_rotate(priv, COMPOSER_ROTATE_PORTRAIT);
317                 break;
318         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
319                 msg_ui_composer_rotate(priv, COMPOSER_ROTATE_PORTRAIT_UPSIDEDOWN);
320                 break;
321         case UG_EVENT_ROTATE_LANDSCAPE:
322                 msg_ui_composer_rotate(priv, COMPOSER_ROTATE_LANDSCAPE);
323                 break;
324         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
325                 msg_ui_composer_rotate(priv, COMPOSER_ROTATE_LANDSCAPE_UPSIDEDOWN);
326                 break;
327         default:
328                 break;
329         }
330 }
331
332 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
333 {
334         if (ops == NULL) {
335                 D_EMSG("[ERROR] OPS is null");
336                 return COMPOSER_RETURN_FAIL;
337         }
338
339         MSG_COMPOSER_VIEW_DATA_S *cd = NULL;
340         cd = (MSG_COMPOSER_VIEW_DATA_S *)calloc(1, sizeof(MSG_COMPOSER_VIEW_DATA_S));
341
342         if (!cd) {
343                 D_EMSG("[ERROR] cd calloc failed");
344                 return COMPOSER_RETURN_FAIL;
345         }
346
347         ops->create = __msg_ui_composer_on_create;
348         ops->start = __msg_ui_composer_on_start;
349         ops->pause = __msg_ui_composer_on_pause;
350         ops->resume = __msg_ui_composer_on_resume;
351         ops->destroy = __msg_ui_composer_on_destroy;
352         ops->message = __msg_ui_composer_on_message;
353         ops->event = __msg_ui_composer_on_event;
354         ops->key_event = NULL;
355         ops->priv = (void *)cd;
356         ops->opt = UG_OPT_INDICATOR_MANUAL;
357
358         return MSG_UI_RET_SUCCESS;
359 }
360
361 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
362 {
363         if (ops == NULL) {
364                 D_EMSG( "[ERROR] OPS is null");
365                 return;
366         }
367
368         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)ops->priv;
369
370         if (cd) {
371                 free(cd);
372                 ops->priv = NULL;
373         }
374
375         D_LEAVE;
376 }