summaryrefslogtreecommitdiff
path: root/src/data/app.c
blob: a0535004052fd7407d1ce23843d33abd0abfe047 (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
/*
 * 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 <glib.h>
#include <stdlib.h>
#include <pkgmgr-info.h>
#include <aul.h>
#include <app_contents.h>
#include <app_debug.h>

#include "datamgr.h"
#include "define.h"

struct app_data {
	char *id;
	char *name;
	char *icon;
};

static void _app_favorite_list_foreach(gpointer data, gpointer user_data)
{
	struct app_data *adata;
	pkgmgrinfo_appinfo_h pah;
	char *id, *name, *icon;
	Eina_List **l;

	if (!data || !user_data) {
		_ERR("Data is NULL.");
		return;
	}

	id = (char *)data;
	l = (Eina_List **)user_data;

	if (pkgmgrinfo_appinfo_get_usr_appinfo(id, getuid(), &pah) != PMINFO_R_OK) {
		_ERR("Get appinfo handle failed.");
		return;
	}

	if (pkgmgrinfo_appinfo_get_label(pah, &name) != PMINFO_R_OK) {
		_ERR("Get app name failed.");
		goto end;
	}

	if (pkgmgrinfo_appinfo_get_icon(pah, &icon) != PMINFO_R_OK) {
		_ERR("Get app icon failed.");
		goto end;
	}

	adata = calloc(1, sizeof(*adata));
	if (!adata) {
		_ERR("Calloc failed.");
		goto end;
	}

	if (id)
		adata->id = strdup(id);
	if (name)
		adata->name = strdup(name);
	if (icon)
		adata->icon = strdup(icon);

	/* FIXME: The browser's name and icon is defined as GUI. */
	if (!strcmp(id, STR_BROWSER_ID)) {
		adata->name = STR_BROWSER;
		adata->icon = BROWSER_ICON_PNG;
	}

	*l = eina_list_append(*l, adata);

end:
	pkgmgrinfo_appinfo_destroy_appinfo(pah);
}

static Eina_List *_get_favorites(enum item_type type)
{
	GList *id_list = NULL;
	Eina_List *l = NULL;

	if (type != ITEM_APPS)
		return NULL;

	if (app_contents_get_favorite_list(CONTENTS_APP, &id_list)
			!= APP_CONTENTS_ERROR_NONE) {
		_ERR("Get app favorite id list failed.");
		return NULL;
	}

	if (!id_list)
		return NULL;

	g_list_foreach(id_list, _app_favorite_list_foreach, &l);

	app_contents_free_favorite_list(id_list);

	return l;
}

static char *_get_data(void *data, enum data_type type)
{
	struct app_data *adata;

	if (!data) {
		_ERR("Invalid argument.");
		return NULL;
	}
	adata = data;

	switch (type) {
	case DATA_NAME:
		return adata->name;

	case DATA_THUMBNAIL:
		return adata->icon;

	default:
		_ERR("Invalid data type.");
		return NULL;
	}
}

static void _free_favorites(Eina_List *list)
{
	struct app_data *adata;

	if (!list)
		return;

	EINA_LIST_FREE(list, adata) {
		if (!adata)
			continue;

		free(adata->id);
		free(adata->name);
		free(adata->icon);
		free(adata);

		adata = NULL;
	}
}

static bool _action(Elm_Object_Item *it)
{
	struct app_data *adata;
	int r;

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

	adata = elm_object_item_data_get(it);
	if (!adata || !adata->id)
		return false;

	r = aul_open_app(adata->id);
	if (r == AUL_R_EINVAL) {
		_ERR("Wrong app id");
		return false;
	}

	return true;
}

static struct datamgr _dmgr = {
	.get_data = _get_data,
	.get_count = NULL,
	.get_favorites = _get_favorites,
	.free_favorites = _free_favorites,
	.action = _action,
};

struct datamgr *get_apps_datamgr(void)
{
	return &_dmgr;
}