1/* Copyright 2015 Red Hat, Inc.
2 *
3 * GTK+ is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as
5 * published by the Free Software Foundation; either version 2 of the
6 * License, or (at your option) any later version.
7 *
8 * GLib is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with GTK+; see the file COPYING. If not,
15 * see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Matthias Clasen
18 */
19
20#include <stdlib.h>
21#include <string.h>
22#include <errno.h>
23
24#include <glib/gi18n.h>
25#include <glib/gprintf.h>
26#include <glib/gstdio.h>
27#include <gtk/gtk.h>
28#include "gtkbuilderprivate.h"
29#include "gtk-builder-tool.h"
30
31static void G_GNUC_NORETURN
32usage (void)
33{
34 g_print (_("Usage:\n"
35 " gtk-builder-tool [COMMAND] [OPTION…] FILE\n"
36 "\n"
37 "Commands:\n"
38 " validate Validate the file\n"
39 " simplify Simplify the file\n"
40 " enumerate List all named objects\n"
41 " preview Preview the file\n"
42 "\n"
43 "Simplify Options:\n"
44 " --replace Replace the file\n"
45 " --3to4 Convert from GTK 3 to GTK 4\n"
46 "\n"
47 "Preview Options:\n"
48 " --id=ID Preview only the named object\n"
49 " --css=FILE Use style from CSS file\n"
50 "\n"
51 "Perform various tasks on GtkBuilder .ui files.\n"));
52 exit (status: 1);
53}
54
55/* A simplified version of g_log_writer_default_would_drop(), to avoid
56 * bumping up the required version of GLib to 2.68
57 */
58static gboolean
59would_drop (GLogLevelFlags level,
60 const char *domain)
61{
62 return (level & (G_LOG_LEVEL_ERROR |
63 G_LOG_LEVEL_CRITICAL |
64 G_LOG_LEVEL_WARNING)) == 0;
65}
66
67static GLogWriterOutput
68log_writer_func (GLogLevelFlags level,
69 const GLogField *fields,
70 gsize n_fields,
71 gpointer user_data)
72{
73 gsize i;
74 const char *domain = NULL;
75 const char *message = NULL;
76
77 for (i = 0; i < n_fields; i++)
78 {
79 if (g_strcmp0 (str1: fields[i].key, str2: "GLIB_DOMAIN") == 0)
80 domain = fields[i].value;
81 else if (g_strcmp0 (str1: fields[i].key, str2: "MESSAGE") == 0)
82 message = fields[i].value;
83 }
84
85 if (message != NULL && !would_drop (level, domain))
86 {
87 const char *prefix;
88 switch (level & G_LOG_LEVEL_MASK)
89 {
90 case G_LOG_LEVEL_ERROR:
91 prefix = "ERROR";
92 break;
93 case G_LOG_LEVEL_CRITICAL:
94 prefix = "CRITICAL";
95 break;
96 case G_LOG_LEVEL_WARNING:
97 prefix = "WARNING";
98 break;
99 default:
100 prefix = "INFO";
101 break;
102 }
103 g_printerr (format: "%s-%s: %s\n", domain, prefix, message);
104 }
105
106 return G_LOG_WRITER_HANDLED;
107}
108
109int
110main (int argc, const char *argv[])
111{
112 gboolean has_display;
113
114 g_set_prgname (prgname: "gtk-builder-tool");
115
116 g_log_set_writer_func (func: log_writer_func, NULL, NULL);
117
118 has_display = gtk_init_check ();
119
120 gtk_test_register_all_types ();
121
122 if (argc < 3)
123 usage ();
124
125 if (strcmp (s1: argv[2], s2: "--help") == 0)
126 usage ();
127
128 argv++;
129 argc--;
130
131 if (strcmp (s1: argv[0], s2: "validate") == 0)
132 do_validate (argc: &argc, argv: &argv);
133 else if (strcmp (s1: argv[0], s2: "simplify") == 0)
134 do_simplify (argc: &argc, argv: &argv);
135 else if (strcmp (s1: argv[0], s2: "enumerate") == 0)
136 do_enumerate (argc: &argc, argv: &argv);
137 else if (strcmp (s1: argv[0], s2: "preview") == 0)
138 {
139 if (!has_display)
140 {
141 g_printerr (format: "Could not initialize windowing system\n");
142 return 1;
143 }
144
145 do_preview (argc: &argc, argv: &argv);
146 }
147 else
148 usage ();
149
150 return 0;
151}
152

source code of gtk/tools/gtk-builder-tool.c