Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * Copyright (C) 2011, Victor Ananjevsky <ananasik@gmail.com>
- */
- /* Compile command: gcc -o fvwmmenu `pkg-config --cflags --libs gtk+-2.0 libgnome-menu` fvwmmenu.c */
- #include <gtk/gtk.h>
- #define GMENU_I_KNOW_THIS_IS_UNSTABLE
- #include <gmenu-tree.h>
- static GtkIconTheme *theme;
- static gint size = -1;
- static gchar *cache_path;
- static gchar *term = "xterm";
- static gchar *
- cache_icon (const gchar * name)
- {
- gchar *filename = NULL;
- GdkPixbuf *pb = NULL;
- if (size == 0)
- return "";
- if (g_file_test (name, G_FILE_TEST_EXISTS))
- {
- filename = g_strdup_printf ("%s/%s", cache_path, basename (name));
- if (!g_file_test (filename, G_FILE_TEST_EXISTS))
- pb = gdk_pixbuf_new_from_file_at_size (name, size, size, NULL);
- }
- else
- {
- gchar *p, *sname = g_strdup (name);
- if (p = g_strrstr (sname, ".png"))
- *p = '\0';
- filename = g_strdup_printf ("%s/%s.png", cache_path, sname);
- if (!g_file_test (filename, G_FILE_TEST_EXISTS))
- pb = gtk_icon_theme_load_icon (theme, sname, size, GTK_ICON_LOOKUP_FORCE_SIZE, NULL);
- g_free (sname);
- }
- if (pb)
- {
- gdk_pixbuf_save (pb, filename, "png", NULL);
- g_object_unref (pb);
- }
- return (gchar *) basename (filename);
- }
- static gchar *
- cstrip (const gchar * cmd)
- {
- gchar *p, *ret = g_strdup (cmd);
- if (p = g_strrstr (ret, "%"))
- *p = '\0';
- return ret;
- }
- gint
- main (gint argc, gchar * argv[])
- {
- gchar **menus;
- GOptionEntry entrs[] = {
- {"size", 's', 0, G_OPTION_ARG_INT, &size, "Set icon size", "SIZE"}
- ,
- {"term", 't', 0, G_OPTION_ARG_STRING, &term, "Set terminal command", "TERM"}
- ,
- {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &menus, NULL, NULL}
- ,
- {NULL}
- };
- gtk_init_with_args (&argc, &argv, "- Create xdg menus for FVWM", entrs, NULL, NULL);
- theme = gtk_icon_theme_get_default ();
- if (size == -1)
- {
- gint w, h;
- gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &w, &h);
- size = MIN (w, h);
- }
- cache_path = g_build_filename (g_get_user_cache_dir (), "fvwm", "menu", NULL);
- g_mkdir_with_parents (cache_path, 0644);
- if (menus && menus[0])
- {
- if (g_ascii_strcasecmp (menus[0], "recent") == 0)
- {
- GList *r = gtk_recent_manager_get_items (gtk_recent_manager_get_default ());
- g_printf ("DestroyMenu recreate %s\n", menus[0]);
- for (; r; r = r->next)
- {
- GtkRecentInfo *ri = (GtkRecentInfo *) r->data;
- if (gtk_recent_info_exists (ri))
- {
- gchar *doc, *cmd;
- doc = (gchar *) gtk_recent_info_get_display_name (ri);
- gtk_recent_info_get_application_info (ri, gtk_recent_info_last_application (ri),
- (const gchar **) &cmd, NULL, NULL);
- g_printf ("AddToMenu %s \"%s\" Exec exec %s\n", menus[0], doc, cmd);
- }
- }
- }
- else
- {
- GMenuTree *tree;
- GMenuTreeDirectory *root, *dir;
- GSList *items;
- gchar *file;
- const gchar *menuname;
- file = g_strdup_printf ("%s.menu", g_ascii_strdown (menus[0], -1));
- if ((tree = gmenu_tree_lookup (file, GMENU_TREE_FLAGS_NONE)) == NULL)
- return 1;
- g_free (file);
- if (menus[1])
- root = gmenu_tree_get_directory_from_path (tree, menus[1]);
- else
- root = gmenu_tree_get_root_directory (tree);
- if (!root)
- return 1;
- if (menus[1])
- menuname = gmenu_tree_directory_get_menu_id (root);
- else
- menuname = menus[0];
- g_printf ("DestroyMenu recreate %s\n", menuname);
- for (items = gmenu_tree_directory_get_contents (root); items; items = items->next)
- {
- GMenuTreeItem *it = (GMenuTreeItem *) items->data;
- switch (gmenu_tree_item_get_type (it))
- {
- case GMENU_TREE_ITEM_ALIAS:
- it = gmenu_tree_alias_get_item (GMENU_TREE_ALIAS (it));
- if (gmenu_tree_item_get_type (it) != GMENU_TREE_ITEM_ENTRY)
- break;
- case GMENU_TREE_ITEM_ENTRY:
- if (!gmenu_tree_entry_get_is_excluded (GMENU_TREE_ENTRY (it)) ||
- !gmenu_tree_entry_get_is_nodisplay (GMENU_TREE_ENTRY (it)))
- {
- const gchar *n, *i, *c;
- n = gmenu_tree_entry_get_generic_name (GMENU_TREE_ENTRY (it));
- if (!n)
- n = gmenu_tree_entry_get_display_name (GMENU_TREE_ENTRY (it));
- i = gmenu_tree_entry_get_icon (GMENU_TREE_ENTRY (it));
- c = gmenu_tree_entry_get_exec (GMENU_TREE_ENTRY (it));
- if (gmenu_tree_entry_get_launch_in_terminal (GMENU_TREE_ENTRY (it)))
- {
- g_printf ("AddToMenu %s \"%s%%menu/%s%%\" Exec exec %s -e \"%s\"\n",
- menuname, g_strstrip ((gchar *) n), cache_icon (i), term, cstrip (c));
- }
- else
- {
- g_printf ("AddToMenu %s \"%s%%menu/%s%%\" Exec exec %s\n",
- menuname, g_strstrip ((gchar *) n), cache_icon (i), cstrip (c));
- }
- }
- break;
- case GMENU_TREE_ITEM_DIRECTORY:
- if (!gmenu_tree_directory_get_is_nodisplay (GMENU_TREE_DIRECTORY (it)))
- {
- const gchar *n, *i, *m;
- n = gmenu_tree_directory_get_name (GMENU_TREE_DIRECTORY (it));
- i = gmenu_tree_directory_get_icon (GMENU_TREE_DIRECTORY (it));
- m = gmenu_tree_directory_get_menu_id (GMENU_TREE_DIRECTORY (it));
- g_printf ("AddToMenu %s \"%s%%menu/%s%%\" Popup %s\n", menuname, n, cache_icon (i), m);
- g_printf ("DestroyMenu %s\n", m);
- g_printf ("AddToMenu %s DynamicPopupAction PipeRead \"%s -s %d -t %s %s %s\"\n",
- m, argv[0], size, term, menus[0],
- gmenu_tree_directory_make_path (GMENU_TREE_DIRECTORY (it), NULL));
- }
- break;
- case GMENU_TREE_ITEM_SEPARATOR:
- g_printf ("AddToMenu %s \"\" Nop\n", menuname);
- break;
- }
- }
- }
- }
- else
- return 1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement