2019-02-07 09:12:09 +00:00
|
|
|
/* Copyright 2015 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* GTK+ 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.
|
|
|
|
*
|
|
|
|
* GLib 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 GTK+; see the file COPYING. If not,
|
|
|
|
* see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Matthias Clasen
|
|
|
|
*/
|
|
|
|
|
2022-04-17 15:59:06 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2019-02-07 09:12:09 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <glib/gprintf.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include "gtkbuilderprivate.h"
|
2020-05-18 10:04:15 +00:00
|
|
|
#include "gtk-builder-tool.h"
|
2022-10-19 15:41:35 +00:00
|
|
|
#include "fake-scope.h"
|
|
|
|
|
2019-02-07 09:12:09 +00:00
|
|
|
|
|
|
|
static GType
|
2020-07-24 18:40:36 +00:00
|
|
|
make_fake_type (const char *type_name,
|
|
|
|
const char *parent_name)
|
2019-02-07 09:12:09 +00:00
|
|
|
{
|
|
|
|
GType parent_type;
|
|
|
|
GTypeQuery query;
|
|
|
|
|
|
|
|
parent_type = g_type_from_name (parent_name);
|
|
|
|
if (parent_type == G_TYPE_INVALID)
|
|
|
|
{
|
|
|
|
g_printerr ("Failed to lookup template parent type %s\n", parent_name);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_type_query (parent_type, &query);
|
|
|
|
return g_type_register_static_simple (parent_type,
|
|
|
|
type_name,
|
|
|
|
query.class_size,
|
|
|
|
NULL,
|
|
|
|
query.instance_size,
|
|
|
|
NULL,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
2022-10-19 15:41:35 +00:00
|
|
|
/* {{{ Deprecations */
|
|
|
|
|
2019-02-07 17:24:49 +00:00
|
|
|
static gboolean
|
2022-10-19 15:41:35 +00:00
|
|
|
is_deprecated (const char *name)
|
2022-10-19 06:46:52 +00:00
|
|
|
{
|
|
|
|
const char *names[] = {
|
|
|
|
"GtkAppChooser",
|
|
|
|
"GtkAppChooserButton",
|
|
|
|
"GtkAppChooserDialog",
|
|
|
|
"GtkAppChooserWidget",
|
|
|
|
"GtkCellAreaBox",
|
|
|
|
"GtkCellAreaBoxContext",
|
|
|
|
"GtkCellArea",
|
|
|
|
"GtkCellEditable",
|
|
|
|
"GtkCellLayout",
|
|
|
|
"GtkCellRendererAccel",
|
|
|
|
"GtkCellRenderer",
|
|
|
|
"GtkCellRendererCombo",
|
|
|
|
"GtkCellRendererPixbuf",
|
|
|
|
"GtkCellRendererProgress",
|
|
|
|
"GtkCellRendererSpin",
|
|
|
|
"GtkCellRendererSpinner",
|
|
|
|
"GtkCellRendererText",
|
|
|
|
"GtkCellRendererToggle",
|
|
|
|
"GtkCellView",
|
|
|
|
"GtkComboBox",
|
|
|
|
"GtkComboBoxText",
|
|
|
|
"GtkEntryCompletion",
|
|
|
|
"GtkIconView",
|
|
|
|
"GtkListStore",
|
|
|
|
"GtkStyleContext",
|
|
|
|
"GtkTreeModel",
|
|
|
|
"GtkTreeModelFilter",
|
|
|
|
"GtkTreeModelSort",
|
|
|
|
"GtkTreePopover",
|
|
|
|
"GtkTreeSelection",
|
|
|
|
"GtkTreeSortable",
|
|
|
|
"GtkTreeStore",
|
|
|
|
"GtkTreeView",
|
|
|
|
"GtkTreeViewColumn",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2022-10-19 15:41:35 +00:00
|
|
|
return g_strv_contains (names, name);
|
2022-10-19 06:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2022-10-19 15:41:35 +00:00
|
|
|
fake_scope_check_deprecations (FakeScope *self,
|
|
|
|
GError **error)
|
2022-10-19 06:46:52 +00:00
|
|
|
{
|
2022-10-19 15:41:35 +00:00
|
|
|
GPtrArray *types;
|
2022-10-19 06:46:52 +00:00
|
|
|
GString *s;
|
|
|
|
|
2022-10-19 15:41:35 +00:00
|
|
|
types = fake_scope_get_types (self);
|
|
|
|
|
2022-10-19 06:46:52 +00:00
|
|
|
s = g_string_new ("");
|
|
|
|
|
2022-10-19 15:41:35 +00:00
|
|
|
for (int i = 0; i < types->len; i++)
|
2022-10-19 06:46:52 +00:00
|
|
|
{
|
2022-10-19 15:41:35 +00:00
|
|
|
const char *name = g_ptr_array_index (types, i);
|
2022-10-19 06:46:52 +00:00
|
|
|
|
2022-10-19 15:41:35 +00:00
|
|
|
if (is_deprecated (name))
|
2022-10-19 06:46:52 +00:00
|
|
|
{
|
|
|
|
if (s->len == 0)
|
|
|
|
g_string_append (s, "Deprecated types:\n");
|
2022-10-19 15:41:35 +00:00
|
|
|
g_string_append_printf (s, "%s", name);
|
2022-10-19 06:46:52 +00:00
|
|
|
g_string_append (s, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->len > 0)
|
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, s->str);
|
|
|
|
|
|
|
|
g_string_free (s, TRUE);
|
|
|
|
|
|
|
|
return *error == NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-19 15:41:35 +00:00
|
|
|
/* }}} */
|
|
|
|
|
2022-10-19 14:30:36 +00:00
|
|
|
static gboolean
|
|
|
|
validate_template (const char *filename,
|
|
|
|
const char *type_name,
|
|
|
|
const char *parent_name,
|
|
|
|
gboolean deprecations)
|
|
|
|
{
|
|
|
|
GType template_type;
|
|
|
|
GObject *object;
|
2022-10-19 15:41:35 +00:00
|
|
|
FakeScope *scope;
|
2022-10-19 14:30:36 +00:00
|
|
|
GtkBuilder *builder;
|
|
|
|
GError *error = NULL;
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
/* Only make a fake type if it doesn't exist yet.
|
|
|
|
* This lets us e.g. validate the GtkFileChooserWidget template.
|
|
|
|
*/
|
|
|
|
template_type = g_type_from_name (type_name);
|
|
|
|
if (template_type == G_TYPE_INVALID)
|
|
|
|
template_type = make_fake_type (type_name, parent_name);
|
|
|
|
|
|
|
|
object = g_object_new (template_type, NULL);
|
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
g_printerr ("Failed to create an instance of the template type %s\n", type_name);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
builder = gtk_builder_new ();
|
2022-10-19 15:41:35 +00:00
|
|
|
scope = fake_scope_new ();
|
|
|
|
gtk_builder_set_scope (builder, GTK_BUILDER_SCOPE (scope));
|
2022-10-19 14:30:36 +00:00
|
|
|
ret = gtk_builder_extend_with_template (builder, object, template_type, " ", 1, &error);
|
|
|
|
if (ret)
|
|
|
|
ret = gtk_builder_add_from_file (builder, filename, &error);
|
|
|
|
if (ret && deprecations)
|
2022-10-19 15:41:35 +00:00
|
|
|
ret = fake_scope_check_deprecations (scope, &error);
|
|
|
|
g_object_unref (scope);
|
2022-10-19 14:30:36 +00:00
|
|
|
g_object_unref (builder);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
parse_template_error (const char *message,
|
|
|
|
char **class_name,
|
|
|
|
char **parent_name)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = strstr (message, "(class '");
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
*class_name = g_strdup (p + strlen ("(class '"));
|
|
|
|
p = strstr (*class_name, "'");
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
p = strstr (message, ", parent '");
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
*parent_name = g_strdup (p + strlen (", parent '"));
|
|
|
|
p = strstr (*parent_name, "'");
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return *class_name && *parent_name;
|
|
|
|
}
|
|
|
|
|
2022-10-19 06:46:52 +00:00
|
|
|
static gboolean
|
|
|
|
validate_file (const char *filename,
|
|
|
|
gboolean deprecations)
|
2019-02-07 09:12:09 +00:00
|
|
|
{
|
2022-10-19 15:41:35 +00:00
|
|
|
FakeScope *scope;
|
2019-02-07 09:12:09 +00:00
|
|
|
GtkBuilder *builder;
|
|
|
|
GError *error = NULL;
|
2022-10-19 14:30:36 +00:00
|
|
|
gboolean ret;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *class_name = NULL;
|
|
|
|
char *parent_name = NULL;
|
2019-02-07 09:12:09 +00:00
|
|
|
|
|
|
|
builder = gtk_builder_new ();
|
2022-10-19 15:41:35 +00:00
|
|
|
scope = fake_scope_new ();
|
|
|
|
gtk_builder_set_scope (builder, GTK_BUILDER_SCOPE (scope));
|
2019-02-07 09:12:09 +00:00
|
|
|
ret = gtk_builder_add_from_file (builder, filename, &error);
|
2022-10-19 06:46:52 +00:00
|
|
|
if (ret && deprecations)
|
2022-10-19 15:41:35 +00:00
|
|
|
ret = fake_scope_check_deprecations (scope, &error);
|
|
|
|
g_object_unref (scope);
|
2019-02-07 09:12:09 +00:00
|
|
|
g_object_unref (builder);
|
|
|
|
|
2022-10-19 14:30:36 +00:00
|
|
|
if (!ret)
|
2019-02-07 09:12:09 +00:00
|
|
|
{
|
2022-10-19 06:46:52 +00:00
|
|
|
if (g_error_matches (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_UNHANDLED_TAG) &&
|
2019-02-07 09:12:09 +00:00
|
|
|
parse_template_error (error->message, &class_name, &parent_name))
|
|
|
|
{
|
2022-10-19 14:30:36 +00:00
|
|
|
ret = validate_template (filename, class_name, parent_name, deprecations);
|
2019-02-07 09:12:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-01 19:47:42 +00:00
|
|
|
g_printerr ("%s\n", error->message);
|
2019-02-07 09:12:09 +00:00
|
|
|
}
|
2022-10-19 14:30:36 +00:00
|
|
|
|
|
|
|
g_error_free (error);
|
2019-02-07 09:12:09 +00:00
|
|
|
}
|
2019-02-07 17:24:49 +00:00
|
|
|
|
2022-10-19 14:30:36 +00:00
|
|
|
return ret;
|
2019-02-07 09:12:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 17:24:49 +00:00
|
|
|
void
|
2020-05-18 10:04:15 +00:00
|
|
|
do_validate (int *argc, const char ***argv)
|
2019-02-07 17:24:49 +00:00
|
|
|
{
|
2022-04-17 15:59:06 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
char **filenames = NULL;
|
2022-10-19 06:46:52 +00:00
|
|
|
gboolean deprecations = FALSE;
|
2022-04-17 15:59:06 +00:00
|
|
|
GOptionContext *context;
|
|
|
|
const GOptionEntry entries[] = {
|
2022-10-19 06:46:52 +00:00
|
|
|
{ "deprecations", 0, 0, G_OPTION_ARG_NONE, &deprecations, NULL, NULL },
|
2022-04-17 15:59:06 +00:00
|
|
|
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
|
|
|
|
{ NULL, }
|
|
|
|
};
|
2019-02-07 17:24:49 +00:00
|
|
|
int i;
|
|
|
|
|
2022-04-17 15:59:06 +00:00
|
|
|
g_set_prgname ("gtk4-builder-tool validate");
|
|
|
|
context = g_option_context_new (NULL);
|
|
|
|
g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
|
|
|
|
g_option_context_add_main_entries (context, entries, NULL);
|
|
|
|
g_option_context_set_summary (context, _("Validate the file."));
|
|
|
|
|
|
|
|
if (!g_option_context_parse (context, argc, (char ***)argv, &error))
|
|
|
|
{
|
|
|
|
g_printerr ("%s\n", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_option_context_free (context);
|
|
|
|
|
|
|
|
for (i = 0; filenames[i]; i++)
|
2019-02-07 17:24:49 +00:00
|
|
|
{
|
2022-10-19 06:46:52 +00:00
|
|
|
if (!validate_file (filenames[i], deprecations))
|
2019-02-07 17:24:49 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
2022-04-17 15:59:06 +00:00
|
|
|
|
|
|
|
g_strfreev (filenames);
|
2019-02-07 17:24:49 +00:00
|
|
|
}
|
2022-10-19 15:41:35 +00:00
|
|
|
|
|
|
|
/* vim:set foldmethod=marker expandtab: */
|