Tizen 2.1 base
[apps/core/preloaded/quickpanel.git] / daemon / list_util.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://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 <Elementary.h>
18 #include <stdlib.h>
19
20 #include "common.h"
21 #include "list_util.h"
22
23 struct _qp_item_data {
24         qp_item_type_e type;
25         void *data;
26 };
27
28 static qp_item_count g_qp_item_count = {
29         .group = 0,
30         .ongoing = 0,
31         .noti = 0,
32         .minicontrol = 0,
33 };
34
35 qp_item_data *quickpanel_list_util_item_new(qp_item_type_e type, void *data)
36 {
37         qp_item_data *qid = NULL;
38
39         qid = malloc(sizeof(struct _qp_item_data));
40         if (!qid) {
41                 ERR("fail to alloc qid");
42                 return NULL;
43         }
44
45         qid->type = type;
46         qid->data = data;
47
48         return qid;
49 }
50
51 qp_item_type_e quickpanel_list_util_item_get_item_type(qp_item_data *qid)
52 {
53         qp_item_type_e item_type = QP_ITEM_TYPE_NONE;
54
55         if (!qid)
56                 return QP_ITEM_TYPE_NONE;
57
58         item_type = qid->type;
59
60         return item_type;
61 }
62
63 void *quickpanel_list_util_item_get_data(qp_item_data *qid)
64 {
65         void *user_data = NULL;
66
67         if (!qid)
68                 return NULL;
69
70         user_data = qid->data;
71
72         return user_data;
73 }
74
75 void quickpanel_list_util_item_set_data(qp_item_data *qid, void *data)
76 {
77         if (!qid)
78                 return ;
79
80         qid->data = data;
81 }
82
83 int quickpanel_list_util_item_compare(const void *data1, const void *data2)
84 {
85         int diff = 0;
86         qp_item_data *qid1 = NULL;
87         qp_item_data *qid2 = NULL;
88         const Elm_Object_Item *it1 = data1;
89         const Elm_Object_Item *it2 = data2;
90
91         if (!it1) {
92                 INFO("it1 is NULL");
93                 return -1;
94         }
95
96         if (!it2) {
97                 INFO("it2 is NULL");
98                 return 1;
99         }
100
101         qid1 = elm_object_item_data_get(it1);
102         qid2 = elm_object_item_data_get(it2);
103
104         if (!qid1) {
105                 INFO("qid1 is NULL");
106                 return -1;
107         }
108
109         if (!qid2) {
110                 INFO("qid2 is NULL");
111                 return 1;
112         }
113
114         /* elm_genlist sort is not working as i expected */
115         if (qid1->type == qid2->type)
116                 return 1;
117
118         diff = qid1->type - qid2->type;
119         return diff;
120 }
121
122 void quickpanel_list_util_item_del_by_type(Evas_Object *list,
123                                 const Elm_Object_Item *refer_item,
124                                 qp_item_type_e type)
125 {
126         const Elm_Object_Item *start_item = NULL;
127
128         if (!list)
129                 return;
130
131         if (refer_item)
132                 start_item = refer_item;
133         else
134                 start_item = elm_genlist_first_item_get(list);
135
136         while (start_item) {
137                 qp_item_data *qid = NULL;
138                 const Elm_Object_Item *next = NULL;
139
140                 next = elm_genlist_item_next_get(start_item);
141                 qid = elm_object_item_data_get(start_item);
142                 if (!qid) {
143                         ERR("fail to get qid, continue loop");
144                         start_item = next;
145                         continue;
146                 }
147
148                 if (qid->type > type)
149                         break;
150                 else if (qid->type == type)
151                         elm_object_item_del((Elm_Object_Item *)start_item);
152
153                 start_item = next;
154         }
155
156         return;
157 }
158
159 void quickpanel_list_util_item_update_by_type(Evas_Object *list,
160                                 Elm_Object_Item *refer_item,
161                                 qp_item_type_e type)
162 {
163         Elm_Object_Item *start_item = NULL;
164
165         if (!list)
166                 return;
167
168         if (refer_item)
169                 start_item = refer_item;
170         else
171                 start_item = elm_genlist_first_item_get(list);
172
173         while (start_item) {
174                 qp_item_data *qid = NULL;
175                 Elm_Object_Item *next = NULL;
176
177                 next = elm_genlist_item_next_get(start_item);
178                 qid = elm_object_item_data_get(start_item);
179                 if (!qid) {
180                         ERR("fail to get qid, continue loop");
181                         start_item = next;
182                         continue;
183                 }
184
185                 if (qid->type > type)
186                         break;
187                 else if (qid->type == type) {
188                         elm_genlist_item_fields_update(start_item, "*", ELM_GENLIST_ITEM_FIELD_ALL);
189                 }
190
191                 start_item = next;
192         }
193
194         return;
195 }
196
197 Elm_Object_Item *quickpanel_list_util_find_item_by_type(Evas_Object *list,
198                                 void *data,
199                                 Elm_Object_Item *refer_item,
200                                 qp_item_type_e type)
201 {
202         Elm_Object_Item *start_item = NULL;
203         Elm_Object_Item *found = NULL;
204
205         if (!list)
206                 return NULL;
207
208         if (refer_item)
209                 start_item = refer_item;
210         else
211                 start_item = elm_genlist_first_item_get(list);
212
213         while (start_item) {
214                 qp_item_data *qid = NULL;
215                 Elm_Object_Item *next = NULL;
216                 void *user_data = NULL;
217
218                 next = elm_genlist_item_next_get(start_item);
219                 qid = elm_object_item_data_get(start_item);
220                 if (!qid) {
221                         ERR("fail to get qid, continue loop");
222                         start_item = next;
223                         continue;
224                 }
225
226                 if (qid->type > type)
227                         break;
228                 else if (qid->type == type) {
229                         user_data = quickpanel_list_util_item_get_data(qid);
230                         if (user_data == data) {
231                                 found = start_item;
232                                 break;
233                         }
234                 }
235
236                 start_item = next;
237         }
238
239         return found;
240 }
241
242 static int __item_compare(const void *data1, const void *data2)
243 {
244         int diff = 0;
245         const Elm_Object_Item *it1 = data1;
246         const qp_item_data *qid1 = NULL;
247         const qp_item_data *qid2 = data2;
248
249         if (!data1) {
250                 INFO("data1 is NULL");
251                 return -1;
252         }
253
254         if (!data2) {
255                 INFO("data2 is NULL");
256                 return 1;
257         }
258
259         qid1 = elm_object_item_data_get(it1);
260
261         if (!qid1) {
262                 INFO("qid1 is NULL");
263                 return -1;
264         }
265
266         diff = qid1->type - qid2->type;
267
268         return diff;
269 }
270
271
272 Elm_Object_Item *quickpanel_list_util_sort_insert(Evas_Object *list,
273                                         const Elm_Genlist_Item_Class *itc,
274                                         const void *item_data,
275                                         Elm_Object_Item *parent,
276                                         Elm_Genlist_Item_Type type,
277                                         Evas_Smart_Cb func,
278                                         const void *func_data)
279 {
280         Elm_Object_Item *it = NULL;
281         Elm_Object_Item *first = NULL;
282
283         retif(!list, NULL, "list is NULL");
284         retif(!itc, NULL, "itc is NULL");
285         retif(!item_data, NULL, "item_data is NULL");
286
287         first = elm_genlist_first_item_get(list);
288         while (first) {
289                 if (__item_compare(first, item_data) >= 0)
290                         break;
291
292                 first = elm_genlist_item_next_get(first);
293         }
294
295         if (!first)
296                 it = elm_genlist_item_append(list, itc, item_data, parent,
297                                 type, func, func_data);
298         else
299                 it = elm_genlist_item_insert_before(list, itc, item_data,
300                                 parent, first, type, func, func_data);
301
302         if (it != NULL) {
303                 quickpanel_list_util_add_count((qp_item_data *)item_data);
304         }
305
306         return it;
307 }
308
309 qp_item_count *quickpanel_list_util_get_item_count(void)
310 {
311         return &g_qp_item_count;
312 }
313
314 void quickpanel_list_util_add_count(qp_item_data *qid)
315 {
316         retif(qid == NULL, , "qid is NULL");
317
318         switch(qid->type)
319         {
320                 case  QP_ITEM_TYPE_ONGOING_NOTI:
321                         g_qp_item_count.ongoing++;
322                         break;
323                 case  QP_ITEM_TYPE_NOTI_GROUP:
324                         g_qp_item_count.group++;
325                         break;
326                 case  QP_ITEM_TYPE_NOTI:
327                         g_qp_item_count.noti++;
328                         break;
329                 case  QP_ITEM_TYPE_MINICTRL_ONGOING:
330                 case  QP_ITEM_TYPE_MINICTRL_TOP:
331                 case  QP_ITEM_TYPE_MINICTRL_MIDDLE:
332                 case  QP_ITEM_TYPE_MINICTRL_LOW:
333                         g_qp_item_count.minicontrol++;
334                         break;
335                 case QP_ITEM_TYPE_NONE:
336                 case QP_ITEM_TYPE_SETTING:
337                 case QP_ITEM_TYPE_TOGGLE:
338                 case QP_ITEM_TYPE_BRIGHTNESS:
339                 case QP_ITEM_TYPE_MAX:
340                         break;
341         }
342
343         DBG("(type:%d)\nnum_ongoing:%d, num_group:%d, num_noti:%d, num_minicontrol:%d"
344                         , qid->type
345                         , g_qp_item_count.ongoing
346                         , g_qp_item_count.group
347                         , g_qp_item_count.noti
348                         , g_qp_item_count.minicontrol);
349 }
350
351 void quickpanel_list_util_del_count(qp_item_data *qid)
352 {
353         retif(qid == NULL, , "qid is NULL");
354
355         quickpanel_list_util_del_count_by_itemtype(qid->type);
356 }
357
358 void quickpanel_list_util_del_count_by_itemtype(qp_item_type_e type)
359 {
360         switch(type)
361         {
362                 case  QP_ITEM_TYPE_ONGOING_NOTI:
363                         g_qp_item_count.ongoing = (g_qp_item_count.ongoing <= 0) ? 0 : g_qp_item_count.ongoing - 1;
364                         break;
365                 case  QP_ITEM_TYPE_NOTI_GROUP:
366                         g_qp_item_count.group = (g_qp_item_count.group <= 0) ? 0 : g_qp_item_count.group - 1;
367                         break;
368                 case  QP_ITEM_TYPE_NOTI:
369                         g_qp_item_count.noti = (g_qp_item_count.noti <= 0) ? 0 : g_qp_item_count.noti - 1;
370                         break;
371                 case  QP_ITEM_TYPE_MINICTRL_ONGOING:
372                 case  QP_ITEM_TYPE_MINICTRL_TOP:
373                 case  QP_ITEM_TYPE_MINICTRL_MIDDLE:
374                 case  QP_ITEM_TYPE_MINICTRL_LOW:
375                         g_qp_item_count.minicontrol = (g_qp_item_count.minicontrol <= 0) ? 0 : g_qp_item_count.minicontrol - 1;
376                         break;
377                 case QP_ITEM_TYPE_NONE:
378                 case QP_ITEM_TYPE_SETTING:
379                 case QP_ITEM_TYPE_TOGGLE:
380                 case QP_ITEM_TYPE_BRIGHTNESS:
381                 case QP_ITEM_TYPE_MAX:
382                         break;
383         }
384
385         DBG("(type:%d)\nnum_ongoing:%d, num_group:%d, num_noti:%d, num_minicontrol:%d"
386                         , type
387                         , g_qp_item_count.ongoing
388                         , g_qp_item_count.group
389                         , g_qp_item_count.noti
390                         , g_qp_item_count.minicontrol);
391 }