summaryrefslogtreecommitdiff
path: root/src/square_view.c
blob: b89888405373eae836c5038594b67b05110a9a66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * 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.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,
 * 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.
 */

#include <Elementary.h>
#include <viewmgr.h>
#include <inputmgr.h>
#include <app_debug.h>
#include <notification.h>
#include <notification_internal.h>
#include <vconf.h>

#include "define.h"

#define KEY_USER_MESSAGE "db/setting/user_message"
#define KEY_NOTI "KEY_NOTI"
#define CLOCK_UPDATE_DUR 1
#define NOTIFICATION_ITEM_PADDING_X 0
#define NOTIFICATION_ITEM_PADDING_Y 20
#define SIG_CHILD_REMOVED "child,removed"

static void _clear_key_up_cb(int id, void *data,
		Evas *e, Evas_Object *obj, Evas_Event_Key_Up *ev);
static void _clear_clicked_cb(int id, void *data, Evas_Object *obj);
static void _child_removed_cb(void *data, Evas_Object *obj, void *ei);
static void _mouse_move_cb(int id, void *data, Evas *e,
		Evas_Object *obj, Evas_Event_Mouse_Move *ev);

struct _priv {
	Evas_Object *base;
	Evas_Object *list;
	Evas_Object *clear_btn;
	Ecore_Timer *clock_timer;
};

static input_handler clear_handler = {
	.key_up = _clear_key_up_cb,
	.clicked = _clear_clicked_cb
};

static input_handler focus_handler = {
	.mouse_move = _mouse_move_cb
};

static void _print_user_message(struct _priv *priv)
{
	char *message;

	message = vconf_get_str(KEY_USER_MESSAGE);
	if (!message)
		message = strdup(STR_DEFAULT_USER_MESSAGE);

	elm_object_part_text_set(priv->base, PART_INFO_USER_MESSAGE, message);
}

static void _print_date_time(struct _priv *priv)
{
	time_t ctime;
	struct tm tm;
	char buf[128];

	ctime = time(&ctime);
	localtime_r(&ctime, &tm);

	if (strftime(buf, sizeof(buf), "%p", &tm) > 0)
		elm_object_part_text_set(priv->base, PART_INFO_TIME_AM_PM, buf);
	else
		elm_object_part_text_set(priv->base,
				PART_INFO_TIME_AM_PM, STR_DEFAULT_TIME_AM_PM);

	if (strftime(buf, sizeof(buf), "%I", &tm) > 0)
		elm_object_part_text_set(priv->base, PART_INFO_TIME_HOUR, buf);
	else
		elm_object_part_text_set(priv->base,
				PART_INFO_TIME_HOUR, STR_DEFAULT_TIME_HOUR);

	if (strftime(buf, sizeof(buf), "%M", &tm) > 0)
		elm_object_part_text_set(priv->base,
				PART_INFO_TIME_MINUTE, buf);
	else
		elm_object_part_text_set(priv->base,
				PART_INFO_TIME_MINUTE, STR_DEFAULT_TIME_MINUTE);

	if (strftime(buf, sizeof(buf), "%A, %B %d", &tm) > 0)
		elm_object_part_text_set(priv->base, PART_INFO_DATE, buf);
	else
		elm_object_part_text_set(priv->base,
				PART_INFO_DATE, STR_DEFAULT_DATE);
}

static void _print_info(struct _priv *priv)
{
	if (!priv) {
		_ERR("failed to get priv");
		return;
	}

	_print_user_message(priv);
	_print_date_time(priv);
}

static void _update_list(struct _priv *priv)
{
	Evas_Object *icon, *item;
	notification_h noti = NULL;
	notification_list_h noti_list = NULL;
	char *title = NULL, *icon_path = NULL;

	elm_box_clear(priv->list);

	notification_get_list(NOTIFICATION_TYPE_NOTI, -1, &noti_list);
	if (noti_list) {
		elm_object_signal_emit(priv->base,
				SIGNAL_NOTIFICATION, SOURCE_ELM);
		elm_object_disabled_set(priv->clear_btn, EINA_FALSE);

		while (noti_list != NULL) {
			noti = notification_list_get_data(noti_list);

			item = elm_layout_add(priv->list);
			elm_layout_file_set(item, EDJEFILE,
					GRP_NOTIFICATION_ITEM);
			elm_object_focus_allow_set(item, EINA_TRUE);
			evas_object_data_set(item, KEY_NOTI, noti);

			notification_get_image(noti,
					NOTIFICATION_IMAGE_TYPE_ICON,
					&icon_path);
			if (icon_path) {
				icon = elm_image_add(item);
				elm_image_file_set(icon, icon_path, NULL);
				elm_object_part_content_set(item,
						PART_ICON, icon);
				evas_object_show(icon);
			}

			notification_get_text(noti,
					NOTIFICATION_TEXT_TYPE_TITLE, &title);
			if (!title)
				notification_get_pkgname(noti, &title);
			elm_object_text_set(item, title);

			elm_box_pack_end(priv->list, item);

			evas_object_show(item);

			inputmgr_add_callback(item, 0, &focus_handler, NULL);

			noti_list = notification_list_remove(noti_list, noti);
		}
	} else {
		elm_object_signal_emit(priv->base,
				SIGNAL_NO_NOTIFICATION, SOURCE_ELM);
		elm_object_disabled_set(priv->clear_btn, EINA_TRUE);
	}
}

static Eina_Bool _update_clock(void *data)
{
	struct _priv *priv;

	if (!data)
		return ECORE_CALLBACK_CANCEL;

	priv = data;

	_print_date_time(priv);

	return ECORE_CALLBACK_RENEW;
}

void start_clock_timer(struct _priv *priv)
{
	if (!priv) {
		_ERR("failed to get priv");
		return;
	}

	if (priv->clock_timer)
		ecore_timer_reset(priv->clock_timer);
	else
		priv->clock_timer =
				ecore_timer_add(CLOCK_UPDATE_DUR,
				_update_clock, priv);
}

void stop_clock_timer(struct _priv *priv)
{
	if (!priv) {
		_ERR("failed to get priv");
		return;
	}

	if (priv->clock_timer) {
		ecore_timer_del(priv->clock_timer);
		priv->clock_timer = NULL;
	}
}

static void _noti_changed_cb(void *data, notification_type_e type)
{
	struct _priv *priv = data;

	if (priv)
		_update_list(priv);
}

static Evas_Object *_create(Evas_Object *win, void *data)
{
	struct _priv *priv;
	Evas_Object *scroller;

	if (!win) {
		_ERR("failed to get win object");
		return NULL;
	}

	priv = calloc(1, sizeof(*priv));
	if (!priv) {
		_ERR("failed to allocate priv");
		return NULL;
	}

	priv->base = elm_layout_add(win);
	if (!priv->base) {
		_ERR("failed to create base object");
		free(priv);
		return NULL;
	}
	elm_layout_file_set(priv->base, EDJEFILE, GRP_SQUARE_VIEW);
	evas_object_size_hint_weight_set(priv->base,
			EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	elm_win_resize_object_add(win, priv->base);

	scroller = elm_scroller_add(priv->base);
	if (!scroller)
		goto error;
	elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF,
			ELM_SCROLLER_POLICY_OFF);
	elm_object_part_content_set(priv->base,
			PART_NOTIFICATION_LIST, scroller);
	evas_object_show(scroller);

	priv->list = elm_box_add(priv->base);
	if (!priv->list) {
		evas_object_del(scroller);
		goto error;
	}
	elm_box_align_set(priv->list, 0.0, 0.0);
	elm_box_padding_set(priv->list,
			NOTIFICATION_ITEM_PADDING_X,
			NOTIFICATION_ITEM_PADDING_Y);
	evas_object_show(priv->list);
	elm_object_content_set(scroller, priv->list);
	evas_object_smart_callback_add(priv->list, SIG_CHILD_REMOVED,
			_child_removed_cb, NULL);

	priv->clear_btn = elm_layout_add(priv->base);
	elm_layout_file_set(priv->clear_btn, EDJEFILE, GRP_NOTIFICATION_CLEAR);
	elm_object_focus_allow_set(priv->clear_btn, EINA_TRUE);
	elm_object_part_content_set(priv->base,
			PART_NOTIFICATION_CLEAR, priv->clear_btn);
	evas_object_show(priv->clear_btn);

	inputmgr_add_callback(priv->clear_btn, 0, &focus_handler, NULL);
	inputmgr_add_callback(priv->clear_btn, 0, &clear_handler, NULL);

	viewmgr_set_view_data(SQUARE_VIEW, priv);

	return priv->base;

error:
	_ERR("failed to create view");

	evas_object_del(priv->base);
	free(priv);

	return NULL;
}

static void _show(void *view_data)
{
	struct _priv *priv;

	if (!view_data) {
		_ERR("failed to get view data");
		return;
	}

	priv = (struct _priv *) view_data;

	_print_info(priv);
	_update_list(priv);

	evas_object_show(priv->base);

	start_clock_timer(priv);
	notification_resister_changed_cb(_noti_changed_cb, priv);
}

static void _hide(void *view_data)
{
	struct _priv *priv;

	if (!view_data) {
		_ERR("failed to get view data");
		return;
	}

	priv = (struct _priv *) view_data;

	stop_clock_timer(priv);
	notification_unresister_changed_cb(_noti_changed_cb);

	evas_object_hide(priv->base);
}

static void _destroy(void *view_data)
{
	struct _priv *priv;

	if (!view_data) {
		_ERR("failed to get view data");
		return;
	}

	priv = (struct _priv *) view_data;

	stop_clock_timer(priv);
	inputmgr_remove_callback(priv->clear_btn, &clear_handler);
	inputmgr_remove_callback(priv->clear_btn, &focus_handler);

	evas_object_del(priv->base);

	free(priv);
}

static void _clear_key_up_cb(int id, void *data, Evas *e,
		Evas_Object *obj, Evas_Event_Key_Up *ev)
{
	if (!strcmp(ev->keyname, KEY_ENTER))
		notification_delete_all(NOTIFICATION_TYPE_NOTI);
}

static void _clear_clicked_cb(int id, void *data, Evas_Object *obj)
{
	notification_delete_all(NOTIFICATION_TYPE_NOTI);
}

static void _mouse_move_cb(int id, void *data, Evas *e,
		Evas_Object *obj, Evas_Event_Mouse_Move *ev)
{
	if (!elm_object_focus_get(obj))
		elm_object_focus_set(obj, EINA_TRUE);
}

static void _child_removed_cb(void *data, Evas_Object *obj, void *ei)
{
	notification_h noti;
	Evas_Object *item = ei;

	if (!ei)
		return;

	noti = evas_object_data_del(item, KEY_NOTI);
	if (noti)
		notification_free(noti);

	inputmgr_remove_callback(item, &focus_handler);
}

static view_class vclass = {
	.view_id = SQUARE_VIEW,
	.create = _create,
	.show = _show,
	.hide = _hide,
	.destroy = _destroy,
};

view_class *square_view_get_vclass(void)
{
	return &vclass;
}