[gimp] app, tools: pre-process release items from AppStream as well.



commit 10d8765101becf07de271c40bd9dbf8105a592ac
Author: Jehan <jehan girinstud io>
Date:   Tue Mar 8 16:32:58 2022 +0100

    app, tools: pre-process release items from AppStream as well.
    
    Since we are pre-processing anyway the AppStream metadata file (because
    appstream-glib doesn't pass unknown XML attributes, cf. a previous
    commit), it does feel silly now to continue loading the file at runtime
    too. Let's just pre-process more data into the constructed C files, i.e.
    get the introduction paragraphs and the change items too.
    
    The only other remaining advantage of appstream-glib was that it was
    handling the localization but since we also have these localized strings
    in our gettext files, we can as well translate with gettext as any other
    strings and it works just fine.
    
    It will also get rid of any packaging issue, forgetting to package the
    metadata file (as we had on the Windows installer, and still have on the
    macOS package). Now it will just always work because data is internal.

 app/dialogs/welcome-dialog.c          | 233 ++++++++++++----------------------
 tools/generate-welcome-dialog-data.py |  52 +++++++-
 2 files changed, 125 insertions(+), 160 deletions(-)
---
diff --git a/app/dialogs/welcome-dialog.c b/app/dialogs/welcome-dialog.c
index c3c86ae6b1..94b626adf7 100644
--- a/app/dialogs/welcome-dialog.c
+++ b/app/dialogs/welcome-dialog.c
@@ -20,7 +20,6 @@
 
 #include "config.h"
 
-#include <appstream-glib.h>
 #include <gegl.h>
 #include <gtk/gtk.h>
 #ifdef GDK_WINDOWING_WAYLAND
@@ -60,102 +59,29 @@ static void   welcome_size_allocate                 (GtkWidget      *welcome_dia
 GtkWidget *
 welcome_dialog_create (Gimp *gimp)
 {
-  GtkWidget     *welcome_dialog;
-  AsApp         *app           = NULL;
-  const gchar   *release_notes = NULL;
-  GError        *error         = NULL;
-
-  GList         *windows;
-
-  GtkWidget     *main_vbox;
-  GtkWidget     *stack;
-  GtkWidget     *grid;
-  GtkWidget     *switcher;
-
-  GtkWidget     *scrolled_window;
-  GtkWidget     *vbox;
-  GtkWidget     *hbox;
-  GtkWidget     *image;
-  GtkWidget     *listbox;
-  GtkWidget     *widget;
-
-  gchar         *release_link;
-  gchar         *appdata_path;
-  gchar         *title;
-  gchar         *markup;
-  gchar         *release_introduction = NULL;
-  GList         *release_items        = NULL;
-  gchar         *tmp;
-
-  gint           row;
+  GtkWidget  *welcome_dialog;
+  GList      *windows;
+
+  GtkWidget  *main_vbox;
+  GtkWidget  *stack;
+  GtkWidget  *grid;
+  GtkWidget  *switcher;
+
+  GtkWidget  *scrolled_window;
+  GtkWidget  *vbox;
+  GtkWidget  *hbox;
+  GtkWidget  *image;
+  GtkWidget  *listbox;
+  GtkWidget  *widget;
+
+  gchar      *release_link;
+  gchar      *title;
+  gchar      *markup;
+  gchar      *tmp;
+  gint        row;
 
   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
 
-  /* Why I am using gimp_data_directory() then backing out 2 directories
-   * instead of directly using gimp_installation_directory () is because
-   * the 'datadir' might be customized, so I don't want to hardcode
-   * "share". It might be something else.
-   */
-  appdata_path = g_build_filename (gimp_data_directory (),
-                                   "..", "..", "metainfo",
-                                   "org.gimp.GIMP.appdata.xml",
-                                   NULL);
-  if (! g_file_test (appdata_path, G_FILE_TEST_IS_REGULAR))
-    {
-      /* This should not happen since we install explicitly this file in
-       * metainfo/, but flatpak at least is overriding our install and
-       * moving the file to appdata/ (which used to be the legacy
-       * location). Hopefully they are the only ones doing it, but just
-       * in case, let's make an alternative check in this other
-       * location.
-       */
-      g_printerr ("%s: AppStream file '%s' is not a regular file.\n",
-                  G_STRFUNC, appdata_path);
-      g_free (appdata_path);
-      appdata_path = g_build_filename (gimp_data_directory (),
-                                       "..", "..", "appdata",
-                                       "org.gimp.GIMP.appdata.xml",
-                                       NULL);
-    }
-  if (g_file_test (appdata_path, G_FILE_TEST_IS_REGULAR))
-    {
-      AsRelease *release;
-
-      app = as_app_new ();
-      if (as_app_parse_file (app, appdata_path,
-                             AS_APP_PARSE_FLAG_USE_HEURISTICS,
-                             &error))
-        {
-          if ((release = as_app_get_release (app, GIMP_VERSION)) != NULL)
-            release_notes = as_release_get_description (release, g_getenv ("LANGUAGE")) ?
-              as_release_get_description (release, g_getenv ("LANGUAGE")) :
-              as_release_get_description (release, NULL);
-          else if (GIMP_MICRO_VERSION % 2 == 0)
-            g_printerr ("%s: no <release> tag for version %s in '%s'\n",
-                        G_STRFUNC, GIMP_VERSION, appdata_path);
-        }
-      else if (error)
-        {
-          g_printerr ("%s: %s\n", G_STRFUNC, error->message);
-          g_clear_error (&error);
-        }
-      else
-        {
-          g_printerr ("%s: failed to load AppStream file '%s'\n", G_STRFUNC, appdata_path);
-        }
-    }
-  else
-    {
-      /* Note that none of the errors here and above should happen.
-       * Each of our releases (even micro value) should have a <release>
-       * tag. But I am just printing to stderr and half-ignoring the
-       * miss because it is not serious enough to break normal GIMP
-       * usage.
-       */
-      g_printerr ("%s: AppStream file '%s' is not a regular file.\n", G_STRFUNC, appdata_path);
-    }
-  g_free (appdata_path);
-
   /* Translators: the %s string will be the version, e.g. "3.0". */
   title = g_strdup_printf (_("Welcome to GIMP %s"), GIMP_VERSION);
   windows = gimp_get_image_windows (gimp);
@@ -310,7 +236,7 @@ welcome_dialog_create (Gimp *gimp)
   /* Release Notes */
   /*****************/
 
-  if (release_notes)
+  if (gimp_welcome_dialog_n_items > 0)
     {
       gint n_demos = 0;
 
@@ -346,15 +272,22 @@ welcome_dialog_create (Gimp *gimp)
       gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
       gtk_widget_show (image);
 
-      /* Release note contents. */
+      /* Release note introduction. */
 
-      gimp_appstream_to_pango_markups (release_notes,
-                                       &release_introduction,
-                                       &release_items);
-      if (release_introduction)
+      if (gimp_welcome_dialog_intro_n_paragraphs)
         {
+          GString *introduction = NULL;
+
+          for (gint i = 0; i < gimp_welcome_dialog_intro_n_paragraphs; i++)
+            {
+              if (i == 0)
+                introduction = g_string_new (_(gimp_welcome_dialog_intro[i]));
+              else
+                g_string_append_printf (introduction, "\n%s",
+                                        _(gimp_welcome_dialog_intro[i]));
+            }
           widget = gtk_label_new (NULL);
-          gtk_label_set_markup (GTK_LABEL (widget), release_introduction);
+          gtk_label_set_markup (GTK_LABEL (widget), introduction->str);
           gtk_label_set_max_width_chars (GTK_LABEL (widget), 70);
           gtk_label_set_selectable (GTK_LABEL (widget), FALSE);
           gtk_label_set_justify (GTK_LABEL (widget), GTK_JUSTIFY_LEFT);
@@ -362,65 +295,59 @@ welcome_dialog_create (Gimp *gimp)
           gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
           gtk_widget_show (widget);
 
-          g_free (release_introduction);
+          g_string_free (introduction, TRUE);
         }
 
-      if (release_items)
-        {
-          GList *item;
-          gint   i;
+      /* Release note's change items. */
 
-          scrolled_window = gtk_scrolled_window_new (NULL, NULL);
-          gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
-          gtk_widget_show (scrolled_window);
+      scrolled_window = gtk_scrolled_window_new (NULL, NULL);
+      gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
+      gtk_widget_show (scrolled_window);
 
-          listbox = gtk_list_box_new ();
+      listbox = gtk_list_box_new ();
 
-          for (item = release_items, i = 0; item; item = item->next, i++)
+      for (gint i = 0; i < gimp_welcome_dialog_n_items; i++)
+        {
+          GtkWidget *row;
+          gchar     *markup;
+
+          /* Add a bold dot for pretty listing. */
+          if (i < gimp_welcome_dialog_n_items &&
+              gimp_welcome_dialog_demos[i] != NULL)
             {
-              GtkWidget *row;
-              gchar     *markup;
-
-              /* Add a bold dot for pretty listing. */
-              if (i < n_gimp_welcome_dialog_demo &&
-                  gimp_welcome_dialog_demo[i] != NULL)
-                {
-                  markup = g_strdup_printf ("<span weight='ultrabold'>\xe2\x96\xb6</span>  %s",
-                                            (gchar *) item->data);
-                  n_demos++;
-                }
-              else
-                {
-                  markup = g_strdup_printf ("<span weight='ultrabold'>\xe2\x80\xa2</span>  %s",
-                                            (gchar *) item->data);
-                }
-
-              row = gtk_list_box_row_new ();
-              widget = gtk_label_new (NULL);
-              gtk_label_set_markup (GTK_LABEL (widget), markup);
-              gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
-              gtk_label_set_line_wrap_mode (GTK_LABEL (widget), PANGO_WRAP_WORD);
-              gtk_label_set_justify (GTK_LABEL (widget), GTK_JUSTIFY_LEFT);
-              gtk_widget_set_halign (widget, GTK_ALIGN_START);
-              gtk_label_set_xalign (GTK_LABEL (widget), 0.0);
-              gtk_container_add (GTK_CONTAINER (row), widget);
-
-              gtk_list_box_insert (GTK_LIST_BOX (listbox), row, -1);
-              gtk_widget_show_all (row);
-
-              g_free (markup);
+              markup = g_strdup_printf ("<span weight='ultrabold'>\xe2\x96\xb6</span>  %s",
+                                        _((gchar *) gimp_welcome_dialog_items[i]));
+              n_demos++;
+            }
+          else
+            {
+              markup = g_strdup_printf ("<span weight='ultrabold'>\xe2\x80\xa2</span>  %s",
+                                        _((gchar *) gimp_welcome_dialog_items[i]));
             }
-          gtk_container_add (GTK_CONTAINER (scrolled_window), listbox);
-          gtk_list_box_set_selection_mode (GTK_LIST_BOX (listbox),
-                                           GTK_SELECTION_NONE);
 
-          g_signal_connect (listbox, "row-activated",
-                            G_CALLBACK (welcome_dialog_release_item_activated),
-                            gimp);
-          gtk_widget_show (listbox);
+          row = gtk_list_box_row_new ();
+          widget = gtk_label_new (NULL);
+          gtk_label_set_markup (GTK_LABEL (widget), markup);
+          gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
+          gtk_label_set_line_wrap_mode (GTK_LABEL (widget), PANGO_WRAP_WORD);
+          gtk_label_set_justify (GTK_LABEL (widget), GTK_JUSTIFY_LEFT);
+          gtk_widget_set_halign (widget, GTK_ALIGN_START);
+          gtk_label_set_xalign (GTK_LABEL (widget), 0.0);
+          gtk_container_add (GTK_CONTAINER (row), widget);
 
-          g_list_free_full (release_items, g_free);
+          gtk_list_box_insert (GTK_LIST_BOX (listbox), row, -1);
+          gtk_widget_show_all (row);
+
+          g_free (markup);
         }
+      gtk_container_add (GTK_CONTAINER (scrolled_window), listbox);
+      gtk_list_box_set_selection_mode (GTK_LIST_BOX (listbox),
+                                       GTK_SELECTION_NONE);
+
+      g_signal_connect (listbox, "row-activated",
+                        G_CALLBACK (welcome_dialog_release_item_activated),
+                        gimp);
+      gtk_widget_show (listbox);
 
       if (n_demos > 0)
         {
@@ -493,8 +420,6 @@ welcome_dialog_create (Gimp *gimp)
   gtk_widget_show (widget);
   gtk_box_pack_start (GTK_BOX (main_vbox), widget, FALSE, FALSE, 0);
 
-  g_clear_object (&app);
-
   return welcome_dialog;
 }
 
@@ -512,9 +437,9 @@ welcome_dialog_release_item_activated (GtkListBox    *listbox,
 
   row_index = gtk_list_box_row_get_index (row);
 
-  g_return_if_fail (row_index < n_gimp_welcome_dialog_demo);
+  g_return_if_fail (row_index < gimp_welcome_dialog_n_items);
 
-  script_string = gimp_welcome_dialog_demo[row_index];
+  script_string = gimp_welcome_dialog_demos[row_index];
 
   if (script_string == NULL)
     /* Not an error. Some release items have no demos. */
diff --git a/tools/generate-welcome-dialog-data.py b/tools/generate-welcome-dialog-data.py
index a4ad4e65a4..146146d661 100755
--- a/tools/generate-welcome-dialog-data.py
+++ b/tools/generate-welcome-dialog-data.py
@@ -23,6 +23,7 @@ Usage: generate-welcome-dialog-data.py
 
 import argparse
 import os.path
+import re
 import sys
 import xml.etree.ElementTree as ET
 
@@ -34,21 +35,42 @@ infile      = os.path.join(desktop_dir, 'org.gimp.GIMP.appdata.xml.in.in')
 outfile     = os.path.join(outdir, 'welcome-dialog-data.h')
 
 def parse_appdata(infile, version):
+  introduction  = []
+  release_texts = []
   release_demos = []
+
+  spaces = re.compile(r'\s+')
   tree = ET.parse(infile)
   root = tree.getroot()
   releases_node = root.find('releases')
   releases = releases_node.findall('release')
   for release in releases:
     if 'version' in release.attrib and release.attrib['version'] == version:
+      intro = release.findall('./description/_p')
+      for p in intro:
+        # Naive conversion for C strings, but it will probably fit for
+        # most cases.
+        p = p.text.strip()
+        p = p.replace('\\', '\\\\')
+        p = p.replace('"', '\\"')
+        # All redundant spaces unwanted as XML merges them anyway.
+        introduction += [spaces.sub(' ', p)]
+
       items = release.findall('./description/ul/_li')
       for item in items:
+        text = item.text.strip()
+        text = text.replace('\\', '\\\\')
+        text = text.replace('"', '\\"')
         demo = None
         if 'demo' in item.attrib:
           demo = item.attrib['demo']
+          # All spaces unneeded in demo string.
+          demo = demo.replace(' ', '')
+        release_texts += [spaces.sub(' ', text)]
         release_demos += [demo]
+      break
 
-  return release_demos
+  return introduction, release_texts, release_demos
 
 if __name__ == "__main__":
   parser = argparse.ArgumentParser()
@@ -87,14 +109,18 @@ if __name__ == "__main__":
 '''
   print(top_comment)
 
-  demos = parse_appdata(infile, args.version)
+  intro_p, items, demos = parse_appdata(infile, args.version)
 
   if args.header:
     print('#ifndef __WELCOME_DIALOG_DATA_H__')
     print('#define __WELCOME_DIALOG_DATA_H__\n\n')
 
-    print('extern gint          n_gimp_welcome_dialog_demo;')
-    print('extern const gchar * gimp_welcome_dialog_demo[];')
+    print('extern gint          gimp_welcome_dialog_n_items;')
+    print('extern const gchar * gimp_welcome_dialog_items[];')
+    print('extern const gchar * gimp_welcome_dialog_demos[];')
+    print()
+    print('extern gint          gimp_welcome_dialog_intro_n_paragraphs;')
+    print('extern const gchar * gimp_welcome_dialog_intro[];')
 
     print('\n\n#endif /* __WELCOME_DIALOG_DATA_H__ */')
   else:
@@ -102,9 +128,15 @@ if __name__ == "__main__":
     print('#include <glib.h>')
     print()
 
-    print('const gint    n_gimp_welcome_dialog_demo = {};'.format(len(demos)))
+    print('const gint   gimp_welcome_dialog_n_items = {};'.format(len(demos)))
+    print()
+    print('const gchar *gimp_welcome_dialog_items[] =')
+    print('{')
+    for item in items:
+      print('  "{}",'.format(item))
+    print('  NULL,\n};')
     print()
-    print('const gchar * gimp_welcome_dialog_demo[] =')
+    print('const gchar *gimp_welcome_dialog_demos[] =')
     print('{')
     for demo in demos:
       if demo is None:
@@ -112,3 +144,11 @@ if __name__ == "__main__":
       else:
         print('  "{}",'.format(demo))
     print('  NULL,\n};')
+    print()
+    print('const gint   gimp_welcome_dialog_intro_n_paragraphs = {};'.format(len(intro_p)))
+    print()
+    print('const gchar *gimp_welcome_dialog_intro[] =')
+    print('{')
+    for p in intro_p:
+      print('  "{}",'.format(p))
+    print('  NULL,\n};')


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