The source code moved from the SPIN with license changed to Flora 1.1
[apps/native/home/homescreen-efl.git] / src / tree.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.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 #include <stdlib.h>
18 #include <string.h>
19 #include <dlog.h>
20
21 #include "tree.h"
22 #include "db/db.h"
23
24 const db_item_t __tree_node_convert(Tree_node_t *node);
25
26 HAPI bool tree_node_new(Tree_node_t **node)
27 {
28         *node = (Tree_node_t *)calloc(1, sizeof(Tree_node_t));
29
30         return (*node) != NULL;
31 }
32
33 /*
34  * detatch node
35  */
36 HAPI bool tree_node_detatch(Tree_node_t *node)
37 {
38         if (!node)
39                 return false;
40
41         if (node->prev) {
42                 node->prev->next = node->next;
43                 db_update_apps(__tree_node_convert(node->prev), true);
44         } else {
45                 node->parent->first = node->next;
46                 db_update_apps(__tree_node_convert(node->parent), true);
47         }
48
49         if (node->next) {
50                 node->next->prev = node->prev;
51         } else {
52                 node->parent->last = node->prev;
53         }
54
55         if (node->parent)
56                 node->parent->count--;
57
58         node->next = NULL;
59         node->prev = NULL;
60         node->parent = NULL;
61
62         db_update_apps(__tree_node_convert(node), true);
63
64         return true;
65 }
66
67 HAPI bool tree_node_append(Tree_node_t *parent, Tree_node_t *node)
68 {
69         if (!node || !parent)
70                 return false;
71
72         if (!parent->last) {
73                 parent->first = node;
74                 node->prev = NULL;
75                 db_update_apps(__tree_node_convert(parent), true);
76         } else {
77                 parent->last->next = node;
78                 node->prev = parent->last;
79                 db_update_apps(__tree_node_convert(parent->last), true);
80         }
81
82         parent->last = node;
83         node->parent = parent;
84         node->parent->count++;
85         node->next = NULL;
86
87         db_update_apps(__tree_node_convert(node), true);
88
89         return true;
90 }
91
92 HAPI bool tree_node_append_relative(Tree_node_t *node, Tree_node_t *relative)
93 {
94         if (!relative->next)
95                 return tree_node_append(relative->parent, node);
96
97         node->parent = relative->parent;
98         node->parent->count++;
99         relative->next->prev = node;
100         node->next = relative->next;
101         relative->next = node;
102         node->prev = relative;
103
104         db_update_apps(__tree_node_convert(node), true);
105         db_update_apps(__tree_node_convert(relative), true);
106
107         return true;
108 }
109
110 HAPI bool tree_node_prepend(Tree_node_t *parent, Tree_node_t *node)
111 {
112         if (!node || !parent)
113                 return false;
114
115         if (!parent->first) {
116                 /*No elements in parent*/
117                 parent->last = node;
118                 node->next = NULL;
119         } else {
120                 parent->first->prev = node;
121                 node->next = parent->first;
122                 db_update_apps(__tree_node_convert(parent), true);
123         }
124
125         parent->first = node;
126         node->parent = parent;
127         node->parent->count++;
128         node->prev = NULL;
129
130         db_update_apps(__tree_node_convert(node), true);
131
132         return true;
133 }
134
135 HAPI bool tree_node_prepend_relative(Tree_node_t *node, Tree_node_t *relative)
136 {
137         if (!relative->prev)
138                 return tree_node_prepend(relative->parent, node);
139
140         node->parent = relative->parent;
141         node->parent->count++;
142         relative->prev->next = node;
143         node->prev = relative->prev;
144         relative->prev = node;
145         node->next = relative;
146
147         db_update_apps(__tree_node_convert(node), true);
148         db_update_apps(__tree_node_convert(node->prev), true);
149
150         return true;
151 }
152
153 HAPI bool tree_in_depth_browse(Tree_node_t *node, tree_browse_cb_t func_cb, void *data)
154 {
155         if (!node)
156                 return false;
157
158         if (func_cb) {
159                 if (!func_cb(NULL, node, data))
160                         return false;
161         }
162
163         Tree_node_t *it;
164         TREE_NODE_FOREACH(node, it) {
165                 if (!tree_in_depth_browse(it, func_cb, data))
166                         return false;
167         }
168
169         return true;
170 }
171
172 void tree_node_update(Tree_node_t *node)
173 {
174         if (!node) {
175                 LOGE("node == NULL");
176                 return;
177         }
178
179         db_update_apps(__tree_node_convert(node), true);
180 }
181
182 HAPI void tree_node_free(Tree_node_t *node, Tree_node_t **first_child, Tree_node_t **last_child, int *child_count)
183 {
184         Tree_node_t *it = NULL;
185
186         if (!node)
187                 return;
188
189         db_update_apps(__tree_node_convert(node), false);
190
191         if (first_child)
192                 *first_child = node->first;
193
194         if (last_child)
195                 *last_child = node->last;
196
197         if (child_count)
198                 *child_count = node->count;
199
200         if (node->parent) {
201                 node->parent->count--;
202
203                 if (node->parent->first == node)
204                         node->parent->first = node->next;
205
206                 if (node->parent->last == node)
207                         node->parent->last = node->prev;
208         }
209
210         if (node->prev)
211                 node->prev->next = node->next;
212
213         if (node->next)
214                 node->next->prev = node->prev;
215
216         for (it = node->first; it; it = it->next)
217                 it->parent = NULL;
218
219         app_item_free(node->data);
220         free(node);
221 }
222
223 HAPI const db_item_t __tree_node_convert(Tree_node_t *node)
224 {
225         db_item_t item = {
226                 id : node->data->unique_id,
227                 type : node->data->type,
228                 appid : node->data->appid ? (char *)node->data->appid : (char *)"",
229                 first_id : node->first ? node->first->data->unique_id : -1,
230                 next_id : node->next ? node->next->data->unique_id : -1,
231                 x : node->data->col,
232                 y : node->data->row,
233                 w : node->data->col_span,
234                 h : node->data->row_span,
235                 content_info : node->data->content_info,
236         };
237         return item;
238 }