summaryrefslogtreecommitdiff
path: root/src/data/data_recent.c
blob: 950b5d26a017105df2b2acfeafdd5f24baf012b7 (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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * 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 <app_debug.h>
#include <Eina.h>
#include <stdbool.h>
#include <app_contents.h>
#include <glib.h>
#include <pkgmgr-info.h>
#include <tv_service_proxy_channel_info.h>
#include <media_content.h>
#include <app_media.h>

#include "data_recent.h"
#include "datamgr.h"
#include "utils.h"
#include "defs.h"

#define BUF_TITLE_MAX 128

static struct datamgr_item *_new_datamgr_item(char *title, char *thumb,
		char *subtitle, char *parameter, char *key, char *value,
		char *icon, int type)
{
	struct datamgr_item *di;

	di = calloc(1, sizeof(*di));
	if (!di) {
		_ERR("failed to calloc datamgr item");
		return NULL;
	}

	di->title = strdup(title);
	di->icon = strdup(thumb);
	di->focus_icon = strdup(icon);
	di->type = type;
	di->parameter = strdup(parameter);
	di->action = ITEM_SELECT_ACTION_LAUNCH;
	if (subtitle)
		di->subtitle = strdup(subtitle);
	if (key && value) {
		di->key = strdup(key);
		di->value= strdup(value);
	}

	return di;
}

static void _app_list_foreach(gpointer data, gpointer user_data)
{
	struct recent_data *rdata;
	char *label, *thumb;
	pkgmgrinfo_appinfo_h handle;
	int r;
	struct datamgr_item *di;
	struct datamgr *dm;
	bool nodisplay;

	if (!data) {
		_ERR("Invalid argument");
		return;
	}

	rdata = data;
	dm = user_data;

	r = pkgmgrinfo_appinfo_get_appinfo(rdata->id, &handle);
	if (r != PMINFO_R_OK) {
		_ERR("failed to get app info");
		return;
	}

	r = pkgmgrinfo_appinfo_is_nodisplay(handle, &nodisplay);
	if (r != PMINFO_R_OK || nodisplay) {
		pkgmgrinfo_appinfo_destroy_appinfo(handle);
		return;
	}

	label = NULL;
	thumb = NULL;
	r = pkgmgrinfo_appinfo_get_label(handle, &label);
	if (r != PMINFO_R_OK)
		_ERR("failed to get app label");

	r = pkgmgrinfo_appinfo_get_icon(handle, &thumb);
	if (r != PMINFO_R_OK)
		_ERR("failed to get app icon");

	if (!thumb || !strcmp(thumb, ""))
		thumb = IMAGE_RECENT_THUMB_APP;

	di = _new_datamgr_item(label, thumb, NULL, rdata->id, NULL, NULL,
			IMAGE_RECENT_ICON_APP, 0);
	if (di)
		dm->list = eina_list_append(dm->list, di);

	pkgmgrinfo_appinfo_destroy_appinfo(handle);
}

static void _channel_list_foreach(gpointer data, gpointer user_data)
{
	struct recent_data *rdata;
	struct datamgr_item *di;
	struct datamgr *dm;
	TvServiceChannel channel;
	int r;
	char buf[BUF_TITLE_MAX];

	if (!data || !user_data) {
		_ERR("Invalid argument");
		return;
	}

	rdata = data;
	dm = user_data;

	r = tv_service_get_channel(atoi(rdata->id), &channel);
	if (r != TVS_ERROR_OK) {
		_ERR("failed to get channel");
		return;
	}

	if (channel.minor > 0)
		snprintf(buf, sizeof(buf), "%ld-%ld %s",
				channel.major, channel.minor,
				channel.program_name ?
						channel.program_name : "");
	else
		snprintf(buf, sizeof(buf), "%ld %s", channel.major,
				channel.program_name ?
						channel.program_name : "");

	di = _new_datamgr_item(buf, IMAGE_RECENT_THUMB_CHANNEL, NULL,
			PACKAGE_LIVETV,	KEY_CHANNEL, rdata->id,
			IMAGE_RECENT_ICON_CHANNEL, 1);
	if (di)
		dm->list = eina_list_append(dm->list, di);
}

static void _gallery_list_foreach(gpointer data, gpointer user_data)
{
	struct recent_data *rdata;
	struct datamgr_item *di;
	struct datamgr *dm;
	app_media *am;
	app_media_info *aminfo;
	media_info_h media;
	int r;

	if (!data || !user_data) {
		_ERR("Invalid arguement");
		return;
	}

	rdata = data;
	dm = user_data;

	r = media_info_get_media_from_db(rdata->id, &media);
	if (r != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to get media");
		return;
	}

	am = app_media_create(media);
	if (!am) {
		_ERR("failed to create media");
		media_info_destroy(media);
		return;
	}

	aminfo = app_media_get_info(am);
	if (!aminfo) {
		_ERR("failed to get media info");
		app_media_destroy(am);
		media_info_destroy(media);
		return;
	}

	di = _new_datamgr_item(aminfo->title, aminfo->thumbnail_path, NULL,
			PACKAGE_MEDIAHUB, KEY_MEDIA, rdata->id,
			IMAGE_RECENT_ICON_GALLERY, 1);
	if (di)
		dm->list = eina_list_append(dm->list, di);

	app_media_destroy(am);
	media_info_destroy(media);
}

static void _get_duration(int duration, char **str)
{
	char buf[BUF_TITLE_MAX];
	int h, m, s, sec;

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

	if (h)
		snprintf(buf, sizeof(buf), "%d:%02d:%02d", h, m, s);
	else
		snprintf(buf, sizeof(buf), "%d:%02d", m, s);

	*str = buf;
}

static void _movie_list_foreach(gpointer data, gpointer user_data)
{
	struct recent_data *rdata;
	struct datamgr_item *di;
	struct datamgr *dm;
	app_media *am;
	app_media_info *aminfo;
	media_info_h media;
	int r;
	char *dur = NULL;

	if (!data || !user_data) {
		_ERR("Invalid arguement");
		return;
	}

	rdata = data;
	dm = user_data;

	r = media_info_get_media_from_db(rdata->id, &media);
	if (r != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to get media");
		return;
	}

	am = app_media_create(media);
	if (!am) {
		_ERR("failed to create media");
		media_info_destroy(media);
		return;
	}

	aminfo = app_media_get_info(am);
	if (!aminfo) {
		_ERR("failed to get media info");
		app_media_destroy(am);
		media_info_destroy(media);
		return;
	}

	if (aminfo->video && aminfo->video->duration)
		_get_duration(aminfo->video->duration, &dur);

	di = _new_datamgr_item(aminfo->title, aminfo->thumbnail_path,
			dur ? dur : NULL, PACKAGE_MEDIAHUB, KEY_MEDIA,
			rdata->id, IMAGE_RECENT_ICON_MOVIE, 1);
	if (di)
		dm->list = eina_list_append(dm->list, di);

	app_media_destroy(am);
	media_info_destroy(media);
}

static void _music_list_foreach(gpointer data, gpointer user_data)
{
	struct recent_data *rdata;
	struct datamgr_item *di;
	struct datamgr *dm;
	app_media *am;
	app_media_info *aminfo;
	media_info_h media;
	int r;

	if (!data || !user_data) {
		_ERR("Invalid arguement");
		return;
	}

	rdata = data;
	dm = user_data;

	r = media_info_get_media_from_db(rdata->id, &media);
	if (r != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to get media");
		return;
	}

	am = app_media_create(media);
	if (!am) {
		_ERR("failed to create media");
		media_info_destroy(media);
		return;
	}

	aminfo = app_media_get_info(am);
	if (!aminfo) {
		_ERR("failed to get media info");
		app_media_destroy(am);
		media_info_destroy(media);
		return;
	}

	di = _new_datamgr_item(aminfo->title, aminfo->thumbnail_path,
			aminfo->audio->artist, PACKAGE_MEDIAHUB, KEY_MEDIA,
			rdata->id, IMAGE_RECENT_ICON_MUSIC, 1);
	if (di)
		dm->list = eina_list_append(dm->list, di);

	app_media_destroy(am);
	media_info_destroy(media);
}

static bool _load_recent(struct datamgr *dm, enum app_contents_type type,
		int count, GFunc func)
{
	GList *rlist = NULL;
	int r;

	r = app_contents_get_recent_list(type, count, &rlist);
	if (r != APP_CONTENTS_ERROR_NONE) {
		_ERR("failed to get recent list");
		return false;
	}

	g_list_foreach(rlist, func, dm);

	app_contents_free_recent_list(rlist);

	return true;
}

static void _unload_recent(struct datamgr *dm)
{
	struct datamgr_item *di;

	EINA_LIST_FREE(dm->list, di) {
		free(di->title);
		free(di->subtitle);
		free(di->icon);
		free(di->focus_icon);
		free(di->parameter);
		free(di->key);
		free(di->value);

		free(di);
	}

	dm->list = NULL;
}

static Eina_List *_get_items(struct datamgr *dm)
{
	if (!dm) {
		_ERR("Invalid argument");
		return NULL;
	}

	_unload_recent(dm);

	if (!_load_recent(dm, CONTENTS_APP, -1, _app_list_foreach))
		_ERR("failed to load recent app contents");

	if (!_load_recent(dm, CONTENTS_CHANNEL, 1, _channel_list_foreach))
		_ERR("failed to load recent channel contents");

	if (!_load_recent(dm, CONTENTS_GALLERY, 1, _gallery_list_foreach))
		_ERR("failed to load recent gallery contents");

	if (!_load_recent(dm, CONTENTS_MOVIE, 1, _movie_list_foreach))
		_ERR("failed to load recent movie contents");

	if (!_load_recent(dm, CONTENTS_MUSIC, 1, _music_list_foreach))
		_ERR("failed to load recent music contents");

	/* TODO: Sort for contents list */

	return dm->list;
}

static void _fini(struct datamgr *dm)
{
	if (!dm) {
		_ERR("Invalid argument");
		return;
	}

	_unload_recent(dm);

	media_content_disconnect();
	tv_service_channel_info_destroy();
}

static bool _init(struct datamgr *dm)
{
	int r;

	if (!dm) {
		_ERR("Invalid argument");
		return false;
	}

	r = tv_service_channel_info_create();
	if (r != TVS_ERROR_OK) {
		_ERR("failed to create tv service");
		return false;
	}

	r = media_content_connect();
	if (r != MEDIA_CONTENT_ERROR_NONE) {
		tv_service_channel_info_destroy();
		_ERR("failed to connect media content");
		return false;
	}

	return true;
}

static void _select(struct datamgr_item *di)
{
	if (!di || !di->parameter)
		return;

	switch (di->action) {
	case ITEM_SELECT_ACTION_LAUNCH:
		utils_launch_app(di->parameter, di->key, di->value);
		break;
	default:
		_ERR("Invalid state");
		return;
	}
}

static void _clear(struct datamgr *dm)
{
	/* It should be implemented later */
}

static struct data_class dclass = {
	.init = _init,
	.fini = _fini,
	.get_items = _get_items,
	.select = _select,
	.clear = _clear
};

struct data_class *datamgr_recent_get_dclass(void)
{
	return &dclass;
}