summaryrefslogtreecommitdiff
path: root/src/util.c
blob: b1a0711d7fa76faa401a4a544c0af4abcd690800 (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
 *
 * 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 <app.h>
#include <Elementary.h>
#include <app_debug.h>
#include <app_define.h>
#include <viewmgr.h>

#include "define.h"
#include "view.h"

#define BUF_MAX 128
#define TOAST_TIMEOUT 5.0
#define STYLE_TOAST "toast"

Evas_Object *util_add_layout(Evas_Object *parent, const char *group)
{
	Evas_Object *ly;

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

	ly = elm_layout_add(parent);
	if (!ly) {
		_ERR("failed to create layout");
		return NULL;
	}
	elm_layout_file_set(ly, EDJEFILE, group);

	evas_object_show(ly);

	return ly;
}

Evas_Object *util_add_box(Evas_Object *parent, bool horizon)
{
	Evas_Object *box;

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

	box = elm_box_add(parent);
	if (!box) {
		_ERR("failed to create box");
		return NULL;
	}

	evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND,
			EVAS_HINT_EXPAND);
	elm_object_content_set(parent, box);
	if (horizon)
		elm_box_horizontal_set(box, EINA_TRUE);
	else
		elm_box_horizontal_set(box, EINA_FALSE);

	evas_object_show(box);

	return box;
}

Evas_Object *util_add_scroller(Evas_Object *parent, const char *part)
{
	Evas_Object *scroll;

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

	scroll = elm_scroller_add(parent);
	if (!scroll) {
		_ERR("failed to create scroll");
		return NULL;
	}

	evas_object_size_hint_weight_set(scroll, EVAS_HINT_EXPAND,
			EVAS_HINT_EXPAND);
	elm_scroller_policy_set(scroll, ELM_SCROLLER_POLICY_OFF,
			ELM_SCROLLER_POLICY_OFF);

	elm_object_part_content_set(parent, part, scroll);

	evas_object_show(scroll);

	return scroll;
}

Evas_Object *util_add_icon(Evas_Object *parent, const char *file,
		const char *part)
{
	Evas_Object *ic;

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

	ic = elm_icon_add(parent);
	if (!ic) {
		_ERR("failed to create icon");
		return NULL;
	}

	if (file)
		elm_image_file_set(ic, file, NULL);

	if (part)
		elm_object_part_content_set(parent, part, ic);

	elm_image_resizable_set(ic, EINA_FALSE, EINA_TRUE);

	evas_object_show(ic);

	return ic;
}

Evas_Object *util_add_button(Evas_Object *parent, const char *part,
		const char *text, const char *style)
{
	Evas_Object *btn;

	if (!parent) {
		_ERR("Invalid argument.");
		return NULL;
	}

	btn = elm_button_add(parent);
	if (!btn) {
		_ERR("elm_button_add failed.");
		return NULL;
	}

	if (part)
		elm_object_part_content_set(parent, part, btn);
	if (text)
		elm_object_text_set(btn, text);
	if (style)
		elm_object_style_set(btn, style);

	evas_object_show(btn);

	return btn;
}

static void _notify_timeout_cb(void *data, Evas_Object *obj, void *ei)
{
	if (!obj)
		return;

	evas_object_del(obj);
}

Evas_Object *util_add_toast(Evas_Object *parent, char *text)
{
	Evas_Object *toast, *content;

	if (!parent) {
		_ERR("Invalid argument.");
		return NULL;
	}

	toast = elm_notify_add(parent);
	if (!toast) {
		_ERR("elm_popup_add failed");
		return NULL;
	}

	elm_object_style_set(toast, STYLE_TOAST);
	elm_notify_align_set(toast, 0.0, 1.0);
	elm_notify_timeout_set(toast, TOAST_TIMEOUT);
	evas_object_smart_callback_add(toast, SIGNAL_TIMEOUT,
			_notify_timeout_cb, NULL);

	content = elm_label_add(toast);
	if (!content) {
		_ERR("elm_label_add failed");
		evas_object_del(toast);
		return NULL;
	}

	elm_object_text_set(content, text);
	elm_object_style_set(content, STYLE_TOAST);
	elm_object_content_set(toast, content);
	evas_object_show(content);

	evas_object_show(toast);

	return toast;
}

static int _get_program_time(char *buf, int buf_len, time_t start_time,
		time_t end_time)
{
	struct tm tm;
	int r;

	if (start_time >= end_time) {
		_ERR("invalid time");
		return -1;
	}

	localtime_r(&start_time, &tm);
	r = strftime(buf, buf_len, "%I:%M %P -", &tm);
	if (r <= 0)
		return -1;

	localtime_r(&end_time, &tm);
	r = strftime(buf + r, buf_len - r, " %I:%M %P", &tm);
	if (r <= 0)
		return -1;

	return 0;
}

static void _set_icon_box(Evas_Object *obj,
		const struct tv_channel_info *channel_info)
{
	elm_object_signal_emit(obj, SIGNAL_RESET, SOURCE_PROGRAM);

	if (channel_info->locked)
		elm_object_signal_emit(obj, SIGNAL_LOCKED, SOURCE_PROGRAM);
	if (channel_info->favorite)
		elm_object_signal_emit(obj, SIGNAL_FAVORITE, SOURCE_PROGRAM);
}

static void _load_channel_text(Evas_Object *obj,
		const struct tv_channel_info *channel_info)
{
	char buf[BUF_MAX];

	if (channel_info->channel_minor > 0 &&
			channel_info->channel_minor < MINOR_MAX)
		snprintf(buf, sizeof(buf), "%lu-%lu %s",
				channel_info->channel_major,
				channel_info->channel_minor,
				channel_info->channel_name);
	else
		snprintf(buf, sizeof(buf), "%lu %s",
				channel_info->channel_major,
				channel_info->channel_name);

	elm_object_part_text_set(obj,
			PART_CHANNELINFO_CHANNEL, buf);

	elm_object_part_text_set(obj,
			PART_CHANNELINFO_TITLE,
			STR_NOTITLE);
	elm_object_part_text_set(obj,
			PART_CHANNELINFO_TIME,
			STR_NOTIME);
}

static void _load_program_info(Evas_Object *obj,
		const struct tv_program_info *prog)
{
	char buf[BUF_MAX];
	int r, service_id;

	service_id = (int) evas_object_data_get(obj, KEY_SVCID);
	if (service_id != prog->service_id)
		return;

	if (prog->start_time && prog->end_time) {
		r = _get_program_time(buf, sizeof(buf),
				prog->start_time,
				prog->end_time);
		if (!r)
			elm_object_part_text_set(obj,
					PART_CHANNELINFO_TIME, buf);
	}

	if (strlen(prog->prog_title) > 0)
		elm_object_part_text_set(obj,
				PART_CHANNELINFO_TITLE, prog->prog_title);
}

static void _tv_program_cb(Eina_List *prog_list, void *data)
{
	int op;
	Evas_Object *obj;
	struct tv_program_info *prog = NULL;

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

	obj = data;

	prog = (struct tv_program_info *) eina_list_nth(prog_list, 0);
	if (prog)
		_load_program_info(obj, prog);
	else
		_ERR("failed to get tv_program_info");

	if (viewmgr_get_view_state(VIEW_CHANNELINFO) == VIEW_STATE_VISIBLE) {
		op = START_HIDE_TIMER;
		viewmgr_update_view(VIEW_CHANNELINFO,
				UPDATE_TYPE_TIMER, &op);
	}
}

void util_draw_channel_info(Evas_Object *obj,
		const struct tv_channel_info *channel_info)
{
	struct tv_program_request *prog_req;
	int r, op;
	int current_service;

	if (!obj || !channel_info) {
		_ERR("Invalid argument");

		return;
	}

	r = tv_get_current_service_id(&current_service);
	if (r < 0)
		current_service = -1;

	if (viewmgr_get_view_state(VIEW_CHANNELINFO) == VIEW_STATE_VISIBLE) {
		op = STOP_HIDE_TIMER;
		viewmgr_update_view(VIEW_CHANNELINFO,
				UPDATE_TYPE_TIMER, &op);
	}

	_load_channel_text(obj, channel_info);
	_set_icon_box(obj, channel_info);
	evas_object_data_set(obj, KEY_SVCID, (void *)channel_info->service_id);

	prog_req = calloc(1, sizeof(*prog_req));
	if (!prog_req)
		goto err;
	prog_req->tv_program_cb = _tv_program_cb;
	prog_req->user_data = obj;

	r = tv_epg_get_cache_program(channel_info->service_id,
			prog_req);
	if (r < 0)
		free(prog_req);

	if (channel_info->service_id == current_service) {
		prog_req = calloc(1, sizeof(*prog_req));
		if (!prog_req)
			goto err;

		prog_req->tv_program_cb = _tv_program_cb;
		prog_req->user_data = obj;
		r = tv_epg_get_program(channel_info->service_id, prog_req);
		if (r < 0)
			free(prog_req);
	}

err:
	if (r < 0 && viewmgr_get_view_state(VIEW_CHANNELINFO) ==
			VIEW_STATE_VISIBLE) {
		op = START_HIDE_TIMER;
		viewmgr_update_view(VIEW_CHANNELINFO,
				UPDATE_TYPE_TIMER, &op);
	}
}

void util_launch_home(void)
{
	app_control_h app_ctrl;
	int r;

	r = app_control_create(&app_ctrl);
	if (r != APP_CONTROL_ERROR_NONE) {
		_ERR("failed to create app control handle");
		return;
	}

	r = app_control_set_operation(app_ctrl, APP_CONTROL_OPERATION_DEFAULT);
	if (r != APP_CONTROL_ERROR_NONE) {
		_ERR("failed to set app control operation");
		app_control_destroy(app_ctrl);
		return;
	}

	r = app_control_set_app_id(app_ctrl, APP_ID_HOME);
	if (r != APP_CONTROL_ERROR_NONE) {
		_ERR("failed to set app control app id");
		app_control_destroy(app_ctrl);
		return;
	}

	r = app_control_send_launch_request(app_ctrl, NULL, NULL);
	if (r != APP_CONTROL_ERROR_NONE) {
		_ERR("failed to send app control launch request");
		app_control_destroy(app_ctrl);
		return;
	}

	app_control_destroy(app_ctrl);
}