[gnome-builder] glade: refuse to open known GTK 4 templates



commit 60cca669ac10ac23ed4aec5439a6a3793cb5c0ea
Author: Christian Hergert <chergert redhat com>
Date:   Sun Nov 28 11:03:11 2021 -0800

    glade: refuse to open known GTK 4 templates

 src/plugins/glade/gbp-glade-workbench-addin.c | 39 +++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
---
diff --git a/src/plugins/glade/gbp-glade-workbench-addin.c b/src/plugins/glade/gbp-glade-workbench-addin.c
index 8e8d93dec..35f40b0d5 100644
--- a/src/plugins/glade/gbp-glade-workbench-addin.c
+++ b/src/plugins/glade/gbp-glade-workbench-addin.c
@@ -22,6 +22,9 @@
 
 #include "config.h"
 
+#include <fcntl.h>
+#include <glib/gstdio.h>
+
 #include <libide-gui.h>
 
 #include "gbp-glade-page.h"
@@ -69,6 +72,36 @@ find_most_recent_editor (GbpGladeWorkbenchAddin *self)
   return surface;
 }
 
+static gboolean
+is_gtk4_template (GFile *file)
+{
+  g_autofree char *page = NULL;
+  gssize len;
+  int fd;
+
+  if (!g_file_is_native (file))
+    return FALSE;
+
+  fd = g_open (g_file_peek_path (file), O_RDONLY, 0);
+  if (fd == -1)
+    return FALSE;
+
+  page = g_malloc (4096);
+  len = read (fd, page, 4096);
+  close (fd);
+
+  if (len > 0)
+    {
+      page[MIN (len, 4095)] = 0;
+
+      if (strstr (page, "<requires lib=\"gtk\" version=\"4.0\"/>") != NULL ||
+          strstr (page, "<requires lib='gtk' version='4.0'/>") != NULL)
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
 static gboolean
 gbp_glade_workbench_addin_can_open (IdeWorkbenchAddin *addin,
                                     GFile             *file,
@@ -97,6 +130,12 @@ gbp_glade_workbench_addin_can_open (IdeWorkbenchAddin *addin,
       g_strcmp0 (content_type, "application/x-designer") == 0 ||
       (path && g_str_has_suffix (path, ".ui")))
     {
+      /* If the file is local, try to sniff the first page on disk so that
+       * we can ensure it is not a GTK 4 template.
+       */
+      if (is_gtk4_template (file))
+        return FALSE;
+
       /* Be lower priority than editor, because glade does not currently
        * handle templates well enough currently.
        */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]