Blame gladeui/glade-previewer-main.c

Packit 1e8aac
/*
Packit 1e8aac
 * Copyright (C) 2010 Marco Diego Aurélio Mesquita
Packit 1e8aac
 *
Packit 1e8aac
 * This program is free software; you can redistribute it and/or modify
Packit 1e8aac
 * it under the terms of the GNU Lesser General Public License as
Packit 1e8aac
 * published by the Free Software Foundation; either version 2 of the
Packit 1e8aac
 * License, or (at your option) any later version.
Packit 1e8aac
 *
Packit 1e8aac
 * This program is distributed in the hope that it will be useful,
Packit 1e8aac
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1e8aac
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 1e8aac
 * GNU General Public License for more details.
Packit 1e8aac
 *
Packit 1e8aac
 * You should have received a copy of the GNU Lesser General Public License
Packit 1e8aac
 * along with this program; if not, write to the Free Software
Packit 1e8aac
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 1e8aac
 *
Packit 1e8aac
 * Authors:
Packit 1e8aac
 *   Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Packit 1e8aac
 */
Packit 1e8aac
Packit 1e8aac
#include <config.h>
Packit 1e8aac
Packit 1e8aac
#include <gladeui/glade.h>
Packit 1e8aac
Packit 1e8aac
#include <stdlib.h>
Packit 1e8aac
#include <locale.h>
Packit 1e8aac
#include <glib/gi18n-lib.h>
Packit 1e8aac
#include <glib/gstdio.h>
Packit 1e8aac
Packit 1e8aac
#include "glade-previewer.h"
Packit 1e8aac
#include "glade-preview-template.h"
Packit 1e8aac
#include "glade-preview-tokens.h"
Packit 1e8aac
Packit 1e8aac
typedef struct
Packit 1e8aac
{
Packit 1e8aac
  GladePreviewer *preview;
Packit 1e8aac
  gchar *file_name, *toplevel;
Packit 1e8aac
  gboolean is_template;
Packit 1e8aac
} GladePreviewerApp;
Packit 1e8aac
Packit 1e8aac
static GObject *
Packit 1e8aac
get_toplevel (GtkBuilder *builder, gchar *name)
Packit 1e8aac
{
Packit 1e8aac
  GObject *toplevel = NULL;
Packit 1e8aac
  GObject *object;
Packit 1e8aac
Packit 1e8aac
  if (name == NULL)
Packit 1e8aac
    {
Packit 1e8aac
      GSList *l, *objects = gtk_builder_get_objects (builder);
Packit 1e8aac
Packit 1e8aac
      /* Iterate trough objects and search for a window or widget */
Packit 1e8aac
      for (l = objects; l; l = g_slist_next (l))
Packit 1e8aac
        {
Packit 1e8aac
          GObject *obj = l->data;
Packit 1e8aac
Packit 1e8aac
          if (!GTK_IS_WIDGET (obj) || gtk_widget_get_parent (GTK_WIDGET (obj)))
Packit 1e8aac
            continue;
Packit 1e8aac
Packit 1e8aac
          if (toplevel == NULL)
Packit 1e8aac
            toplevel = obj;
Packit 1e8aac
          else if (GTK_IS_WINDOW (obj))
Packit 1e8aac
            toplevel = obj;
Packit 1e8aac
        }
Packit 1e8aac
Packit 1e8aac
      g_slist_free (objects);
Packit 1e8aac
      if (toplevel == NULL)
Packit 1e8aac
        {
Packit 1e8aac
          g_printerr (_("UI definition has no previewable widgets.\n"));
Packit 1e8aac
          exit (1);
Packit 1e8aac
        }
Packit 1e8aac
    }
Packit 1e8aac
  else
Packit 1e8aac
    {
Packit 1e8aac
      object = gtk_builder_get_object (builder, name);
Packit 1e8aac
Packit 1e8aac
      if (object == NULL)
Packit 1e8aac
        {
Packit 1e8aac
          g_printerr (_("Object %s not found in UI definition.\n"), name);
Packit 1e8aac
          exit (1);
Packit 1e8aac
        }
Packit 1e8aac
Packit 1e8aac
      if (!GTK_IS_WIDGET (object))
Packit 1e8aac
        {
Packit 1e8aac
          g_printerr (_("Object is not previewable.\n"));
Packit 1e8aac
          exit (1);
Packit 1e8aac
        }
Packit 1e8aac
Packit 1e8aac
      toplevel = object;
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  return g_object_ref_sink (toplevel);
Packit 1e8aac
}
Packit 1e8aac
Packit 1e8aac
static GObject *
Packit 1e8aac
get_toplevel_from_string (GladePreviewerApp *app, gchar *name, gchar *string, gsize size)
Packit 1e8aac
{
Packit 1e8aac
  gchar *wd = NULL;
Packit 1e8aac
  GObject *retval = NULL;
Packit 1e8aac
Packit 1e8aac
  /* We need to change the working directory so builder get a chance to load resources */
Packit 1e8aac
  if (app->file_name)
Packit 1e8aac
    {
Packit 1e8aac
      gchar *dirname = g_path_get_dirname (app->file_name);
Packit 1e8aac
      wd = g_get_current_dir ();
Packit 1e8aac
      g_chdir (dirname);
Packit 1e8aac
      g_free (dirname);
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  /* We use template flag as a hint since the user can turn on and off template
Packit 1e8aac
   * while the preview is live.
Packit 1e8aac
   */
Packit 1e8aac
  if (app->is_template)
Packit 1e8aac
    retval = glade_preview_template_object_new (string, size,
Packit 1e8aac
                                                  glade_previewer_connect_function,
Packit 1e8aac
                                                  app->preview);
Packit 1e8aac
Packit 1e8aac
  if (!retval)
Packit 1e8aac
    {
Packit 1e8aac
      GtkBuilder *builder = gtk_builder_new ();
Packit 1e8aac
      GError *error = NULL;
Packit 1e8aac
Packit 1e8aac
      /* We do not know if its a template yet */
Packit 1e8aac
      app->is_template = FALSE;
Packit 1e8aac
Packit 1e8aac
      if (gtk_builder_add_from_string (builder, string, size, &error))
Packit 1e8aac
        {
Packit 1e8aac
          gtk_builder_connect_signals_full (builder,
Packit 1e8aac
                                            glade_previewer_connect_function,
Packit 1e8aac
                                            app->preview);
Packit 1e8aac
          retval = get_toplevel (builder, name);
Packit 1e8aac
        }
Packit 1e8aac
      else
Packit 1e8aac
        {
Packit 1e8aac
          if (error->code == GTK_BUILDER_ERROR_UNHANDLED_TAG &&
Packit 1e8aac
              (retval = glade_preview_template_object_new (string, size,
Packit 1e8aac
                                                             glade_previewer_connect_function,
Packit 1e8aac
                                                             app->preview)))
Packit 1e8aac
            {
Packit 1e8aac
              /* At this point we know it is a template, so keep a hint for next time */
Packit 1e8aac
              app->is_template = TRUE;
Packit 1e8aac
            }
Packit 1e8aac
          else
Packit 1e8aac
            {
Packit 1e8aac
              gchar *message = g_strdup_printf (_("Couldn't load builder definition: %s"), error->message);
Packit 1e8aac
              glade_previewer_set_message (app->preview, GTK_MESSAGE_ERROR, message);
Packit 1e8aac
              g_free (message);
Packit 1e8aac
            }
Packit 1e8aac
Packit 1e8aac
          g_error_free (error);
Packit 1e8aac
        }
Packit 1e8aac
Packit 1e8aac
      g_object_unref (builder);
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  /* restore directory */
Packit 1e8aac
  if (wd)
Packit 1e8aac
    {
Packit 1e8aac
      g_chdir (wd);
Packit 1e8aac
      g_free (wd);
Packit 1e8aac
    }
Packit 1e8aac
  
Packit 1e8aac
  return retval;
Packit 1e8aac
}
Packit 1e8aac
Packit 1e8aac
static gchar *
Packit 1e8aac
read_buffer (GIOChannel * source)
Packit 1e8aac
{
Packit 1e8aac
  gchar *buffer;
Packit 1e8aac
  gchar *token;
Packit 1e8aac
  gchar *tmp;
Packit 1e8aac
  GError *error = NULL;
Packit 1e8aac
Packit 1e8aac
  if (g_io_channel_read_line (source, &token, NULL, NULL, &error) !=
Packit 1e8aac
      G_IO_STATUS_NORMAL)
Packit 1e8aac
    {
Packit 1e8aac
      g_printerr (_("Error: %s.\n"), error->message);
Packit 1e8aac
      g_error_free (error);
Packit 1e8aac
      exit (1);
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  /* Check for quit token */
Packit 1e8aac
  if (g_strcmp0 (QUIT_TOKEN, token) == 0)
Packit 1e8aac
    {
Packit 1e8aac
      g_free (token);
Packit 1e8aac
      return NULL;
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  /* Loop to load the UI */
Packit 1e8aac
  buffer = g_strdup (token);
Packit 1e8aac
  do
Packit 1e8aac
    {
Packit 1e8aac
      g_free (token);
Packit 1e8aac
      if (g_io_channel_read_line (source, &token, NULL, NULL, &error) !=
Packit 1e8aac
          G_IO_STATUS_NORMAL)
Packit 1e8aac
        {
Packit 1e8aac
          g_printerr (_("Error: %s.\n"), error->message);
Packit 1e8aac
          g_error_free (error);
Packit 1e8aac
          exit (1);
Packit 1e8aac
        }
Packit 1e8aac
      tmp = buffer;
Packit 1e8aac
      buffer = g_strconcat (buffer, token, NULL);
Packit 1e8aac
      g_free (tmp);
Packit 1e8aac
    }
Packit 1e8aac
  while (g_strcmp0 ("</interface>\n", token) != 0);
Packit 1e8aac
  g_free (token);
Packit 1e8aac
Packit 1e8aac
  return buffer;
Packit 1e8aac
}
Packit 1e8aac
Packit 1e8aac
static gboolean
Packit 1e8aac
on_data_incoming (GIOChannel *source, GIOCondition condition, gpointer data)
Packit 1e8aac
{
Packit 1e8aac
  GladePreviewerApp *app = data;
Packit 1e8aac
  GObject *new_widget;
Packit 1e8aac
  gchar *buffer;
Packit 1e8aac
Packit 1e8aac
  buffer = read_buffer (source);
Packit 1e8aac
  if (buffer == NULL)
Packit 1e8aac
    {
Packit 1e8aac
      gtk_main_quit ();
Packit 1e8aac
      return FALSE;
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  if (condition & G_IO_HUP)
Packit 1e8aac
    {
Packit 1e8aac
      g_printerr (_("Broken pipe!\n"));
Packit 1e8aac
      exit (1);
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  /* We have an update */
Packit 1e8aac
  if (g_str_has_prefix (buffer, UPDATE_TOKEN))
Packit 1e8aac
    {
Packit 1e8aac
     gchar **split_buffer = g_strsplit_set (buffer + UPDATE_TOKEN_SIZE, "\n", 2);
Packit 1e8aac
Packit 1e8aac
      if (!split_buffer)
Packit 1e8aac
        {
Packit 1e8aac
          g_free (buffer);
Packit 1e8aac
          return FALSE;
Packit 1e8aac
        }
Packit 1e8aac
Packit 1e8aac
      new_widget = get_toplevel_from_string (app, split_buffer[0], split_buffer[1], -1);
Packit 1e8aac
Packit 1e8aac
      g_strfreev (split_buffer);
Packit 1e8aac
    }
Packit 1e8aac
  else
Packit 1e8aac
    {
Packit 1e8aac
      new_widget = get_toplevel_from_string (app, app->toplevel, buffer, -1);
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  if (new_widget)
Packit 1e8aac
    {
Packit 1e8aac
      glade_previewer_set_widget (app->preview, GTK_WIDGET (new_widget));
Packit 1e8aac
      gtk_widget_show (GTK_WIDGET (new_widget));
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  glade_previewer_present (app->preview);
Packit 1e8aac
  
Packit 1e8aac
  g_free (buffer);
Packit 1e8aac
  
Packit 1e8aac
  return TRUE;
Packit 1e8aac
}
Packit 1e8aac
Packit 1e8aac
static GladePreviewerApp *
Packit 1e8aac
glade_previewer_app_new (gchar *filename, gchar *toplevel)
Packit 1e8aac
{
Packit 1e8aac
  GladePreviewerApp *app = g_new0 (GladePreviewerApp, 1);
Packit 1e8aac
Packit 1e8aac
  app->preview = GLADE_PREVIEWER (glade_previewer_new ());
Packit 1e8aac
  g_object_ref_sink (app->preview);
Packit 1e8aac
Packit 1e8aac
  app->file_name = g_strdup (filename);
Packit 1e8aac
  app->toplevel = g_strdup (toplevel);
Packit 1e8aac
Packit 1e8aac
  return app;
Packit 1e8aac
}
Packit 1e8aac
Packit 1e8aac
static void
Packit 1e8aac
glade_previewer_free (GladePreviewerApp *app)
Packit 1e8aac
{
Packit 1e8aac
  g_object_unref (app->preview);
Packit 1e8aac
  g_free (app->file_name);
Packit 1e8aac
  g_free (app->toplevel);
Packit 1e8aac
  g_free (app);
Packit 1e8aac
}
Packit 1e8aac
Packit 1e8aac
static gboolean listen = FALSE;
Packit 1e8aac
static gboolean version = FALSE;
Packit 1e8aac
static gboolean slideshow = FALSE;
Packit 1e8aac
static gboolean template = FALSE;
Packit 1e8aac
static gboolean print_handler = FALSE;
Packit 1e8aac
static gchar *file_name = NULL;
Packit 1e8aac
static gchar *toplevel_name = NULL;
Packit 1e8aac
static gchar *css_file_name = NULL;
Packit 1e8aac
static gchar *screenshot_file_name = NULL;
Packit 1e8aac
Packit 1e8aac
static GOptionEntry option_entries[] =
Packit 1e8aac
{
Packit 1e8aac
    {"filename", 'f', 0, G_OPTION_ARG_FILENAME, &file_name, N_("Name of the file to preview"), "FILENAME"},
Packit 1e8aac
    {"template", 0, 0, G_OPTION_ARG_NONE, &template, N_("Creates dummy widget class to load a template"), NULL},
Packit 1e8aac
    {"toplevel", 't', 0, G_OPTION_ARG_STRING, &toplevel_name, N_("Name of the toplevel to preview"), "TOPLEVELNAME"},
Packit 1e8aac
    {"screenshot", 0, 0, G_OPTION_ARG_FILENAME, &screenshot_file_name, N_("File name to save a screenshot"), NULL},
Packit 1e8aac
    {"css", 0, 0, G_OPTION_ARG_FILENAME, &css_file_name, N_("CSS file to use"), NULL},
Packit 1e8aac
    {"listen", 'l', 0, G_OPTION_ARG_NONE, &listen, N_("Listen standard input"), NULL},
Packit 1e8aac
    {"slideshow", 0, 0, G_OPTION_ARG_NONE, &slideshow, N_("make a slideshow of every toplevel widget by adding them in a GtkStack"), NULL},
Packit 1e8aac
    {"print-handler", 0, 0, G_OPTION_ARG_NONE, &print_handler, N_("Print handlers signature on invocation"), NULL},
Packit 1e8aac
    {"version", 'v', 0, G_OPTION_ARG_NONE, &version, N_("Display previewer version"), NULL},
Packit 1e8aac
    {NULL}
Packit 1e8aac
};
Packit 1e8aac
Packit 1e8aac
int
Packit 1e8aac
main (int argc, char **argv)
Packit 1e8aac
{
Packit 1e8aac
  GladePreviewerApp *app;
Packit 1e8aac
  GOptionContext *context;
Packit 1e8aac
  GError *error = NULL;
Packit 1e8aac
  GObject *toplevel = NULL;
Packit 1e8aac
Packit 1e8aac
#ifdef ENABLE_NLS
Packit 1e8aac
  setlocale (LC_ALL, "");
Packit 1e8aac
  bindtextdomain (GETTEXT_PACKAGE, glade_app_get_locale_dir ());
Packit 1e8aac
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Packit 1e8aac
  textdomain (GETTEXT_PACKAGE);
Packit 1e8aac
#endif
Packit 1e8aac
Packit 1e8aac
  context = g_option_context_new (_("- previews a glade UI definition"));
Packit 1e8aac
  g_option_context_add_main_entries (context, option_entries, GETTEXT_PACKAGE);
Packit 1e8aac
  g_option_context_add_group (context, gtk_get_option_group (TRUE));
Packit 1e8aac
  
Packit 1e8aac
  if (!g_option_context_parse (context, &argc, &argv, &error))
Packit 1e8aac
    {
Packit 1e8aac
      g_printerr (_("%s\nRun '%s --help' to see a full list of available command line "
Packit 1e8aac
                   "options.\n"), error->message, argv[0]);
Packit 1e8aac
      g_error_free (error);
Packit 1e8aac
      g_option_context_free (context);
Packit 1e8aac
      return 1;
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  g_option_context_free (context);
Packit 1e8aac
Packit 1e8aac
  if (version)
Packit 1e8aac
    {
Packit 1e8aac
      g_print ("glade-previewer " VERSION "\n");
Packit 1e8aac
      return 0;
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  if (!listen && !file_name)
Packit 1e8aac
    {
Packit 1e8aac
      g_printerr (_("Either --listen or --filename must be specified.\n"));
Packit 1e8aac
      return 0;
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  gtk_init (&argc, &argv);
Packit 1e8aac
  glade_app_get ();
Packit 1e8aac
Packit 1e8aac
  app = glade_previewer_app_new (file_name, toplevel_name);
Packit 1e8aac
Packit 1e8aac
  app->is_template = template;
Packit 1e8aac
Packit 1e8aac
  if (print_handler)
Packit 1e8aac
    glade_previewer_set_print_handlers (GLADE_PREVIEWER (app->preview), TRUE);
Packit 1e8aac
Packit 1e8aac
  if (css_file_name)
Packit 1e8aac
    glade_previewer_set_css_file (app->preview, css_file_name);
Packit 1e8aac
Packit 1e8aac
  if (listen)
Packit 1e8aac
    {
Packit 1e8aac
#ifdef WINDOWS
Packit 1e8aac
      GIOChannel *input = g_io_channel_win32_new_fd (fileno (stdin));
Packit 1e8aac
#else
Packit 1e8aac
      GIOChannel *input = g_io_channel_unix_new (fileno (stdin));
Packit 1e8aac
#endif
Packit 1e8aac
Packit 1e8aac
      g_io_add_watch (input, G_IO_IN | G_IO_HUP, on_data_incoming, app);
Packit 1e8aac
Packit 1e8aac
      gtk_main ();
Packit 1e8aac
    }
Packit 1e8aac
  else if (template)
Packit 1e8aac
    {
Packit 1e8aac
      gchar *contents = NULL;
Packit 1e8aac
      gsize size;
Packit 1e8aac
Packit 1e8aac
      if (g_file_get_contents (file_name, &contents, &size, NULL))
Packit 1e8aac
        toplevel = get_toplevel_from_string (app, NULL, contents, size);
Packit 1e8aac
Packit 1e8aac
      g_free (contents);
Packit 1e8aac
    }
Packit 1e8aac
  else if (file_name)
Packit 1e8aac
    {
Packit 1e8aac
      GtkBuilder *builder = gtk_builder_new ();
Packit 1e8aac
      GError *error = NULL;
Packit 1e8aac
Packit 1e8aac
      /* Use from_file() function gives builder a chance to know where to load resources from */
Packit 1e8aac
      if (!gtk_builder_add_from_file (builder, app->file_name, &error))
Packit 1e8aac
        {
Packit 1e8aac
          g_printerr (_("Couldn't load builder definition: %s"), error->message);
Packit 1e8aac
          g_error_free (error);
Packit 1e8aac
          return 1;
Packit 1e8aac
        }
Packit 1e8aac
Packit 1e8aac
      if (slideshow)
Packit 1e8aac
        {
Packit 1e8aac
          GSList *objects = gtk_builder_get_objects (builder);
Packit 1e8aac
Packit 1e8aac
          glade_previewer_set_slideshow_widgets (app->preview, objects);
Packit 1e8aac
          glade_previewer_present (app->preview);
Packit 1e8aac
Packit 1e8aac
          if (screenshot_file_name)
Packit 1e8aac
            glade_previewer_slideshow_save (app->preview, screenshot_file_name);
Packit 1e8aac
          else
Packit 1e8aac
            gtk_main ();
Packit 1e8aac
Packit 1e8aac
          g_slist_free (objects);
Packit 1e8aac
        }
Packit 1e8aac
      else
Packit 1e8aac
        {
Packit 1e8aac
          toplevel = get_toplevel (builder, toplevel_name);
Packit 1e8aac
Packit 1e8aac
          gtk_builder_connect_signals_full (builder,
Packit 1e8aac
                                            glade_previewer_connect_function,
Packit 1e8aac
                                            app->preview);
Packit 1e8aac
        }
Packit 1e8aac
Packit 1e8aac
      g_object_unref (builder);
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  if (toplevel)
Packit 1e8aac
    {
Packit 1e8aac
      glade_previewer_set_widget (app->preview, GTK_WIDGET (toplevel));
Packit 1e8aac
      g_object_unref (toplevel);
Packit 1e8aac
      glade_previewer_present (app->preview);
Packit 1e8aac
Packit 1e8aac
      if (screenshot_file_name)
Packit 1e8aac
        glade_previewer_screenshot (app->preview, TRUE, screenshot_file_name);
Packit 1e8aac
      else
Packit 1e8aac
        gtk_main ();
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  /* free unused resources */
Packit 1e8aac
  g_free (file_name);
Packit 1e8aac
  g_free (toplevel_name);
Packit 1e8aac
  g_free (css_file_name);
Packit 1e8aac
  g_free (screenshot_file_name);
Packit 1e8aac
  glade_previewer_free (app);
Packit 1e8aac
Packit 1e8aac
  return 0;
Packit 1e8aac
}