2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <app_debug.h>
20 #include <app_contents.h>
22 #include <pkgmgr-info.h>
23 #include <tv_service_proxy_channel_info.h>
24 #include <tv_service_proxy_epg.h>
25 #include <media_content.h>
26 #include <app_media.h>
27 #include <app_define.h>
28 #include <app_contents.h>
32 #include "data_recent.h"
37 #define BUF_TITLE_MAX 128
40 static struct datamgr_item *_new_datamgr_item(char *title, char *thumb,
41 char *subtitle, char *parameter, char *key, char *value,
42 char *icon, int type, time_t time)
44 struct datamgr_item *di;
46 di = calloc(1, sizeof(*di));
48 _ERR("failed to calloc datamgr item");
52 di->title = strdup(title);
53 di->focus_icon = strdup(icon);
55 di->parameter = strdup(parameter);
56 di->action = ITEM_SELECT_ACTION_LAUNCH;
59 di->icon = strdup(thumb);
61 di->subtitle = strdup(subtitle);
63 di->key = strdup(key);
64 di->value = strdup(value);
70 static void _app_list_foreach(gpointer data, gpointer user_data)
72 struct recent_data *rdata;
74 pkgmgrinfo_appinfo_h handle;
76 struct datamgr_item *di;
81 _ERR("Invalid argument");
88 if (!strcmp(rdata->id, APP_ID_BROWSER))
91 r = pkgmgrinfo_appinfo_get_usr_appinfo(rdata->id, getuid(), &handle);
92 if (r != PMINFO_R_OK) {
93 _ERR("failed to get app info");
97 r = pkgmgrinfo_appinfo_is_nodisplay(handle, &nodisplay);
98 if (r != PMINFO_R_OK || nodisplay) {
99 pkgmgrinfo_appinfo_destroy_appinfo(handle);
105 r = pkgmgrinfo_appinfo_get_label(handle, &label);
106 if (r != PMINFO_R_OK)
107 _ERR("failed to get app label");
109 r = pkgmgrinfo_appinfo_get_icon(handle, &thumb);
110 if (r != PMINFO_R_OK)
111 _ERR("failed to get app icon");
113 if (!thumb || !strcmp(thumb, ""))
114 thumb = IMAGE_RECENT_THUMB_APP;
116 di = _new_datamgr_item(label, thumb, NULL, rdata->id, NULL, NULL,
117 IMAGE_RECENT_ICON_APP, 0, rdata->time);
119 dm->list = eina_list_append(dm->list, di);
121 pkgmgrinfo_appinfo_destroy_appinfo(handle);
124 static void _get_program(int service_id, char **program)
126 TvServiceEpg epg_svc;
127 TvServiceEpgEventData epg_data;
130 r = tv_service_epg_create(&epg_svc);
131 if (r != TVS_ERROR_OK)
134 r = tv_service_epg_get_cache_current_program(epg_svc, service_id,
136 if (r != TVS_ERROR_OK) {
137 _ERR("failed to get epg data: %d", service_id);
138 tv_service_epg_destroy(epg_svc);
142 *program = (char *)epg_data.title_text;
144 tv_service_epg_destroy(epg_svc);
147 static void _channel_list_foreach(gpointer data, gpointer user_data)
149 struct recent_data *rdata;
150 struct datamgr_item *di;
152 TvServiceChannel channel;
154 char buf[BUF_TITLE_MAX];
155 char *thumb = NULL, *program = NULL;
157 if (!data || !user_data) {
158 _ERR("Invalid argument");
165 r = tv_service_get_channel(atoi(rdata->id), &channel);
166 if (r != TVS_ERROR_OK) {
167 _ERR("failed to get channel");
171 if (channel.minor > 0)
172 snprintf(buf, sizeof(buf), "%ld-%ld %s",
173 channel.major, channel.minor,
174 channel.program_name ?
175 channel.program_name : "");
177 snprintf(buf, sizeof(buf), "%ld %s", channel.major,
178 channel.program_name ?
179 channel.program_name : "");
181 _get_program(atoi(rdata->id), &program);
183 di = _new_datamgr_item(buf, thumb ? thumb : IMAGE_RECENT_THUMB_CHANNEL,
184 program, APP_ID_LIVETV, PARAM_SERVICE_ID,
185 rdata->id, IMAGE_RECENT_ICON_CHANNEL, 1, rdata->time);
187 dm->list = eina_list_append(dm->list, di);
190 static void _set_up_string(char *str)
193 *str = toupper(*str);
198 static void _gallery_list_foreach(gpointer data, gpointer user_data)
200 struct recent_data *rdata;
201 struct datamgr_item *di;
204 app_media_info *aminfo;
207 char buf[BUF_TITLE_MAX], day[BUF_TITLE_MAX], date[BUF_TITLE_MAX];
209 if (!data || !user_data) {
210 _ERR("Invalid arguement");
217 r = media_info_get_media_from_db(rdata->id, &media);
218 if (r != MEDIA_CONTENT_ERROR_NONE) {
219 _ERR("failed to get media");
223 am = app_media_create(media);
225 _ERR("failed to create media");
226 media_info_destroy(media);
230 aminfo = app_media_get_info(am);
232 _ERR("failed to get media info");
233 app_media_destroy(am);
234 media_info_destroy(media);
238 strftime(date, sizeof(date), "%d %b, %Y", aminfo->content_time);
239 strftime(day, sizeof(day), "%a", aminfo->content_time);
241 snprintf(buf, sizeof(buf), "%s, %s", day, date);
243 di = _new_datamgr_item(buf, aminfo->thumbnail_path, NULL,
244 APP_ID_MEDIAHUB, PARAM_MEDIA_ID, rdata->id,
245 IMAGE_RECENT_ICON_GALLERY, 1, rdata->time);
247 dm->list = eina_list_append(dm->list, di);
249 app_media_destroy(am);
250 media_info_destroy(media);
253 static void _get_duration(int duration, char *str, size_t length)
257 sec = duration / 1000;
259 m = (sec % 3600) / 60;
263 snprintf(str, length, "%d:%02d:%02d", h, m, s);
265 snprintf(str, length, "%d:%02d", m, s);
268 static void _movie_list_foreach(gpointer data, gpointer user_data)
270 struct recent_data *rdata;
271 struct datamgr_item *di;
274 app_media_info *aminfo;
277 char dur[BUF_TITLE_MAX];
279 if (!data || !user_data) {
280 _ERR("Invalid arguement");
287 r = media_info_get_media_from_db(rdata->id, &media);
288 if (r != MEDIA_CONTENT_ERROR_NONE) {
289 _ERR("failed to get media");
293 am = app_media_create(media);
295 _ERR("failed to create media");
296 media_info_destroy(media);
300 aminfo = app_media_get_info(am);
302 _ERR("failed to get media info");
303 app_media_destroy(am);
304 media_info_destroy(media);
308 if (aminfo->video && aminfo->video->duration)
309 _get_duration(aminfo->video->duration, dur, sizeof(dur));
311 di = _new_datamgr_item(aminfo->title, aminfo->thumbnail_path,
312 dur, APP_ID_MEDIAHUB, PARAM_MEDIA_ID,
313 rdata->id, IMAGE_RECENT_ICON_MOVIE, 1, rdata->time);
315 dm->list = eina_list_append(dm->list, di);
317 app_media_destroy(am);
318 media_info_destroy(media);
321 static void _music_list_foreach(gpointer data, gpointer user_data)
323 struct recent_data *rdata;
324 struct datamgr_item *di;
327 app_media_info *aminfo;
331 if (!data || !user_data) {
332 _ERR("Invalid arguement");
339 r = media_info_get_media_from_db(rdata->id, &media);
340 if (r != MEDIA_CONTENT_ERROR_NONE) {
341 _ERR("failed to get media");
345 am = app_media_create(media);
347 _ERR("failed to create media");
348 media_info_destroy(media);
352 aminfo = app_media_get_info(am);
354 _ERR("failed to get media info");
355 app_media_destroy(am);
356 media_info_destroy(media);
360 di = _new_datamgr_item(aminfo->title, aminfo->thumbnail_path,
361 aminfo->audio->artist, APP_ID_MEDIAHUB, PARAM_MEDIA_ID,
362 rdata->id, IMAGE_RECENT_ICON_MUSIC, 1, rdata->time);
364 dm->list = eina_list_append(dm->list, di);
366 app_media_destroy(am);
367 media_info_destroy(media);
370 static bool _load_recent(struct datamgr *dm, enum app_contents_type type,
371 int count, GFunc func)
376 r = app_contents_get_recent_list(type, count, &rlist);
377 if (r != APP_CONTENTS_ERROR_NONE) {
378 _ERR("failed to get recent list");
382 g_list_foreach(rlist, func, dm);
384 app_contents_free_recent_list(rlist);
389 static void _unload_recent(struct datamgr *dm)
391 struct datamgr_item *di;
393 EINA_LIST_FREE(dm->list, di) {
397 free(di->focus_icon);
408 static int _sort_list(const void *data1, const void *data2)
410 struct datamgr_item *di1, *di2;
412 di1 = (struct datamgr_item *)data1;
413 di2 = (struct datamgr_item *)data2;
418 if (di1->time < di2->time)
424 static bool _load_recent_notification(struct datamgr *dm)
426 struct datamgr_item *di;
430 snprintf(buf, sizeof(buf), "%d", utils_get_notification_count());
432 app_contents_get_basis_time(CONTENTS_NOTI, &time);
434 _ERR("Invalid time");
438 di = _new_datamgr_item(MESSAGE_NOTIFICATION, IMAGE_RECENT_THUMB_NOTI,
439 buf, APP_ID_INFOSQUARE, NULL, NULL,
440 IMAGE_RECENT_ICON_APP, 0, time);
442 dm->list = eina_list_append(dm->list, di);
447 static Eina_List *_get_items(struct datamgr *dm)
450 _ERR("Invalid argument");
456 if (!_load_recent(dm, CONTENTS_APP, -1, _app_list_foreach))
457 _ERR("failed to load recent app contents");
459 if (!_load_recent(dm, CONTENTS_CHANNEL, 1, _channel_list_foreach))
460 _ERR("failed to load recent channel contents");
462 if (!_load_recent(dm, CONTENTS_GALLERY, 1, _gallery_list_foreach))
463 _ERR("failed to load recent gallery contents");
465 if (!_load_recent(dm, CONTENTS_MOVIE, 1, _movie_list_foreach))
466 _ERR("failed to load recent movie contents");
468 if (!_load_recent(dm, CONTENTS_MUSIC, 1, _music_list_foreach))
469 _ERR("failed to load recent music contents");
471 if (!_load_recent_notification(dm))
472 _ERR("failed to load recent notification contents");
474 dm->list = eina_list_sort(dm->list, 0, _sort_list);
479 static void _fini(struct datamgr *dm)
482 _ERR("Invalid argument");
488 media_content_disconnect();
489 tv_service_channel_info_destroy();
492 static bool _init(struct datamgr *dm)
497 _ERR("Invalid argument");
501 r = tv_service_channel_info_create();
502 if (r != TVS_ERROR_OK) {
503 _ERR("failed to create tv service");
507 r = media_content_connect();
508 if (r != MEDIA_CONTENT_ERROR_NONE) {
509 tv_service_channel_info_destroy();
510 _ERR("failed to connect media content");
517 static bool _select(struct datamgr_item *di)
521 if (!di || !di->parameter)
525 switch (di->action) {
526 case ITEM_SELECT_ACTION_LAUNCH:
527 r = utils_launch_app(di->parameter, di->key, di->value);
530 _ERR("Invalid state");
537 static void _clear(struct datamgr *dm)
539 if (!app_contents_recent_clear_all())
540 _ERR("failed to clear all recents");
543 static struct data_class dclass = {
546 .get_items = _get_items,
551 struct data_class *datamgr_recent_get_dclass(void)