1 /* vim: set sw=4 ts=4 sts=4 et: */
3 #include "efreet_private.h"
8 typedef struct Efreet_Menu_Move Efreet_Menu_Move;
12 * Info on a menu movement
14 struct Efreet_Menu_Move
16 char *old_name; /**< The menu path to move from */
17 char *new_name; /**< The menu path to move too */
21 * Efreet_Menu_Internal
23 typedef struct Efreet_Menu_Internal Efreet_Menu_Internal;
26 * Efreet_Menu_Internal
27 * Contains the information about a menu
29 struct Efreet_Menu_Internal
33 char *path; /**< The base file path */
34 char *name; /**< The filename for this menu */
35 } file; /**< The menu file information */
39 const char *internal; /**< The menu name */
40 const char *name; /**< Name to use in the menus */
41 } name; /**< The names for this menu */
43 Efreet_Desktop *directory; /**< The directory */
44 Ecore_DList *directories; /**< All the directories set in the menu file */
46 Efreet_Menu_Move *current_move; /**< The current move */
48 Eina_List *app_dirs; /**< .desktop application directories */
50 Eina_List *app_pool; /**< application pool */
51 Eina_List *applications; /**< applications in this menu */
53 Ecore_DList *directory_dirs; /**< .directory file directories */
54 Eina_Hash *directory_cache; /**< .directory dirs */
56 Eina_List *moves; /**< List of moves to be handled by the menu */
57 Eina_List *filters; /**< Include and Exclude filters */
59 Efreet_Menu_Internal *parent; /**< Our parent menu */
60 Eina_List *sub_menus; /**< Our sub menus */
62 Eina_List *layout; /**< This menus layout */
63 Eina_List *default_layout; /**< Default layout */
64 signed char show_empty; /**< Whether to show empty menus */
65 signed char in_line; /**< Whether this meny can be inlined */
66 signed char inline_limit; /**< Number of elements which triggers inline */
67 signed char inline_header; /**< Whether we should use the header name when this menu is inlined */
68 signed char inline_alias; /**< Whether we should use the menu name when inlining */
70 unsigned char seen_allocated:1; /**< have we set the only_unallocated */
71 unsigned char only_unallocated:1; /**< Show only unallocated .desktops */
73 unsigned char seen_deleted:1; /**< Have we seen the deleted item yet */
74 unsigned char deleted:1; /**< The menu is deleted */
80 typedef struct Efreet_Menu_App_Dir Efreet_Menu_App_Dir;
83 * Holds information on an app dir
85 struct Efreet_Menu_App_Dir
87 char *path; /**< directory path */
88 char *prefix; /**< If it's legacy it can have a prefix */
89 unsigned int legacy:1; /**< is this a legacy dir */
93 * The type of operations we can perform with a filter
95 enum Efreet_Menu_Filter_Op_Type
97 EFREET_MENU_FILTER_OP_OR,
98 EFREET_MENU_FILTER_OP_AND,
99 EFREET_MENU_FILTER_OP_NOT
103 * Efreet_Menu_Filter_Op_Type
105 typedef enum Efreet_Menu_Filter_Op_Type Efreet_Menu_Filter_Op_Type;
110 enum Efreet_Menu_Filter_Type
112 EFREET_MENU_FILTER_INCLUDE,
113 EFREET_MENU_FILTER_EXCLUDE
117 * Efreet_Menu_Filter_Type
119 typedef enum Efreet_Menu_Filter_Type Efreet_Menu_Filter_Type;
122 * Efreet_Menu_Filter_Op
124 typedef struct Efreet_Menu_Filter_Op Efreet_Menu_Filter_Op;
127 * Efreet_Menu_Filter_Op
128 * Contains information on a filter operation
130 struct Efreet_Menu_Filter_Op
132 Efreet_Menu_Filter_Op_Type type; /**< The type of operation */
133 Eina_List *categories; /**< The categories this op applies too */
134 Eina_List *filenames; /**< The filenames this op applies too */
136 Eina_List *filters; /**< Child filters */
138 unsigned char all:1; /**< Applies to all .desktop files */
144 typedef struct Efreet_Menu_Filter Efreet_Menu_Filter;
148 * Stores information on a filter
150 struct Efreet_Menu_Filter
152 Efreet_Menu_Filter_Type type; /**< The type of filter */
153 Efreet_Menu_Filter_Op *op; /**< The filter operations */
159 enum Efreet_Menu_Layout_Type
161 EFREET_MENU_LAYOUT_MENUNAME,
162 EFREET_MENU_LAYOUT_FILENAME,
163 EFREET_MENU_LAYOUT_SEPARATOR,
164 EFREET_MENU_LAYOUT_MERGE
168 * Efreet_Menu_Layout_Type
170 typedef enum Efreet_Menu_Layout_Type Efreet_Menu_Layout_Type;
175 typedef struct Efreet_Menu_Layout Efreet_Menu_Layout;
179 * Stores information on a layout
181 struct Efreet_Menu_Layout
183 Efreet_Menu_Layout_Type type; /**< The type of layout */
184 char *name; /**< The name of the element */
186 /* The items below are for Menuname Layout elements */
187 signed char show_empty; /**< Whether to show empty menus */
188 signed char in_line; /**< Whether this meny can be inlined */
189 signed char inline_limit; /**< Number of elements which triggers inline */
190 signed char inline_header; /**< Whether we should use the header name when this menu is inlined */
191 signed char inline_alias; /**< Whether we should use the menu name when inlining */
195 * Efreet_Menu_Desktop
197 typedef struct Efreet_Menu_Desktop Efreet_Menu_Desktop;
200 * Efreet_Menu_Desktop
201 * Stores information on a desktop for the menu
203 struct Efreet_Menu_Desktop
205 Efreet_Desktop *desktop; /**< The desktop we refer too */
206 const char *id; /**< The desktop file id */
207 unsigned char allocated:1; /**< If this desktop has been allocated */
210 static char *efreet_menu_prefix = NULL; /**< The $XDG_MENU_PREFIX env var */
211 Eina_List *efreet_menu_kde_legacy_dirs = NULL; /**< The directories to use for KDELegacy entries */
212 static const char *efreet_tag_menu = NULL;
213 static char *efreet_menu_file = NULL; /**< A menu file set explicityl as default */
215 static Eina_Hash *efreet_merged_menus = NULL;
216 static Eina_Hash *efreet_merged_dirs = NULL;
218 static Eina_Hash *efreet_menu_handle_cbs = NULL;
219 static Eina_Hash *efreet_menu_filter_cbs = NULL;
220 static Eina_Hash *efreet_menu_move_cbs = NULL;
221 static Eina_Hash *efreet_menu_layout_cbs = NULL;
223 static const char *efreet_menu_prefix_get(void);
225 static Efreet_Menu_Internal *efreet_menu_by_name_find(Efreet_Menu_Internal *internal,
227 Efreet_Menu_Internal **parent);
228 static int efreet_menu_cb_compare_names(Efreet_Menu_Internal *internal, const char *name);
229 static int efreet_menu_cb_md_compare_ids(Efreet_Menu_Desktop *md, const char *name);
231 static int efreet_menu_cb_entry_compare_menu(Efreet_Menu *entry, Efreet_Menu_Internal *internal);
232 static int efreet_menu_cb_entry_compare_desktop(Efreet_Menu *entry, Efreet_Desktop *desktop);
234 static int efreet_menu_cb_move_compare(Efreet_Menu_Move *move, const char *old);
236 static int efreet_menu_process(Efreet_Menu_Internal *internal, unsigned int only_unallocated);
237 static int efreet_menu_process_dirs(Efreet_Menu_Internal *internal);
238 static int efreet_menu_app_dirs_process(Efreet_Menu_Internal *internal);
239 static int efreet_menu_app_dir_scan(Efreet_Menu_Internal *internal,
243 static int efreet_menu_directory_dirs_process(Efreet_Menu_Internal *internal);
244 static int efreet_menu_directory_dir_scan(const char *path,
245 const char *relative_path,
247 static Efreet_Desktop *efreet_menu_directory_get(Efreet_Menu_Internal *internal,
249 static void efreet_menu_process_filters(Efreet_Menu_Internal *internal,
250 unsigned int only_unallocated);
251 static Eina_List * efreet_menu_process_app_pool(Eina_List *pool, Eina_List *applications,
253 Efreet_Menu_Filter *filter,
254 unsigned int only_unallocated);
255 static int efreet_menu_filter_matches(Efreet_Menu_Filter_Op *op,
256 Efreet_Menu_Desktop *md);
257 static int efreet_menu_filter_or_matches(Efreet_Menu_Filter_Op *op,
258 Efreet_Menu_Desktop *md);
259 static int efreet_menu_filter_and_matches(Efreet_Menu_Filter_Op *op,
260 Efreet_Menu_Desktop *md);
261 static int efreet_menu_filter_not_matches(Efreet_Menu_Filter_Op *op,
262 Efreet_Menu_Desktop *md);
264 static Efreet_Menu *efreet_menu_layout_menu(Efreet_Menu_Internal *internal);
265 static Efreet_Menu *efreet_menu_layout_desktop(Efreet_Menu_Desktop *md);
266 static void efreet_menu_layout_entries_get(Efreet_Menu *entry, Efreet_Menu_Internal *internal,
267 Efreet_Menu_Layout *layout);
268 static int efreet_menu_layout_is_empty(Efreet_Menu *entry);
270 static Efreet_Menu_Internal *efreet_menu_internal_new(void);
271 static void efreet_menu_internal_free(Efreet_Menu_Internal *internal);
272 static void efreet_menu_create_sub_menu_list(Efreet_Menu_Internal *internal);
273 static void efreet_menu_create_app_dirs_list(Efreet_Menu_Internal *internal);
274 static void efreet_menu_create_directory_dirs_list(Efreet_Menu_Internal *internal);
275 static void efreet_menu_create_directories_list(Efreet_Menu_Internal *internal);
276 static void efreet_menu_create_move_list(Efreet_Menu_Internal *internal);
277 static void efreet_menu_create_filter_list(Efreet_Menu_Internal *internal);
278 static void efreet_menu_create_layout_list(Efreet_Menu_Internal *internal);
279 static void efreet_menu_create_default_layout_list(Efreet_Menu_Internal *internal);
280 static char *efreet_menu_path_get(Efreet_Menu_Internal *internal, const char *suffix);
282 static Efreet_Menu_App_Dir *efreet_menu_app_dir_new(void);
283 static void efreet_menu_app_dir_free(Efreet_Menu_App_Dir *dir);
285 static Efreet_Menu_Move *efreet_menu_move_new(void);
286 static void efreet_menu_move_free(Efreet_Menu_Move *move);
288 static Efreet_Menu_Filter *efreet_menu_filter_new(void);
289 static void efreet_menu_filter_free(Efreet_Menu_Filter *filter);
291 static Efreet_Menu_Layout *efreet_menu_layout_new(void);
293 static Efreet_Menu_Filter_Op *efreet_menu_filter_op_new(void);
294 static void efreet_menu_filter_op_free(Efreet_Menu_Filter_Op *op);
296 static Efreet_Menu_Desktop *efreet_menu_desktop_new(void);
297 static void efreet_menu_desktop_free(Efreet_Menu_Desktop *md);
299 static Efreet_Menu *efreet_menu_entry_new(void);
301 static int efreet_menu_handle_menu(Efreet_Menu_Internal *internal, Efreet_Xml *xml);
302 static int efreet_menu_handle_name(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
304 static int efreet_menu_handle_sub_menu(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
305 static int efreet_menu_handle_app_dir(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
306 static int efreet_menu_handle_default_app_dirs(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
307 static int efreet_menu_handle_directory_dir(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
308 static int efreet_menu_handle_default_directory_dirs(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
309 static int efreet_menu_handle_directory(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
310 static int efreet_menu_handle_only_unallocated(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
311 static int efreet_menu_handle_not_only_unallocated(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
312 static int efreet_menu_handle_deleted(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
313 static int efreet_menu_handle_not_deleted(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
314 static int efreet_menu_handle_include(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
315 static int efreet_menu_handle_exclude(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
316 static int efreet_menu_handle_filename(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml);
317 static int efreet_menu_handle_category(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml);
318 static int efreet_menu_handle_all(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml);
319 static int efreet_menu_handle_and(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml);
320 static int efreet_menu_handle_or(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml);
321 static int efreet_menu_handle_not(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml);
322 static int efreet_menu_handle_merge_file(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
323 static int efreet_menu_handle_merge_dir(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
324 static int efreet_menu_handle_default_merge_dirs(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
325 static int efreet_menu_handle_legacy_dir(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
326 static Efreet_Menu_Internal *efreet_menu_handle_legacy_dir_helper(Efreet_Menu_Internal *root,
327 Efreet_Menu_Internal *parent,
328 const char *legacy_dir,
330 static int efreet_menu_handle_kde_legacy_dirs(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
331 static int efreet_menu_handle_move(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
332 static int efreet_menu_handle_old(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
333 static int efreet_menu_handle_new(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
334 static int efreet_menu_handle_layout(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
335 static int efreet_menu_handle_default_layout(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
337 static int efreet_menu_handle_filter(Efreet_Menu_Internal *parent, Efreet_Xml *xml,
338 Efreet_Menu_Filter_Type type);
339 static int efreet_menu_handle_filter_op(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml);
340 static int efreet_menu_handle_filter_child_op(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml,
341 Efreet_Menu_Filter_Op_Type type);
343 static int efreet_menu_handle_layout_menuname(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def);
344 static int efreet_menu_handle_layout_filename(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def);
345 static int efreet_menu_handle_layout_separator(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def);
346 static int efreet_menu_handle_layout_merge(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def);
348 static int efreet_menu_merge(Efreet_Menu_Internal *parent, Efreet_Xml *xml, const char *path);
349 static int efreet_menu_merge_dir(Efreet_Menu_Internal *parent, Efreet_Xml *xml, const char *path);
351 static int efreet_menu_cb_app_dirs_compare(Efreet_Menu_App_Dir *a, const char *b);
353 static void efreet_menu_resolve_moves(Efreet_Menu_Internal *internal);
354 static void efreet_menu_concatenate(Efreet_Menu_Internal *dest, Efreet_Menu_Internal *src);
356 static int efreet_menu_cb_menu_compare(Efreet_Menu_Internal *a, Efreet_Menu_Internal *b);
357 static int efreet_menu_cb_md_compare(Efreet_Menu_Desktop *a, Efreet_Menu_Desktop *b);
359 static int efreet_menu_save_menu(Efreet_Menu *menu, FILE *f, int indent);
360 static int efreet_menu_save_indent(FILE *f, int indent);
362 static void efreet_menu_path_set(Efreet_Menu_Internal *internal, const char *path);
365 * @return Returns 1 on success, 0 on failure
366 * @brief Initializes the Efreet Menu system.
369 efreet_menu_init(void)
376 int (*cb)(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
378 {"Menu", efreet_menu_handle_sub_menu},
379 {"AppDir", efreet_menu_handle_app_dir},
380 {"DefaultAppDirs", efreet_menu_handle_default_app_dirs},
381 {"DirectoryDir", efreet_menu_handle_directory_dir},
382 {"DefaultDirectoryDirs", efreet_menu_handle_default_directory_dirs},
383 {"Name", efreet_menu_handle_name},
384 {"Directory", efreet_menu_handle_directory},
385 {"OnlyUnallocated", efreet_menu_handle_only_unallocated},
386 {"NotOnlyUnallocated", efreet_menu_handle_not_only_unallocated},
387 {"Deleted", efreet_menu_handle_deleted},
388 {"NotDeleted", efreet_menu_handle_not_deleted},
389 {"Include", efreet_menu_handle_include},
390 {"Exclude", efreet_menu_handle_exclude},
391 {"MergeFile", efreet_menu_handle_merge_file},
392 {"MergeDir", efreet_menu_handle_merge_dir},
393 {"DefaultMergeDirs", efreet_menu_handle_default_merge_dirs},
394 {"LegacyDir", efreet_menu_handle_legacy_dir},
395 {"KDELegacyDirs", efreet_menu_handle_kde_legacy_dirs},
396 {"Move", efreet_menu_handle_move},
397 {"Layout", efreet_menu_handle_layout},
398 {"DefaultLayout", efreet_menu_handle_default_layout},
405 int (*cb)(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml);
407 {"Filename", efreet_menu_handle_filename},
408 {"Category", efreet_menu_handle_category},
409 {"All", efreet_menu_handle_all},
410 {"And", efreet_menu_handle_and},
411 {"Or", efreet_menu_handle_or},
412 {"Not", efreet_menu_handle_not},
419 int (*cb)(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
421 {"Old", efreet_menu_handle_old},
422 {"New", efreet_menu_handle_new},
429 int (*cb)(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def);
431 {"Menuname", efreet_menu_handle_layout_menuname},
432 {"Filename", efreet_menu_handle_layout_filename},
433 {"Separator", efreet_menu_handle_layout_separator},
434 {"Merge", efreet_menu_handle_layout_merge},
438 if (!eina_stringshare_init()) return 0;
439 if (!efreet_xml_init()) return 0;
441 efreet_menu_handle_cbs = eina_hash_string_superfast_new(NULL);
442 efreet_menu_filter_cbs = eina_hash_string_superfast_new(NULL);
443 efreet_menu_move_cbs = eina_hash_string_superfast_new(NULL);
444 efreet_menu_layout_cbs = eina_hash_string_superfast_new(NULL);
445 if (!efreet_menu_handle_cbs || !efreet_menu_filter_cbs
446 || !efreet_menu_move_cbs || !efreet_menu_layout_cbs)
449 /* set Menu into it's own so we can check the XML is valid before trying
451 efreet_tag_menu = eina_stringshare_add(menu_cbs[0].key);
453 for (i = 0; menu_cbs[i].key != NULL; i++)
455 eina_hash_del(efreet_menu_handle_cbs,
458 eina_hash_add(efreet_menu_handle_cbs,
462 for (i = 0; filter_cbs[i].key != NULL; i++)
464 eina_hash_del(efreet_menu_filter_cbs,
467 eina_hash_add(efreet_menu_filter_cbs,
471 for (i = 0; move_cbs[i].key != NULL; i++)
473 eina_hash_del(efreet_menu_move_cbs,
476 eina_hash_add(efreet_menu_move_cbs,
480 for (i = 0; layout_cbs[i].key != NULL; i++)
482 eina_hash_del(efreet_menu_layout_cbs,
485 eina_hash_add(efreet_menu_layout_cbs,
493 * @return Returns no value
494 * @brief Initialize legacy kde support. This function blocks while
495 * the kde-config script is run.
498 efreet_menu_kde_legacy_init(void)
504 IF_FREE_LIST(efreet_menu_kde_legacy_dirs);
506 f = popen("kde-config --path apps", "r");
509 /* XXX if the return from kde-config is a line longer than PATH_MAX,
510 * this won't be correct (increase buffer and get the rest...) */
511 if (!fgets(buf, PATH_MAX, f))
513 printf("Error initializing KDE legacy information\n");
522 efreet_menu_kde_legacy_dirs = eina_list_append(efreet_menu_kde_legacy_dirs,
523 (void *)eina_stringshare_add(s));
529 efreet_menu_kde_legacy_dirs = eina_list_append(efreet_menu_kde_legacy_dirs,
530 (void *)eina_stringshare_add(s));
537 * @return Returns no value
538 * @brief Shuts down the Efreet menu system
541 efreet_menu_shutdown(void)
543 IF_FREE(efreet_menu_prefix);
544 IF_FREE(efreet_menu_file);
546 IF_FREE_HASH(efreet_menu_handle_cbs);
547 IF_FREE_HASH(efreet_menu_filter_cbs);
548 IF_FREE_HASH(efreet_menu_move_cbs);
549 IF_FREE_HASH(efreet_menu_layout_cbs);
551 IF_FREE_LIST(efreet_menu_kde_legacy_dirs);
553 IF_FREE_HASH(efreet_merged_menus);
554 IF_FREE_HASH(efreet_merged_dirs);
556 IF_RELEASE(efreet_tag_menu);
558 efreet_xml_shutdown();
559 eina_stringshare_shutdown();
563 * @param name The internal name of the menu
564 * @return Returns the Efreet_Menu on success or
566 * @brief Creates a new menu
569 efreet_menu_new(const char *name)
575 printf("Error creating a new menu, name is missing\n");
578 menu = efreet_menu_entry_new();
579 menu->type = EFREET_MENU_ENTRY_MENU;
580 menu->name = eina_stringshare_add(name);
585 efreet_menu_file_set(const char *file)
587 IF_FREE(efreet_menu_file);
588 efreet_menu_file = NULL;
589 if (file) efreet_menu_file = strdup(file);
593 * @return Returns the Efreet_Menu_Internal representation of the default menu or
595 * @brief Creates the default menu representation
598 efreet_menu_get(void)
602 Eina_List *config_dirs, *l;
604 /* check the users config directory first */
605 snprintf(menu, sizeof(menu), "%s/menus/%sapplications.menu",
606 efreet_config_home_get(), efreet_menu_prefix_get());
607 if (ecore_file_exists(menu))
608 return efreet_menu_parse(menu);
610 if (efreet_menu_file)
612 if (ecore_file_exists(efreet_menu_file))
613 return efreet_menu_parse(efreet_menu_file);
616 /* fallback to the XDG_CONFIG_DIRS */
617 config_dirs = efreet_config_dirs_get();
618 EINA_LIST_FOREACH(config_dirs, l, dir)
620 snprintf(menu, sizeof(menu), "%s/menus/%sapplications.menu",
621 dir, efreet_menu_prefix_get());
622 if (ecore_file_exists(menu))
623 return efreet_menu_parse(menu);
630 * @param path: The path of the menu to load
631 * @return Returns the Efreet_Menu_Internal representation on success or NULL on
633 * @brief Parses the given .menu file and creates the menu representation
636 efreet_menu_parse(const char *path)
639 Efreet_Menu_Internal *internal = NULL;
640 Efreet_Menu *entry = NULL;
641 Eina_List *search_dirs;
643 xml = efreet_xml_new(path);
644 if (!xml) return NULL;
646 /* make sure we've got a <Menu> to start with */
647 if (xml->tag != efreet_tag_menu)
649 printf("Menu file didn't start with <Menu> tag.\n");
654 IF_FREE_HASH(efreet_merged_menus);
655 efreet_merged_menus = eina_hash_string_superfast_new(NULL);
657 IF_FREE_HASH(efreet_merged_dirs);
658 efreet_merged_dirs = eina_hash_string_superfast_new(NULL);
660 /* split appart the filename and the path */
661 internal = efreet_menu_internal_new();
663 /* Set default values */
664 internal->show_empty = 0;
665 internal->in_line = 0;
666 internal->inline_limit = 4;
667 internal->inline_header = 1;
668 internal->inline_alias = 0;
670 search_dirs = efreet_config_dirs_get();
672 efreet_menu_path_set(internal, path);
673 if (!efreet_menu_handle_menu(internal, xml))
676 efreet_menu_internal_free(internal);
681 efreet_menu_resolve_moves(internal);
683 if (!efreet_menu_process_dirs(internal))
685 efreet_menu_internal_free(internal);
689 /* handle all .desktops */
690 if (!efreet_menu_process(internal, 0))
692 efreet_menu_internal_free(internal);
696 /* handle menus with only unallocated .desktops */
697 if (!efreet_menu_process(internal, 1))
699 efreet_menu_internal_free(internal);
704 entry = efreet_menu_layout_menu(internal);
705 efreet_menu_internal_free(internal);
710 * @param menu: The menu to work with
711 * @param path: The path where the menu should be saved
712 * @return Returns 1 on success, 0 on failure
713 * @brief Saves the menu to file
716 efreet_menu_save(Efreet_Menu *menu, const char *path)
721 f = fopen(path, "w");
723 fprintf(f, "<?xml version=\"1.0\"?>\n");
724 fprintf(f, "<!DOCTYPE Menu PUBLIC \"-//freedesktop//DTD Menu 1.0//EN\" "
725 "\"http://standards.freedesktop.org/menu-spec/menu-1.0.dtd\">\n");
726 ret = efreet_menu_save_menu(menu, f, 0);
732 efreet_menu_save_menu(Efreet_Menu *menu, FILE *f, int indent)
736 efreet_menu_save_indent(f, indent);
737 fprintf(f, "<Menu>\n");
740 efreet_menu_save_indent(f, indent + 1);
741 fprintf(f, "<Name>%s</Name>\n", menu->name);
746 /* Only save these for the root element */
747 efreet_menu_save_indent(f, indent + 1);
748 fprintf(f, "<DefaultAppDirs/>\n");
749 efreet_menu_save_indent(f, indent + 1);
750 fprintf(f, "<DefaultDirectoryDirs/>\n");
755 efreet_menu_save_indent(f, indent + 1);
756 fprintf(f, "<Directory>%s</Directory>\n", menu->desktop->orig_path);
762 int has_desktop = 0, has_menu = 0;
764 efreet_menu_save_indent(f, indent + 1);
765 fprintf(f, "<Layout>\n");
766 EINA_LIST_FOREACH(menu->entries, l, entry)
768 if (entry->type == EFREET_MENU_ENTRY_MENU)
770 efreet_menu_save_indent(f, indent + 2);
771 fprintf(f, "<Menuname>%s</Menuname>\n", entry->id);
774 else if (entry->type == EFREET_MENU_ENTRY_DESKTOP)
776 efreet_menu_save_indent(f, indent + 2);
777 fprintf(f, "<Filename>%s</Filename>\n", entry->id);
780 else if (entry->type == EFREET_MENU_ENTRY_SEPARATOR)
782 efreet_menu_save_indent(f, indent + 2);
783 fprintf(f, "<Separator/>\n");
786 efreet_menu_save_indent(f, indent + 1);
787 fprintf(f, "</Layout>\n");
791 efreet_menu_save_indent(f, indent + 1);
792 fprintf(f, "<Include>\n");
793 EINA_LIST_FOREACH(menu->entries, l, entry)
795 if (entry->type == EFREET_MENU_ENTRY_DESKTOP)
797 efreet_menu_save_indent(f, indent + 2);
798 fprintf(f, "<Filename>%s</Filename>\n", entry->id);
801 efreet_menu_save_indent(f, indent + 1);
802 fprintf(f, "</Include>\n");
807 EINA_LIST_FOREACH(menu->entries, l, entry)
809 if (entry->type == EFREET_MENU_ENTRY_MENU)
810 efreet_menu_save_menu(entry, f, indent + 1);
814 efreet_menu_save_indent(f, indent);
815 fprintf(f, "</Menu>\n");
820 efreet_menu_save_indent(FILE *f, int indent)
824 for (i = 0; i < indent; i++)
830 * @param menu: The menu to work with
831 * @param desktop: The desktop to insert
832 * @param pos: The position to place the new desktop
833 * @return Returns 1 on success, 0 on failure
834 * @brief Insert a desktop element in a menu structure. Only accepts desktop files
835 * in default directories.
838 efreet_menu_desktop_insert(Efreet_Menu *menu, Efreet_Desktop *desktop, int pos)
843 if (!desktop || !menu) return 0;
844 id = efreet_util_path_to_file_id(desktop->orig_path);
847 entry = efreet_menu_entry_new();
848 entry->type = EFREET_MENU_ENTRY_DESKTOP;
849 entry->id = eina_stringshare_add(id);
850 entry->name = eina_stringshare_add(desktop->name);
851 if (desktop->icon) entry->icon = eina_stringshare_add(desktop->icon);
852 efreet_desktop_ref(desktop);
853 entry->desktop = desktop;
855 if (pos < 0 || pos >= eina_list_count(menu->entries))
856 menu->entries = eina_list_append(menu->entries, entry);
859 menu->entries = eina_list_append_relative(menu->entries, entry,
860 eina_list_nth(menu->entries, pos));
866 * @param menu: The menu to work with
867 * @param desktop: The desktop to remove
868 * @return Returns 1 on success, 0 on failure
869 * @brief Remove a desktop element in a menu structure. Only accepts desktop files
870 * in default directories.
873 efreet_menu_desktop_remove(Efreet_Menu *menu, Efreet_Desktop *desktop)
877 if (!desktop || !menu) return 0;
879 entry = eina_list_search_unsorted(menu->entries,
880 (Eina_Compare_Cb)efreet_menu_cb_entry_compare_desktop,
884 menu->entries = eina_list_remove(menu->entries, entry);
885 efreet_menu_free(entry);
892 * @param menu: The menu to work with
893 * @param menu: The menu to work with
894 * @param indent: The indent level to print the menu at
895 * @return Returns no value
896 * @brief Dumps the contents of the menu to the command line
899 efreet_menu_dump(Efreet_Menu *menu, const char *indent)
903 printf("%s%s: ", indent, menu->name);
904 printf("%s\n", (menu->icon ? menu->icon : "No icon"));
906 /* XXX dump the rest of the menu info */
914 len = strlen(indent) + 3;
915 new_indent = malloc(sizeof(char *) * len);
916 snprintf(new_indent, len, "%s ", indent);
918 EINA_LIST_FOREACH(menu->entries, l, entry)
920 if (entry->type == EFREET_MENU_ENTRY_SEPARATOR)
921 printf("%s|---\n", new_indent);
922 else if (entry->type == EFREET_MENU_ENTRY_DESKTOP)
923 printf("%s|-%s\n", new_indent, entry->name);
924 else if (entry->type == EFREET_MENU_ENTRY_MENU)
925 efreet_menu_dump(entry, new_indent);
926 else if (entry->type == EFREET_MENU_ENTRY_HEADER)
927 printf("%s|---%s\n", new_indent, entry->name);
935 * @param user_dir: The user directory to work with
936 * @param system_dirs: The system directories to work with
937 * @param suffix: The path suffix to add
938 * @return Returns the list of directories
939 * @brief Creates the list of directories based on the user
940 * dir, system dirs and given suffix.
943 efreet_default_dirs_get(const char *user_dir, Eina_List *system_dirs,
948 Eina_List *list = NULL;
951 snprintf(dir, sizeof(dir), "%s/%s", user_dir, suffix);
952 list = eina_list_append(list, strdup(dir));
954 EINA_LIST_FOREACH(system_dirs, l, xdg_dir)
956 snprintf(dir, sizeof(dir), "%s/%s", xdg_dir, suffix);
957 list = eina_list_append(list, strdup(dir));
965 * @return Returns a new Efreet_Menu_Internal struct
966 * @brief Allocates and initializes a new Efreet_Menu_Internal structure
968 static Efreet_Menu_Internal *
969 efreet_menu_internal_new(void)
971 Efreet_Menu_Internal *internal;
973 internal = NEW(Efreet_Menu_Internal, 1);
974 internal->show_empty = -1;
975 internal->in_line = -1;
976 internal->inline_limit = -1;
977 internal->inline_header = -1;
978 internal->inline_alias = -1;
984 * @param menu: The menu to free
985 * @return Returns no value
986 * @brief Frees up the given menu structure
989 efreet_menu_internal_free(Efreet_Menu_Internal *internal)
993 if (!internal) return;
995 IF_FREE(internal->file.path);
996 IF_FREE(internal->file.name);
998 IF_RELEASE(internal->name.internal);
999 internal->name.name = NULL;
1001 IF_FREE_LIST(internal->applications);
1003 IF_FREE_DLIST(internal->directories);
1004 EINA_LIST_FREE(internal->app_dirs, d) efreet_menu_app_dir_free(d);
1005 // IF_FREE_LIST(internal->app_dirs);
1006 IF_FREE_LIST(internal->app_pool);
1007 IF_FREE_DLIST(internal->directory_dirs);
1008 IF_FREE_HASH(internal->directory_cache);
1010 IF_FREE_LIST(internal->moves);
1011 IF_FREE_LIST(internal->filters);
1013 IF_FREE_LIST(internal->sub_menus);
1015 IF_FREE_LIST(internal->layout);
1016 IF_FREE_LIST(internal->default_layout);
1023 * @return Returns the XDG_MENU_PREFIX env variable or "" if none set
1024 * @brief Retrieves the XDG_MENU_PREFIX or "" if not set.
1027 efreet_menu_prefix_get(void)
1031 if (efreet_menu_prefix) return efreet_menu_prefix;
1033 prefix = getenv("XDG_MENU_PREFIX");
1034 if (prefix) efreet_menu_prefix = strdup(prefix);
1035 else efreet_menu_prefix = strdup("");
1037 return efreet_menu_prefix;
1042 * @param menu: The menu to populate
1043 * @param xml: The xml dom tree to populate from
1044 * @return Returns 1 if this XML tree is valid, 0 otherwise
1045 * @brief Populates the given menu from the given xml structure
1047 * We walk the Menu children backwards. The reason for this is so that we
1048 * can deal with all the things that make us select the 'last' element
1049 * (MergeFile, Directory, etc). We'll see the last one first and can deal
1050 * with it right away.
1053 efreet_menu_handle_menu(Efreet_Menu_Internal *internal, Efreet_Xml *xml)
1056 int (*cb)(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
1058 ecore_list_last_goto(xml->children);
1059 while ((child = ecore_dlist_previous(xml->children)))
1061 cb = eina_hash_find(efreet_menu_handle_cbs, child->tag);
1064 if (!cb(internal, child))
1069 printf("Unknown XML tag: %s\n", child->tag);
1078 * @param parent: The parent Menu
1079 * @param xml: The xml that defines the menu
1080 * @return Returns 1 on success or 0 on failure
1081 * @brief Handles the sub-menu nodes of the XML file
1084 efreet_menu_handle_sub_menu(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1086 Efreet_Menu_Internal *internal, *match;
1088 efreet_menu_create_sub_menu_list(parent);
1090 internal = efreet_menu_internal_new();
1091 internal->file.path = strdup(parent->file.path);
1092 if (!efreet_menu_handle_menu(internal, xml))
1094 efreet_menu_internal_free(internal);
1098 /* if this menu already exists we just take this one and stick it on the
1099 * start of the existing one */
1100 if ((match = eina_list_search_unsorted(parent->sub_menus,
1101 (Eina_Compare_Cb)efreet_menu_cb_menu_compare,
1105 efreet_menu_concatenate(match, internal);
1106 efreet_menu_internal_free(internal);
1109 parent->sub_menus = eina_list_prepend(parent->sub_menus, internal);
1116 * @param parent: The parent menu
1117 * @param xml: The xml tree
1118 * @return Returns 1 on success or 0 on failure
1119 * @brief Handles the AppDir tag
1122 efreet_menu_handle_app_dir(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1125 Efreet_Menu_App_Dir *app_dir;
1127 if (!parent || !xml) return 0;
1129 efreet_menu_create_app_dirs_list(parent);
1130 path = efreet_menu_path_get(parent, xml->text);
1131 if (!path) return 0;
1133 /* we've already got this guy in our list we can skip it */
1134 if (eina_list_search_unsorted(parent->app_dirs,
1135 (Eina_Compare_Cb)efreet_menu_cb_app_dirs_compare,
1142 app_dir = efreet_menu_app_dir_new();
1143 app_dir->path = path;
1145 parent->app_dirs = eina_list_prepend(parent->app_dirs, app_dir);
1152 * @param parent: The parent menu
1153 * @param xml: UNUSED
1154 * @return Returns 1 on success or 0 on failure
1155 * @brief Handles the DefaultAppDirs
1158 efreet_menu_handle_default_app_dirs(Efreet_Menu_Internal *parent, Efreet_Xml *xml __UNUSED__)
1160 Eina_List *prepend = NULL;
1164 if (!parent) return 0;
1166 efreet_menu_create_app_dirs_list(parent);
1167 dirs = efreet_default_dirs_get(efreet_data_home_get(), efreet_data_dirs_get(),
1169 EINA_LIST_FREE(dirs, dir)
1171 Efreet_Menu_App_Dir *app_dir;
1173 if (eina_list_search_unsorted(parent->app_dirs,
1174 (Eina_Compare_Cb)efreet_menu_cb_app_dirs_compare,
1178 app_dir = efreet_menu_app_dir_new();
1179 app_dir->path = strdup(dir);
1181 prepend = eina_list_append(prepend, app_dir);
1185 parent->app_dirs = eina_list_merge(prepend, parent->app_dirs);
1192 * @param parent: The parent menu
1193 * @param xml: The xml tree
1194 * @return Returns 1 on success or 0 on failure
1195 * @brief Handles the DirectoryDir tag
1198 efreet_menu_handle_directory_dir(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1202 if (!parent || !xml) return 0;
1204 efreet_menu_create_directory_dirs_list(parent);
1205 path = efreet_menu_path_get(parent, xml->text);
1206 if (!path) return 0;
1208 /* we've already got this guy in our list we can skip it */
1209 if (ecore_list_find(parent->directory_dirs, ECORE_COMPARE_CB(strcmp), path))
1215 ecore_dlist_prepend(parent->directory_dirs, path);
1222 * @param parent: The parent menu
1223 * @param xml: UNUSED
1224 * @return Returns 1 on success or 0 on failure
1225 * @brief Handles the DefaultDirectoryDirs tag
1228 efreet_menu_handle_default_directory_dirs(Efreet_Menu_Internal *parent, Efreet_Xml *xml __UNUSED__)
1230 Eina_List *dirs, *l;
1233 if (!parent) return 0;
1235 efreet_menu_create_directory_dirs_list(parent);
1236 dirs = efreet_default_dirs_get(efreet_data_home_get(), efreet_data_dirs_get(),
1237 "desktop-directories");
1238 EINA_LIST_FOREACH(dirs, l, dir)
1240 if (ecore_list_find(parent->directory_dirs, ECORE_COMPARE_CB(strcmp), dir))
1243 ecore_dlist_prepend(parent->directory_dirs, strdup(dir));
1248 free(eina_list_data_get(dirs));
1249 dirs = eina_list_remove_list(dirs, dirs);
1257 * @param parent: The parent Menu
1258 * @param xml: The xml to work with
1259 * @return Returns 1 on success or 0 on failure
1260 * @brief Sets the menu name from the given XML fragment.
1263 efreet_menu_handle_name(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1265 /* not allowed to have two Name settings in a menu */
1266 if (parent->name.internal)
1268 printf("efreet_menu_handle_name() setting second name into menu\n");
1272 /* ignore the name if it contains a / */
1273 if (strchr(xml->text, '/')) return 1;
1275 parent->name.internal = eina_stringshare_add(xml->text);
1282 * @param parent: The parent menu
1283 * @param xml: The xml tree
1284 * @return Returns 1 on success or 0 on failure
1285 * @brief Handles the Directory tag
1287 * This just adds the given directory path to a list which we'll walk once
1288 * we've traversed the entire menu into memory.
1291 efreet_menu_handle_directory(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1293 if (!parent || !xml) return 0;
1295 efreet_menu_create_directories_list(parent);
1296 ecore_dlist_prepend(parent->directories, strdup(xml->text));
1303 * @param parent: The parent menu
1304 * @param xml: The xml tree
1305 * @return Returns 1 on success or 0 on failure
1306 * @brief Handles the OnlyUnallocated tag
1309 efreet_menu_handle_only_unallocated(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1311 if (!parent || !xml) return 0;
1313 /* a later instance has been seen so we can ignore this one */
1314 if (parent->seen_allocated) return 1;
1316 parent->seen_allocated = 1;
1317 parent->only_unallocated = 1;
1324 * @param parent: The parent menu
1325 * @param xml: The xml tree
1326 * @return Returns 1 on success or 0 on failure
1327 * @brief Handles the NotOnlyUnallocated tag
1330 efreet_menu_handle_not_only_unallocated(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1332 if (!parent || !xml) return 0;
1334 /* a later instance has been seen so we can ignore this one */
1335 if (parent->seen_allocated) return 1;
1337 parent->seen_allocated = 1;
1338 parent->only_unallocated = 0;
1345 * @param parent: The parent menu
1346 * @param xml: The xml tree
1347 * @return Returns 1 on success or 0 on failure
1348 * @brief Handles the Deleted tag
1351 efreet_menu_handle_deleted(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1353 if (!parent || !xml) return 0;
1355 /* a later instance has been seen so we can ignore this one */
1356 if (parent->seen_deleted) return 1;
1358 parent->seen_deleted = 1;
1359 parent->deleted = 1;
1366 * @param parent: The parent menu
1367 * @param xml: The xml tree
1368 * @return Returns 1 on success or 0 on failure
1369 * @brief Handles the NotDeleted tag
1372 efreet_menu_handle_not_deleted(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1374 if (!parent || !xml) return 0;
1376 /* a later instance has been seen so we can ignore this one */
1377 if (parent->seen_deleted) return 1;
1379 parent->seen_deleted = 1;
1380 parent->deleted = 0;
1387 * @param parent: The parent menu
1388 * @param xml: The XML tree to work with
1389 * @return Returns 1 on success or 0 on failure
1390 * @brief Handles parsing the Include tag and all subtags
1393 efreet_menu_handle_include(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1395 return efreet_menu_handle_filter(parent, xml,
1396 EFREET_MENU_FILTER_INCLUDE);
1401 * @param parent: The parent menu
1402 * @param xml: The xml tree
1403 * @return Returns 1 on success or 0 on failure
1404 * @brief Handles the Exclude tag and all subtags
1407 efreet_menu_handle_exclude(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1409 return efreet_menu_handle_filter(parent, xml,
1410 EFREET_MENU_FILTER_EXCLUDE);
1415 * @param op: The filter operation
1416 * @param xml: The xml tree
1417 * @return Returns 1 on success or 0 on failure
1418 * @brief Handles the Filename tag
1421 efreet_menu_handle_filename(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml)
1423 if (!op || !xml) return 0;
1425 op->filenames = eina_list_append(op->filenames, strdup(xml->text));
1432 * @param op: The filter operation
1433 * @param xml: The xml tree
1434 * @return Returns 1 on success or 0 on failure
1435 * @brief Handles the Category tag
1438 efreet_menu_handle_category(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml)
1440 if (!op || !xml) return 0;
1443 op->categories = eina_list_append(op->categories, strdup(xml->text));
1450 * @param op: The filter operation
1451 * @param xml: The xml tree
1452 * @return Returns 1 on success or 0 on failure
1453 * @brief Handles the All tag and all subtags
1456 efreet_menu_handle_all(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml)
1458 if (!op || !xml) return 0;
1467 * @param op: The filter operation
1468 * @param xml: The xml tree
1469 * @return Returns 1 on success or 0 on failure
1470 * @brief Handles the And tag and all subtags
1473 efreet_menu_handle_and(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml)
1475 if (!op || !xml) return 0;
1477 return efreet_menu_handle_filter_child_op(op, xml,
1478 EFREET_MENU_FILTER_OP_AND);
1483 * @param op: The filter operation
1484 * @param xml: The xml tree
1485 * @return Returns 1 on success or 0 on failure
1486 * @brief Handles the Or tag and all subtags
1489 efreet_menu_handle_or(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml)
1491 if (!op || !xml) return 0;
1493 return efreet_menu_handle_filter_child_op(op, xml,
1494 EFREET_MENU_FILTER_OP_OR);
1499 * @param op: The filter operation
1500 * @param xml: The xml tree
1501 * @return Returns 1 on success or 0 on failure
1502 * @brief Handles the Not tag and all subtags
1505 efreet_menu_handle_not(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml)
1507 if (!op || !xml) return 0;
1509 return efreet_menu_handle_filter_child_op(op, xml,
1510 EFREET_MENU_FILTER_OP_NOT);
1515 * @param parent: The parent menu
1516 * @param xml: The xml tree
1517 * @return Returns 1 on success or 0 on failure
1518 * @brief Handles the MergeFile tag
1521 efreet_menu_handle_merge_file(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1525 const char *attr = NULL;
1529 if (!parent || !xml) return 0;
1531 /* check to see if this is a path or parent type */
1532 attr = efreet_xml_attribute_get(xml, "type");
1533 if (attr && !strcmp(attr, "parent"))
1536 /* we're given a path */
1538 path = efreet_menu_path_get(parent, xml->text);
1540 /* need to find the next menu with the same name as ours in the config
1541 * dir after ours (if we're in a config dir) */
1544 Eina_List *search_dirs;
1545 const char *dir, *p;
1548 if (!parent->file.path)
1550 printf("efreet_menu_handle_merge_file() missing menu path ...\n");
1554 search_dirs = efreet_config_dirs_get();
1556 /* we need to find the next menu with the same name in the directory
1557 * after the on the the menu was found in. to do that we first check
1558 * if it's in the config_home_directory() if so we need to search
1559 * all of the dirs. If it isn't in the config home directory then we
1560 * scan the search dirs and look for it. The search_dirs list will
1561 * be left at the next pointer so we can start looking for the menu
1562 * from that point */
1564 dir = efreet_config_home_get();
1566 if (strncmp(dir, parent->file.path, len))
1568 EINA_LIST_FOREACH(search_dirs, l, dir)
1570 if (!strncmp(dir, parent->file.path, len))
1577 printf("efreet_menu_handle_merge_file() failed to find "
1578 "menu parent directory\n");
1582 /* the parent file path may have more path then just the base
1583 * directory so we need to append that as well */
1584 p = parent->file.path + len;
1586 /* whatever dirs are left in the search dir we need to look for the
1587 * menu with the same relative filename */
1588 EINA_LIST_FOREACH(search_dirs, l, dir)
1590 char file[PATH_MAX];
1592 snprintf(file, sizeof(file), "%s/%s/%s", dir, (p ? p : ""),
1594 if (ecore_file_exists(file))
1596 path = strdup(file);
1602 /* nothing to do if no file found */
1603 if (!path) return 1;
1605 if (!efreet_menu_merge(parent, xml, path))
1615 * @param parent: The parent menu to merge into
1616 * @param xml: The XML to be merged
1617 * @param path: The path to the .menu file to merge
1620 efreet_menu_merge(Efreet_Menu_Internal *parent, Efreet_Xml *xml, const char *path)
1622 Efreet_Xml *merge_xml;
1623 Efreet_Menu_Internal *internal;
1626 if (!parent || !xml || !path) return 0;
1628 /* do nothing if the file doesn't exist */
1629 if (!ecore_file_exists(path)) return 1;
1631 realpath = ecore_file_realpath(path);
1632 if (realpath[0] == '\0')
1634 printf("efreet_menu_merge() unable to get real path for %s\n", path);
1638 /* don't merge the same path twice */
1639 if (eina_hash_find(efreet_merged_menus, realpath))
1642 eina_hash_add(efreet_merged_menus, realpath, (void *)1);
1644 merge_xml = efreet_xml_new(realpath);
1649 printf("efreet_menu_merge() failed to read in the "
1650 "merge file (%s)\n", realpath);
1654 internal = efreet_menu_internal_new();
1655 efreet_menu_path_set(internal, path);
1656 efreet_menu_handle_menu(internal, merge_xml);
1657 efreet_menu_concatenate(parent, internal);
1658 efreet_menu_internal_free(internal);
1660 efreet_xml_del(merge_xml);
1667 * @param parent: The parent menu
1668 * @param xml: The xml tree
1669 * @return Returns 1 on success or 0 on failure
1670 * @brief Handles the MergeDir tag
1673 efreet_menu_handle_merge_dir(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1678 if (!parent || !xml || !xml->text) return 0;
1680 path = efreet_menu_path_get(parent, xml->text);
1681 if (!path) return 1;
1682 if (!ecore_file_exists(path))
1688 ret = efreet_menu_merge_dir(parent, xml, path);
1696 * @param parent: the parent menu of the merge
1697 * @param xml: The xml tree
1698 * @param path: The path to the merge directory
1699 * @return Returns 1 on success or 0 on failure
1700 * @brief Find all of the .menu files in the given directory and merge them
1701 * into the @a parent menu.
1704 efreet_menu_merge_dir(Efreet_Menu_Internal *parent, Efreet_Xml *xml, const char *path)
1706 char dir_path[PATH_MAX];
1708 struct dirent *file;
1710 if (!parent || !xml || !path) return 0;
1712 /* check to see if we've merged this directory already */
1713 if (eina_hash_find(efreet_merged_dirs, path)) return 1;
1714 eina_hash_add(efreet_merged_dirs, path, (void *)1);
1716 files = opendir(path);
1717 if (!files) return 1;
1719 while ((file = readdir(files)))
1723 if (!strcmp(file->d_name, ".") || !strcmp(file->d_name, "..")) continue;
1724 p = strrchr(file->d_name, '.');
1726 if (strcmp(p, ".menu")) continue;
1728 snprintf(dir_path, PATH_MAX, "%s/%s", path, file->d_name);
1729 if (!efreet_menu_merge(parent, xml, dir_path))
1742 * @param parent: The parent menu
1743 * @param xml: The xml tree
1744 * @return Returns 1 on success or 0 on failure
1745 * @brief Handles the DefaultMergeDirs tag
1748 efreet_menu_handle_default_merge_dirs(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1751 char path[PATH_MAX], *p;
1754 if (!parent || !xml) return 0;
1756 prefix = efreet_menu_prefix_get();
1757 if (!strcmp(prefix, "gnome-") &&
1758 (!strcmp(parent->file.name, "gnome-applications.menu")))
1759 p = strdup("applications");
1761 else if ((!strcmp(prefix, "kde-") &&
1762 (!strcmp(parent->file.name, "kde-applications.menu"))))
1763 p = strdup("applications");
1769 p = strdup(parent->file.name);
1770 s = strrchr(p, '.');
1773 snprintf(path, sizeof(path), "menus/%s-merged", p);
1776 dirs = efreet_default_dirs_get(efreet_config_home_get(),
1777 efreet_config_dirs_get(), path);
1779 while ((p = eina_list_data_get(dirs)))
1781 dirs = eina_list_remove_list(dirs, dirs);
1782 efreet_menu_merge_dir(parent, xml, p);
1791 * @param parent: The parent menu
1792 * @param xml: The xml tree
1793 * @return Returns 1 on success or 0 on failure
1794 * @brief Handles the LegacyDir tag
1797 efreet_menu_handle_legacy_dir(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
1799 Efreet_Menu_Internal *legacy;
1801 if (!parent || !xml) return 0;
1803 legacy = efreet_menu_handle_legacy_dir_helper(NULL, parent, xml->text,
1804 efreet_xml_attribute_get(xml, "prefix"));
1805 efreet_menu_concatenate(parent, legacy);
1806 efreet_menu_internal_free(legacy);
1814 * @param parent: The parent menu
1815 * @param legacy_dir: The legacy directory path
1816 * @param prefix: The legacy directory prefix if one set
1817 * @return Returns the Efreet_Menu_Internal representing the legacy hierarchy
1818 * @brief Handles the process of merging @a legacy_dir into @a parent menu
1820 static Efreet_Menu_Internal *
1821 efreet_menu_handle_legacy_dir_helper(Efreet_Menu_Internal *root,
1822 Efreet_Menu_Internal *parent,
1823 const char *legacy_dir,
1826 char *path, file_path[PATH_MAX];
1827 Efreet_Menu_Internal *legacy_internal;
1828 Efreet_Menu_Filter *filter;
1829 Efreet_Menu_App_Dir *app_dir;
1830 int path_len, count = 0;
1832 struct dirent *file;
1834 if (!parent || !legacy_dir) return 0;
1836 path = efreet_menu_path_get(parent, legacy_dir);
1838 /* nothing to do if the legacy path doesn't exist */
1839 if (!ecore_file_exists(path))
1845 legacy_internal = efreet_menu_internal_new();
1846 legacy_internal->name.internal = eina_stringshare_add(ecore_file_file_get(path));
1848 /* add the legacy dir as an app dir */
1849 app_dir = efreet_menu_app_dir_new();
1850 app_dir->path = strdup(path);
1851 app_dir->legacy = 1;
1852 if (prefix && !strchr(prefix, '/')) app_dir->prefix = strdup(prefix);
1854 efreet_menu_create_app_dirs_list(legacy_internal);
1855 legacy_internal->app_dirs = eina_list_append(legacy_internal->app_dirs, app_dir);
1859 /* XXX This seems wrong, but it makes efreet pass the fdo tests */
1860 app_dir = efreet_menu_app_dir_new();
1861 app_dir->path = strdup(path);
1862 app_dir->legacy = 1;
1863 if (prefix && !strchr(prefix, '/')) app_dir->prefix = strdup(prefix);
1864 root->app_dirs = eina_list_append(root->app_dirs, app_dir);
1868 /* add the legacy dir as a directory dir */
1869 efreet_menu_create_directory_dirs_list(legacy_internal);
1870 ecore_dlist_append(legacy_internal->directory_dirs, strdup(path));
1872 /* setup a filter for all the conforming .desktop files in the legacy
1874 filter = efreet_menu_filter_new();
1875 filter->type = EFREET_MENU_FILTER_INCLUDE;
1877 filter->op->type = EFREET_MENU_FILTER_OP_OR;
1879 efreet_menu_create_filter_list(legacy_internal);
1880 legacy_internal->filters = eina_list_append(legacy_internal->filters, filter);
1882 path_len = strlen(path);
1883 files = opendir(path);
1884 while ((file = readdir(files)))
1886 Efreet_Desktop *desktop = NULL;
1890 if (!strcmp(file->d_name, ".") || !strcmp(file->d_name, "..")) continue;
1891 file_path[0] = '\0';
1892 ecore_strlcpy(file_path, path, PATH_MAX);
1893 ecore_strlcpy(file_path + path_len, "/", PATH_MAX - path_len);
1894 ecore_strlcpy(file_path + path_len + 1, file->d_name, PATH_MAX - path_len - 1);
1896 /* recurse into sub directories */
1897 if (ecore_file_is_dir(file_path))
1899 Efreet_Menu_Internal *ret;
1901 ret = efreet_menu_handle_legacy_dir_helper(root ? root : legacy_internal,
1902 legacy_internal, file_path, prefix);
1905 efreet_menu_filter_free(filter);
1906 efreet_menu_internal_free(legacy_internal);
1912 efreet_menu_create_sub_menu_list(legacy_internal);
1913 legacy_internal->sub_menus = eina_list_prepend(legacy_internal->sub_menus, ret);
1918 if (!strcmp(file->d_name, ".directory"))
1920 legacy_internal->directory = efreet_desktop_get(file_path);
1921 if (legacy_internal->directory
1922 && legacy_internal->directory->type != EFREET_DESKTOP_TYPE_DIRECTORY)
1924 efreet_desktop_free(legacy_internal->directory);
1925 legacy_internal->directory = NULL;
1930 exten = strrchr(file->d_name, '.');
1932 if (exten && !strcmp(exten, ".desktop"))
1933 desktop = efreet_desktop_get(file_path);
1935 if (!desktop) continue;
1937 /* if the .desktop has categories it isn't legacy */
1938 if (efreet_desktop_category_count_get(desktop) != 0)
1941 /* XXX: This will disappear when the .desktop is free'd */
1942 efreet_desktop_category_add(desktop, "Legacy");
1946 snprintf(buf, PATH_MAX, "%s%s", prefix, file->d_name);
1947 filter->op->filenames = eina_list_append(filter->op->filenames, strdup(buf));
1950 filter->op->filenames = eina_list_append(filter->op->filenames, strdup(file->d_name));
1953 efreet_desktop_free(desktop);
1958 efreet_menu_filter_free(filter);
1959 return legacy_internal;
1964 * @param parent: The parent menu
1965 * @param xml: UNUSED
1966 * @return Returns 1 on success or 0 on failure
1967 * @brief Handles the KDELegacyDirs tag
1970 efreet_menu_handle_kde_legacy_dirs(Efreet_Menu_Internal *parent, Efreet_Xml *xml __UNUSED__)
1975 if (!parent) return 0;
1977 if (!efreet_menu_kde_legacy_dirs) return 1;
1979 /* XXX if one _helper() call succeeds, we return success. should this be flipped?
1980 * (return fail if on of them failed) */
1981 EINA_LIST_FOREACH(efreet_menu_kde_legacy_dirs, l, dir)
1983 Efreet_Menu_Internal *kde;
1985 kde = efreet_menu_handle_legacy_dir_helper(NULL, parent, dir, "kde");
1988 efreet_menu_concatenate(parent, kde);
1989 efreet_menu_internal_free(kde);
1999 * @param parent: The parent menu
2000 * @param xml: The xml tree
2001 * @return Returns 1 on success or 0 on failure
2002 * @brief Handles the Move tag and all subtags
2005 efreet_menu_handle_move(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
2009 if (!parent || !xml) return 0;
2011 efreet_menu_create_move_list(parent);
2013 ecore_list_first_goto(xml->children);
2014 while ((child = ecore_list_next(xml->children)))
2016 int (*cb)(Efreet_Menu_Internal *parent, Efreet_Xml *xml);
2018 cb = eina_hash_find(efreet_menu_move_cbs, child->tag);
2021 if (!cb(parent, child))
2026 printf("efreet_menu_handle_move() unknown tag found "
2027 "in Move (%s)\n", child->tag);
2032 parent->current_move = NULL;
2039 * @param parent: The parent menu
2040 * @param xml: The xml tree
2041 * @return Returns 1 on success or 0 on failure
2042 * @brief Handles the Old tag
2045 efreet_menu_handle_old(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
2047 Efreet_Menu_Move *move;
2049 if (!parent || !xml || !xml->text) return 0;
2051 if (parent->current_move)
2053 printf("efreet_menu_handle_old() saw second <Old> "
2054 "before seeing <New>\n");
2058 /* If we already moved this menu, remove the old move */
2059 /* XXX This seems wrong, but it makes efreet pass the fdo tests */
2061 move = eina_list_search_unsorted(parent->moves,
2062 (Eina_Compare_Cb)efreet_menu_cb_move_compare,
2066 efreet_menu_move_free(move);
2067 parent->moves = eina_list_remove(parent->moves, move);
2071 move = efreet_menu_move_new();
2072 move->old_name = strdup(xml->text);
2074 parent->current_move = move;
2075 parent->moves = eina_list_append(parent->moves, move);
2082 * @param parent: The parent menu
2083 * @param xml: The xml tree
2084 * @return Returns 1 on success or 0 on failure
2085 * @brief Handles the New tag
2088 efreet_menu_handle_new(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
2090 if (!parent || !xml || !xml->text) return 0;
2092 if (!parent->current_move)
2094 printf("efreet_menu_handle_new() saw New before seeing Old\n");
2098 parent->current_move->new_name = strdup(xml->text);
2099 parent->current_move = NULL;
2106 * @param parent: The parent menu
2107 * @param xml: The xml tree
2108 * @return Returns 1 on success or 0 on failure
2109 * @brief Handles the Layout tag and all subtags
2112 efreet_menu_handle_layout(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
2116 if (!parent || !xml) return 0;
2118 /* We use the last existing layout */
2119 if (parent->layout) return 1;
2121 efreet_menu_create_layout_list(parent);
2123 ecore_list_first_goto(xml->children);
2124 while ((child = ecore_list_next(xml->children)))
2126 int (*cb)(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def);
2128 cb = eina_hash_find(efreet_menu_layout_cbs, child->tag);
2131 if (!cb(parent, child, 0))
2136 printf("efreet_menu_handle_move() unknown tag found "
2137 "in Layout (%s)\n", child->tag);
2147 * @param parent: The parent menu
2148 * @param xml: The xml tree
2149 * @return Returns 1 on success or 0 on failure
2150 * @brief Handles the DefaultLayout tag
2153 efreet_menu_handle_default_layout(Efreet_Menu_Internal *parent, Efreet_Xml *xml)
2158 if (!parent || !xml) return 0;
2160 /* We use the last existing layout */
2161 if (parent->default_layout) return 1;
2163 val = efreet_xml_attribute_get(xml, "show_empty");
2164 if (val) parent->show_empty = !strcmp(val, "true");
2166 val = efreet_xml_attribute_get(xml, "inline");
2167 if (val) parent->in_line = !strcmp(val, "true");
2169 val = efreet_xml_attribute_get(xml, "inline_limit");
2170 if (val) parent->inline_limit = atoi(val);
2172 val = efreet_xml_attribute_get(xml, "inline_header");
2173 if (val) parent->inline_header = !strcmp(val, "true");
2175 val = efreet_xml_attribute_get(xml, "inline_alias");
2176 if (val) parent->inline_alias = !strcmp(val, "true");
2178 efreet_menu_create_default_layout_list(parent);
2180 ecore_list_first_goto(xml->children);
2181 while ((child = ecore_list_next(xml->children)))
2183 int (*cb)(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def);
2185 cb = eina_hash_find(efreet_menu_layout_cbs, child->tag);
2188 if (!cb(parent, child, 1))
2193 printf("efreet_menu_handle_move() unknown tag found in "
2194 "DefaultLayout (%s)\n", child->tag);
2203 efreet_menu_handle_layout_menuname(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def)
2205 Efreet_Menu_Layout *layout;
2208 if (!parent || !xml) return 0;
2212 printf("efreet_menu_handle_layout_menuname() The Menuname tag in "
2213 "layout needs a filename.\n");
2217 layout = efreet_menu_layout_new();
2218 layout->type = EFREET_MENU_LAYOUT_MENUNAME;
2219 layout->name = strdup(xml->text);
2221 val = efreet_xml_attribute_get(xml, "show_empty");
2222 if (val) layout->show_empty = !strcmp(val, "true");
2224 val = efreet_xml_attribute_get(xml, "inline");
2225 if (val) layout->in_line = !strcmp(val, "true");
2227 val = efreet_xml_attribute_get(xml, "inline_limit");
2228 if (val) layout->inline_limit = atoi(val);
2230 val = efreet_xml_attribute_get(xml, "inline_header");
2231 if (val) layout->inline_header = !strcmp(val, "true");
2233 val = efreet_xml_attribute_get(xml, "inline_alias");
2234 if (val) layout->inline_alias = !strcmp(val, "true");
2236 if (def) parent->default_layout = eina_list_append(parent->default_layout, layout);
2237 else parent->layout = eina_list_append(parent->layout, layout);
2243 efreet_menu_handle_layout_filename(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def)
2245 Efreet_Menu_Layout *layout;
2247 if (!parent || !xml) return 0;
2251 printf("efreet_menu_handle_layout_filename() The Filename tag in "
2252 "layout needs a filename.\n");
2256 layout = efreet_menu_layout_new();
2257 layout->type = EFREET_MENU_LAYOUT_FILENAME;
2258 layout->name = strdup(xml->text);
2260 if (def) parent->default_layout = eina_list_append(parent->default_layout, layout);
2261 else parent->layout = eina_list_append(parent->layout, layout);
2267 efreet_menu_handle_layout_separator(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def)
2269 Efreet_Menu_Layout *layout;
2271 if (!parent || !xml) return 0;
2273 layout = efreet_menu_layout_new();
2274 layout->type = EFREET_MENU_LAYOUT_SEPARATOR;
2276 parent->default_layout = eina_list_append(parent->default_layout, layout);
2278 parent->layout = eina_list_append(parent->layout, layout);
2283 efreet_menu_handle_layout_merge(Efreet_Menu_Internal *parent, Efreet_Xml *xml, int def)
2285 Efreet_Menu_Layout *layout;
2288 if (!parent || !xml) return 0;
2290 attr = efreet_xml_attribute_get(xml, "type");
2293 printf("efreet_menu_handle_layout_merge() The Merge tag in layout "
2294 "needs a type attribute.\n");
2298 if (strcmp(attr, "files") && strcmp(attr, "menus") && strcmp(attr, "all"))
2300 printf("efreet_menu_handle_layout_merge() The type attribute for "
2301 "the Merge tag contains an unknown value (%s).\n", attr);
2305 layout = efreet_menu_layout_new();
2306 layout->type = EFREET_MENU_LAYOUT_MERGE;
2307 layout->name = strdup(attr);
2309 if (def) parent->default_layout = eina_list_append(parent->default_layout, layout);
2310 else parent->layout = eina_list_append(parent->layout, layout);
2317 * @param parent: The parent menu
2318 * @param xml: The XML tree to parse
2319 * @param type: The type of filter
2320 * @return Returns 1 on success or 0 on failure
2321 * @brief Parses the given XML tree and adds the filter to the parent menu
2324 efreet_menu_handle_filter(Efreet_Menu_Internal *parent, Efreet_Xml *xml,
2325 Efreet_Menu_Filter_Type type)
2327 Efreet_Menu_Filter *filter;
2329 efreet_menu_create_filter_list(parent);
2331 /* filters have a default or relationship */
2332 filter = efreet_menu_filter_new();
2333 filter->type = type;
2334 filter->op->type = EFREET_MENU_FILTER_OP_OR;
2336 if (!efreet_menu_handle_filter_op(filter->op, xml))
2338 efreet_menu_filter_free(filter);
2342 parent->filters = eina_list_prepend(parent->filters, filter);
2349 * @param op: The operation to work with
2350 * @param xml: The XML tree representing this operation
2351 * @return Returns 1 on success or 0 on failure
2352 * @brief Parses the given XML tree and populates the operation
2355 efreet_menu_handle_filter_op(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml)
2359 ecore_list_first_goto(xml->children);
2360 while ((child = ecore_list_next(xml->children)))
2362 int (*cb)(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml);
2364 cb = eina_hash_find(efreet_menu_filter_cbs, child->tag);
2372 printf("efreet_menu_handle_filter_op() unknown tag in filter (%s)\n", child->tag);
2381 * @return Returns a new Efreet_Menu_Filter on success or NULL on failure
2382 * @brief Creates and initializes an Efreet_Menu_Filter object
2384 static Efreet_Menu_Filter *
2385 efreet_menu_filter_new(void)
2387 Efreet_Menu_Filter *filter;
2389 filter = NEW(Efreet_Menu_Filter, 1);
2390 filter->op = efreet_menu_filter_op_new();
2402 * @param filter: The filter to work with
2403 * @return Returns no data
2404 * @brief Frees the given filter and all data
2407 efreet_menu_filter_free(Efreet_Menu_Filter *filter)
2409 if (!filter) return;
2411 if (filter->op) efreet_menu_filter_op_free(filter->op);
2419 * @return Returns a new Efreet_Menu_Layout on success or NULL on failure
2420 * @brief Creates and initializes an Efreet_Menu_Layout object
2422 static Efreet_Menu_Layout *
2423 efreet_menu_layout_new(void)
2425 Efreet_Menu_Layout *layout;
2427 layout = NEW(Efreet_Menu_Layout, 1);
2428 layout->show_empty = -1;
2429 layout->in_line = -1;
2430 layout->inline_limit = -1;
2431 layout->inline_header = -1;
2432 layout->inline_alias = -1;
2439 * @return Returns a new Efreet_Menu_Filter_Op on success or NULL on failure
2440 * @brief Creates and initializes an Efreet_Menu_Filter_Op structure
2442 static Efreet_Menu_Filter_Op *
2443 efreet_menu_filter_op_new(void)
2445 Efreet_Menu_Filter_Op *op;
2447 op = NEW(Efreet_Menu_Filter_Op, 1);
2454 * @param op: The operation to work with
2455 * @return Returns no value.
2456 * @brief Frees the given operation and all sub data
2459 efreet_menu_filter_op_free(Efreet_Menu_Filter_Op *op)
2463 IF_FREE_LIST(op->categories);
2464 IF_FREE_LIST(op->filenames);
2465 IF_FREE_LIST(op->filters);
2472 * @return Returns a new Efreet_Menu_Desktop on success or NULL on failure
2473 * @brief Creates and returns an Efreet_Menu_Desktop
2475 static Efreet_Menu_Desktop *
2476 efreet_menu_desktop_new(void)
2478 Efreet_Menu_Desktop *md;
2480 md = NEW(Efreet_Menu_Desktop, 1);
2487 * @param md: The Efreet_Menu_Desktop to free
2488 * @return Returns no value
2489 * @brief Frees the given structure
2492 efreet_menu_desktop_free(Efreet_Menu_Desktop *md)
2495 if (md->desktop) efreet_desktop_free(md->desktop);
2501 * @return Returns a new Efreet_Menu on success or NULL on failure
2502 * @brief Creates and returns an Efreet_Menu
2504 static Efreet_Menu *
2505 efreet_menu_entry_new(void)
2509 entry = NEW(Efreet_Menu, 1);
2516 * @param entry: The Efreet_Menu to free
2517 * @return Returns no value
2518 * @brief Frees the given structure
2521 efreet_menu_free(Efreet_Menu *entry)
2523 IF_RELEASE(entry->name);
2524 IF_RELEASE(entry->icon);
2525 IF_FREE_LIST(entry->entries);
2526 IF_RELEASE(entry->id);
2527 if (entry->desktop) efreet_desktop_free(entry->desktop);
2533 * @param op: The op to add a child too
2534 * @param xml: The XML tree of the child
2535 * @param type: The type of child to add
2536 * @return Returns 1 on success or 0 on failure
2537 * @brief Parses the given XML tree and populates a new child operation.
2540 efreet_menu_handle_filter_child_op(Efreet_Menu_Filter_Op *op, Efreet_Xml *xml,
2541 Efreet_Menu_Filter_Op_Type type)
2543 Efreet_Menu_Filter_Op *child_op;
2545 child_op = efreet_menu_filter_op_new();
2546 child_op->type = type;
2548 if (!efreet_menu_handle_filter_op(child_op, xml))
2550 efreet_menu_filter_op_free(child_op);
2554 op->filters = eina_list_append(op->filters, child_op);
2561 * @param menu: The menu to work with
2562 * @param only_unallocated: Do we only look for unallocated items?
2563 * @return Returns 1 if we've successfully processed the menu, 0 otherwise
2564 * @brief Handles the processing of the menu data to retrieve the .desktop
2565 * files for the menu
2568 efreet_menu_process(Efreet_Menu_Internal *internal, unsigned int only_unallocated)
2572 /* a menu _MUST_ have a name */
2573 if (!internal->name.internal || (internal->name.internal[0] == '\0'))
2576 /* handle filtering out .desktop files as needed. This deals with all
2578 efreet_menu_process_filters(internal, only_unallocated);
2580 if (internal->sub_menus)
2582 Efreet_Menu_Internal *sub_internal;
2584 EINA_LIST_FOREACH(internal->sub_menus, l, sub_internal)
2586 sub_internal->parent = internal;
2587 efreet_menu_process(sub_internal, only_unallocated);
2594 /* This will walk through all of the app dirs and load all the .desktop
2595 * files into the cache for the menu. The .desktop files will have their
2596 * allocated flag set to 0 */
2598 efreet_menu_process_dirs(Efreet_Menu_Internal *internal)
2602 /* Scan application directories for .desktop files */
2603 if (!efreet_menu_app_dirs_process(internal))
2606 /* Scan directory directories for .directory file */
2607 if (!efreet_menu_directory_dirs_process(internal))
2610 if (internal->sub_menus)
2612 Efreet_Menu_Internal *sub_internal;
2614 EINA_LIST_FOREACH(internal->sub_menus, l, sub_internal)
2616 sub_internal->parent = internal;
2617 efreet_menu_process_dirs(sub_internal);
2626 * @param menu: the menu to process
2627 * @param only_unallocated: Only handle menus taht deal with unallocated items
2628 * @return Returns no value
2629 * @brief Handles the processing of the filters attached to the given menu.
2631 * For each include filter we'll add the items to our applications array. Each
2632 * exclude filter will remove items from the applications array
2635 efreet_menu_process_filters(Efreet_Menu_Internal *internal, unsigned int only_unallocated)
2637 Efreet_Menu_Filter *filter;
2638 Efreet_Menu_Desktop *md;
2643 /* nothing to do if we're checking the other option */
2644 if (only_unallocated != internal->only_unallocated) return;
2646 while (internal->applications)
2647 internal->applications = eina_list_remove_list(internal->applications,
2648 internal->applications);
2650 if (!internal->filters) return;
2652 EINA_LIST_FOREACH(internal->filters, l, filter)
2654 /* skip excludes until we get an include */
2655 if (!included && (filter->type == EFREET_MENU_FILTER_EXCLUDE))
2659 if (filter->type == EFREET_MENU_FILTER_INCLUDE)
2663 matches = eina_hash_string_superfast_new(NULL);
2664 internal->applications = efreet_menu_process_app_pool(internal->app_pool, internal->applications,
2665 matches, filter, internal->only_unallocated);
2666 if (internal->parent)
2668 Efreet_Menu_Internal *parent;
2670 parent = internal->parent;
2672 internal->applications = efreet_menu_process_app_pool(parent->app_pool,
2673 internal->applications, matches, filter,
2674 internal->only_unallocated);
2675 } while ((parent = parent->parent));
2677 eina_hash_free(matches);
2681 /* check each item in our menu so far and see if it's excluded */
2682 l = internal->applications;
2683 while ((md = eina_list_data_get(l)))
2685 ll = eina_list_next(l);
2686 if (efreet_menu_filter_matches(filter->op, md))
2687 internal->applications = eina_list_remove_list(internal->applications, l);
2693 /* sort the menu applications. we do this in process filters so it will only
2694 * be done once per menu.*/
2695 if (internal->applications)
2699 count = eina_list_count(internal->applications);
2703 Efreet_Menu_Desktop *md;
2705 sheap = ecore_sheap_new(
2706 ECORE_COMPARE_CB(efreet_menu_cb_md_compare), count);
2707 while ((md = eina_list_data_get(internal->applications)))
2709 internal->applications = eina_list_remove_list(internal->applications,
2710 internal->applications);
2711 ecore_sheap_insert(sheap, md);
2713 while ((md = ecore_sheap_extract(sheap)))
2715 if (md->desktop->no_display) continue;
2716 internal->applications = eina_list_append(internal->applications, md);
2719 ecore_sheap_destroy(sheap);
2726 * @param pool: The app pool to iterate
2727 * @param applications: The list of applications to append too
2728 * @param matches: The hash of previously matched ids
2729 * @param filter: The menu filter to run on the pool items
2730 * @param only_unallocated: Do we check only unallocated pool items?
2731 * @return Returns no value.
2732 * @brief This will iterate the items in @a pool and append them to @a
2733 * applications if they match the @a filter given and aren't previoulsy entered
2734 * in @a matches. If @a only_unallocated is set we'll only only at the
2735 * .desktop files that haven't been previoulsy matched
2738 efreet_menu_process_app_pool(Eina_List *pool, Eina_List *applications,
2740 Efreet_Menu_Filter *filter,
2741 unsigned int only_unallocated)
2743 Efreet_Menu_Desktop *md;
2746 if (!pool) return NULL;
2748 EINA_LIST_FOREACH(pool, l, md)
2750 if (eina_hash_find(matches, md->id)) continue;
2751 if (only_unallocated && md->allocated) continue;
2752 if (efreet_menu_filter_matches(filter->op, md))
2754 applications = eina_list_append(applications, md);
2755 eina_hash_add(matches, (void *)md->id, md);
2759 return applications;
2764 * @param op: The filter operation to execute
2765 * @param md: The desktop to run the filter on
2766 * @return Returns 1 if this desktop matches the given filter, 0 otherwise
2767 * @brief This will execute the given @a filter on the given desktop
2770 efreet_menu_filter_matches(Efreet_Menu_Filter_Op *op, Efreet_Menu_Desktop *md)
2772 if (op->type == EFREET_MENU_FILTER_OP_OR)
2773 return efreet_menu_filter_or_matches(op, md);
2775 if (op->type == EFREET_MENU_FILTER_OP_AND)
2776 return efreet_menu_filter_and_matches(op, md);
2778 if (op->type == EFREET_MENU_FILTER_OP_NOT)
2779 return efreet_menu_filter_not_matches(op, md);
2786 * @param op: The filter operation to execute
2787 * @param md: The desktop to execute on
2788 * @return Returns 1 if the desktop matches, 0 otherwise
2789 * @brief Executes the OR operation, @a op, on the desktop, @a md.
2792 efreet_menu_filter_or_matches(Efreet_Menu_Filter_Op *op, Efreet_Menu_Desktop *md)
2794 Efreet_Menu_Filter_Op *child;
2798 if (op->all) return 1;
2800 if (op->categories && md->desktop->categories)
2802 EINA_LIST_FOREACH(op->categories, l, t)
2804 if (eina_list_search_unsorted(md->desktop->categories,
2805 (Eina_Compare_Cb)strcmp, t))
2812 EINA_LIST_FOREACH(op->filenames, l, t)
2813 if (!strcmp(t, md->id)) return 1;
2818 EINA_LIST_FOREACH(op->filters, l, child)
2820 if (efreet_menu_filter_matches(child, md))
2830 * @param op: The filter operation to execute
2831 * @param md: The desktop to execute on
2832 * @return Returns 1 if the desktop matches, 0 otherwise
2833 * @brief Executes the AND operation, @a op, on the desktop, @a md.
2836 efreet_menu_filter_and_matches(Efreet_Menu_Filter_Op *op, Efreet_Menu_Desktop *md)
2838 Efreet_Menu_Filter_Op *child;
2844 if ((eina_list_count(op->categories) > 0) && !md->desktop->categories)
2847 EINA_LIST_FOREACH(op->categories, l, t)
2849 if (!eina_list_search_unsorted(md->desktop->categories,
2850 (Eina_Compare_Cb)strcmp, t))
2857 EINA_LIST_FOREACH(op->filenames, l, t)
2859 if (strcmp(t, md->id)) return 0;
2865 EINA_LIST_FOREACH(op->filters, l, child)
2867 if (!efreet_menu_filter_matches(child, md))
2877 * @param op: The filter operation to execute
2878 * @param md: The desktop to execute on
2879 * @return Returns 1 if the desktop matches, 0 otherwise
2880 * @brief Executes the NOT operation, @a op, on the desktop, @a md.
2883 efreet_menu_filter_not_matches(Efreet_Menu_Filter_Op *op, Efreet_Menu_Desktop *md)
2885 Efreet_Menu_Filter_Op *child;
2889 /* !all means no desktops match */
2890 if (op->all) return 0;
2894 if ((eina_list_count(op->categories) > 0) && !md->desktop->categories)
2897 EINA_LIST_FOREACH(op->categories, l, t)
2899 if (eina_list_search_unsorted(md->desktop->categories,
2900 (Eina_Compare_Cb)strcmp, t))
2907 EINA_LIST_FOREACH(op->filenames, l, t)
2909 if (!strcmp(t, md->id)) return 0;
2915 EINA_LIST_FOREACH(op->filters, l, child)
2917 if (efreet_menu_filter_matches(child, md))
2927 * @param dest: The destination menu
2928 * @param src: The source menu
2929 * @return Returns no value
2930 * @brief Takes the child elements of the menu @a src and puts then on the
2931 * _start_ of the menu @a dest.
2934 efreet_menu_concatenate(Efreet_Menu_Internal *dest, Efreet_Menu_Internal *src)
2936 Efreet_Menu_Internal *submenu;
2938 if (!dest || !src) return;
2940 if (!dest->directory && src->directory)
2942 dest->directory = src->directory;
2943 src->directory = NULL;
2946 if (!dest->seen_allocated && src->seen_allocated)
2948 dest->only_unallocated = src->only_unallocated;
2949 dest->seen_allocated = 1;
2952 if (!dest->seen_deleted && src->seen_deleted)
2954 dest->deleted = src->deleted;
2955 dest->seen_deleted = 1;
2958 if (src->directories)
2960 efreet_menu_create_directories_list(dest);
2961 ecore_dlist_prepend_list(dest->directories, src->directories);
2966 efreet_menu_create_app_dirs_list(dest);
2967 dest->app_dirs = eina_list_merge(src->app_dirs, dest->app_dirs);
2968 src->app_dirs = NULL;
2971 if (src->directory_dirs)
2973 efreet_menu_create_directory_dirs_list(dest);
2974 ecore_dlist_prepend_list(dest->directory_dirs, src->directory_dirs);
2979 efreet_menu_create_move_list(dest);
2980 dest->moves = eina_list_merge(src->moves, dest->moves);
2986 efreet_menu_create_filter_list(dest);
2987 dest->filters = eina_list_merge(src->filters, dest->filters);
2988 src->filters = NULL;
2993 efreet_menu_create_sub_menu_list(dest);
2995 while ((submenu = eina_list_data_get(eina_list_last(src->sub_menus))))
2997 Efreet_Menu_Internal *match;
2999 src->sub_menus = eina_list_remove(src->sub_menus, submenu);
3000 /* if this menu is in the list already we just add to that */
3001 if ((match = eina_list_search_unsorted(dest->sub_menus,
3002 (Eina_Compare_Cb)efreet_menu_cb_menu_compare,
3005 efreet_menu_concatenate(match, submenu);
3006 efreet_menu_internal_free(submenu);
3009 dest->sub_menus = eina_list_prepend(dest->sub_menus, submenu);
3016 * @param menu: The menu to work with
3017 * @return Returns no value
3018 * @brief Handles any \<Move\> commands in the menus
3021 efreet_menu_resolve_moves(Efreet_Menu_Internal *internal)
3023 Efreet_Menu_Internal *child;
3024 Efreet_Menu_Move *move;
3027 /* child moves are handled before parent moves */
3028 if (internal->sub_menus)
3030 EINA_LIST_FOREACH(internal->sub_menus, l, child)
3031 efreet_menu_resolve_moves(child);
3034 /* nothing to do if this menu has no moves */
3035 if (!internal->moves) return;
3037 EINA_LIST_FOREACH(internal->moves, l, move)
3039 Efreet_Menu_Internal *origin, *dest, *parent;
3041 /* if the origin path doesn't exist we do nothing */
3042 origin = efreet_menu_by_name_find(internal, move->old_name, &parent);
3043 if (!origin) continue;
3045 /* remove the origin menu from the parent */
3046 parent->sub_menus = eina_list_remove(parent->sub_menus, origin);
3048 /* if the destination path doesn't exist we just rename the origin
3049 * menu and append to the parents list of children */
3050 dest = efreet_menu_by_name_find(internal, move->new_name, &parent);
3053 char *path, *tmp, *t;
3055 /* if the dest path has /'s in it then we need to add menus to
3056 * fill out the paths */
3057 t = strdup(move->new_name);
3059 path = strchr(tmp, '/');
3062 Efreet_Menu_Internal *ancestor;
3066 ancestor = efreet_menu_internal_new();
3067 ancestor->name.internal = eina_stringshare_add(tmp);
3069 efreet_menu_create_sub_menu_list(parent);
3070 parent->sub_menus = eina_list_append(parent->sub_menus, ancestor);
3074 path = strchr(tmp, '/');
3077 IF_RELEASE(origin->name.internal);
3078 origin->name.internal = eina_stringshare_add(tmp);
3080 efreet_menu_create_sub_menu_list(parent);
3081 parent->sub_menus = eina_list_append(parent->sub_menus, origin);
3087 efreet_menu_concatenate(dest, origin);
3088 efreet_menu_internal_free(origin);
3091 IF_FREE_LIST(internal->moves);
3096 * @param menu: The menu to start searching from
3097 * @param name: The menu name to find
3098 * @param parent: The parent of the found menu
3099 * @return Returns the menu with the given @a name or NULL if none found
3100 * @brief Searches the menu tree starting at @a menu looking for a menu with
3103 static Efreet_Menu_Internal *
3104 efreet_menu_by_name_find(Efreet_Menu_Internal *internal, const char *name, Efreet_Menu_Internal **parent)
3106 char *part, *tmp, *ptr;
3108 if (parent) *parent = internal;
3110 /* find the correct parent menu */
3113 part = strchr(ptr, '/');
3118 if (!(internal = eina_list_search_unsorted(internal->sub_menus,
3119 (Eina_Compare_Cb)efreet_menu_cb_compare_names,
3127 part = strchr(ptr, '/');
3130 if (parent) *parent = internal;
3132 /* find the menu in the parent list */
3133 if (!(internal = eina_list_search_unsorted(internal->sub_menus,
3134 (Eina_Compare_Cb)efreet_menu_cb_compare_names,
3146 efreet_menu_path_set(Efreet_Menu_Internal *internal, const char *path)
3151 p = strrchr(tmp, '/');
3157 internal->file.path = strdup(tmp);
3158 internal->file.name = strdup(p);
3165 * @return Returns a new Efreet_Menu_Move struct on success or NULL on failure
3166 * @brief Creates an returns a new Efreet_Menu_Move struct or NULL on failure
3168 static Efreet_Menu_Move *
3169 efreet_menu_move_new(void)
3171 Efreet_Menu_Move *move;
3173 move = NEW(Efreet_Menu_Move, 1);
3180 * @param move: The Efreet_Menu_Move to free
3181 * @return Returns no value.
3182 * @brief Frees the given move structure
3185 efreet_menu_move_free(Efreet_Menu_Move *move)
3189 IF_FREE(move->old_name);
3190 IF_FREE(move->new_name);
3197 * @return Returns a new Efreet_Menu_App_Dir on success or NULL on failure
3198 * @brief Creates and initializes a new Efreet_Menu_App_Dir structure
3200 static Efreet_Menu_App_Dir *
3201 efreet_menu_app_dir_new(void)
3203 Efreet_Menu_App_Dir *dir;
3205 dir = NEW(Efreet_Menu_App_Dir, 1);
3211 efreet_menu_app_dir_free(Efreet_Menu_App_Dir *dir)
3220 * @param a: The app dir to compare too
3221 * @param b: The path to compare too
3222 * @return Returns 1 if the strings are equals, 0 otherwise
3223 * @brief Compares the too strings
3226 efreet_menu_cb_app_dirs_compare(Efreet_Menu_App_Dir *a, const char *b)
3228 return ecore_str_compare(a->path, b);
3232 efreet_menu_create_sub_menu_list(Efreet_Menu_Internal *internal)
3234 if (!internal || internal->sub_menus) return;
3236 internal->sub_menus = NULL;
3240 efreet_menu_create_app_dirs_list(Efreet_Menu_Internal *internal)
3242 if (!internal || internal->app_dirs) return;
3244 internal->app_dirs = NULL;
3248 efreet_menu_create_directory_dirs_list(Efreet_Menu_Internal *internal)
3250 if (!internal || internal->directory_dirs) return;
3252 internal->directory_dirs = ecore_dlist_new();
3253 ecore_dlist_free_cb_set(internal->directory_dirs, free);
3257 efreet_menu_create_move_list(Efreet_Menu_Internal *internal)
3259 if (!internal || internal->moves) return;
3261 internal->moves = NULL;
3265 efreet_menu_create_filter_list(Efreet_Menu_Internal *internal)
3267 if (!internal || internal->filters) return;
3269 internal->filters = NULL;
3273 efreet_menu_create_layout_list(Efreet_Menu_Internal *internal)
3275 if (!internal || internal->layout) return;
3277 internal->layout = NULL;
3281 efreet_menu_create_default_layout_list(Efreet_Menu_Internal *internal)
3283 if (!internal || internal->default_layout) return;
3285 internal->default_layout = NULL;
3289 efreet_menu_create_directories_list(Efreet_Menu_Internal *internal)
3291 if (!internal || internal->directories) return;
3293 internal->directories = ecore_dlist_new();
3294 ecore_dlist_free_cb_set(internal->directories, free);
3298 efreet_menu_path_get(Efreet_Menu_Internal *internal, const char *suffix)
3300 char path[PATH_MAX];
3303 /* see if we've got an absolute or relative path */
3304 if (suffix[0] == '/')
3305 snprintf(path, sizeof(path), "%s", suffix);
3309 if (!internal->file.path)
3311 printf("efreet_menu_handle_app_dir() missing menu path ...\n");
3314 snprintf(path, sizeof(path), "%s/%s", internal->file.path, suffix);
3318 while (path[len] == '/') path[len--] = '\0';
3320 return strdup(path);
3324 efreet_menu_cb_menu_compare(Efreet_Menu_Internal *a, Efreet_Menu_Internal *b)
3326 return ecore_str_compare(a->name.internal, b->name.internal);
3330 efreet_menu_app_dirs_process(Efreet_Menu_Internal *internal)
3332 Efreet_Menu_App_Dir *app_dir;
3333 Efreet_Menu_Desktop *md;
3336 EINA_LIST_FREE(internal->app_pool, md)
3337 efreet_menu_desktop_free(md);
3339 EINA_LIST_FOREACH(internal->app_dirs, l, app_dir)
3340 efreet_menu_app_dir_scan(internal, app_dir->path, app_dir->prefix, app_dir->legacy);
3346 efreet_menu_app_dir_scan(Efreet_Menu_Internal *internal, const char *path, const char *id, int legacy)
3348 Efreet_Desktop *desktop;
3349 Efreet_Menu_Desktop *menu_desktop;
3351 char buf[PATH_MAX], buf2[PATH_MAX];
3352 struct dirent *file;
3355 files = opendir(path);
3356 if (!files) return 1;
3358 while ((file = readdir(files)))
3360 if (!strcmp(file->d_name, ".") || !strcmp(file->d_name, "..")) continue;
3361 snprintf(buf, PATH_MAX, "%s/%s", path, file->d_name);
3363 snprintf(buf2, PATH_MAX, "%s-%s", id, file->d_name);
3365 strcpy(buf2, file->d_name);
3367 if (ecore_file_is_dir(buf))
3370 efreet_menu_app_dir_scan(internal, buf, buf2, legacy);
3374 ext = strrchr(buf, '.');
3376 if (!ext || strcmp(ext, ".desktop")) continue;
3377 desktop = efreet_desktop_get(buf);
3379 if (!desktop || desktop->type != EFREET_DESKTOP_TYPE_APPLICATION)
3381 if (desktop) efreet_desktop_free(desktop);
3384 /* Don't add two files with the same id in the app pool */
3385 if (eina_list_search_unsorted(internal->app_pool,
3386 (Eina_Compare_Cb)efreet_menu_cb_md_compare_ids,
3390 menu_desktop = efreet_menu_desktop_new();
3391 menu_desktop->desktop = desktop;
3392 menu_desktop->id = eina_stringshare_add(buf2);
3393 internal->app_pool = eina_list_prepend(internal->app_pool, menu_desktop);
3403 * @param menu: The menu to work with
3404 * @return Returns 1 on success or 0 on failure
3405 * @brief Process the directory dirs in @a menu
3408 efreet_menu_directory_dirs_process(Efreet_Menu_Internal *internal)
3412 if (internal->directory_dirs)
3414 internal->directory_cache =
3415 eina_hash_string_superfast_new(EINA_FREE_CB(efreet_desktop_free));
3417 ecore_dlist_last_goto(internal->directory_dirs);
3418 while ((path = ecore_dlist_previous(internal->directory_dirs)))
3419 efreet_menu_directory_dir_scan(path, NULL, internal->directory_cache);
3422 if (internal->directories)
3424 ecore_dlist_last_goto(internal->directories);
3425 while ((path = ecore_dlist_previous(internal->directories)))
3427 internal->directory = efreet_menu_directory_get(internal, path);
3428 if (internal->directory) break;
3431 if (!internal->directory)
3432 internal->name.name = internal->name.internal;
3434 internal->name.name = internal->directory->name;
3441 * @param path: The path to scan
3442 * @param relative_path: The relative portion of the path
3443 * @param cache: The cache to populate
3444 * @return Returns 1 on success or 0 on failure
3445 * @brief Scans the given directory dir for .directory files and adds the
3446 * applications to the cache
3449 efreet_menu_directory_dir_scan(const char *path, const char *relative_path,
3452 Efreet_Desktop *desktop;
3454 char buf[PATH_MAX], buf2[PATH_MAX];
3455 struct dirent *file;
3458 files = opendir(path);
3459 if (!files) return 1;
3461 while ((file = readdir(files)))
3463 if (!strcmp(file->d_name, ".") || !strcmp(file->d_name, "..")) continue;
3464 snprintf(buf, PATH_MAX, "%s/%s", path, file->d_name);
3466 snprintf(buf2, PATH_MAX, "%s/%s", relative_path, file->d_name);
3468 strcpy(buf2, file->d_name);
3470 if (ecore_file_is_dir(buf))
3471 efreet_menu_directory_dir_scan(buf, buf2, cache);
3475 ext = strrchr(buf, '.');
3476 if (!ext || strcmp(ext, ".directory")) continue;
3478 desktop = efreet_desktop_get(buf);
3479 if (!desktop || desktop->type != EFREET_DESKTOP_TYPE_DIRECTORY)
3481 efreet_desktop_free(desktop);
3485 eina_hash_del(cache, buf2, NULL);
3486 eina_hash_add(cache, buf2, desktop);
3496 * @param menu: The menu to work with
3497 * @param path: The path to work with
3498 * @return Returns the desktop file for this path or NULL if none exists
3499 * @brief Finds the desktop file for the given path.
3501 static Efreet_Desktop *
3502 efreet_menu_directory_get(Efreet_Menu_Internal *internal, const char *path)
3504 Efreet_Desktop *dir;
3506 if (internal->directory_cache)
3508 dir = eina_hash_find(internal->directory_cache, path);
3509 if (dir) return dir;
3512 if (internal->parent)
3513 return efreet_menu_directory_get(internal->parent, path);
3520 * @param a: The first desktop
3521 * @param b: The second desktop
3522 * @return Returns the comparison of the desktop files
3523 * @brief Compares the desktop files.
3526 efreet_menu_cb_md_compare(Efreet_Menu_Desktop *a, Efreet_Menu_Desktop *b)
3529 return strcmp(ecore_file_file_get(a->desktop->orig_path), ecore_file_file_get(b->desktop->orig_path));
3531 return strcasecmp(a->desktop->name, b->desktop->name);
3536 efreet_menu_cb_compare_names(Efreet_Menu_Internal *internal, const char *name)
3538 return strcmp(internal->name.internal, name);
3542 efreet_menu_cb_md_compare_ids(Efreet_Menu_Desktop *md, const char *name)
3544 return strcmp(md->id, name);
3547 static Efreet_Menu *
3548 efreet_menu_layout_menu(Efreet_Menu_Internal *internal)
3551 Eina_List *layout = NULL;
3554 if (internal->parent)
3556 /* Copy default layout rules */
3557 if (internal->show_empty == -1) internal->show_empty = internal->parent->show_empty;
3558 if (internal->in_line == -1) internal->in_line = internal->parent->in_line;
3559 if (internal->inline_limit == -1) internal->inline_limit = internal->parent->inline_limit;
3560 if (internal->inline_header == -1) internal->inline_header = internal->parent->inline_header;
3561 if (internal->inline_alias == -1) internal->inline_alias = internal->parent->inline_alias;
3564 if (internal->layout)
3565 layout = internal->layout;
3567 else if (internal->parent)
3569 Efreet_Menu_Internal *parent;
3570 parent = internal->parent;
3573 layout = parent->default_layout;
3574 parent = parent->parent;
3575 } while (!layout && parent);
3579 entry = efreet_menu_entry_new();
3580 entry->type = EFREET_MENU_ENTRY_MENU;
3581 entry->id = eina_stringshare_add(internal->name.internal);
3582 entry->name = eina_stringshare_add(internal->name.name);
3583 if (internal->directory)
3585 entry->icon = eina_stringshare_add(internal->directory->icon);
3586 efreet_desktop_ref(internal->directory);
3587 entry->desktop = internal->directory;
3589 entry->entries = NULL;
3592 if (internal->sub_menus)
3594 internal->sub_menus = eina_list_sort(internal->sub_menus,
3596 (Eina_Compare_Cb)efreet_menu_cb_menu_compare);
3602 Efreet_Menu_Layout *lay;
3604 EINA_LIST_FOREACH(layout, l, lay)
3605 efreet_menu_layout_entries_get(entry, internal, lay);
3609 /* Default layout, first menus, then desktop */
3610 if (internal->sub_menus)
3612 Efreet_Menu_Internal *sub;
3614 EINA_LIST_FOREACH(internal->sub_menus, l, sub)
3616 Efreet_Menu *sub_entry;
3617 if ((sub->directory && sub->directory->no_display) || sub->deleted) continue;
3618 sub_entry = efreet_menu_layout_menu(sub);
3619 /* Don't show empty menus */
3620 if (!sub_entry->entries)
3622 efreet_menu_free(sub_entry);
3625 entry->entries = eina_list_append(entry->entries, sub_entry);
3629 if (internal->applications)
3631 Efreet_Menu_Desktop *md;
3633 EINA_LIST_FOREACH(internal->applications, l, md)
3635 Efreet_Menu *sub_entry;
3636 sub_entry = efreet_menu_layout_desktop(md);
3637 entry->entries = eina_list_append(entry->entries, sub_entry);
3642 /* Don't keep this list around if it is empty */
3647 static Efreet_Menu *
3648 efreet_menu_layout_desktop(Efreet_Menu_Desktop *md)
3653 entry = efreet_menu_entry_new();
3654 entry->type = EFREET_MENU_ENTRY_DESKTOP;
3655 entry->id = eina_stringshare_add(md->id);
3656 entry->name = eina_stringshare_add(md->desktop->name);
3657 if (md->desktop->icon) entry->icon = eina_stringshare_add(md->desktop->icon);
3658 efreet_desktop_ref(md->desktop);
3659 entry->desktop = md->desktop;
3665 efreet_menu_layout_entries_get(Efreet_Menu *entry, Efreet_Menu_Internal *internal,
3666 Efreet_Menu_Layout *layout)
3668 Efreet_Menu *sub_entry;
3670 if (internal->sub_menus && layout->type == EFREET_MENU_LAYOUT_MENUNAME)
3672 Efreet_Menu_Internal *sub;
3674 /* Efreet_Menu_Layout might be from DefaultLayout, so we need a local copy */
3675 int show_empty, in_line, inline_limit, inline_header, inline_alias;
3677 if (layout->show_empty == -1) show_empty = internal->show_empty;
3678 else show_empty = layout->show_empty;
3680 if (layout->in_line == -1) in_line = internal->in_line;
3681 else in_line = layout->in_line;
3683 if (layout->inline_limit == -1) inline_limit = internal->inline_limit;
3684 else inline_limit = layout->inline_limit;
3686 if (layout->inline_header == -1) inline_header = internal->inline_header;
3687 else inline_header = layout->inline_header;
3689 if (layout->inline_alias == -1) inline_alias = internal->inline_alias;
3690 else inline_alias = layout->inline_alias;
3692 sub = eina_list_search_unsorted(internal->sub_menus,
3693 (Eina_Compare_Cb)efreet_menu_cb_compare_names, layout->name);
3696 if (!(sub->directory && sub->directory->no_display) && !sub->deleted)
3698 sub_entry = efreet_menu_layout_menu(sub);
3699 if (!show_empty && efreet_menu_layout_is_empty(sub_entry))
3700 efreet_menu_free(sub_entry);
3702 ((inline_limit == 0) ||
3703 (!sub_entry->entries || (eina_list_count(sub_entry->entries) <= inline_limit))))
3706 if (!sub_entry->entries)
3708 /* Can't inline an empty submenu */
3709 entry->entries = eina_list_append(entry->entries, sub_entry);
3711 else if (inline_alias && (eina_list_count(sub_entry->entries) == 1))
3715 tmp = eina_list_data_get(sub_entry->entries);
3716 sub_entry->entries = eina_list_remove_list(sub_entry->entries, sub_entry->entries);
3717 IF_RELEASE(tmp->name);
3718 tmp->name = sub_entry->name;
3719 sub_entry->name = NULL;
3720 IF_RELEASE(tmp->icon);
3721 tmp->icon = sub_entry->icon;
3722 sub_entry->icon = NULL;
3723 entry->entries = eina_list_append(entry->entries, tmp);
3724 efreet_menu_free(sub_entry);
3732 tmp = efreet_menu_entry_new();
3733 tmp->type = EFREET_MENU_ENTRY_HEADER;
3734 tmp->name = sub_entry->name;
3735 sub_entry->name = NULL;
3736 tmp->icon = sub_entry->icon;
3737 sub_entry->icon = NULL;
3738 entry->entries = eina_list_append(entry->entries, tmp);
3740 while ((tmp = eina_list_data_get(sub_entry->entries)))
3742 sub_entry->entries = eina_list_remove_list(sub_entry->entries, sub_entry->entries);
3743 entry->entries = eina_list_append(entry->entries, tmp);
3745 efreet_menu_free(sub_entry);
3749 entry->entries = eina_list_append(entry->entries, sub_entry);
3751 internal->sub_menus = eina_list_remove(internal->sub_menus, sub);
3752 efreet_menu_internal_free(sub);
3755 else if (internal->applications && layout->type == EFREET_MENU_LAYOUT_FILENAME)
3757 Efreet_Menu_Desktop *md;
3758 md = eina_list_search_unsorted(internal->applications,
3759 (Eina_Compare_Cb)efreet_menu_cb_md_compare_ids, layout->name);
3762 sub_entry = efreet_menu_layout_desktop(md);
3763 entry->entries = eina_list_append(entry->entries, sub_entry);
3764 internal->applications = eina_list_remove(internal->applications, md);
3767 else if (layout->type == EFREET_MENU_LAYOUT_MERGE)
3769 if (internal->applications && !strcmp(layout->name, "files"))
3771 Efreet_Menu_Desktop *md;
3773 while ((md = eina_list_data_get(internal->applications)))
3775 internal->applications = eina_list_remove_list(internal->applications,
3776 internal->applications);
3777 sub_entry = eina_list_search_unsorted(entry->entries,
3778 (Eina_Compare_Cb)efreet_menu_cb_entry_compare_desktop,
3782 sub_entry = efreet_menu_layout_desktop(md);
3783 entry->entries = eina_list_append(entry->entries, sub_entry);
3786 IF_FREE_LIST(internal->applications);
3788 else if (internal->sub_menus && !strcmp(layout->name, "menus"))
3790 Efreet_Menu_Internal *sub;
3792 while ((sub = eina_list_data_get(internal->sub_menus)))
3794 internal->sub_menus = eina_list_remove_list(internal->sub_menus, internal->sub_menus);
3795 if ((sub->directory && sub->directory->no_display) || sub->deleted)
3797 efreet_menu_internal_free(sub);
3800 sub_entry = eina_list_search_unsorted(entry->entries,
3801 (Eina_Compare_Cb)efreet_menu_cb_entry_compare_menu,
3805 sub_entry = efreet_menu_layout_menu(sub);
3806 if (!internal->show_empty && efreet_menu_layout_is_empty(sub_entry))
3807 efreet_menu_free(sub_entry);
3808 else if (internal->in_line &&
3809 ((internal->inline_limit == 0) ||
3810 (!sub_entry->entries || (eina_list_count(sub_entry->entries) <= internal->inline_limit))))
3813 if (!sub_entry->entries)
3815 /* Can't inline an empty submenu */
3816 entry->entries = eina_list_append(entry->entries, sub_entry);
3818 else if (internal->inline_alias && (eina_list_count(sub_entry->entries) == 1))
3822 tmp = eina_list_data_get(sub_entry->entries);
3823 sub_entry->entries = eina_list_remove_list(sub_entry->entries, sub_entry->entries);
3824 eina_stringshare_del(tmp->name);
3825 tmp->name = sub_entry->name;
3826 sub_entry->name = NULL;
3827 IF_RELEASE(tmp->icon);
3828 if (sub_entry->icon)
3830 tmp->icon = sub_entry->icon;
3831 sub_entry->icon = NULL;
3833 entry->entries = eina_list_append(entry->entries, tmp);
3834 efreet_menu_free(sub_entry);
3840 if (internal->inline_header)
3842 tmp = efreet_menu_entry_new();
3843 tmp->type = EFREET_MENU_ENTRY_HEADER;
3844 tmp->name = sub_entry->name;
3845 sub_entry->name = NULL;
3846 if (sub_entry->icon) tmp->icon = sub_entry->icon;
3847 sub_entry->icon = NULL;
3848 entry->entries = eina_list_append(entry->entries, tmp);
3850 while ((tmp = eina_list_data_get(sub_entry->entries)))
3852 sub_entry->entries = eina_list_remove_list(sub_entry->entries,
3853 sub_entry->entries);
3854 entry->entries = eina_list_append(entry->entries, tmp);
3856 efreet_menu_free(sub_entry);
3860 entry->entries = eina_list_append(entry->entries, sub_entry);
3862 efreet_menu_internal_free(sub);
3864 IF_FREE_LIST(internal->sub_menus);
3866 else if (internal->sub_menus && !strcmp(layout->name, "all"))
3870 orig = layout->name;
3871 layout->name = "menus";
3872 efreet_menu_layout_entries_get(entry, internal, layout);
3873 layout->name = "files";
3874 efreet_menu_layout_entries_get(entry, internal, layout);
3875 layout->name = orig;
3878 else if (layout->type == EFREET_MENU_LAYOUT_SEPARATOR)
3880 sub_entry = efreet_menu_entry_new();
3881 sub_entry->type = EFREET_MENU_ENTRY_SEPARATOR;
3882 entry->entries = eina_list_append(entry->entries, sub_entry);
3887 efreet_menu_cb_entry_compare_menu(Efreet_Menu *entry, Efreet_Menu_Internal *internal)
3889 if (entry->type != EFREET_MENU_ENTRY_MENU) return 1;
3890 return ecore_str_compare(entry->name, internal->name.name);
3894 efreet_menu_cb_entry_compare_desktop(Efreet_Menu *entry, Efreet_Desktop *desktop)
3896 if (entry->type != EFREET_MENU_ENTRY_DESKTOP) return -1;
3897 return ecore_str_compare(entry->name, desktop->name);
3901 efreet_menu_cb_move_compare(Efreet_Menu_Move *move, const char *old)
3903 return ecore_str_compare(move->old_name, old);
3907 efreet_menu_layout_is_empty(Efreet_Menu *entry)
3909 Efreet_Menu *sub_entry;
3912 if (!entry->entries) return 1;
3914 EINA_LIST_FOREACH(entry->entries, l, sub_entry)
3916 if (sub_entry->type == EFREET_MENU_ENTRY_MENU) return 0;
3917 if (sub_entry->type == EFREET_MENU_ENTRY_DESKTOP) return 0;