summaryrefslogtreecommitdiff
path: root/src/data/data_recent.c
blob: d9950257ceec0125fd44515a92c4ee7843f2557a (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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
 * 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 <tv_service_proxy_epg.h>
#include <media_content.h>
#include <app_media.h>
#include <app_define.h>
#include <app_contents.h>
#include <web/web_history.h>
#include <time.h>
#include <ctype.h>
#include <bundle.h>

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

#define BUF_TITLE_MAX 128
#define MAX_BUF 64

static struct datamgr_item *_new_datamgr_item(char *title, char *subtitle,
		char *parameter, char *key, char *value, char *uri, char *icon,
		int type, time_t time)
{
	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(icon);
	di->type = type;
	di->parameter = strdup(parameter);
	di->action = ITEM_SELECT_ACTION_LAUNCH;
	di->time = time;

	if (subtitle)
		di->subtitle = strdup(subtitle);

	if ((key && value) || uri)
		di->b = utils_add_bundle(key, value, uri);

	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 thumbnail_data *thumbnail;
	struct datamgr *dm;
	bool nodisplay;

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

	rdata = data;
	dm = user_data;

	if (!strcmp(rdata->id, APP_ID_BROWSER))
		return;

	r = pkgmgrinfo_appinfo_get_usr_appinfo(rdata->id, getuid(), &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, NULL, rdata->id, NULL, NULL, NULL,
			IMAGE_RECENT_ICON_APP, 0, rdata->time);
	if (di) {
		thumbnail = calloc(1, sizeof(*thumbnail));
		if (thumbnail) {
			thumbnail->file = strdup(thumb);
			thumbnail->type = THUMBNAIL_FILE;
			di->thumbnail = thumbnail;
		}
		dm->list = eina_list_append(dm->list, di);
	}

	pkgmgrinfo_appinfo_destroy_appinfo(handle);
}

static void _get_program(int service_id, char **program)
{
	TvServiceEpg epg_svc;
	TvServiceEpgEventData epg_data;
	int r;

	r = tv_service_epg_create(&epg_svc);
	if (r != TVS_ERROR_OK)
		return;

	r = tv_service_epg_get_cache_current_program(epg_svc, service_id,
			&epg_data);
	if (r != TVS_ERROR_OK) {
		_ERR("failed to get epg data: %d", service_id);
		tv_service_epg_destroy(epg_svc);
		return;
	}

	*program = (char *)epg_data.title_text;

	tv_service_epg_destroy(epg_svc);
}

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

	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 : "");

	_get_program(atoi(rdata->id), &program);

	di = _new_datamgr_item(buf, program, APP_ID_LIVETV, PARAM_SERVICE_ID,
			rdata->id, NULL, IMAGE_RECENT_ICON_CHANNEL, 1, rdata->time);
	if (di) {
		thumbnail = calloc(1, sizeof(*thumbnail));
		if (thumbnail) {
			thumbnail->file = strdup(IMAGE_RECENT_THUMB_CHANNEL);
			thumbnail->type = THUMBNAIL_FILE;
			di->thumbnail = thumbnail;
		}
		dm->list = eina_list_append(dm->list, di);
	}
}

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

static void _gallery_list_foreach(gpointer data, gpointer user_data)
{
	struct recent_data *rdata;
	struct datamgr_item *di;
	struct thumbnail_data *thumbnail;
	struct datamgr *dm;
	app_media *am;
	app_media_info *aminfo;
	media_info_h media;
	int r;
	char buf[BUF_TITLE_MAX], day[BUF_TITLE_MAX], date[BUF_TITLE_MAX];

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

	strftime(date, sizeof(date), "%d %b, %Y", aminfo->content_time);
	strftime(day, sizeof(day), "%a", aminfo->content_time);
	_set_up_string(day);
	snprintf(buf, sizeof(buf), "%s, %s", day, date);

	di = _new_datamgr_item(buf, NULL, APP_ID_MEDIAHUB, PARAM_MEDIA_ID,
			rdata->id, NULL, IMAGE_RECENT_ICON_GALLERY, 1, rdata->time);
	if (di) {
		thumbnail = calloc(1, sizeof(*thumbnail));
		if (thumbnail) {
			thumbnail->file = strdup(aminfo->thumbnail_path);
			thumbnail->type = THUMBNAIL_FILE;
			di->thumbnail = thumbnail;
		}
		dm->list = eina_list_append(dm->list, di);
	}

	app_media_destroy(am);
	media_info_destroy(media);
}

static void _get_duration(int duration, char *str, size_t length)
{
	int h, m, s, sec;

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

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

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

	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, sizeof(dur));

	di = _new_datamgr_item(aminfo->title, dur, APP_ID_MEDIAHUB,
			PARAM_MEDIA_ID, rdata->id, NULL, IMAGE_RECENT_ICON_MOVIE, 1,
			rdata->time);
	if (di) {
		thumbnail = calloc(1, sizeof(*thumbnail));
		if (thumbnail) {
			thumbnail->file = strdup(aminfo->thumbnail_path);
			thumbnail->type = THUMBNAIL_FILE;
			di->thumbnail = thumbnail;
		}
		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 thumbnail_data *thumbnail;
	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->audio->artist,
			APP_ID_MEDIAHUB, PARAM_MEDIA_ID, rdata->id, NULL,
			IMAGE_RECENT_ICON_MUSIC, 1, rdata->time);
	if (di) {
		thumbnail = calloc(1, sizeof(*thumbnail));
		if (thumbnail) {
			thumbnail->file = strdup(aminfo->thumbnail_path);
			thumbnail->type = THUMBNAIL_FILE;
			di->thumbnail = thumbnail;
		}
		dm->list = eina_list_append(dm->list, di);
	}

	app_media_destroy(am);
	media_info_destroy(media);
}

static void _web_list_foreach(gpointer data, gpointer user_data)
{
	struct recent_data *rdata;
	struct datamgr *dm;
	int r, w, h, size, time, id;
	char *title, *url;
	unsigned char *value;
	struct thumbnail_data *thumbnail;
	struct datamgr_item *di;

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

	rdata = data;
	dm = user_data;

	r = bp_history_adaptor_initialize();
	if (r < 0) {
		_ERR("Initialize history adaptor failed.");
		return;
	}

	id = atoi(rdata->id);

	r = bp_history_adaptor_get_title(id, &title);
	if (r < 0) {
		_ERR("failed to get title of history");
		goto err;
	}

	r = bp_history_adaptor_get_url(id, &url);
	if (r < 0) {
		_ERR("failed to get url of history");
		goto err;
	}

	r = bp_history_adaptor_get_date_visited(id, &time);
	if (r < 0) {
		_ERR("failed to get icon of history");
		goto err;
	}

	r = bp_history_adaptor_get_snapshot(id, &w, &h, &value, &size);
	if (r < 0) {
		_ERR("failed to get snapshot of history");
		goto err;
	}

	di = _new_datamgr_item(title, url, APP_ID_BROWSER, NULL, NULL, url,
			IMAGE_RECENT_ICON_WEB, 1, time);
	if (!di)
		goto err;

	thumbnail = calloc(1, sizeof(*thumbnail));
	if (thumbnail) {
		thumbnail->type = THUMBNAIL_DATA;
		thumbnail->data = calloc(1, size);
		if (thumbnail->data) {
			memcpy(thumbnail->data, value, size);
			thumbnail->format = strdup(FORMAT_PNG);
			thumbnail->size = size;
			di->thumbnail = thumbnail;
		} else
			free(thumbnail);
	}
	dm->list = eina_list_append(dm->list, di);

err:
	bp_history_adaptor_deinitialize();
}

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_thumbnail(struct thumbnail_data *thumbnail)
{
	if (!thumbnail)
		return;

	free(thumbnail->file);
	free(thumbnail->data);
	free(thumbnail->format);

	free(thumbnail);
}

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);

		_unload_thumbnail(di->thumbnail);

		if (di->b)
			bundle_free(di->b);

		free(di);
	}

	dm->list = NULL;
}

static int _sort_list(const void *data1, const void *data2)
{
	struct datamgr_item *di1, *di2;

	di1 = (struct datamgr_item *)data1;
	di2 = (struct datamgr_item *)data2;

	if (!di1 || !di2)
		return -1;

	if (di1->time < di2->time)
		return 1;

	return -1;
}

static bool _load_recent_notification(struct datamgr *dm)
{
	struct datamgr_item *di;
	struct thumbnail_data *thumbnail;
	int time, count;
	char buf[MAX_BUF];

	count = utils_get_notification_count();

	if (count > 0)
		snprintf(buf, sizeof(buf), "%d Unread Noti.", count);
	else
		buf[0] = '\0';

	app_contents_get_basis_time(CONTENTS_NOTI, &time);
	if (time < 0) {
		_ERR("Invalid time");
		return false;
	}

	di = _new_datamgr_item(MESSAGE_NOTIFICATION, buf, APP_ID_INFOSQUARE,
			NULL, NULL, NULL, IMAGE_RECENT_ICON_APP, 0, time);
	if (di) {
		thumbnail = calloc(1, sizeof(*thumbnail));
		if (thumbnail) {
			thumbnail->file = strdup(IMAGE_RECENT_THUMB_NOTI);
			thumbnail->type = THUMBNAIL_FILE;
			di->thumbnail = thumbnail;
		}
		dm->list = eina_list_append(dm->list, di);
	}

	return true;
}

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");

	if (!_load_recent_notification(dm))
		_ERR("failed to load recent notification contents");

	if (!_load_recent(dm, CONTENTS_WEB, 1, _web_list_foreach))
		_ERR("failed to load recent web contents");

	dm->list = eina_list_sort(dm->list, 0, _sort_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 int _select(struct datamgr_item *di)
{
	int r;

	if (!di || !di->parameter)
		return ITEM_SELECT_ERROR_INVALID_PARAMETER;

	r = ITEM_SELECT_ERROR_INVALID_OPERATION;
	switch (di->action) {
	case ITEM_SELECT_ACTION_LAUNCH:
		if (utils_launch_app(di->parameter, di->b))
			r = ITEM_SELECT_ERROR_NONE;
		break;
	default:
		_ERR("Invalid state");
		break;
	}

	return r;
}

static void _clear(struct datamgr *dm)
{
	int r;

	r = app_contents_recent_clear_all();
	if (r != APP_CONTENTS_ERROR_NONE)
		_ERR("failed to clear all recents");
}

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