glade-previewer.c 8.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (C) 2010 Marco Diego Aurélio Mesquita
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
 */


#include <config.h>

#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

29 30
#include <gladeui/glade.h>

31 32
#include <stdlib.h>
#include <locale.h>
33 34 35
#include <glib/gi18n-lib.h>

static void
36
display_help_and_quit (const GOptionEntry * entries)
37
{
38 39 40 41
  GOptionContext *context;
  context = g_option_context_new (_("- previews a glade UI definition"));
  g_option_context_add_main_entries (context, entries, NULL);
  g_option_context_add_group (context, gtk_get_option_group (TRUE));
42

43
  g_print ("%s\n", g_option_context_get_help (context, TRUE, NULL));
44

45 46
  g_option_context_free (context);
  exit (1);
47 48 49
}

static void
50 51
parse_arguments (int argc, char **argv, gchar ** toplevel_name,
                 gchar ** file_name)
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
  *toplevel_name = NULL;
  *file_name = NULL;
  gboolean listen = FALSE;
  gboolean version = FALSE;
  GError *error = NULL;

  GOptionEntry entries[] = {
    {"filename", 'f', G_OPTION_FLAG_FILENAME | G_OPTION_FLAG_OPTIONAL_ARG,
     G_OPTION_ARG_FILENAME, file_name, _("Name of the file to preview"),
     "FILENAME"},
    {"toplevel", 't', G_OPTION_FLAG_OPTIONAL_ARG,
     G_OPTION_ARG_STRING, toplevel_name, _("Name of the toplevel to preview"),
     "TOPLEVELNAME"},
    {"listen", 'l', 0, G_OPTION_ARG_NONE, &listen, _("Listen standard input"),
     NULL},
    {"version", 'v', 0, G_OPTION_ARG_NONE, &version,
     _("Display previewer version"), NULL},

    {NULL}
  };

  if (!gtk_init_with_args (&argc, &argv, _("- previews a glade UI definition"),
                           entries, NULL, &error))
    {
      g_printerr (_
                  ("%s\nRun '%s --help' to see a full list of available command line"
                   "options.\n"), error->message, argv[0]);
      g_error_free (error);
      exit (1);
    }

  if (version)
    {
      g_print ("glade-previewer " VERSION "\n");
      exit (0);
    }

  if (listen && *file_name != NULL)
    {
      g_printerr (_
                  ("--listen and --filename must not be simultaneously specified.\n"));
      display_help_and_quit (entries);
    }

  if (!listen && *file_name == NULL)
    {
      g_printerr (_("Either --listen or --filename must be specified.\n"));
      display_help_and_quit (entries);
    }
102 103 104
}

static GtkWidget *
105
get_toplevel (gchar * name, gchar * string, gsize length)
106
{
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  GError *error = NULL;
  GtkWidget *toplevel = NULL;
  GtkWidget *window = NULL;
  GtkBuilder *builder;
  GObject *object;
  GSList *objects;

  builder = gtk_builder_new ();
  if (!gtk_builder_add_from_string (builder, string, length, &error))
    {
      g_printerr (_("Couldn't load builder definition: %s"), error->message);
      g_error_free (error);
      exit (1);
    }

  if (name == NULL)
    {
      objects = gtk_builder_get_objects (builder);

      /* Iterate trough objects and search for a window or widget */
      while (objects != NULL)
        {
          if (GTK_IS_WIDGET (objects->data) && toplevel == NULL)
            {
              toplevel = GTK_WIDGET (objects->data);
            }
          if (GTK_IS_WINDOW (objects->data))
            {
              window = GTK_WIDGET (objects->data);
              break;
            }
          objects = objects->next;
        }

      if (window != NULL)
        {
          toplevel = window;
        }
      else if (toplevel == NULL)
        {
          g_printerr (_("UI definition has no previewable widgets.\n"));
          exit (1);
        }
    }
  else
    {
      object = gtk_builder_get_object (builder, name);

      if (object == NULL)
        {
          g_printerr (_("Object %s not found in UI definition.\n"), name);
          exit (1);
        }

      if (!GTK_IS_WIDGET (object))
        {
          g_printerr (_("Object is not previewable.\n"));
          exit (1);
        }

      toplevel = GTK_WIDGET (object);
    }

  g_object_unref (builder);
  return toplevel;
172 173 174
}

static void
175
preview_widget (gchar * name, gchar * buffer, gsize length)
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
  GtkWidget *widget;
  GtkWidget *widget_parent;
  GtkWidget *window;

  widget = get_toplevel (name, buffer, length);

  if (GTK_IS_WINDOW (widget))
    {
      window = widget;
    }
  else
    {
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (window), _("Preview"));

      /* Reparenting snippet */
      g_object_ref (widget);
      widget_parent = gtk_widget_get_parent (widget);
      if (widget_parent != NULL)
        gtk_container_remove (GTK_CONTAINER (widget_parent), widget);
      gtk_container_add (GTK_CONTAINER (window), widget);
      g_object_unref (widget);
    }

  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_widget_show_all (window);
203 204 205 206 207
}

static GIOChannel *
channel_from_stream (gint stream)
{
208
  GIOChannel *channel;
209

210 211 212 213 214
#ifdef WINDOWS
  channel = g_io_channel_win32_new_fd (stream);
#else
  channel = g_io_channel_unix_new (stream);
#endif
215

216
  return channel;
217 218 219
}

static void
220
preview_file (gchar * toplevel_name, gchar * file_name)
221
{
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  gchar *buffer;
  gsize length;
  GError *error = NULL;
  GIOChannel *input;
  gint stream;

  stream = fileno (fopen (file_name, "r"));
  input = channel_from_stream (stream);

  if (g_io_channel_read_to_end (input, &buffer, &length, &error) !=
      G_IO_STATUS_NORMAL)
    {
      g_printerr (_("Error: %s.\n"), error->message);
      g_error_free (error);
      exit (1);
    }

  preview_widget (toplevel_name, buffer, length);

  g_free (buffer);
  g_io_channel_unref (input);
243 244 245
}

static gchar *
246
read_buffer (GIOChannel * source)
247
{
248 249 250 251 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
  gchar *buffer;
  gchar *token;
  gchar *tmp;
  GError *error = NULL;

  if (g_io_channel_read_line (source, &token, NULL, NULL, &error) !=
      G_IO_STATUS_NORMAL)
    {
      g_printerr (_("Error: %s.\n"), error->message);
      g_error_free (error);
      exit (1);
    }

  /* Check for quit token */
  if (g_strcmp0 ("<quit>", token) == 0)
    {
      g_free (token);
      return NULL;
    }

  /* Loop to load the UI */
  buffer = g_strdup (token);
  do
    {
      g_free (token);
      if (g_io_channel_read_line (source, &token, NULL, NULL, &error) !=
          G_IO_STATUS_NORMAL)
        {
          g_printerr (_("Error: %s.\n"), error->message);
          g_error_free (error);
          exit (1);
        }
      tmp = buffer;
      buffer = g_strconcat (buffer, token, NULL);
      g_free (tmp);
    }
  while (g_strcmp0 ("</interface>\n", token) != 0);
  g_free (token);

  return buffer;
288 289 290
}

static gboolean
291
on_data_incoming (GIOChannel * source, GIOCondition condition, gpointer data)
292
{
293 294
  gchar *buffer;
  gsize length;
295

296
  gchar *toplevel_name = (gchar *) data;
297

298 299 300 301 302 303
  buffer = read_buffer (source);
  if (buffer == NULL)
    {
      gtk_main_quit ();
      return FALSE;
    }
304

305 306 307 308 309
  if (condition & G_IO_HUP)
    {
      g_printerr (_("Broken pipe!\n"));
      exit (1);
    }
310

311
  length = strlen (buffer);
312

313 314
  preview_widget (toplevel_name, buffer, length);
  g_free (buffer);
315

316
  return TRUE;
317 318 319
}

static void
320
start_listener (gchar * toplevel_name)
321
{
322 323
  GIOChannel *input;
  gint stream;
324

325 326
  stream = fileno (stdin);
  input = channel_from_stream (stream);
327

328
  g_io_add_watch (input, G_IO_IN | G_IO_HUP, on_data_incoming, toplevel_name);
329 330
}

331 332
int
main (int argc, char **argv)
333
{
334 335
  gchar *file_name;
  gchar *toplevel_name;
336

337
#ifdef ENABLE_NLS
338 339 340 341
  setlocale (LC_ALL, "");
  bindtextdomain (GETTEXT_PACKAGE, glade_app_get_locale_dir ());
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
342
#endif
343

344
  parse_arguments (argc, argv, &toplevel_name, &file_name);
345

346 347 348 349 350 351 352 353
  if (file_name != NULL)
    {
      preview_file (toplevel_name, file_name);
    }
  else
    {
      start_listener (toplevel_name);
    }
354

355
  gtk_main ();
356

357
  return 0;
358
}