summaryrefslogtreecommitdiff
path: root/src/lib/efl/interfaces/efl_vpath_manager.c
blob: 8a4d752e85658cb90380fb9b45ba0fe6609e4cec (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
#include "config.h"
#include "Efl.h"

#define MY_CLASS EFL_VPATH_MANAGER_CLASS

typedef struct _Efl_Vpath_Manager_Data Efl_Vpath_Manager_Data;
typedef struct _Efl_Vpath_Manager_Entry Efl_Vpath_Manager_Entry;

struct _Efl_Vpath_Manager_Data
{
   Eina_List *list;
};

struct _Efl_Vpath_Manager_Entry
{
   Efl_Vpath *vpath;
   int priority;
};

static Efl_Vpath_Manager_Data vpath_manager =
{
   NULL
};

EOLIAN static Efl_Vpath_File *
_efl_vpath_manager_fetch(Eo *obj EINA_UNUSED, void *pd EINA_UNUSED, const char *path)
{
   Efl_Vpath_Manager_Entry *entry;
   Eina_List *l;
   Efl_Vpath_File *file;

   EINA_LIST_FOREACH(vpath_manager.list, l, entry)
     {
        file = efl_vpath_fetch(entry->vpath, path);
        if (file) return file;
     }
   file = eo_add(EFL_VPATH_FILE_CLASS, NULL);
   if (file)
     {
        efl_vpath_file_path_set(file, path);
        efl_vpath_file_result_set(file, path);
     }
   return file;
}

static int
_register_sort_cb(Efl_Vpath_Manager_Entry *e1, Efl_Vpath_Manager_Entry *e2)
{
   // sort higher numbers first in  list
   return (e2->priority - e1->priority);
}

static Eina_Bool
_cb_vpath_del(void *data, const Eo_Event *event)
{
   efl_vpath_manager_unregister(EFL_VPATH_MANAGER_CLASS, event->obj);
   eo_event_callback_del(event->obj, EO_EVENT_DEL, _cb_vpath_del, data);
   return EINA_TRUE;
}

EOLIAN static void
_efl_vpath_manager_register(Eo *obj, void *pd EINA_UNUSED, int priority, Efl_Vpath *vpath)
{
   Efl_Vpath_Manager_Entry *entry = malloc(sizeof(Efl_Vpath_Manager_Entry));
   entry->vpath = vpath;
   entry->priority = priority;
   eo_event_callback_add(vpath, EO_EVENT_DEL, _cb_vpath_del, obj);
   vpath_manager.list = eina_list_sorted_insert
     (vpath_manager.list, EINA_COMPARE_CB(_register_sort_cb), entry);
}

EOLIAN static void
_efl_vpath_manager_unregister(Eo *obj EINA_UNUSED, void *pd EINA_UNUSED, Efl_Vpath *vpath)
{
   Efl_Vpath_Manager_Entry *entry;
   Eina_List *l;

   EINA_LIST_FOREACH(vpath_manager.list, l, entry)
     {
        if (entry->vpath == vpath)
          {
             vpath_manager.list = eina_list_remove_list(vpath_manager.list, l);
             free(entry);
             return;
          }
     }
}

#include "interfaces/efl_vpath_manager.eo.c"