1 | /* |
---|
2 | * GNOME panel menu module. |
---|
3 | * (C) 1997, 1998, 1999, 2000 The Free Software Foundation |
---|
4 | * Copyright 2000 Eazel, Inc. |
---|
5 | * |
---|
6 | * Authors: Miguel de Icaza |
---|
7 | * Federico Mena |
---|
8 | */ |
---|
9 | |
---|
10 | #include <config.h> |
---|
11 | #include <ctype.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <sys/types.h> |
---|
14 | #include <sys/stat.h> |
---|
15 | #include <sys/wait.h> |
---|
16 | #include <dirent.h> |
---|
17 | #include <unistd.h> |
---|
18 | #include <string.h> |
---|
19 | #include <limits.h> |
---|
20 | #include <errno.h> |
---|
21 | #include <gnome.h> |
---|
22 | |
---|
23 | #include "panel-include.h" |
---|
24 | |
---|
25 | /*#define PANEL_DEBUG 1*/ |
---|
26 | |
---|
27 | /* the minimum number of seconds between stats of files */ |
---|
28 | #define STAT_EVERY 3 |
---|
29 | |
---|
30 | static GSList *dir_list = NULL; |
---|
31 | |
---|
32 | static GMemChunk *file_chunk = NULL; |
---|
33 | static GMemChunk *dir_chunk = NULL; |
---|
34 | |
---|
35 | extern char *merge_main_dir; |
---|
36 | extern int merge_main_dir_len; |
---|
37 | extern char *merge_merge_dir; |
---|
38 | |
---|
39 | extern GlobalConfig global_config; |
---|
40 | |
---|
41 | void |
---|
42 | init_fr_chunks (void) |
---|
43 | { |
---|
44 | file_chunk = g_mem_chunk_create (FileRec, 64, G_ALLOC_AND_FREE); |
---|
45 | dir_chunk = g_mem_chunk_create (DirRec, 16, G_ALLOC_AND_FREE); |
---|
46 | } |
---|
47 | |
---|
48 | static GSList * |
---|
49 | prepend_mfile (GSList *list, const char *name, gboolean merged, |
---|
50 | gboolean verified) |
---|
51 | { |
---|
52 | MFile *mfile = g_new0 (MFile, 1); |
---|
53 | |
---|
54 | mfile->name = g_strdup (name); |
---|
55 | mfile->merged = merged; |
---|
56 | mfile->verified = verified; |
---|
57 | |
---|
58 | return g_slist_prepend (list, mfile); |
---|
59 | } |
---|
60 | |
---|
61 | /* merged is 1/0/-1 (-1 is don't care) */ |
---|
62 | static MFile * |
---|
63 | find_mfile (GSList *list, const char *string, int merged) |
---|
64 | { |
---|
65 | GSList *li; |
---|
66 | |
---|
67 | g_return_val_if_fail (string != NULL, NULL); |
---|
68 | |
---|
69 | for (li = list; li != NULL; li = li->next) { |
---|
70 | MFile *mfile = li->data; |
---|
71 | |
---|
72 | if (mfile->name != NULL && |
---|
73 | strcmp (mfile->name, string) == 0 && |
---|
74 | (merged < 0 || |
---|
75 | (merged ? 1 : 0) == (mfile->merged ? 1 : 0))) |
---|
76 | return mfile; |
---|
77 | } |
---|
78 | |
---|
79 | return NULL; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /*reads in the order file and makes a list*/ |
---|
84 | static GSList * |
---|
85 | get_presorted_from(GSList *list, const char *dir, gboolean merged) |
---|
86 | { |
---|
87 | char buf[PATH_MAX+1]; |
---|
88 | char *fname = g_concat_dir_and_file(dir, ".order"); |
---|
89 | FILE *fp = fopen(fname, "r"); |
---|
90 | |
---|
91 | if(!fp) { |
---|
92 | g_free(fname); |
---|
93 | return list; |
---|
94 | } |
---|
95 | while(fgets(buf, PATH_MAX+1, fp)!=NULL) { |
---|
96 | MFile *mfile; |
---|
97 | char *p = strchr(buf, '\n'); |
---|
98 | if(p) |
---|
99 | *p = '\0'; |
---|
100 | |
---|
101 | if(is_ext (buf, ".desktop") || |
---|
102 | is_ext (buf, ".kdelnk")) { |
---|
103 | mfile = find_mfile (list, buf, merged ? 1 : 0); |
---|
104 | } else { |
---|
105 | mfile = find_mfile (list, buf, -1); |
---|
106 | } |
---|
107 | |
---|
108 | if (mfile == NULL) |
---|
109 | list = prepend_mfile (list, buf, merged, FALSE); |
---|
110 | } |
---|
111 | fclose(fp); |
---|
112 | g_free(fname); |
---|
113 | return list; |
---|
114 | } |
---|
115 | |
---|
116 | gboolean |
---|
117 | fr_is_subdir (const char *dir, const char *superdir, int superdir_len) |
---|
118 | { |
---|
119 | if (superdir == NULL || superdir_len == 0) |
---|
120 | return FALSE; |
---|
121 | |
---|
122 | if (strncmp (dir, superdir, superdir_len-1) == 0 && |
---|
123 | (dir[superdir_len-1] == '/' || |
---|
124 | dir[superdir_len-1] == '\0')) { |
---|
125 | return TRUE; |
---|
126 | } else { |
---|
127 | return FALSE; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | char * |
---|
132 | fr_get_mergedir (const char *dir) |
---|
133 | { |
---|
134 | char *mergedir; |
---|
135 | |
---|
136 | /* If we never merge, just return NULL */ |
---|
137 | if ( ! global_config.merge_menus) |
---|
138 | return NULL; |
---|
139 | |
---|
140 | if(merge_merge_dir != NULL && |
---|
141 | fr_is_subdir(dir, merge_main_dir, merge_main_dir_len)) { |
---|
142 | if (dir[merge_main_dir_len-1] == '/') |
---|
143 | mergedir = |
---|
144 | g_strconcat(merge_merge_dir, |
---|
145 | &dir[merge_main_dir_len], NULL); |
---|
146 | else |
---|
147 | mergedir = |
---|
148 | g_strconcat(merge_merge_dir, |
---|
149 | &dir[merge_main_dir_len-1], NULL); |
---|
150 | } else { |
---|
151 | mergedir = NULL; |
---|
152 | } |
---|
153 | |
---|
154 | return mergedir; |
---|
155 | } |
---|
156 | |
---|
157 | static GSList * |
---|
158 | read_directory (GSList *list, const char *menudir, gboolean merged) |
---|
159 | { |
---|
160 | DIR *dir; |
---|
161 | struct dirent *dent; |
---|
162 | |
---|
163 | dir = opendir (menudir); |
---|
164 | if (dir != NULL) { |
---|
165 | while((dent = readdir (dir)) != NULL) { |
---|
166 | MFile *mfile; |
---|
167 | |
---|
168 | if (dent->d_name[0] == '.') |
---|
169 | continue; |
---|
170 | |
---|
171 | if(is_ext (dent->d_name, ".desktop") || |
---|
172 | is_ext (dent->d_name, ".kdelnk")) { |
---|
173 | mfile = find_mfile (list, dent->d_name, |
---|
174 | merged ? 1 : 0); |
---|
175 | } else { |
---|
176 | mfile = find_mfile (list, dent->d_name, -1); |
---|
177 | |
---|
178 | /* if this is a bogus unmerged dir, |
---|
179 | * then append it again for the merged */ |
---|
180 | if (mfile != NULL && |
---|
181 | merged && |
---|
182 | ! mfile->merged && |
---|
183 | ! mfile->verified) |
---|
184 | mfile = NULL; |
---|
185 | } |
---|
186 | |
---|
187 | if (mfile == NULL) |
---|
188 | list = prepend_mfile (list, dent->d_name, |
---|
189 | merged, TRUE); |
---|
190 | /* if this is the same merge foo and we've |
---|
191 | * already foudn it in the presorted, just verify it */ |
---|
192 | else if ((mfile->merged ? 1 : 0) == (merged ? 1 : 0)) |
---|
193 | mfile->verified = TRUE; |
---|
194 | } |
---|
195 | |
---|
196 | closedir(dir); |
---|
197 | } |
---|
198 | |
---|
199 | return list; |
---|
200 | } |
---|
201 | |
---|
202 | void |
---|
203 | free_mfile (MFile *mfile) |
---|
204 | { |
---|
205 | if (mfile != NULL) { |
---|
206 | g_free (mfile->name); |
---|
207 | mfile->name = NULL; |
---|
208 | |
---|
209 | g_free (mfile); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | void |
---|
214 | free_mfile_list (GSList *list) |
---|
215 | { |
---|
216 | GSList *li; |
---|
217 | for (li = list; li != NULL; li = li->next) { |
---|
218 | free_mfile (li->data); |
---|
219 | li->data = NULL; |
---|
220 | } |
---|
221 | g_slist_free (list); |
---|
222 | } |
---|
223 | |
---|
224 | GSList * |
---|
225 | get_mfiles_from_menudir (const char *menudir) |
---|
226 | { |
---|
227 | GSList *list = NULL; |
---|
228 | char *mergedir; |
---|
229 | |
---|
230 | mergedir = fr_get_mergedir (menudir); |
---|
231 | |
---|
232 | list = get_presorted_from (list, menudir, FALSE /*merged*/); |
---|
233 | list = read_directory (list, menudir, FALSE /*merged*/); |
---|
234 | |
---|
235 | if (mergedir != NULL) { |
---|
236 | list = get_presorted_from (list, mergedir, TRUE /*merged*/); |
---|
237 | list = read_directory (list, mergedir, TRUE /*merged*/); |
---|
238 | |
---|
239 | g_free (mergedir); |
---|
240 | } |
---|
241 | |
---|
242 | return g_slist_reverse (list); |
---|
243 | } |
---|
244 | |
---|
245 | char * |
---|
246 | get_applet_goad_id_from_dentry(GnomeDesktopEntry *ii) |
---|
247 | { |
---|
248 | int i; |
---|
249 | int constantlen = strlen ("--activate-goad-server"); |
---|
250 | |
---|
251 | g_return_val_if_fail (ii != NULL, NULL); |
---|
252 | |
---|
253 | if (ii->exec == NULL || |
---|
254 | ii->type == NULL) |
---|
255 | return NULL; |
---|
256 | |
---|
257 | if (strcmp (ii->type, "PanelApplet") == 0) { |
---|
258 | return g_strjoinv (" ", ii->exec); |
---|
259 | } else { |
---|
260 | if (ii->exec[0] == NULL) |
---|
261 | return NULL; |
---|
262 | /*this is here as a horrible hack since that's the way it |
---|
263 | used to work, but now one should make the .desktop type |
---|
264 | PanelApplet*/ |
---|
265 | for(i=1;ii->exec[i];i++) { |
---|
266 | if(strncmp("--activate-goad-server", |
---|
267 | ii->exec[i],constantlen)==0) { |
---|
268 | if(strlen(ii->exec[i])>constantlen) |
---|
269 | return g_strdup(&ii->exec[i][constantlen+1]); |
---|
270 | else |
---|
271 | return g_strdup(ii->exec[i+1]); |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | return NULL; |
---|
276 | } |
---|
277 | |
---|
278 | static void |
---|
279 | fr_free (FileRec *fr, gboolean free_fr) |
---|
280 | { |
---|
281 | if (fr == NULL) |
---|
282 | return; |
---|
283 | |
---|
284 | g_free (fr->name); |
---|
285 | fr->name = NULL; |
---|
286 | g_free (fr->fullname); |
---|
287 | fr->fullname = NULL; |
---|
288 | g_free (fr->comment); |
---|
289 | fr->comment = NULL; |
---|
290 | g_free (fr->icon); |
---|
291 | fr->icon = NULL; |
---|
292 | g_free (fr->goad_id); |
---|
293 | fr->goad_id = NULL; |
---|
294 | g_free (fr->tryexec_path); |
---|
295 | fr->tryexec_path = NULL; |
---|
296 | |
---|
297 | if (fr->parent != NULL && |
---|
298 | free_fr) |
---|
299 | fr->parent->recs = g_slist_remove (fr->parent->recs, fr); |
---|
300 | fr->parent = NULL; |
---|
301 | |
---|
302 | if (fr->type == FILE_REC_DIR) { |
---|
303 | DirRec *dr = (DirRec *)fr; |
---|
304 | GSList *li; |
---|
305 | |
---|
306 | for (li = dr->mfl; li != NULL; li = li->next) { |
---|
307 | MenuFinfo *mf = li->data; |
---|
308 | li->data = NULL; |
---|
309 | mf->fr = NULL; |
---|
310 | } |
---|
311 | g_slist_free (dr->mfl); |
---|
312 | dr->mfl = NULL; |
---|
313 | |
---|
314 | for (li = dr->recs; li != NULL; li = li->next) { |
---|
315 | FileRec *ffr = li->data; |
---|
316 | li->data = NULL; |
---|
317 | ffr->parent = NULL; |
---|
318 | fr_free (ffr, TRUE); |
---|
319 | } |
---|
320 | g_slist_free (dr->recs); |
---|
321 | dr->recs = NULL; |
---|
322 | |
---|
323 | if (dr->tryexecs != NULL) { |
---|
324 | g_slist_foreach (dr->tryexecs, (GFunc) g_free, NULL); |
---|
325 | g_slist_free (dr->tryexecs); |
---|
326 | dr->tryexecs = NULL; |
---|
327 | } |
---|
328 | dir_list = g_slist_remove (dir_list, fr); |
---|
329 | } |
---|
330 | |
---|
331 | if (free_fr) { |
---|
332 | if (fr->type == FILE_REC_DIR) |
---|
333 | g_chunk_free (fr, dir_chunk); |
---|
334 | else |
---|
335 | g_chunk_free (fr, file_chunk); |
---|
336 | } else { |
---|
337 | int type = fr->type; |
---|
338 | if (fr->type == FILE_REC_DIR) |
---|
339 | memset (fr, 0, sizeof(DirRec)); |
---|
340 | else |
---|
341 | memset (fr, 0, sizeof(FileRec)); |
---|
342 | /* we must reset the type so that we don't crash |
---|
343 | * if we call fr_free on this again */ |
---|
344 | fr->type = type; |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | static void |
---|
349 | fr_fill_dir(FileRec *fr, int sublevels) |
---|
350 | { |
---|
351 | GSList *flist; |
---|
352 | struct stat s; |
---|
353 | DirRec *dr = (DirRec *)fr; |
---|
354 | FileRec *ffr; |
---|
355 | time_t curtime = time(NULL); |
---|
356 | char *mergedir; |
---|
357 | |
---|
358 | g_return_if_fail (dr->recs == NULL); |
---|
359 | g_return_if_fail (fr != NULL); |
---|
360 | g_return_if_fail (fr->name != NULL); |
---|
361 | |
---|
362 | ffr = g_chunk_new0 (FileRec, file_chunk); |
---|
363 | ffr->type = FILE_REC_EXTRA; |
---|
364 | ffr->name = g_concat_dir_and_file (fr->name, ".order"); |
---|
365 | ffr->parent = dr; |
---|
366 | if (stat (ffr->name, &s) != -1) |
---|
367 | ffr->mtime = s.st_mtime; |
---|
368 | ffr->last_stat = curtime; |
---|
369 | dr->recs = g_slist_prepend(dr->recs, ffr); |
---|
370 | |
---|
371 | mergedir = fr_get_mergedir (fr->name); |
---|
372 | |
---|
373 | flist = get_mfiles_from_menudir(fr->name); |
---|
374 | while (flist != NULL) { |
---|
375 | gboolean merged; |
---|
376 | MFile *mfile = flist->data; |
---|
377 | char *name; |
---|
378 | GSList *tmp = flist; |
---|
379 | flist = flist->next; |
---|
380 | g_slist_free_1(tmp); |
---|
381 | |
---|
382 | if ( ! mfile->merged) { |
---|
383 | name = g_concat_dir_and_file (fr->name, mfile->name); |
---|
384 | } else if (mergedir != NULL) { |
---|
385 | name = g_concat_dir_and_file (mergedir, mfile->name); |
---|
386 | } else { |
---|
387 | free_mfile (mfile); |
---|
388 | continue; |
---|
389 | } |
---|
390 | merged = mfile->merged; |
---|
391 | |
---|
392 | if (stat (name, &s) == -1) { |
---|
393 | g_free(name); |
---|
394 | free_mfile (mfile); |
---|
395 | continue; |
---|
396 | } |
---|
397 | free_mfile (mfile); |
---|
398 | |
---|
399 | if (S_ISDIR (s.st_mode)) { |
---|
400 | if (merged) |
---|
401 | ffr = fr_read_dir (NULL, name, NULL, &s, sublevels-1); |
---|
402 | else |
---|
403 | ffr = fr_read_dir (NULL, name, &s, NULL, sublevels-1); |
---|
404 | g_free(name); |
---|
405 | if(ffr) { |
---|
406 | ffr->merged = merged; |
---|
407 | dr->recs = g_slist_prepend(dr->recs,ffr); |
---|
408 | } |
---|
409 | } else { |
---|
410 | GnomeDesktopEntry *dentry; |
---|
411 | char *tryexec_path; |
---|
412 | char *p = strrchr(name,'.'); |
---|
413 | if (p == NULL || |
---|
414 | (strcmp (p, ".desktop") != 0 && |
---|
415 | strcmp (p, ".kdelnk") != 0)) { |
---|
416 | g_free (name); |
---|
417 | continue; |
---|
418 | } |
---|
419 | |
---|
420 | tryexec_path = NULL; |
---|
421 | |
---|
422 | dentry = gnome_desktop_entry_load_unconditional (name); |
---|
423 | if (dentry != NULL && |
---|
424 | dentry->tryexec != NULL) { |
---|
425 | tryexec_path = panel_is_program_in_path (dentry->tryexec); |
---|
426 | if (tryexec_path == NULL) { |
---|
427 | dr->tryexecs = g_slist_prepend (dr->tryexecs, dentry->tryexec); |
---|
428 | dentry->tryexec = NULL; |
---|
429 | gnome_desktop_entry_free (dentry); |
---|
430 | dentry = NULL; |
---|
431 | } |
---|
432 | } |
---|
433 | if (dentry != NULL) { |
---|
434 | ffr = g_chunk_new0 (FileRec, file_chunk); |
---|
435 | if (dentry->type != NULL && |
---|
436 | strcasecmp_no_locale (dentry->type, |
---|
437 | "separator") == 0) |
---|
438 | ffr->type = FILE_REC_SEP; |
---|
439 | else |
---|
440 | ffr->type = FILE_REC_FILE; |
---|
441 | ffr->merged = merged; |
---|
442 | ffr->name = name; |
---|
443 | ffr->mtime = s.st_mtime; |
---|
444 | ffr->last_stat = curtime; |
---|
445 | ffr->parent = dr; |
---|
446 | ffr->icon = dentry->icon; |
---|
447 | dentry->icon = NULL; |
---|
448 | ffr->fullname = dentry->name; |
---|
449 | ffr->comment = g_strdup (dentry->comment); |
---|
450 | dentry->name = NULL; |
---|
451 | ffr->tryexec_path = tryexec_path; |
---|
452 | ffr->goad_id = |
---|
453 | get_applet_goad_id_from_dentry (dentry); |
---|
454 | gnome_desktop_entry_free (dentry); |
---|
455 | |
---|
456 | dr->recs = g_slist_prepend (dr->recs, ffr); |
---|
457 | } else { |
---|
458 | g_free (name); |
---|
459 | } |
---|
460 | } |
---|
461 | } |
---|
462 | dr->recs = g_slist_reverse (dr->recs); |
---|
463 | |
---|
464 | g_free (mergedir); |
---|
465 | } |
---|
466 | |
---|
467 | FileRec * |
---|
468 | fr_read_dir (DirRec *dr, const char *mdir, struct stat *dstat, |
---|
469 | struct stat *merge_dstat, int sublevels) |
---|
470 | { |
---|
471 | char *fname; |
---|
472 | struct stat s; |
---|
473 | FileRec *fr; |
---|
474 | time_t curtime = time (NULL); |
---|
475 | char *mergedir; |
---|
476 | |
---|
477 | g_return_val_if_fail (mdir != NULL, NULL); |
---|
478 | |
---|
479 | mergedir = fr_get_mergedir (mdir); |
---|
480 | |
---|
481 | /*this will zero all fields*/ |
---|
482 | if (dr == NULL) { |
---|
483 | dr = g_chunk_new0 (DirRec, dir_chunk); |
---|
484 | dr->force_reread = FALSE; |
---|
485 | /* this must be set otherwise we may messup on |
---|
486 | fr_free */ |
---|
487 | dr->frec.type = FILE_REC_DIR; |
---|
488 | } |
---|
489 | fr = (FileRec *)dr; |
---|
490 | |
---|
491 | if (fr->last_stat < curtime-STAT_EVERY) { |
---|
492 | if (dstat == NULL) { |
---|
493 | if (stat (mdir, &s) == -1) { |
---|
494 | fr_free (fr, TRUE); |
---|
495 | g_free (mergedir); |
---|
496 | return NULL; |
---|
497 | } |
---|
498 | |
---|
499 | fr->mtime = s.st_mtime; |
---|
500 | } else { |
---|
501 | fr->mtime = dstat->st_mtime; |
---|
502 | } |
---|
503 | |
---|
504 | if (mergedir != NULL) { |
---|
505 | if (merge_dstat == NULL) { |
---|
506 | if (stat (mergedir, &s) == -1) { |
---|
507 | dr->merge_mtime = 0; |
---|
508 | } else { |
---|
509 | dr->merge_mtime = s.st_mtime; |
---|
510 | } |
---|
511 | } else |
---|
512 | dr->merge_mtime = merge_dstat->st_mtime; |
---|
513 | } |
---|
514 | |
---|
515 | fr->last_stat = curtime; |
---|
516 | } |
---|
517 | |
---|
518 | g_free (mergedir); |
---|
519 | |
---|
520 | fr->type = FILE_REC_DIR; |
---|
521 | g_free (fr->name); |
---|
522 | fr->name = g_strdup (mdir); |
---|
523 | |
---|
524 | s.st_mtime = 0; |
---|
525 | fname = g_concat_dir_and_file (mdir, ".directory"); |
---|
526 | if (dr->dentrylast_stat >= curtime-STAT_EVERY || |
---|
527 | stat (fname, &s) != -1) { |
---|
528 | GnomeDesktopEntry *dentry; |
---|
529 | dentry = gnome_desktop_entry_load(fname); |
---|
530 | if (dentry != NULL) { |
---|
531 | g_free (fr->icon); |
---|
532 | fr->icon = dentry->icon; |
---|
533 | dentry->icon = NULL; |
---|
534 | g_free (fr->fullname); |
---|
535 | fr->fullname = dentry->name; |
---|
536 | g_free (fr->comment); |
---|
537 | fr->comment = g_strdup (dentry->comment); |
---|
538 | dentry->name = NULL; |
---|
539 | gnome_desktop_entry_free (dentry); |
---|
540 | } else { |
---|
541 | g_free (fr->icon); |
---|
542 | fr->icon = NULL; |
---|
543 | g_free (fr->fullname); |
---|
544 | fr->fullname = NULL; |
---|
545 | g_free (fr->comment); |
---|
546 | fr->comment = NULL; |
---|
547 | } |
---|
548 | /*if we statted*/ |
---|
549 | if (s.st_mtime != 0) |
---|
550 | dr->dentrylast_stat = curtime; |
---|
551 | dr->dentrymtime = s.st_mtime; |
---|
552 | } |
---|
553 | g_free (fname); |
---|
554 | |
---|
555 | /* add if missing from list of directories */ |
---|
556 | if (g_slist_find (dir_list, fr) == NULL) |
---|
557 | dir_list = g_slist_prepend (dir_list, fr); |
---|
558 | |
---|
559 | /*if this is a fake structure, so we don't actually look into |
---|
560 | the directory*/ |
---|
561 | if (sublevels > 0) |
---|
562 | fr_fill_dir (fr, sublevels); |
---|
563 | |
---|
564 | return fr; |
---|
565 | } |
---|
566 | |
---|
567 | |
---|
568 | FileRec * |
---|
569 | fr_replace (FileRec *fr) |
---|
570 | { |
---|
571 | char *name = fr->name; |
---|
572 | DirRec *par = fr->parent; |
---|
573 | |
---|
574 | g_assert (fr->type == FILE_REC_DIR); |
---|
575 | |
---|
576 | /* null these so they don't get freed */ |
---|
577 | fr->parent = NULL; |
---|
578 | fr->name = NULL; |
---|
579 | |
---|
580 | /* don't free the actual structure */ |
---|
581 | fr_free (fr, FALSE); |
---|
582 | |
---|
583 | /* sanity */ |
---|
584 | fr->type = FILE_REC_DIR; |
---|
585 | |
---|
586 | fr = fr_read_dir ((DirRec *)fr, name, NULL, NULL, 1); |
---|
587 | if (fr != NULL) |
---|
588 | fr->parent = par; |
---|
589 | g_free (name); |
---|
590 | |
---|
591 | return fr; |
---|
592 | } |
---|
593 | |
---|
594 | |
---|
595 | FileRec * |
---|
596 | fr_check_and_reread (FileRec *fr) |
---|
597 | { |
---|
598 | DirRec *dr = (DirRec *)fr; |
---|
599 | FileRec *ret = fr; |
---|
600 | time_t curtime; |
---|
601 | |
---|
602 | g_return_val_if_fail (fr != NULL, fr); |
---|
603 | g_return_val_if_fail (fr->type == FILE_REC_DIR, fr); |
---|
604 | |
---|
605 | if (dr->recs == NULL) { |
---|
606 | fr_fill_dir (fr, 1); |
---|
607 | } else { |
---|
608 | gboolean reread = FALSE; |
---|
609 | gboolean any_change = FALSE; |
---|
610 | struct stat ds; |
---|
611 | GSList *li; |
---|
612 | |
---|
613 | if ( ! global_config.menu_check) |
---|
614 | return ret; |
---|
615 | |
---|
616 | if (dr->force_reread) |
---|
617 | reread = TRUE; |
---|
618 | dr->force_reread = FALSE; |
---|
619 | |
---|
620 | /* recheck tryexecs */ |
---|
621 | for (li = dr->tryexecs; ! reread && li != NULL; li = li->next) { |
---|
622 | char *tryexec = li->data; |
---|
623 | char *p = panel_is_program_in_path (tryexec); |
---|
624 | |
---|
625 | if (p != NULL) { |
---|
626 | reread = TRUE; |
---|
627 | g_free (p); |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | curtime = time (NULL); |
---|
632 | |
---|
633 | if ( ! reread && |
---|
634 | fr->last_stat < curtime-STAT_EVERY) { |
---|
635 | if(stat(fr->name, &ds)==-1) { |
---|
636 | fr_free (fr, TRUE); |
---|
637 | return NULL; |
---|
638 | } |
---|
639 | if(ds.st_mtime != fr->mtime) |
---|
640 | reread = TRUE; |
---|
641 | |
---|
642 | if(dr->merge_mtime > 0) { |
---|
643 | char *mergedir = fr_get_mergedir (fr->name); |
---|
644 | if(mergedir != NULL) { |
---|
645 | if(stat(mergedir, &ds) >= 0 && |
---|
646 | ds.st_mtime != dr->merge_mtime) |
---|
647 | reread = TRUE; |
---|
648 | g_free(mergedir); |
---|
649 | } |
---|
650 | } |
---|
651 | } |
---|
652 | for(li = dr->recs; !reread && li!=NULL; li=g_slist_next(li)) { |
---|
653 | FileRec *ffr = li->data; |
---|
654 | DirRec *ddr; |
---|
655 | int r; |
---|
656 | char *p; |
---|
657 | struct stat s; |
---|
658 | |
---|
659 | switch(ffr->type) { |
---|
660 | case FILE_REC_DIR: |
---|
661 | ddr = (DirRec *)ffr; |
---|
662 | p = g_concat_dir_and_file(ffr->name, |
---|
663 | ".directory"); |
---|
664 | if (ddr->dentrylast_stat >= curtime-STAT_EVERY) { |
---|
665 | g_free (p); |
---|
666 | break; |
---|
667 | } |
---|
668 | dr->dentrylast_stat = curtime; |
---|
669 | if(stat(p,&s)==-1) { |
---|
670 | /* perhaps the directory is gone */ |
---|
671 | if ( ! panel_file_exists (ffr->name)) { |
---|
672 | reread = TRUE; |
---|
673 | break; |
---|
674 | } |
---|
675 | /* if not, we're just now missing a |
---|
676 | * desktop file */ |
---|
677 | if(dr->dentrymtime) { |
---|
678 | g_free(ffr->icon); |
---|
679 | ffr->icon = NULL; |
---|
680 | g_free(ffr->fullname); |
---|
681 | ffr->fullname = NULL; |
---|
682 | g_free(ffr->comment); |
---|
683 | ffr->comment = NULL; |
---|
684 | ddr->dentrymtime = 0; |
---|
685 | any_change = TRUE; |
---|
686 | } |
---|
687 | dr->dentrylast_stat = 0; |
---|
688 | g_free(p); |
---|
689 | break; |
---|
690 | } |
---|
691 | if(ddr->dentrymtime != s.st_mtime) { |
---|
692 | GnomeDesktopEntry *dentry; |
---|
693 | dentry = gnome_desktop_entry_load(p); |
---|
694 | if(dentry) { |
---|
695 | g_free(ffr->icon); |
---|
696 | ffr->icon = dentry->icon; |
---|
697 | dentry->icon = NULL; |
---|
698 | g_free(ffr->fullname); |
---|
699 | ffr->fullname = dentry->name; |
---|
700 | g_free(ffr->comment); |
---|
701 | ffr->comment = g_strdup (dentry->comment); |
---|
702 | dentry->name = NULL; |
---|
703 | gnome_desktop_entry_free(dentry); |
---|
704 | } else { |
---|
705 | g_free(ffr->icon); |
---|
706 | ffr->icon = NULL; |
---|
707 | g_free(ffr->fullname); |
---|
708 | ffr->fullname = NULL; |
---|
709 | g_free(ffr->comment); |
---|
710 | ffr->comment = NULL; |
---|
711 | } |
---|
712 | ddr->dentrymtime = s.st_mtime; |
---|
713 | any_change = TRUE; |
---|
714 | } |
---|
715 | g_free(p); |
---|
716 | break; |
---|
717 | case FILE_REC_FILE: |
---|
718 | if (ffr->last_stat >= curtime-STAT_EVERY) |
---|
719 | break; |
---|
720 | if(stat(ffr->name,&s)==-1) { |
---|
721 | reread = TRUE; |
---|
722 | break; |
---|
723 | } |
---|
724 | ffr->last_stat = curtime; |
---|
725 | if(ffr->mtime != s.st_mtime) { |
---|
726 | GnomeDesktopEntry *dentry; |
---|
727 | dentry = gnome_desktop_entry_load(ffr->name); |
---|
728 | if (dentry != NULL) { |
---|
729 | /* take over memory */ |
---|
730 | g_free (ffr->icon); |
---|
731 | ffr->icon = dentry->icon; |
---|
732 | dentry->icon = NULL; |
---|
733 | |
---|
734 | /* take over memory */ |
---|
735 | g_free (ffr->fullname); |
---|
736 | ffr->fullname = dentry->name; |
---|
737 | dentry->name = NULL; |
---|
738 | |
---|
739 | /* take over memory */ |
---|
740 | g_free (ffr->comment); |
---|
741 | ffr->comment = dentry->comment; |
---|
742 | dentry->comment = NULL; |
---|
743 | |
---|
744 | gnome_desktop_entry_free (dentry); |
---|
745 | } else { |
---|
746 | reread = TRUE; |
---|
747 | break; |
---|
748 | } |
---|
749 | ffr->mtime = s.st_mtime; |
---|
750 | any_change = TRUE; |
---|
751 | } |
---|
752 | if (ffr->tryexec_path != NULL && |
---|
753 | ! panel_file_exists (ffr->tryexec_path)) { |
---|
754 | reread = TRUE; |
---|
755 | } |
---|
756 | break; |
---|
757 | case FILE_REC_EXTRA: |
---|
758 | if (ffr->last_stat >= curtime-STAT_EVERY) |
---|
759 | break; |
---|
760 | r = stat(ffr->name,&s); |
---|
761 | if((r==-1 && ffr->mtime) || |
---|
762 | (r!=-1 && ffr->mtime != s.st_mtime)) |
---|
763 | reread = TRUE; |
---|
764 | ffr->last_stat = curtime; |
---|
765 | break; |
---|
766 | } |
---|
767 | } |
---|
768 | if(reread) { |
---|
769 | ret = fr_replace(fr); |
---|
770 | } else if(any_change) { |
---|
771 | GSList *li; |
---|
772 | for(li = dr->mfl; li!=NULL; li = g_slist_next(li)) { |
---|
773 | MenuFinfo *mf = li->data; |
---|
774 | li->data = NULL; |
---|
775 | mf->fr = NULL; |
---|
776 | } |
---|
777 | g_slist_free (dr->mfl); |
---|
778 | dr->mfl = NULL; |
---|
779 | } |
---|
780 | } |
---|
781 | return ret; |
---|
782 | } |
---|
783 | |
---|
784 | FileRec * |
---|
785 | fr_get_dir (const char *mdir) |
---|
786 | { |
---|
787 | GSList *li; |
---|
788 | g_return_val_if_fail (mdir!=NULL, NULL); |
---|
789 | |
---|
790 | for(li = dir_list; li != NULL; li = li->next) { |
---|
791 | FileRec *fr = li->data; |
---|
792 | g_assert(fr != NULL); |
---|
793 | g_assert(fr->name != NULL); |
---|
794 | if (strcmp (fr->name, mdir) == 0) |
---|
795 | return fr_check_and_reread (fr); |
---|
796 | } |
---|
797 | return fr_read_dir (NULL, mdir, NULL, NULL, 1); |
---|
798 | } |
---|
799 | |
---|
800 | /* Get all directories we have in memory */ |
---|
801 | GSList* |
---|
802 | fr_get_all_dirs (void) |
---|
803 | { |
---|
804 | return dir_list; |
---|
805 | } |
---|
806 | |
---|
807 | void |
---|
808 | fr_force_reread (void) |
---|
809 | { |
---|
810 | GSList *li; |
---|
811 | for(li = dir_list; li != NULL; li = li->next) { |
---|
812 | DirRec *dr = li->data; |
---|
813 | g_assert (dr != NULL); |
---|
814 | |
---|
815 | dr->force_reread = TRUE; |
---|
816 | } |
---|
817 | } |
---|