summaryrefslogtreecommitdiff
path: root/src/lib/edje/edje_cache.c
blob: 22414df4e93b68c4cc602add0f9191b13ae8bfed (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#include "edje_private.h"

Eina_Hash *_edje_file_hash = NULL;

static int _edje_file_cache_size = 16;
static Eina_List *_edje_file_cache = NULL;

static int _edje_collection_cache_size = 16;

EAPI void
edje_cache_emp_alloc(Edje_Part_Collection_Directory_Entry *ce)
{
   /* Init Eina Mempools this is also used in edje_pick.c */
   char *buffer;
   ce->mp = calloc(1, sizeof(Edje_Part_Collection_Directory_Entry_Mp));
   if (!ce->mp) return;

#define INIT_EMP(Tp, Sz, Ce) \
  buffer = alloca(strlen(ce->entry) + strlen(#Tp) + 2); \
  sprintf(buffer, "%s/%s", ce->entry, #Tp); \
  Ce->mp->mp.Tp = eina_mempool_add("one_big", buffer, NULL, sizeof (Sz), Ce->count.Tp); \
  _emp_##Tp = Ce->mp->mp.Tp;

#define INIT_EMP_BOTH(Tp, Sz, Ce) \
  INIT_EMP(Tp, Sz, Ce) \
  Ce->mp->mp_rtl.Tp = eina_mempool_add("one_big", buffer, NULL, \
                                       sizeof(Sz), Ce->count.Tp);

  INIT_EMP_BOTH(RECTANGLE, Edje_Part_Description_Common, ce);
  INIT_EMP_BOTH(TEXT, Edje_Part_Description_Text, ce);
  INIT_EMP_BOTH(IMAGE, Edje_Part_Description_Image, ce);
  INIT_EMP_BOTH(PROXY, Edje_Part_Description_Proxy, ce);
  INIT_EMP_BOTH(SWALLOW, Edje_Part_Description_Common, ce);
  INIT_EMP_BOTH(TEXTBLOCK, Edje_Part_Description_Text, ce);
  INIT_EMP_BOTH(GROUP, Edje_Part_Description_Common, ce);
  INIT_EMP_BOTH(BOX, Edje_Part_Description_Box, ce);
  INIT_EMP_BOTH(TABLE, Edje_Part_Description_Table, ce);
  INIT_EMP_BOTH(EXTERNAL, Edje_Part_Description_External, ce);
  INIT_EMP_BOTH(SPACER, Edje_Part_Description_Common, ce);
  INIT_EMP_BOTH(SNAPSHOT, Edje_Part_Description_Snapshot, ce);
  INIT_EMP_BOTH(MESH_NODE, Edje_Part_Description_Mesh_Node, ce);
  INIT_EMP_BOTH(LIGHT, Edje_Part_Description_Light, ce);
  INIT_EMP_BOTH(CAMERA, Edje_Part_Description_Camera, ce);
  INIT_EMP_BOTH(VECTOR, Edje_Part_Description_Vector, ce);
  INIT_EMP(part, Edje_Part, ce);
}

EAPI void
edje_cache_emp_free(Edje_Part_Collection_Directory_Entry *ce)
{  /* Free Eina Mempools this is also used in edje_pick.c */
   /* Destroy all part and description. */
   ce->ref = NULL;

   if (!ce->mp) return;
   eina_mempool_del(ce->mp->mp.RECTANGLE);
   eina_mempool_del(ce->mp->mp.TEXT);
   eina_mempool_del(ce->mp->mp.IMAGE);
   eina_mempool_del(ce->mp->mp.PROXY);
   eina_mempool_del(ce->mp->mp.SWALLOW);
   eina_mempool_del(ce->mp->mp.TEXTBLOCK);
   eina_mempool_del(ce->mp->mp.GROUP);
   eina_mempool_del(ce->mp->mp.BOX);
   eina_mempool_del(ce->mp->mp.TABLE);
   eina_mempool_del(ce->mp->mp.EXTERNAL);
   eina_mempool_del(ce->mp->mp.SPACER);
   eina_mempool_del(ce->mp->mp.SNAPSHOT);
   eina_mempool_del(ce->mp->mp.MESH_NODE);
   eina_mempool_del(ce->mp->mp.LIGHT);
   eina_mempool_del(ce->mp->mp.CAMERA);
   eina_mempool_del(ce->mp->mp.VECTOR);
   eina_mempool_del(ce->mp->mp.part);
   memset(&ce->mp->mp, 0, sizeof(ce->mp->mp));

   eina_mempool_del(ce->mp->mp_rtl.RECTANGLE);
   eina_mempool_del(ce->mp->mp_rtl.TEXT);
   eina_mempool_del(ce->mp->mp_rtl.IMAGE);
   eina_mempool_del(ce->mp->mp_rtl.PROXY);
   eina_mempool_del(ce->mp->mp_rtl.SWALLOW);
   eina_mempool_del(ce->mp->mp_rtl.TEXTBLOCK);
   eina_mempool_del(ce->mp->mp_rtl.GROUP);
   eina_mempool_del(ce->mp->mp_rtl.BOX);
   eina_mempool_del(ce->mp->mp_rtl.TABLE);
   eina_mempool_del(ce->mp->mp_rtl.EXTERNAL);
   eina_mempool_del(ce->mp->mp_rtl.SPACER);
   eina_mempool_del(ce->mp->mp_rtl.SNAPSHOT);
   eina_mempool_del(ce->mp->mp_rtl.MESH_NODE);
   eina_mempool_del(ce->mp->mp_rtl.LIGHT);
   eina_mempool_del(ce->mp->mp_rtl.CAMERA);
   eina_mempool_del(ce->mp->mp_rtl.VECTOR);
   memset(&ce->mp->mp_rtl, 0, sizeof(ce->mp->mp_rtl));

   free(ce->mp);
}

void
_edje_programs_patterns_init(Edje_Part_Collection *edc)
{
   Edje_Signals_Sources_Patterns *ssp = &edc->patterns.programs;
   Edje_Program **all;
   unsigned int i, j;

   if (ssp->signals_patterns)
     return;

   if (getenv("EDJE_DUMP_PROGRAMS"))
     {
        INF("Group '%s' programs:", edc->part);
#define EDJE_DUMP_PROGRAM(Section)                    \
  for (i = 0; i < edc->programs.Section##_count; i++) \
    INF(#Section " for ('%s', '%s')", edc->programs.Section[i]->signal, edc->programs.Section[i]->source);

        EDJE_DUMP_PROGRAM(strcmp);
        EDJE_DUMP_PROGRAM(strncmp);
        EDJE_DUMP_PROGRAM(strrncmp);
        EDJE_DUMP_PROGRAM(fnmatch);
        EDJE_DUMP_PROGRAM(nocmp);
     }

   edje_match_program_hash_build(edc->programs.strcmp,
                                 edc->programs.strcmp_count,
                                 &ssp->exact_match);

   j = edc->programs.strncmp_count
     + edc->programs.strrncmp_count
     + edc->programs.fnmatch_count
     + edc->programs.nocmp_count;
   if (j == 0) return;

   all = malloc(sizeof (Edje_Program *) * j);
   if (!all) return;
   j = 0;

   /* FIXME: Build specialized data type for each case */
#define EDJE_LOAD_PROGRAMS_ADD(Array, Edc, It, Git, All)      \
  for (It = 0; It < Edc->programs.Array##_count; ++It, ++Git) \
    All[Git] = Edc->programs.Array[It];

   EDJE_LOAD_PROGRAMS_ADD(fnmatch, edc, i, j, all);
   EDJE_LOAD_PROGRAMS_ADD(strncmp, edc, i, j, all);
   EDJE_LOAD_PROGRAMS_ADD(strrncmp, edc, i, j, all);
   /* FIXME: Do a special pass for that one */
   EDJE_LOAD_PROGRAMS_ADD(nocmp, edc, i, j, all);

   ssp->u.programs.globing = all;
   ssp->u.programs.count = j;
   ssp->signals_patterns = edje_match_programs_signal_init(all, j);
   ssp->sources_patterns = edje_match_programs_source_init(all, j);
}

static Edje_Part_Collection *
_edje_file_coll_open(Edje_File *edf, const char *coll)
{
   Edje_Part_Collection *edc = NULL;
   Edje_Part_Collection_Directory_Entry *ce;
   int id = -1, size = 0;
   unsigned int n;
   Eina_List *l;
   char buf[256];
   void *data;

   ce = eina_hash_find(edf->collection, coll);
   if (!ce) return NULL;

   if (ce->ref)
     {
        ce->ref->references++;
        return ce->ref;
     }

   EINA_LIST_FOREACH(edf->collection_cache, l, edc)
     {
        if (!strcmp(edc->part, coll))
          {
             edc->references = 1;
             ce->ref = edc;

             edf->collection_cache = eina_list_remove_list(edf->collection_cache, l);
             return ce->ref;
          }
     }

   id = ce->id;
   if (id < 0) return NULL;

   edje_cache_emp_alloc(ce);
   snprintf(buf, sizeof(buf), "edje/collections/%i", id);
   edc = eet_data_read(edf->ef, _edje_edd_edje_part_collection, buf);
   if (!edc) return NULL;

   edc->references = 1;
   edc->part = ce->entry;

   /* For Edje file build with Edje 1.0 */
   if (edf->version <= 3 && edf->minor <= 1)
     {
        /* This will preserve previous rendering */
        unsigned int i;

        /* people expect signal to not be broadcasted */
        edc->broadcast_signal = EINA_FALSE;

        /* people expect text.align to be 0.0 0.0 */
        for (i = 0; i < edc->parts_count; ++i)
          {
             if (edc->parts[i]->type == EDJE_PART_TYPE_TEXTBLOCK)
               {
                  Edje_Part_Description_Text *text;
                  unsigned int j;

                  text = (Edje_Part_Description_Text *)edc->parts[i]->default_desc;
                  text->text.align.x = TO_DOUBLE(0.0);
                  text->text.align.y = TO_DOUBLE(0.0);

                  for (j = 0; j < edc->parts[i]->other.desc_count; ++j)
                    {
                       text = (Edje_Part_Description_Text *)edc->parts[i]->other.desc[j];
                       text->text.align.x = TO_DOUBLE(0.0);
                       text->text.align.y = TO_DOUBLE(0.0);
                    }
               }
          }
     }

   snprintf(buf, sizeof(buf), "edje/scripts/embryo/compiled/%i", id);
   data = eet_read(edf->ef, buf, &size);

   if (data)
     {
        edc->script = embryo_program_new(data, size);
        _edje_embryo_script_init(edc);
        free(data);
     }

   snprintf(buf, sizeof(buf), "edje/scripts/lua/%i", id);
   data = eet_read(edf->ef, buf, &size);

   if (data)
     {
        _edje_lua2_script_load(edc, data, size);
        free(data);
     }

   ce->ref = edc;

   _edje_programs_patterns_init(edc);

   n = edc->programs.fnmatch_count +
     edc->programs.strcmp_count +
     edc->programs.strncmp_count +
     edc->programs.strrncmp_count +
     edc->programs.nocmp_count;

   if (n > 0)
     {
        Edje_Program *pr;
        unsigned int i;

        edc->patterns.table_programs = malloc(sizeof(Edje_Program *) * n);
        if (edc->patterns.table_programs)
          {
             edc->patterns.table_programs_size = n;

#define EDJE_LOAD_BUILD_TABLE(Array, Edc, It, Tmp)     \
  for (It = 0; It < Edc->programs.Array##_count; ++It) \
    {                                                  \
       Tmp = Edc->programs.Array[It];                  \
       Edc->patterns.table_programs[Tmp->id] = Tmp;    \
    }

             EDJE_LOAD_BUILD_TABLE(fnmatch, edc, i, pr);
             EDJE_LOAD_BUILD_TABLE(strcmp, edc, i, pr);
             EDJE_LOAD_BUILD_TABLE(strncmp, edc, i, pr);
             EDJE_LOAD_BUILD_TABLE(strrncmp, edc, i, pr);
             EDJE_LOAD_BUILD_TABLE(nocmp, edc, i, pr);
          }
     }

   return edc;
}

// XXX: this is not pretty. some oooooold edje files do not store strings
// in their dictionary for hashes. this works around crashes loading such
// files
extern size_t  _edje_data_string_mapping_size;
extern void   *_edje_data_string_mapping;

static Edje_File *
_edje_file_open(const Eina_File *f, int *error_ret, time_t mtime, Eina_Bool coll)
{
   Edje_Color_Tree_Node *ctn;
   Edje_Color_Class *cc;
   Edje_Text_Class *tc;
   Edje_Size_Class *sc;
   Edje_File *edf;
   Eina_List *l, *ll;
   Eet_File *ef;
   char *name;
   void *mapping;
   size_t mapping_size;

   ef = eet_mmap(f);
   if (!ef)
     {
        *error_ret = EDJE_LOAD_ERROR_UNKNOWN_FORMAT;
        return NULL;
     }
   // XXX: ancient edje file workaround
   mapping = eina_file_map_all((Eina_File *)f, EINA_FILE_SEQUENTIAL);
   if (mapping)
     {
        mapping_size = eina_file_size_get(f);
        _edje_data_string_mapping = mapping;
        _edje_data_string_mapping_size = mapping_size;
     }
   edf = eet_data_read(ef, _edje_edd_edje_file, "edje/file");
   // XXX: ancient edje file workaround
   if (mapping)
     {
        eina_file_map_free((Eina_File *)f, mapping);
        _edje_data_string_mapping = NULL;
     }
   if (!edf)
     {
        *error_ret = EDJE_LOAD_ERROR_CORRUPT_FILE;
        eet_close(ef);
        return NULL;
     }

   edf->f = eina_file_dup(f);
   edf->ef = ef;
   edf->mtime = mtime;

   if (edf->version != EDJE_FILE_VERSION)
     {
        *error_ret = EDJE_LOAD_ERROR_INCOMPATIBLE_FILE;
        _edje_file_free(edf);
        return NULL;
     }
   if (!edf->collection && coll)
     {
        *error_ret = EDJE_LOAD_ERROR_CORRUPT_FILE;
        _edje_file_free(edf);
        return NULL;
     }

   if (edf->minor > EDJE_FILE_MINOR)
     {
        WRN("`%s` may use feature from a newer edje and could not show up as expected.",
            eina_file_filename_get(f));
     }
   if (edf->base_scale <= ZERO)
     {
        edf->base_scale = FROM_INT(1);
        WRN("The base_scale can not be a 0.0. It is changed the default value(1.0)");
     }

   /* Set default efl_version if there is no version information */
   if ((edf->efl_version.major == 0) && (edf->efl_version.minor == 0))
     {
        edf->efl_version.major = 1;
        edf->efl_version.minor = 18;
     }

   edf->path = eina_stringshare_add(eina_file_filename_get(f));
   edf->references = 1;

   /* This should be done at edje generation time */
   _edje_textblock_style_parse_and_fix(edf);

   edf->color_tree_hash = eina_hash_string_small_new(NULL);
   EINA_LIST_FOREACH(edf->color_tree, l, ctn)
     EINA_LIST_FOREACH(ctn->color_classes, ll, name)
       eina_hash_add(edf->color_tree_hash, name, ctn);

   edf->color_hash = eina_hash_string_small_new(NULL);
   EINA_LIST_FOREACH(edf->color_classes, l, cc)
     if (cc->name)
       eina_hash_direct_add(edf->color_hash, cc->name, cc);

   edf->text_hash = eina_hash_string_small_new(NULL);
   EINA_LIST_FOREACH(edf->text_classes, l, tc)
      if (tc->name)
         eina_hash_direct_add(edf->text_hash, tc->name, tc);

   edf->size_hash = eina_hash_string_small_new(NULL);
   EINA_LIST_FOREACH(edf->size_classes, l, sc)
     if (sc->name)
       eina_hash_direct_add(edf->size_hash, sc->name, sc);

   return edf;
}

#if 0
// FIXME: find a way to remove dangling file earlier
static void
_edje_file_dangling(Edje_File *edf)
{
   if (edf->dangling) return;
   edf->dangling = EINA_TRUE;

   eina_hash_del(_edje_file_hash, &edf->f, edf);
   if (!eina_hash_population(_edje_file_hash))
     {
        eina_hash_free(_edje_file_hash);
        _edje_file_hash = NULL;
     }
}

#endif

Edje_File *
_edje_cache_file_coll_open(const Eina_File *file, const char *coll, int *error_ret, Edje_Part_Collection **edc_ret, Edje *ed EINA_UNUSED)
{
   Edje_File *edf;
   Eina_List *l, *hist;
   Edje_Part_Collection *edc;
   Edje_Part *ep;

   if (!_edje_file_hash)
     {
        _edje_file_hash = eina_hash_pointer_new(NULL);
        goto find_list;
     }

   edf = eina_hash_find(_edje_file_hash, &file);
   if (edf)
     {
        edf->references++;
        goto open;
     }

find_list:
   EINA_LIST_FOREACH(_edje_file_cache, l, edf)
     {
        if (edf->f == file)
          {
             edf->references = 1;
             _edje_file_cache = eina_list_remove_list(_edje_file_cache, l);
             eina_hash_direct_add(_edje_file_hash, &edf->f, edf);
             goto open;
          }
     }

   edf = _edje_file_open(file, error_ret, eina_file_mtime_get(file), !!coll);
   if (!edf) return NULL;

   eina_hash_direct_add(_edje_file_hash, &edf->f, edf);
   /* return edf; */

open:
   if (!coll)
     return edf;

   edc = _edje_file_coll_open(edf, coll);
   if (!edc)
     {
        *error_ret = EDJE_LOAD_ERROR_UNKNOWN_COLLECTION;
     }
   else
     {
        if (!edc->checked)
          {
             unsigned int j;

             for (j = 0; j < edc->parts_count; ++j)
               {
                  Edje_Part *ep2;

                  ep = edc->parts[j];

                  /* Register any color classes in this parts descriptions. */
                  hist = NULL;
                  hist = eina_list_append(hist, ep);
                  ep2 = ep;
                  while (ep2->dragable.confine_id >= 0)
                    {
                       if (ep2->dragable.confine_id >= (int)edc->parts_count)
                         {
                            ERR("confine_to above limit. invalidating it.");
                            ep2->dragable.confine_id = -1;
                            break;
                         }

                       ep2 = edc->parts[ep2->dragable.confine_id];
                       if (eina_list_data_find(hist, ep2))
                         {
                            ERR("confine_to loops. invalidating loop.");
                            ep2->dragable.confine_id = -1;
                            break;
                         }
                       hist = eina_list_append(hist, ep2);
                    }
                  eina_list_free(hist);
                  hist = NULL;
                  hist = eina_list_append(hist, ep);
                  ep2 = ep;
                  while (ep2->dragable.event_id >= 0)
                    {
                       Edje_Part *prev;

                       if (ep2->dragable.event_id >= (int)edc->parts_count)
                         {
                            ERR("event_id above limit. invalidating it.");
                            ep2->dragable.event_id = -1;
                            break;
                         }
                       prev = ep2;

                       ep2 = edc->parts[ep2->dragable.event_id];
                       /* events_to may be used only with dragable */
                       if (!ep2->dragable.x && !ep2->dragable.y)
                         {
                            prev->dragable.event_id = -1;
                            break;
                         }

                       if (eina_list_data_find(hist, ep2))
                         {
                            ERR("events_to loops. invalidating loop.");
                            ep2->dragable.event_id = -1;
                            break;
                         }
                       hist = eina_list_append(hist, ep2);
                    }
                  eina_list_free(hist);
                  hist = NULL;
                  hist = eina_list_append(hist, ep);
                  ep2 = ep;
                  while (ep2->clip_to_id >= 0)
                    {
                       if (ep2->clip_to_id >= (int)edc->parts_count)
                         {
                            ERR("clip_to_id above limit. invalidating it.");
                            ep2->clip_to_id = -1;
                            break;
                         }

                       ep2 = edc->parts[ep2->clip_to_id];
                       if (eina_list_data_find(hist, ep2))
                         {
                            ERR("clip_to loops. invalidating loop.");
                            ep2->clip_to_id = -1;
                            break;
                         }
                       hist = eina_list_append(hist, ep2);
                    }
                  eina_list_free(hist);
                  hist = NULL;
               }
             edc->checked = 1;
          }
     }

   if (edc_ret) *edc_ret = edc;

   return edf;
}

void
_edje_cache_coll_clean(Edje_File *edf)
{
   while ((edf->collection_cache) &&
          (eina_list_count(edf->collection_cache) > (unsigned int)_edje_collection_cache_size))
     {
        Edje_Part_Collection_Directory_Entry *ce;
        Edje_Part_Collection *edc;

        edc = eina_list_data_get(eina_list_last(edf->collection_cache));
        edf->collection_cache = eina_list_remove_list(edf->collection_cache, eina_list_last(edf->collection_cache));

        ce = eina_hash_find(edf->collection, edc->part);
        _edje_collection_free(edf, edc, ce);
     }
}

void
_edje_cache_coll_flush(Edje_File *edf)
{
   while (edf->collection_cache)
     {
        Edje_Part_Collection_Directory_Entry *ce;
        Edje_Part_Collection *edc;
        Eina_List *last;

        last = eina_list_last(edf->collection_cache);
        edc = eina_list_data_get(last);
        edf->collection_cache = eina_list_remove_list(edf->collection_cache,
                                                      last);

        ce = eina_hash_find(edf->collection, edc->part);
        _edje_collection_free(edf, edc, ce);
     }
}

void
_edje_cache_coll_unref(Edje_File *edf, Edje_Part_Collection *edc)
{
   Edje_Part_Collection_Directory_Entry *ce;

   edc->references--;
   if (edc->references != 0) return;

   ce = eina_hash_find(edf->collection, edc->part);
   if (!ce)
     {
        ERR("Something is wrong with reference count of '%s'.", edc->part);
     }
   else if (ce->ref)
     {
        ce->ref = NULL;

        if (edf->dangling)
          {
             /* No need to keep the collection around if the file is dangling */
             _edje_collection_free(edf, edc, ce);
             _edje_cache_coll_flush(edf);
          }
        else
          {
             edf->collection_cache = eina_list_prepend(edf->collection_cache, edc);
             _edje_cache_coll_clean(edf);
          }
     }
}

static void
_edje_cache_file_clean(void)
{
   int count;

   count = eina_list_count(_edje_file_cache);
   while ((_edje_file_cache) && (count > _edje_file_cache_size))
     {
        Eina_List *last;
        Edje_File *edf;

        last = eina_list_last(_edje_file_cache);
        edf = eina_list_data_get(last);
        _edje_file_cache = eina_list_remove_list(_edje_file_cache, last);
        _edje_file_free(edf);
        count = eina_list_count(_edje_file_cache);
     }
}

EAPI void
_edje_cache_file_unref(Edje_File *edf)
{
   edf->references--;
   if (edf->references != 0) return;

   if (edf->dangling)
     {
        _edje_file_free(edf);
        return;
     }

   eina_hash_del(_edje_file_hash, &edf->f, edf);
   if (!eina_hash_population(_edje_file_hash))
     {
        eina_hash_free(_edje_file_hash);
        _edje_file_hash = NULL;
     }
   _edje_file_cache = eina_list_prepend(_edje_file_cache, edf);
   _edje_cache_file_clean();
}

void
_edje_file_cache_shutdown(void)
{
   edje_file_cache_flush();
}

/*============================================================================*
*                                 Global                                     *
*============================================================================*/

/*============================================================================*
*                                   API                                      *
*============================================================================*/

EAPI void
edje_file_cache_set(int count)
{
   if (count < 0) count = 0;
   _edje_file_cache_size = count;
   _edje_cache_file_clean();
}

EAPI int
edje_file_cache_get(void)
{
   return _edje_file_cache_size;
}

EAPI void
edje_file_cache_flush(void)
{
   int ps;

   ps = _edje_file_cache_size;
   _edje_file_cache_size = 0;
   _edje_cache_file_clean();
   _edje_file_cache_size = ps;
}

EAPI void
edje_collection_cache_set(int count)
{
   Eina_List *l;
   Edje_File *edf;

   if (count < 0) count = 0;
   _edje_collection_cache_size = count;
   EINA_LIST_FOREACH(_edje_file_cache, l, edf)
     _edje_cache_coll_clean(edf);
   /* FIXME: freach in file hash too! */
}

EAPI int
edje_collection_cache_get(void)
{
   return _edje_collection_cache_size;
}

EAPI void
edje_collection_cache_flush(void)
{
   int ps;
   Eina_List *l;
   Edje_File *edf;

   ps = _edje_collection_cache_size;
   _edje_collection_cache_size = 0;
   EINA_LIST_FOREACH(_edje_file_cache, l, edf)
     _edje_cache_coll_flush(edf);
   /* FIXME: freach in file hash too! */
   _edje_collection_cache_size = ps;
}