aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/e_apps_cache.c
blob: a1380f4204b8bae5e6f97c39fb6e477750364aa3 (plain) (blame)
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
/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */
#include "e.h"

#define NEWD(str, typ) \
     { eddc.name = str; eddc.size = sizeof(typ); }

#define FREED(eed) \
   if (eed) \
       { \
	  eet_data_descriptor_free((eed)); \
	  (eed) = NULL; \
       }
#define NEWI(str, it, type) \
   EET_DATA_DESCRIPTOR_ADD_BASIC(_e_app_cache_edd, E_App_Cache, str, it, type)
#define NEWL(str, it, type) \
   EET_DATA_DESCRIPTOR_ADD_LIST(_e_app_cache_edd, E_App_Cache, str, it, type)

static void _e_eapp_cache_fill(E_App_Cache *ac, E_App *a);

static Eet_Data_Descriptor *_e_app_cache_edd = NULL;

EAPI int
e_app_cache_init(void)
{
   Eet_Data_Descriptor_Class eddc;
   
   eddc.version = EET_DATA_DESCRIPTOR_CLASS_VERSION;
   eddc.func.mem_alloc = NULL;
   eddc.func.mem_free = NULL;
   eddc.func.str_alloc = evas_stringshare_add;
   eddc.func.str_free = evas_stringshare_del;
   eddc.func.list_next = evas_list_next;
   eddc.func.list_append = evas_list_append;
   eddc.func.list_data = evas_list_data;
   eddc.func.list_free = evas_list_free;
   eddc.func.hash_foreach = evas_hash_foreach;
   eddc.func.hash_add = evas_hash_add;
   eddc.func.hash_free = evas_hash_free;
   
   NEWD("E_App_Cache", E_App_Cache);
   _e_app_cache_edd = eet_data_descriptor2_new(&eddc);
   
   NEWI("nm", name, EET_T_STRING);
   NEWI("gn", generic, EET_T_STRING);
   NEWI("cm", comment, EET_T_STRING);
   NEWI("ex", exe, EET_T_STRING);
   NEWI("fl", file, EET_T_STRING);
   NEWI("fm", file_mod_time, EET_T_ULONG_LONG);
   NEWI("wn", win_name, EET_T_STRING);
   NEWI("wc", win_class, EET_T_STRING);
   NEWI("wt", win_title, EET_T_STRING);
   NEWI("wr", win_role, EET_T_STRING);
   NEWI("ic", icon_class, EET_T_STRING);
   NEWL("ap", subapps, _e_app_cache_edd);
   NEWI("sn", startup_notify, EET_T_UCHAR);
   NEWI("wx", wait_exit, EET_T_UCHAR);
   NEWI("il", is_link, EET_T_UCHAR);
   NEWI("id", is_dir, EET_T_UCHAR);
   return 1;
}

EAPI int
e_app_cache_shutdown(void)
{
   FREED(_e_app_cache_edd);
   return 1;
}

EAPI E_App_Cache *
e_app_cache_load(char *path)
{
   Eet_File *ef;
   char buf[PATH_MAX];
   E_App_Cache *ac;
   
   if (!path) return NULL;
   snprintf(buf, sizeof(buf), "%s/.eap.cache.cfg", path);
   ef = eet_open(buf, EET_FILE_MODE_READ);
   if (!ef) return NULL;
   ac = eet_data_read(ef, _e_app_cache_edd, "cache");
   eet_close(ef);
   if (ac)
     {
	Evas_List *l;
	
	for (l = ac->subapps; l; l = l->next)
	  {
	     E_App_Cache *ac2;
	     
	     ac2 = l->data;
	     ac->subapps_hash = evas_hash_add(ac->subapps_hash, ac2->file, ac2);
	  }
     }
   return ac;
}

EAPI E_App_Cache *
e_app_cache_generate(E_App *a)
{
   E_App_Cache *ac;
   Evas_List *l;
   char buf[PATH_MAX];
   
   if (!a) return NULL;
   ac = calloc(1, sizeof(E_App_Cache));
   if (!ac) return NULL;
   _e_eapp_cache_fill(ac, a);
   for (l = a->subapps; l; l = l->next)
     {
	E_App *a2;
	E_App_Cache *ac2;
	
	a2 = l->data;
	ac2 = calloc(1, sizeof(E_App_Cache));
	if (ac2)
	  {
	     _e_eapp_cache_fill(ac2, a2);
	     ac2->is_dir = ecore_file_is_dir(a2->path);
	     snprintf(buf, sizeof(buf), "%s/%s", a->path, ecore_file_get_file(a2->path));
	     if (a2->orig) ac2->is_link = 1;
	     if ((!ac2->is_link) && (!ac2->is_dir))
	       ac2->file_mod_time = ecore_file_mod_time(buf);
	     ac->subapps = evas_list_append(ac->subapps, ac2);
	     ac->subapps_hash = evas_hash_direct_add(ac->subapps_hash, ac2->file, ac2);
	  }
     }
   return ac;
}

EAPI void
e_app_cache_free(E_App_Cache *ac)
{
   if (!ac) return;
   if (ac->name) evas_stringshare_del(ac->name);
   if (ac->generic) evas_stringshare_del(ac->generic);
   if (ac->comment) evas_stringshare_del(ac->comment);
   if (ac->exe) evas_stringshare_del(ac->exe);
   if (ac->file) evas_stringshare_del(ac->file);
   if (ac->win_name) evas_stringshare_del(ac->win_name);
   if (ac->win_class) evas_stringshare_del(ac->win_class);
   if (ac->win_title) evas_stringshare_del(ac->win_title);
   if (ac->win_role) evas_stringshare_del(ac->win_role);
   if (ac->icon_class) evas_stringshare_del(ac->icon_class);
   while (ac->subapps)
     {
	E_App_Cache *ac2;
	
	ac2 = ac->subapps->data;
	ac->subapps = evas_list_remove_list(ac->subapps, ac->subapps);
	e_app_cache_free(ac2);
     }
   evas_hash_free(ac->subapps_hash);
   free(ac);
}

EAPI int
e_app_cache_save(E_App_Cache *ac, char *path)
{
   Eet_File *ef;
   char buf[4096];
   int ret;
   
   if ((!ac) || (!path)) return 0;
   snprintf(buf, sizeof(buf), "%s/.eap.cache.cfg", path);
   ef = eet_open(buf, EET_FILE_MODE_WRITE);
   if (!ef) return 0;
   ret = eet_data_write(ef, _e_app_cache_edd, "cache", ac, 1);
   eet_close(ef);
   return ret;
}

static void
_e_eapp_cache_fill(E_App_Cache *ac, E_App *a)
{
#define IF_DUP(x) if (a->x) ac->x = evas_stringshare_add(a->x)
   IF_DUP(name);
   IF_DUP(generic);
   IF_DUP(comment);
   IF_DUP(exe);
   ac->file = evas_stringshare_add(ecore_file_get_file(a->path));
   IF_DUP(win_name);
   IF_DUP(win_class);
   IF_DUP(win_title);
   IF_DUP(win_role);
   IF_DUP(icon_class);
   ac->startup_notify = a->startup_notify;
   ac->wait_exit = a->wait_exit;
}