test-menu-spec.c 6.16 KB
Newer Older
Mark McLoughlin's avatar
Mark McLoughlin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2004 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

22
#include "gmenu-tree.h"
Mark McLoughlin's avatar
Mark McLoughlin committed
23 24 25

#include <string.h>

26 27
/* This is only a test program, so we don't need translations. Still keep the
 * infrastructure in place in case we suddenly decide we want to localize this
28 29
 * program. Don't forget to reenable the call to bindtextdomain() if going back
 * to real localization. */
30 31 32
#define _(x) x
#define N_(x) x

33 34 35
static char     *menu_file = NULL;
static gboolean  monitor = FALSE;
static gboolean  include_excluded = FALSE;
36
static gboolean  include_nodisplay = FALSE;
37 38

static GOptionEntry options[] = {
39 40 41 42
  { "file",              'f', 0, G_OPTION_ARG_STRING, &menu_file,         N_("Menu file"),                      N_("MENU_FILE") },
  { "monitor",           'm', 0, G_OPTION_ARG_NONE,   &monitor,           N_("Monitor for menu changes"),       NULL },
  { "include-excluded",  'i', 0, G_OPTION_ARG_NONE,   &include_excluded,  N_("Include <Exclude>d entries"),     NULL },
  { "include-nodisplay", 'n', 0, G_OPTION_ARG_NONE,   &include_nodisplay, N_("Include NoDisplay=true entries"), NULL },
43 44 45
  { NULL }
};

46
static void
47 48
append_directory_path (GMenuTreeDirectory *directory,
		       GString            *path)
49
{
50
  GMenuTreeDirectory *parent;
51

52
  parent = gmenu_tree_item_get_parent (GMENU_TREE_ITEM (directory));
53 54 55 56 57 58 59 60 61

  if (!parent)
    {
      g_string_append_c (path, '/');
      return;
    }

  append_directory_path (parent, path);

62
  g_string_append (path, gmenu_tree_directory_get_name (directory));
63 64
  g_string_append_c (path, '/');

65
  gmenu_tree_item_unref (parent);
66 67 68
}

static char *
69
make_path (GMenuTreeDirectory *directory)
70 71 72 73 74 75 76 77 78 79 80 81
{
  GString *path;

  g_return_val_if_fail (directory != NULL, NULL);

  path = g_string_new (NULL);

  append_directory_path (directory, path);

  return g_string_free (path, FALSE);
}

82
static void
83 84
print_entry (GMenuTreeEntry *entry,
	     const char     *path)
85
{
Mark McLoughlin's avatar
Mark McLoughlin committed
86
  char *utf8_path;
87
  char *utf8_file_id;
Mark McLoughlin's avatar
Mark McLoughlin committed
88 89 90 91

  utf8_path = g_filename_to_utf8 (gmenu_tree_entry_get_desktop_file_path (entry),
				  -1, NULL, NULL, NULL);

92 93 94
  utf8_file_id = g_filename_to_utf8 (gmenu_tree_entry_get_desktop_file_id (entry),
				     -1, NULL, NULL, NULL);

95
  g_print ("%s\t%s\t%s%s\n",
96
	   path,
97
	   utf8_file_id ? utf8_file_id : _("Invalid desktop file ID"),
Mark McLoughlin's avatar
Mark McLoughlin committed
98 99 100
	   utf8_path ? utf8_path : _("[Invalid Filename]"),
	   gmenu_tree_entry_get_is_excluded (entry) ? _(" <excluded>") : "");

101
  g_free (utf8_file_id);
Mark McLoughlin's avatar
Mark McLoughlin committed
102
  g_free (utf8_path);
103 104
}

Mark McLoughlin's avatar
Mark McLoughlin committed
105
static void
106
print_directory (GMenuTreeDirectory *directory)
Mark McLoughlin's avatar
Mark McLoughlin committed
107
{
108
  GSList     *items;
Mark McLoughlin's avatar
Mark McLoughlin committed
109 110 111 112
  GSList     *tmp;
  const char *path;
  char       *freeme;

113
  freeme = make_path (directory);
Mark McLoughlin's avatar
Mark McLoughlin committed
114 115 116 117 118
  if (!strcmp (freeme, "/"))
    path = freeme;
  else
    path = freeme + 1;

119
  items = gmenu_tree_directory_get_contents (directory);
Mark McLoughlin's avatar
Mark McLoughlin committed
120

121
  tmp = items;
Mark McLoughlin's avatar
Mark McLoughlin committed
122 123
  while (tmp != NULL)
    {
124
      GMenuTreeItem *item = tmp->data;
Mark McLoughlin's avatar
Mark McLoughlin committed
125

126
      switch (gmenu_tree_item_get_type (item))
127
	{
128 129
	case GMENU_TREE_ITEM_ENTRY:
	  print_entry (GMENU_TREE_ENTRY (item), path);
130
	  break;
Mark McLoughlin's avatar
Mark McLoughlin committed
131

132 133
	case GMENU_TREE_ITEM_DIRECTORY:
	  print_directory (GMENU_TREE_DIRECTORY (item));
134
	  break;
Mark McLoughlin's avatar
Mark McLoughlin committed
135

136 137
	case GMENU_TREE_ITEM_HEADER:
	case GMENU_TREE_ITEM_SEPARATOR:
138
	  break;
Mark McLoughlin's avatar
Mark McLoughlin committed
139

140
	case GMENU_TREE_ITEM_ALIAS:
141
	  {
142
	    GMenuTreeItem *aliased_item;
Mark McLoughlin's avatar
Mark McLoughlin committed
143

144 145 146
	    aliased_item = gmenu_tree_alias_get_item (GMENU_TREE_ALIAS (item));
	    if (gmenu_tree_item_get_type (aliased_item) == GMENU_TREE_ITEM_ENTRY)
	      print_entry (GMENU_TREE_ENTRY (aliased_item), path);
147 148
	  }
	  break;
Mark McLoughlin's avatar
Mark McLoughlin committed
149

150 151 152 153
	default:
	  g_assert_not_reached ();
	  break;
	}
Mark McLoughlin's avatar
Mark McLoughlin committed
154

155
      gmenu_tree_item_unref (tmp->data);
Mark McLoughlin's avatar
Mark McLoughlin committed
156 157 158 159

      tmp = tmp->next;
    }

160
  g_slist_free (items);
Mark McLoughlin's avatar
Mark McLoughlin committed
161 162 163 164

  g_free (freeme);
}

165
static void
166
handle_tree_changed (GMenuTree *tree)
167
{
168
  GMenuTreeDirectory *root;
169

Mark McLoughlin's avatar
Mark McLoughlin committed
170
  g_print (_("\n\n\n==== Menu changed, reloading ====\n\n\n"));
171

172
  root = gmenu_tree_get_root_directory (tree);
173 174
  if (root == NULL)
    {
Mark McLoughlin's avatar
Mark McLoughlin committed
175
      g_warning (_("Menu tree is empty"));
176 177 178 179
      return;
    }

  print_directory (root);
180
  gmenu_tree_item_unref (root);
181 182
}

Mark McLoughlin's avatar
Mark McLoughlin committed
183 184 185
int
main (int argc, char **argv)
{
186
  GOptionContext    *options_context;
187 188
  GMenuTree          *tree;
  GMenuTreeDirectory *root;
189
  GMenuTreeFlags      flags;
Mark McLoughlin's avatar
Mark McLoughlin committed
190

191 192
  g_type_init ();

193 194
#if 0
  /* See comment when defining _() at the top of this file. */
Mark McLoughlin's avatar
Mark McLoughlin committed
195 196 197
  bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
198
#endif
Mark McLoughlin's avatar
Mark McLoughlin committed
199 200

  options_context = g_option_context_new (_("- test GNOME's implementation of the Desktop Menu Specification"));
201 202
  g_option_context_add_main_entries (options_context, options, GETTEXT_PACKAGE);
  g_option_context_parse (options_context, &argc, &argv, NULL);
Mark McLoughlin's avatar
Mark McLoughlin committed
203
  g_option_context_free (options_context);
204

205 206 207 208 209 210
  flags = GMENU_TREE_FLAGS_NONE;
  if (include_excluded)
    flags |= GMENU_TREE_FLAGS_INCLUDE_EXCLUDED;
  if (include_nodisplay)
    flags |= GMENU_TREE_FLAGS_INCLUDE_NODISPLAY;

211
  tree = gmenu_tree_new (menu_file ? menu_file : "applications.menu", flags);
212
  g_assert (tree != NULL);
Mark McLoughlin's avatar
Mark McLoughlin committed
213

214
  root = gmenu_tree_get_root_directory (tree);
215 216 217
  if (root != NULL)
    {
      print_directory (root);
218
      gmenu_tree_item_unref (root);
219 220
    }
  else
Mark McLoughlin's avatar
Mark McLoughlin committed
221
    {
Mark McLoughlin's avatar
Mark McLoughlin committed
222
      g_warning (_("Menu tree is empty"));
Mark McLoughlin's avatar
Mark McLoughlin committed
223 224
    }

225 226 227 228
  if (monitor)
    {
      GMainLoop *main_loop;

229 230 231
      gmenu_tree_add_monitor (tree,
			      (GMenuTreeChangedFunc) handle_tree_changed,
			      NULL);
232 233 234 235 236

      main_loop = g_main_loop_new (NULL, FALSE);
      g_main_loop_run (main_loop);
      g_main_loop_unref (main_loop);

237 238 239
      gmenu_tree_remove_monitor (tree,
				 (GMenuTreeChangedFunc) handle_tree_changed,
				 NULL);
240 241

    }
Mark McLoughlin's avatar
Mark McLoughlin committed
242

243
  g_object_unref (tree);
Mark McLoughlin's avatar
Mark McLoughlin committed
244 245 246

  return 0;
}