summaryrefslogtreecommitdiff
path: root/src/util/util.c
blob: 43daa06766c138f1e66c8cea6131abe175af4788 (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
415
416
417
418
419
420
421
/*
 * 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 <stdbool.h>
#include <Elementary.h>
#include <app_control.h>
#include <app_debug.h>
#include <app_contents.h>

#include "define.h"
#include "util/util.h"

#define STR_GRID_CONTENT "grid_content"

#define VIDEO_COPYRIGHT "Unknown"

/*
 * NOTE: Workaround
 * we assumed that if video content have the copyright then it's a movie.
 */
bool util_check_movie_type(const char *str)
{
	return strcmp(str, VIDEO_COPYRIGHT);
}

bool util_launch_request(const char *appid, const char *key, const char *value)
{
	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 false;
	}

	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 false;
	}

	if (key && value) {
		r = app_control_add_extra_data(app_ctrl, key, value);
		if (r != APP_CONTROL_ERROR_NONE) {
			_ERR("failed to add extra data");
			app_control_destroy(app_ctrl);
			return false;
		}
	}

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

	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 false;
	}

	app_control_destroy(app_ctrl);

	return true;
}

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

	if (!base)
		return NULL;

	btn = elm_button_add(base);
	if (!btn) {
		_ERR("failed to create button object");
		return NULL;
	}

	if (style)
		elm_object_style_set(btn, style);

	if (text)
		elm_object_text_set(btn, text);

	return btn;
}

Evas_Object *util_add_box(Evas_Object *base, Eina_Bool horizontal)
{
	Evas_Object *box;

	if (!base)
		return NULL;

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

	evas_object_size_hint_align_set(box, 0.0, EVAS_HINT_FILL);
	evas_object_size_hint_weight_set(box, 0.0, EVAS_HINT_EXPAND);
	elm_box_horizontal_set(box, horizontal);

	return box;
}

Evas_Object *util_add_gengrid(Evas_Object *base, int item_size_x,
			int item_size_y, Eina_Bool horizontal)
{
	Evas_Object *grid;

	if (!base)
		return NULL;

	grid = elm_gengrid_add(base);
	if (!grid) {
		_ERR("failed to create gengrid object");
		return NULL;
	}

	evas_object_size_hint_weight_set(grid,
			EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);

	elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_ALWAYS);
	elm_gengrid_multi_select_set(grid, EINA_FALSE);

	elm_gengrid_horizontal_set(grid, horizontal);

	elm_gengrid_align_set(grid, 0.0, 0.0);

	elm_gengrid_item_size_set(grid,
			elm_config_scale_get() * item_size_x,
			elm_config_scale_get() * item_size_y);

	elm_scroller_policy_set(grid, ELM_SCROLLER_POLICY_OFF,
			ELM_SCROLLER_POLICY_OFF);

	return grid;
}

Evas_Object *util_add_genlist(Evas_Object *base)
{
	Evas_Object *list;

	list = elm_genlist_add(base);
	if (!list) {
		_ERR("failed to adding genlist");
		return NULL;
	}

	evas_object_size_hint_weight_set(list,
			EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

	elm_genlist_homogeneous_set(list, EINA_TRUE);
	elm_genlist_select_mode_set(list, ELM_OBJECT_SELECT_MODE_ALWAYS);
	elm_genlist_multi_select_set(list, EINA_FALSE);

	return list;
}

Evas_Object *util_add_image(Evas_Object *base, const char *file)
{
	Evas_Object *image;

	if (!base)
		return NULL;

	image = elm_image_add(base);
	if (!image) {
		_ERR("failed to create image object");
		return NULL;
	}

	if (file) {
		elm_image_file_set(image, file, NULL);
		elm_image_aspect_fixed_set(image, EINA_FALSE);
	}

	return image;
}

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

	evas_object_del(obj);
}

Evas_Object *util_add_notify(Evas_Object *base, const char *notify_style,
			const char *label_style, const char *text, double time)
{
	Evas_Object *notify, *label;

	if (!base)
		return NULL;

	notify = elm_notify_add(base);
	if (!notify) {
		_ERR("failed to create notify object");
		return NULL;
	}

	if (notify_style)
		elm_object_style_set(notify, notify_style);

	evas_object_size_hint_weight_set(notify, EVAS_HINT_EXPAND,
				EVAS_HINT_EXPAND);
	elm_notify_align_set(notify, 0.0, 1.0);
	elm_notify_timeout_set(notify, time);
	evas_object_smart_callback_add(notify, SIG_TIMEOUT,
				_notify_timeout, NULL);

	label = elm_label_add(notify);
	if (!label) {
		_ERR("failed to create label object");
		evas_object_del(notify);
		return NULL;
	}

	if (label_style)
		elm_object_style_set(label, label_style);

	elm_object_content_set(notify, label);

	if (text)
		elm_object_text_set(label, text);

	evas_object_show(notify);

	return notify;
}

Evas_Object *util_add_scroller(Evas_Object *base)
{
	Evas_Object *scr;

	if (!base)
		return NULL;

	scr = elm_scroller_add(base);
	if (!scr) {
		_ERR("failed to create scroller object");
		return NULL;
	}

	elm_scroller_policy_set(scr, ELM_SCROLLER_POLICY_OFF,
			ELM_SCROLLER_POLICY_OFF);

	return scr;
}

Evas_Object *util_add_table(Evas_Object *base, int padding_x, int padding_y)
{
	Evas_Object *table;

	if (!base)
		return NULL;

	table = elm_table_add(base);
	if (!table) {
		_ERR("failed to create table object");
		return NULL;
	}

	elm_table_padding_set(table, padding_x, padding_y);

	return table;
}

void util_time_string(char *str, int size, unsigned int ms, bool full)
{
	int sec;
	int h, m, s;

	sec = ms / 1000;

	h = sec / 3600;
	m = (sec % 3600) / 60;
	s = sec % 60;

	if (full) {
		snprintf(str, size, "%02d:%02d:%02d", h, m, s);
	} else {
		if (h)
			snprintf(str, size, "%d:%02d:%02d", h, m, s);
		else
			snprintf(str, size, "%d:%02d", m, s);
	}
}

void util_up_string(char *str)
{
	while (*str) {
		*str = toupper(*str);
		str++;
	}
}

int util_get_media_index(Eina_List *list, void *data)
{
	Eina_List *l;
	int index;
	void *obj;

	index = 0;
	EINA_LIST_FOREACH(list, l, obj) {
		if (data == obj)
			return index;

		index++;
	}

	return -1;
}

int util_get_media_index_from_id(Eina_List *list, const char *id)
{
	Eina_List *l;
	app_media *am;
	app_media_info *info;
	int index;

	index = 0;
	EINA_LIST_FOREACH(list, l, am) {
		info = app_media_get_info(am);
		if (!info)
			continue;

		if (!strcmp(id, info->media_id))
			return index;

		index++;
	}

	return -1;
}

app_media *util_find_media_info(Eina_List *list, const char *id)
{
	Eina_List *l;
	app_media *am;
	app_media_info *info;

	EINA_LIST_FOREACH(list, l, am) {
		info = app_media_get_info(am);
		if (!info)
			continue;

		if (!strcmp(info->media_id, id))
			return am;
	}

	return NULL;
}

void util_add_to_recent(Eina_List *list, int index)
{
	app_media *am;
	app_media_info *mi;

	am = eina_list_nth(list, index);
	if (!am) {
		_ERR("failed to get app_media");
		return;
	}

	mi = app_media_get_info(am);
	if (!mi) {
		_ERR("failed to getting media info");
		return;
	}

	app_contents_recent_add(CONTENTS_MEDIA, mi->media_id);

	app_media_update(am);
}

void util_create_thumbnail(Evas_Object *grid, app_media *am,
			void (*completed_cb)(media_content_error_e,
			const char *, void *))
{
	Eina_List *list;
	Elm_Object_Item *it;
	int index;
	int r;

	list = evas_object_data_get(grid, STR_GRID_CONTENT);
	index = util_get_media_index(list, am);

	it = elm_gengrid_first_item_get(grid);
	while (index--)
		it = elm_gengrid_item_next_get(it);

	r = media_info_create_thumbnail(app_media_get_media_handle(am),
				completed_cb, it);
	if (r != MEDIA_CONTENT_ERROR_NONE)
		_ERR("failed to create thumbnail");
}