[gimp] app: unstable versions will check available development releases.



commit 6c26d39c8e817bc333e6c494c08e81e627f52ea8
Author: Jehan <jehan girinstud io>
Date:   Thu Oct 22 16:11:20 2020 +0200

    app: unstable versions will check available development releases.
    
    Stable versions (i.e. minor version number even, e.g. 2.10.22) will only
    look for higher stable releases. But for unstable versions, we will want
    to look up the development releases too. So for instance GIMP 2.99.2
    will warn if the development version 2.99.4 has been released, but also
    if the stable version 3.0.0 has been released (whatever is the highest,
    which is the stable version in this example).

 app/gimp-update.c | 455 +++++++++++++++++++++++++++++++++---------------------
 1 file changed, 283 insertions(+), 172 deletions(-)
---
diff --git a/app/gimp-update.c b/app/gimp-update.c
index cee1d53842..7791bf61c3 100644
--- a/app/gimp-update.c
+++ b/app/gimp-update.c
@@ -47,10 +47,12 @@ static gboolean gimp_update_known           (GimpCoreConfig   *config,
                                              gint64            release_timestamp,
                                              gint              build_revision,
                                              const gchar      *comment);
-static gboolean gimp_version_break          (const gchar      *v,
-                                             gint             *major,
-                                             gint             *minor,
-                                             gint             *micro);
+static gboolean gimp_update_get_highest     (JsonParser       *parser,
+                                             gchar           **highest_version,
+                                             gint64           *release_timestamp,
+                                             gint             *build_revision,
+                                             gchar           **build_comment,
+                                             gboolean          unstable);
 static void     gimp_check_updates_callback (GObject          *source,
                                              GAsyncResult     *result,
                                              gpointer          user_data);
@@ -58,6 +60,14 @@ static void     gimp_update_about_dialog    (GimpCoreConfig   *config,
                                              const GParamSpec *pspec,
                                              gpointer          user_data);
 
+static gboolean gimp_version_break          (const gchar      *v,
+                                             gint             *major,
+                                             gint             *minor,
+                                             gint             *micro);
+static gint     gimp_version_cmp            (const gchar      *v1,
+                                             const gchar      *v2);
+
+
 /* Private Functions */
 
 /**
@@ -88,9 +98,6 @@ gimp_update_known (GimpCoreConfig *config,
                    const gchar    *build_comment)
 {
   gboolean new_check = (last_version != NULL);
-  gint     major;
-  gint     minor;
-  gint     micro;
 
   if (last_version && release_timestamp == 0)
     {
@@ -111,37 +118,18 @@ gimp_update_known (GimpCoreConfig *config,
       build_comment     = config->last_release_comment;
     }
 
-  if (last_version)
+  if (last_version &&
+      (/* We are using a newer version than last check. This could
+        * happen if updating the config files without having
+        * re-checked the remote JSON file.
+        */
+       gimp_version_cmp (last_version, NULL) < 0 ||
+       /* Already using the last officially released
+        * revision. */
+       (gimp_version_cmp (last_version, NULL) == 0 &&
+        build_revision <= gimp_version_get_revision ())))
     {
-      if (gimp_version_break (last_version, &major, &minor, &micro))
-        {
-          if (/* We are using a newer version than last check. This could
-               * happen if updating the config files without having
-               * re-checked the remote JSON file.
-               */
-              (major < GIMP_MAJOR_VERSION ||
-               (major == GIMP_MAJOR_VERSION && minor < GIMP_MINOR_VERSION) ||
-               (major == GIMP_MAJOR_VERSION && minor == GIMP_MINOR_VERSION && micro < GIMP_MICRO_VERSION)) ||
-              /* Already using the last officially released
-               * revision. */
-              (major == GIMP_MAJOR_VERSION &&
-               minor == GIMP_MINOR_VERSION &&
-               micro == GIMP_MICRO_VERSION &&
-               build_revision <= gimp_version_get_revision ()))
-            {
-              last_version      = NULL;
-            }
-        }
-      else
-        {
-          /* If version is not properly parsed, something is wrong with
-           * upstream version number or parsing. This should not happen.
-           */
-          g_printerr ("%s: version not properly formatted: %s\n",
-                      G_STRFUNC, last_version);
-
-          return FALSE;
-        }
+      last_version = NULL;
     }
 
   if (last_version == NULL)
@@ -168,37 +156,162 @@ gimp_update_known (GimpCoreConfig *config,
 }
 
 static gboolean
-gimp_version_break (const gchar *v,
-                    gint        *major,
-                    gint        *minor,
-                    gint        *micro)
+gimp_update_get_highest (JsonParser  *parser,
+                         gchar      **highest_version,
+                         gint64      *release_timestamp,
+                         gint        *build_revision,
+                         gchar      **build_comment,
+                         gboolean     unstable)
 {
-  gchar **versions;
+  JsonPath    *path;
+  JsonNode    *result;
+  JsonArray   *versions;
+  const gchar *platform;
+  const gchar *path_str;
+  const gchar *release_date = NULL;
+  GError      *error        = NULL;
+  gint         i;
+
+  g_return_val_if_fail (highest_version   != NULL, FALSE);
+  g_return_val_if_fail (release_timestamp != NULL, FALSE);
+  g_return_val_if_fail (build_revision    != NULL, FALSE);
+  g_return_val_if_fail (build_comment     != NULL, FALSE);
+
+  *highest_version   = NULL;
+  *release_timestamp = 0;
+  *build_revision    = 0;
+  *build_comment     = NULL;
+
+  if (unstable)
+    path_str = "$['DEVELOPMENT'][*]";
+  else
+    path_str = "$['STABLE'][*]";
+
+  /* For Windows and macOS, let's look if installers are available.
+   * For other platforms, let's just look for source release.
+   */
+  if (g_strcmp0 (GIMP_BUILD_PLATFORM_FAMILY, "windows") == 0 ||
+      g_strcmp0 (GIMP_BUILD_PLATFORM_FAMILY, "macos") == 0)
+    platform = GIMP_BUILD_PLATFORM_FAMILY;
+  else
+    platform = "source";
+
+  path = json_path_new ();
+  /* Ideally we could just use Json path filters like this to
+   * retrieve only released binaries for a given platform:
+   * g_strdup_printf ("$['STABLE'][?(@.%s)]['version']", platform);
+   * json_array_get_string_element (result, 0);
+   * And that would be it! We'd have our last release for given
+   * platform.
+   * Unfortunately json-glib does not support filter syntax, so we
+   * end up looping through releases.
+   */
+  if (! json_path_compile (path, path_str, &error))
+    {
+      g_warning ("%s: path compilation failed: %s\n",
+                 G_STRFUNC, error->message);
+      g_clear_error (&error);
+      g_object_unref (path);
 
-  *major = 0;
-  *minor = 0;
-  *micro = 0;
+      return FALSE;
+    }
+  result = json_path_match (path, json_parser_get_root (parser));
+  if (! JSON_NODE_HOLDS_ARRAY (result))
+    {
+      g_printerr ("%s: match for \"%s\" is not a JSON array.\n",
+                  G_STRFUNC, path_str);
+      g_object_unref (path);
 
-  if (v == NULL)
-    return FALSE;
+      return FALSE;
+    }
 
-  versions = g_strsplit_set (v, ".", 3);
-  if (versions[0] != NULL)
+  versions = json_node_get_array (result);
+  for (i = 0; i < (gint) json_array_get_length (versions); i++)
     {
-      *major = g_ascii_strtoll (versions[0], NULL, 10);
-      if (versions[1] != NULL)
+      JsonObject *version;
+
+      /* Note that we don't actually look for the highest version,
+       * but for the highest version for which a build for your
+       * platform (and optional build-id) is available.
+       *
+       * So we loop through the version list then the build array
+       * and break at first compatible release, since JSON arrays
+       * are ordered.
+       */
+      version = json_array_get_object_element (versions, i);
+      if (json_object_has_member (version, platform))
         {
-          *minor = g_ascii_strtoll (versions[1], NULL, 10);
-          if (versions[2] != NULL)
+          JsonArray *builds;
+          gint       j;
+
+          builds = json_object_get_array_member (version, platform);
+
+          for (j = 0; j < (gint) json_array_get_length (builds); j++)
             {
-              *micro = g_ascii_strtoll (versions[2], NULL, 10);
-              return TRUE;
+              const gchar *build_id = NULL;
+              JsonObject  *build;
+
+              build = json_array_get_object_element (builds, j);
+              if (json_object_has_member (build, "build-id"))
+                build_id = json_object_get_string_member (build, "build-id");
+              if (g_strcmp0 (build_id, GIMP_BUILD_ID) == 0 ||
+                  g_strcmp0 (platform, "source") == 0)
+                {
+                  /* Release date is the build date if any set,
+                   * otherwise the main version release date.
+                   */
+                  if (json_object_has_member (build, "date"))
+                    release_date = json_object_get_string_member (build, "date");
+                  else
+                    release_date = json_object_get_string_member (version, "date");
+
+                  /* These are optional data. */
+                  if (json_object_has_member (build, "revision"))
+                    *build_revision = json_object_get_int_member (build, "revision");
+                  if (json_object_has_member (build, "comment"))
+                    *build_comment = g_strdup (json_object_get_string_member (build, "comment"));
+                  break;
+                }
+            }
+
+          if (release_date)
+            {
+              *highest_version = g_strdup (json_object_get_string_member (version, "version"));
+              break;
             }
         }
     }
-  g_strfreev (versions);
 
-  return (*major > 0);
+  if (*highest_version && *release_date)
+    {
+      GDateTime *datetime;
+      gchar     *str;
+
+      str = g_strdup_printf ("%s 00:00:00Z", release_date);
+      datetime = g_date_time_new_from_iso8601 (str, NULL);
+      g_free (str);
+
+      if (datetime)
+        {
+          *release_timestamp = g_date_time_to_unix (datetime);
+          g_date_time_unref (datetime);
+        }
+      else
+        {
+          /* JSON file data bug. */
+          g_printerr ("%s: release date for version %s not properly formatted: %s\n",
+                      G_STRFUNC, *highest_version, release_date);
+
+          g_clear_pointer (highest_version, g_free);
+          g_clear_pointer (build_comment, g_free);
+          *build_revision = 0;
+        }
+    }
+
+  json_node_unref (result);
+  g_object_unref (path);
+
+  return (*highest_version != NULL);
 }
 
 static void
@@ -215,26 +328,11 @@ gimp_check_updates_callback (GObject      *source,
                                    &file_contents, &file_length,
                                    NULL, &error))
     {
-      const gchar *platform;
-      const gchar *last_version      = NULL;
-      const gchar *release_date      = NULL;
-      const gchar *build_comment     = NULL;
+      JsonParser  *parser;
+      gchar       *last_version      = NULL;
+      gchar       *build_comment     = NULL;
       gint64       release_timestamp = 0;
       gint         build_revision    = 0;
-      JsonParser  *parser;
-      JsonPath    *path;
-      JsonNode    *result;
-      JsonArray   *versions;
-      gint         i;
-
-      /* For Windows and macOS, let's look if installers are available.
-       * For other platforms, let's just look for source release.
-       */
-      if (g_strcmp0 (GIMP_BUILD_PLATFORM_FAMILY, "windows") == 0 ||
-          g_strcmp0 (GIMP_BUILD_PLATFORM_FAMILY, "macos") == 0)
-        platform = GIMP_BUILD_PLATFORM_FAMILY;
-      else
-        platform = "source";
 
       parser = json_parser_new ();
       if (! json_parser_load_from_data (parser, file_contents, file_length, &error))
@@ -248,115 +346,42 @@ gimp_check_updates_callback (GObject      *source,
           return;
         }
 
-      path = json_path_new ();
-      /* Ideally we could just use Json path filters like this to
-       * retrieve only released binaries for a given platform:
-       * g_strdup_printf ("$['STABLE'][?(@.%s)]['version']", platform);
-       * json_array_get_string_element (result, 0);
-       * And that would be it! We'd have our last release for given
-       * platform.
-       * Unfortunately json-glib does not support filter syntax, so we
-       * end up looping through releases.
-       */
-      if (! json_path_compile (path, "$['STABLE'][*]", &error))
-        {
-#ifdef GIMP_UNSTABLE
-          g_printerr("Path compilation failed: %s\n", error->message);
-#endif
-          g_free (file_contents);
-          g_clear_object (&parser);
-          g_clear_error (&error);
-
-          return;
-        }
-      result = json_path_match (path, json_parser_get_root (parser));
-      g_return_if_fail (JSON_NODE_HOLDS_ARRAY (result));
+      gimp_update_get_highest (parser, &last_version, &release_timestamp,
+                               &build_revision, &build_comment, FALSE);
 
-      versions = json_node_get_array (result);
-      for (i = 0; i < (gint) json_array_get_length (versions); i++)
+#ifdef GIMP_UNSTABLE
         {
-          JsonObject *version;
-
-          /* Note that we don't actually look for the highest version,
-           * but for the highest version for which a build for your
-           * platform (and optional build-id) is available.
-           *
-           * So we loop through the version list then the build array
-           * and break at first compatible release, since JSON arrays
-           * are ordered.
-           */
-          version = json_array_get_object_element (versions, i);
-          if (json_object_has_member (version, platform))
+          gchar  *dev_version = NULL;
+          gchar  *dev_comment      = NULL;
+          gint64  dev_timestamp    = 0;
+          gint    dev_revision     = 0;
+
+          gimp_update_get_highest (parser, &dev_version, &dev_timestamp,
+                                   &dev_revision, &dev_comment, TRUE);
+          if (dev_version)
             {
-              JsonArray *builds;
-              gint       j;
-
-              builds = json_object_get_array_member (version, platform);
-
-              for (j = 0; j < (gint) json_array_get_length (builds); j++)
+              if (! last_version || gimp_version_cmp (dev_version, last_version) > 0)
                 {
-                  const gchar *build_id = NULL;
-                  JsonObject  *build;
-
-                  build = json_array_get_object_element (builds, j);
-                  if (json_object_has_member (build, "build-id"))
-                    build_id = json_object_get_string_member (build, "build-id");
-                  if (g_strcmp0 (build_id, GIMP_BUILD_ID) == 0)
-                    {
-                      /* Release date is the build date if any set,
-                       * otherwise the main version release date.
-                       */
-                      if (json_object_has_member (build, "date"))
-                        release_date = json_object_get_string_member (build, "date");
-                      else
-                        release_date = json_object_get_string_member (version, "date");
-
-                      /* These are optional data. */
-                      if (json_object_has_member (build, "revision"))
-                        build_revision = json_object_get_int_member (build, "revision");
-                      if (json_object_has_member (build, "comment"))
-                        build_comment = json_object_get_string_member (build, "comment");
-                      break;
-                    }
+                  g_clear_pointer (&last_version, g_free);
+                  g_clear_pointer (&build_comment, g_free);
+                  last_version      = dev_version;
+                  build_comment     = dev_comment;
+                  release_timestamp = dev_timestamp;
+                  build_revision    = dev_revision;
                 }
-
-              if (release_date)
+              else
                 {
-                  last_version = json_object_get_string_member (version, "version");
-                  break;
+                  g_clear_pointer (&dev_version, g_free);
+                  g_clear_pointer (&dev_comment, g_free);
                 }
             }
         }
+#endif
 
-      if (last_version && release_date)
-        {
-          GDateTime *datetime;
-          gchar     *str;
-
-          str = g_strdup_printf ("%s 00:00:00Z", release_date);
-          datetime = g_date_time_new_from_iso8601 (str, NULL);
-          g_free (str);
-
-          if (datetime)
-            {
-              release_timestamp = g_date_time_to_unix (datetime);
-              g_date_time_unref (datetime);
-            }
-          else
-            {
-              /* JSON file data bug. */
-              g_printerr ("%s: release date for version %s not properly formatted: %s\n",
-                          G_STRFUNC, last_version, release_date);
-
-              last_version   = NULL;
-              release_date   = NULL;
-              build_revision = 0;
-              build_comment  = NULL;
-            }
-        }
       gimp_update_known (config, last_version, release_timestamp, build_revision, build_comment);
 
-      g_object_unref (path);
+      g_clear_pointer (&last_version, g_free);
+      g_clear_pointer (&build_comment, g_free);
       g_object_unref (parser);
       g_free (file_contents);
     }
@@ -389,6 +414,92 @@ gimp_update_about_dialog (GimpCoreConfig   *config,
     }
 }
 
+static gboolean
+gimp_version_break (const gchar *v,
+                    gint        *major,
+                    gint        *minor,
+                    gint        *micro)
+{
+  gchar **versions;
+
+  *major = 0;
+  *minor = 0;
+  *micro = 0;
+
+  if (v == NULL)
+    return FALSE;
+
+  versions = g_strsplit_set (v, ".", 3);
+  if (versions[0] != NULL)
+    {
+      *major = g_ascii_strtoll (versions[0], NULL, 10);
+      if (versions[1] != NULL)
+        {
+          *minor = g_ascii_strtoll (versions[1], NULL, 10);
+          if (versions[2] != NULL)
+            {
+              *micro = g_ascii_strtoll (versions[2], NULL, 10);
+              return TRUE;
+            }
+        }
+    }
+  g_strfreev (versions);
+
+  return (*major > 0);
+}
+
+/**
+ * gimp_version_cmp:
+ * @v1: a string representing a version, ex. "2.10.22".
+ * @v2: a string representing another version, ex. "2.99.2".
+ *
+ * If @v2 is %NULL, @v1 is compared to the currently running version.
+ *
+ * Returns: an integer less than, equal to, or greater than zero if @v1
+ *          is found to represent a version respectively, lower than,
+ *          matching, or greater than @v2.
+ */
+static gint
+gimp_version_cmp (const gchar *v1,
+                  const gchar *v2)
+{
+  gint     major1;
+  gint     minor1;
+  gint     micro1;
+  gint     major2 = GIMP_MAJOR_VERSION;
+  gint     minor2 = GIMP_MINOR_VERSION;
+  gint     micro2 = GIMP_MICRO_VERSION;
+
+  g_return_val_if_fail (v1 != NULL, -1);
+
+  if (! gimp_version_break (v1, &major1, &minor1, &micro1))
+    {
+      /* If version is not properly parsed, something is wrong with
+       * upstream version number or parsing. This should not happen.
+       */
+      g_printerr ("%s: version not properly formatted: %s\n",
+                  G_STRFUNC, v1);
+
+      return -1;
+    }
+  if (v2 && ! gimp_version_break (v2, &major2, &minor2, &micro2))
+    {
+      g_printerr ("%s: version not properly formatted: %s\n",
+                  G_STRFUNC, v2);
+
+      return 1;
+    }
+
+  if (major1 == major2 && minor1 == minor2 && micro1 == micro2)
+    return 0;
+  else if (major1 > major2 ||
+           (major1 == major2 && minor1 > minor2) ||
+           (major1 == major2 && minor1 == minor2 && micro1 > micro2))
+    return 1;
+  else
+    return -1;
+}
+
 /* Public Functions */
 
 /*


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