beba273e2ca65806552ff6a570040d12b7956a41
[framework/uifw/cbhm.git] / src / item_manager.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 #include "item_manager.h"
19
20 static void item_free(CNP_ITEM *item)
21 {
22         CALLED();
23         if (!item)
24         {
25                 DMSG("WRONG PARAMETER in %s\n", __func__);
26                 return;
27         }
28         // remove gengrid
29         if (item->ad)
30         {
31                 if (item->ad->draw_item_del)
32                         item->ad->draw_item_del(item->ad, item);
33                 if (item->ad->storage_item_del)
34                         item->ad->storage_item_del(item->ad, item);
35         }
36         if (item->data)
37                 FREE(item->data);
38
39         if (item->ad->clip_selected_item == item)
40                 item->ad->clip_selected_item = NULL;
41         FREE(item);
42 }
43
44 CNP_ITEM *item_add_by_CNP_ITEM(AppData *ad, CNP_ITEM *item)
45 {
46         if (!ad || !item)
47         {
48                 DMSG("WRONG PARAMETER in %s, ad: 0x%x, item: 0x%x\n", __func__, ad, item);
49                 return NULL;
50         }
51         item->ad = ad;
52
53         ad->item_list = eina_list_prepend(ad->item_list, item);
54         if (ad && ad->draw_item_add)
55                 ad->draw_item_add(ad, item);
56         if (ad && ad->storage_item_add)
57                 ad->storage_item_add(ad, item);
58
59         while (ITEM_CNT_MAX < eina_list_count(ad->item_list))
60         {
61                 CNP_ITEM *ditem = eina_list_nth(ad->item_list, ITEM_CNT_MAX);
62
63                 ad->item_list = eina_list_remove(ad->item_list, ditem);
64                 item_free(ditem);
65         }
66
67         slot_property_set(ad, -1);
68         slot_item_count_set(ad);
69
70         return item;
71 }
72
73 CNP_ITEM *item_add_by_data(AppData *ad, Ecore_X_Atom type, void *data, int len)
74 {
75         if (!ad || !data)
76         {
77                 DMSG("WRONG PARAMETER in %s\n", __func__);
78                 return NULL;
79         }
80         CNP_ITEM *item;
81         item = CALLOC(1, sizeof(CNP_ITEM));
82         if (!item)
83                 return NULL;
84         item->type_index = atom_type_index_get(ad, type);
85         item->data = data;
86         item->len = len;
87
88         item = item_add_by_CNP_ITEM(ad, item);
89         return item;
90 }
91
92 CNP_ITEM *item_get_by_index(AppData *ad, int index)
93 {
94         if (!ad || eina_list_count(ad->item_list) <= index || 0 > index)
95         {
96                 DMSG("WRONG PARAMETER in %s\n", __func__);
97                 return NULL;
98         }
99         CNP_ITEM *item;
100         item = eina_list_nth(ad->item_list, index);
101         return item;
102 }
103
104 CNP_ITEM *item_get_by_data(AppData *ad, void *data)
105 {
106         if (!ad || !data)
107         {
108                 DMSG("WRONG PARAMETER in %s\n", __func__);
109                 return NULL;
110         }
111         CNP_ITEM *item;
112         Eina_List *l;
113         EINA_LIST_FOREACH(ad->item_list, l, item)
114         {
115                 if (item && item->data == data)
116                 {
117                         return item;
118                 }
119         }
120         return NULL;
121 }
122
123 CNP_ITEM *item_get_last(AppData *ad)
124 {
125         if (!ad)
126         {
127                 DMSG("WRONG PARAMETER in %s\n", __func__);
128                 return NULL;
129         }
130         return eina_list_data_get(ad->item_list);
131 }
132
133 void item_delete_by_CNP_ITEM(AppData *ad, CNP_ITEM *item)
134 {
135         CALLED();
136         if (!ad || !item)
137         {
138                 DMSG("WRONG PARAMETER in %s\n", __func__);
139                 return;
140         }
141         DMSG("item: 0x%x, item->gitem: 0x%x\n", item, item->gitem);
142         ad->item_list = eina_list_remove(ad->item_list, item);
143         item_free(item);
144         slot_property_set(ad, -1);
145         slot_item_count_set(ad);
146 }
147
148 void item_delete_by_data(AppData *ad, void *data)
149 {
150         CALLED();
151         if (!ad || !data)
152         {
153                 DMSG("WRONG PARAMETER in %s\n", __func__);
154                 return;
155         }
156         CNP_ITEM *item;
157         item = item_get_by_data(ad, data);
158         item_delete_by_CNP_ITEM(ad, item);
159 }
160
161 void item_delete_by_index(AppData *ad, int index)
162 {
163         CALLED();
164         if (!ad || eina_list_count(ad->item_list) <= index || 0 > index)
165         {
166                 DMSG("WRONG PARAMETER in %s\n", __func__);
167                 return;
168         }
169         CNP_ITEM *item;
170         item = item_get_by_index(ad, index);
171         item_delete_by_CNP_ITEM(ad, item);
172 }
173
174 void item_clear_all(AppData *ad)
175 {
176         CALLED();
177         while(ad->item_list)
178         {
179                 CNP_ITEM *item = eina_list_data_get(ad->item_list);
180                 ad->item_list = eina_list_remove(ad->item_list, item);
181                 if (item)
182                         item_free(item);
183         }
184 }
185
186 int item_count_get(AppData *ad)
187 {
188         return eina_list_count(ad->item_list);
189 }