apply FSL(Flora Software License)
[profile/ivi/menu-screen.git] / src / list.c
1  /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
4   * Licensed under the Flora License, Version 1.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
7   *
8   *     http://www.tizenopensource.org/license
9   *
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.
15   */
16
17
18
19 #include <stdbool.h>
20 #include <stdlib.h>
21 #include <Elementary.h>
22 #include <ail.h>
23
24 #include "list.h"
25 #include "util.h"
26 #include "all_apps/list.h"
27
28
29
30 menu_screen_error_e list_count(app_list *list, int *count)
31 {
32         retv_if(NULL == list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
33         retv_if(NULL == list->list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
34         retv_if(NULL == count, MENU_SCREEN_ERROR_INVALID_PARAMETER);
35
36         *count = eina_list_count(list->list);
37
38         return MENU_SCREEN_ERROR_OK;
39 }
40
41
42
43 menu_screen_error_e list_first(app_list *list)
44 {
45         retv_if(NULL == list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
46
47         list->cur_idx = 0;
48
49         return MENU_SCREEN_ERROR_OK;
50 }
51
52
53
54 menu_screen_error_e list_next(app_list *list)
55 {
56         int count;
57
58         retv_if(NULL == list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
59         retv_if(NULL == list->list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
60
61         count = eina_list_count(list->list);
62         if (list->cur_idx + 1 == count) return MENU_SCREEN_ERROR_NO_DATA;
63
64         list->cur_idx ++;
65
66         return MENU_SCREEN_ERROR_OK;
67 }
68
69
70
71 menu_screen_error_e list_is_ended(app_list *list, bool *flag)
72 {
73         int count;
74
75         retv_if(NULL == list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
76         retv_if(NULL == list->list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
77
78         count = eina_list_count(list->list);
79         *flag = (list->cur_idx == count) ? true : false;
80
81         return MENU_SCREEN_ERROR_OK;
82 }
83
84
85
86 menu_screen_error_e list_get_item(app_list *list, app_list_item **item)
87 {
88         retv_if(NULL == list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
89         retv_if(NULL == list->list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
90
91         *item = eina_list_nth(list->list, list->cur_idx);
92
93         return MENU_SCREEN_ERROR_OK;
94 }
95
96
97
98
99 menu_screen_error_e list_get_values(const char *package, app_info_t *ai)
100 {
101         ail_appinfo_h appinfo_h;
102         char *exec;
103         char *name;
104         char *icon;
105         ail_error_e ret;
106
107         retv_if(NULL == package, MENU_SCREEN_ERROR_FAIL);
108         retv_if(NULL == ai, MENU_SCREEN_ERROR_FAIL);
109         retv_if(NULL == (ai->package = strdup(package)), MENU_SCREEN_ERROR_FAIL);
110
111         ret = ail_package_get_appinfo(ai->package, &appinfo_h);
112         if (AIL_ERROR_OK == ret) {
113                 do {
114                         break_if(ail_appinfo_get_str(appinfo_h, AIL_PROP_EXEC_STR, &exec) < 0);
115                         break_if(ail_appinfo_get_str(appinfo_h, AIL_PROP_NAME_STR, &name) < 0);
116                         break_if(ail_appinfo_get_str(appinfo_h, AIL_PROP_ICON_STR, &icon) < 0);
117                         break_if(ail_appinfo_get_bool(appinfo_h, AIL_PROP_NODISPLAY_BOOL, &ai->nodisplay) < 0);
118                         break_if(ail_appinfo_get_bool(appinfo_h, AIL_PROP_X_SLP_REMOVABLE_BOOL, &ai->x_slp_removable) < 0);
119                         break_if(ail_appinfo_get_bool(appinfo_h, AIL_PROP_X_SLP_TASKMANAGE_BOOL, &ai->x_slp_taskmanage) < 0);
120
121                         break_if(NULL == exec || NULL == (ai->exec = strdup(exec)));
122                         break_if(NULL == name || NULL == (ai->name = strdup(name)));
123                         break_if(NULL == icon || NULL == (ai->icon = strdup(icon)));
124
125                         ail_package_destroy_appinfo(appinfo_h);
126
127                         return MENU_SCREEN_ERROR_OK;
128                 } while(0);
129
130                 ail_package_destroy_appinfo(appinfo_h);
131                 list_free_values(ai);
132                 return MENU_SCREEN_ERROR_FAIL;
133         } else if (AIL_ERROR_NO_DATA == ret) {
134                 return MENU_SCREEN_ERROR_OK;
135         }
136
137         return MENU_SCREEN_ERROR_FAIL;
138 }
139
140
141
142 void list_free_values(app_info_t *ai)
143 {
144         ret_if(NULL == ai);
145
146         /* Origin field */
147         if (ai->package) free(ai->package);
148         if (ai->exec) free(ai->exec);
149         if (ai->name) free(ai->name);
150         if (ai->icon) free(ai->icon);
151 }
152
153
154
155 menu_screen_error_e list_append_item(app_list *list, app_list_item *item)
156 {
157         retv_if(NULL == list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
158         retv_if(NULL == item, MENU_SCREEN_ERROR_INVALID_PARAMETER);
159
160         list->list = eina_list_append(list->list, item);
161         retv_if(NULL == list->list, MENU_SCREEN_ERROR_FAIL);
162
163         return MENU_SCREEN_ERROR_OK;
164 }
165
166
167
168 menu_screen_error_e list_remove_item(app_list *list, app_list_item *item)
169 {
170         retv_if(NULL == list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
171         retv_if(NULL == list->list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
172         retv_if(NULL == item, MENU_SCREEN_ERROR_INVALID_PARAMETER);
173
174         list->list = eina_list_remove(list->list, item);
175
176         return MENU_SCREEN_ERROR_OK;
177 }
178
179
180
181 menu_screen_error_e list_sort(app_list *list, int (*_sort_cb)(const void *d1, const void *d2))
182 {
183         retv_if(NULL == list, MENU_SCREEN_ERROR_INVALID_PARAMETER);
184
185         list->list = eina_list_sort(list->list, eina_list_count(list->list), _sort_cb);
186         retv_if(NULL == list->list, MENU_SCREEN_ERROR_FAIL);
187
188         return MENU_SCREEN_ERROR_OK;
189 }
190
191
192
193 // End of a file