gtkbuilder-menus.c 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright © 2011, 2012 Canonical Ltd.
 *
 * 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
 * licence, 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "config.h"

#include "gtkbuilderprivate.h"
#include "gtkintl.h"

#include <gio/gio.h>
#include <string.h>

struct frame
{
  GMenu        *menu;
  GMenuItem    *item;
  struct frame *prev;
};

typedef struct
{
  ParserData *parser_data;
  struct frame frame;

  /* attributes */
  gchar        *attribute;
  GVariantType *type;
  GString      *string;

  /* translation */
  gchar        *context;
  gboolean      translatable;
} GtkBuilderMenuState;

static void
gtk_builder_menu_push_frame (GtkBuilderMenuState *state,
                             GMenu               *menu,
                             GMenuItem           *item)
{
  struct frame *new;

  new = g_slice_new (struct frame);
  *new = state->frame;

  state->frame.menu = menu;
  state->frame.item = item;
  state->frame.prev = new;
}

static void
gtk_builder_menu_pop_frame (GtkBuilderMenuState *state)
{
  struct frame *prev = state->frame.prev;

  if (state->frame.item)
    {
      g_assert (prev->menu != NULL);
      g_menu_append_item (prev->menu, state->frame.item);
      g_object_unref (state->frame.item);
    }

  state->frame = *prev;

  g_slice_free (struct frame, prev);
}

static void
gtk_builder_menu_start_element (GMarkupParseContext  *context,
                                const gchar          *element_name,
                                const gchar         **attribute_names,
                                const gchar         **attribute_values,
                                gpointer              user_data,
                                GError              **error)
{
  GtkBuilderMenuState *state = user_data;

#define COLLECT(first, ...) \
  g_markup_collect_attributes (element_name,                                 \
                               attribute_names, attribute_values, error,     \
                               first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
#define OPTIONAL   G_MARKUP_COLLECT_OPTIONAL
#define BOOLEAN    G_MARKUP_COLLECT_BOOLEAN
#define STRING     G_MARKUP_COLLECT_STRING

  if (state->frame.menu)
    {
      /* Can have '<item>', '<submenu>' or '<section>' here. */
      if (g_str_equal (element_name, "item"))
        {
          GMenuItem *item;

107 108 109 110 111 112
          if (COLLECT (G_MARKUP_COLLECT_INVALID, NULL))
            {
              item = g_menu_item_new (NULL, NULL);
              gtk_builder_menu_push_frame (state, NULL, item);
            }

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
          return;
        }

      else if (g_str_equal (element_name, "submenu"))
        {
          const gchar *id;

          if (COLLECT (STRING | OPTIONAL, "id", &id))
            {
              GMenuItem *item;
              GMenu *menu;

              menu = g_menu_new ();
              item = g_menu_item_new_submenu (NULL, G_MENU_MODEL (menu));
              gtk_builder_menu_push_frame (state, menu, item);

              if (id != NULL)
                _gtk_builder_add_object (state->parser_data->builder, id, G_OBJECT (menu));
131
              g_object_unref (menu);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            }

          return;
        }

      else if (g_str_equal (element_name, "section"))
        {
          const gchar *id;

          if (COLLECT (STRING | OPTIONAL, "id", &id))
            {
              GMenuItem *item;
              GMenu *menu;

              menu = g_menu_new ();
              item = g_menu_item_new_section (NULL, G_MENU_MODEL (menu));
              gtk_builder_menu_push_frame (state, menu, item);

              if (id != NULL)
                _gtk_builder_add_object (state->parser_data->builder, id, G_OBJECT (menu));
152
              g_object_unref (menu);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            }

          return;
        }
    }

  if (state->frame.item)
    {
      /* Can have '<attribute>' or '<link>' here. */
      if (g_str_equal (element_name, "attribute"))
        {
          const gchar *typestr;
          const gchar *name;
          const gchar *context;

          if (COLLECT (STRING,             "name", &name,
                       OPTIONAL | BOOLEAN, "translatable", &state->translatable,
                       OPTIONAL | STRING,  "context", &context,
                       OPTIONAL | STRING,  "comments", NULL, /* ignore, just for translators */
                       OPTIONAL | STRING,  "type", &typestr))
            {
              if (typestr && !g_variant_type_string_is_valid (typestr))
                {
                  g_set_error (error, G_VARIANT_PARSE_ERROR,
                               G_VARIANT_PARSE_ERROR_INVALID_TYPE_STRING,
                               "Invalid GVariant type string '%s'", typestr);
                  return;
                }

              state->type = typestr ? g_variant_type_new (typestr) : NULL;
              state->string = g_string_new (NULL);
              state->attribute = g_strdup (name);
              state->context = g_strdup (context);

              gtk_builder_menu_push_frame (state, NULL, NULL);
            }

          return;
        }

      if (g_str_equal (element_name, "link"))
        {
          const gchar *name;
          const gchar *id;

          if (COLLECT (STRING,            "name", &name,
                       STRING | OPTIONAL, "id",   &id))
            {
              GMenu *menu;

              menu = g_menu_new ();
              g_menu_item_set_link (state->frame.item, name, G_MENU_MODEL (menu));
              gtk_builder_menu_push_frame (state, menu, NULL);

              if (id != NULL)
                _gtk_builder_add_object (state->parser_data->builder, id, G_OBJECT (menu));
209
              g_object_unref (menu);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
            }

          return;
        }
    }

  {
    const GSList *element_stack;

    element_stack = g_markup_parse_context_get_element_stack (context);

    if (element_stack->next)
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
                   _("Element <%s> not allowed inside <%s>"),
                   element_name, (const gchar *) element_stack->next->data);

    else
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
                   _("Element <%s> not allowed at toplevel"), element_name);
  }
}

static void
gtk_builder_menu_end_element (GMarkupParseContext  *context,
                              const gchar          *element_name,
                              gpointer              user_data,
                              GError              **error)
{
  GtkBuilderMenuState *state = user_data;

  gtk_builder_menu_pop_frame (state);

  if (state->string)
    {
      GVariant *value;
      gchar *text;

      text = g_string_free (state->string, FALSE);
      state->string = NULL;

      /* do the translation if necessary */
251
      if (state->translatable)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
        {
          const gchar *translated;

          if (state->context)
            translated = g_dpgettext2 (state->parser_data->domain, state->context, text);
          else
            translated = g_dgettext (state->parser_data->domain, text);

         if (translated != text)
           {
             /* it's safe because we know that translated != text */
             g_free (text);
             text = g_strdup (translated);
           }
        }

      if (state->type == NULL)
        /* No type string specified -> it's a normal string. */
        g_menu_item_set_attribute (state->frame.item, state->attribute, "s", text);

      /* Else, we try to parse it according to the type string.  If
       * error is set here, it will follow us out, ending the parse.
       *
       * We still need to free everything, though, so ignore it here.
       */
      else if ((value = g_variant_parse (state->type, text, NULL, NULL, error)))
        {
          g_menu_item_set_attribute_value (state->frame.item, state->attribute, value);
          g_variant_unref (value);
        }

      if (state->type)
        {
          g_variant_type_free (state->type);
          state->type = NULL;
        }

      g_free (state->context);
      state->context = NULL;

      g_free (state->attribute);
      state->attribute = NULL;

      g_free (text);
    }
}

static void
gtk_builder_menu_text (GMarkupParseContext  *context,
                       const gchar          *text,
                       gsize                 text_len,
                       gpointer              user_data,
                       GError              **error)
{
  GtkBuilderMenuState *state = user_data;
  gint i;

  for (i = 0; i < text_len; i++)
    if (!g_ascii_isspace (text[i]))
      {
        if (state->string)
          g_string_append_len (state->string, text, text_len);

        else
          g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
317
                       _("Text may not appear inside <%s>"),
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
                       g_markup_parse_context_get_element (context));
        break;
      }
}

static void
gtk_builder_menu_error (GMarkupParseContext *context,
                        GError              *error,
                        gpointer             user_data)
{
  GtkBuilderMenuState *state = user_data;

  while (state->frame.prev)
    {
      struct frame *prev = state->frame.prev;

      state->frame = *prev;

      g_slice_free (struct frame, prev);
    }

  if (state->string)
    g_string_free (state->string, TRUE);

  if (state->type)
    g_variant_type_free (state->type);

  g_free (state->attribute);
  g_free (state->context);

  g_slice_free (GtkBuilderMenuState, state);
}

static GMarkupParser gtk_builder_menu_subparser =
{
  gtk_builder_menu_start_element,
  gtk_builder_menu_end_element,
  gtk_builder_menu_text,
  NULL,                            /* passthrough */
  gtk_builder_menu_error
};

void
_gtk_builder_menu_start (ParserData   *parser_data,
                         const gchar  *element_name,
                         const gchar **attribute_names,
                         const gchar **attribute_values,
                         GError      **error)
{
  GtkBuilderMenuState *state;
  gchar *id;

  state = g_slice_new0 (GtkBuilderMenuState);
  state->parser_data = parser_data;
  g_markup_parse_context_push (parser_data->ctx, &gtk_builder_menu_subparser, state);

  if (COLLECT (STRING, "id", &id))
    {
      GMenu *menu;

      menu = g_menu_new ();
      _gtk_builder_add_object (state->parser_data->builder, id, G_OBJECT (menu));
      gtk_builder_menu_push_frame (state, menu, NULL);
381
      g_object_unref (menu);
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    }
}

void
_gtk_builder_menu_end (ParserData *parser_data)
{
  GtkBuilderMenuState *state;

  state = g_markup_parse_context_pop (parser_data->ctx);
  gtk_builder_menu_pop_frame (state);

  g_assert (state->frame.prev == NULL);
  g_assert (state->frame.item == NULL);
  g_assert (state->frame.menu == NULL);
  g_slice_free (GtkBuilderMenuState, state);
}