[gnome-software] Show more detailed version numbers when required



commit f47b7d9db4877a845b3a96eeb8b1ad80534bae32
Author: Richard Hughes <richard hughsie com>
Date:   Tue Oct 8 10:58:26 2013 +0100

    Show more detailed version numbers when required
    
    If we make the version numbers 'pretty' we can get to a situation where we show
    the same version string for the installed package *and* the update.
    If this happens, make the version number progressively more un-pretty until the
    version numbers differ.
    
    Resolves: https://bugzilla.gnome.org/show_bug.cgi?id=707820

 src/gs-app-widget.c    |    4 +-
 src/gs-app.c           |  159 ++++++++++++++++++++++++++++++++++++++++--------
 src/gs-app.h           |    2 +
 src/gs-self-test.c     |    9 +++
 src/gs-shell-updates.c |    4 +-
 5 files changed, 149 insertions(+), 29 deletions(-)
---
diff --git a/src/gs-app-widget.c b/src/gs-app-widget.c
index 3dd1a3a..96e6086 100644
--- a/src/gs-app-widget.c
+++ b/src/gs-app-widget.c
@@ -89,10 +89,10 @@ gs_app_widget_refresh (GsAppWidget *app_widget)
        if (priv->show_update &&
            gs_app_get_state (priv->app) == GS_APP_STATE_UPDATABLE) {
                gtk_label_set_label (GTK_LABEL (priv->version_label),
-                                    gs_app_get_update_version (priv->app));
+                                    gs_app_get_update_version_ui (priv->app));
        } else {
                gtk_label_set_label (GTK_LABEL (priv->version_label),
-                                    gs_app_get_version (priv->app));
+                                    gs_app_get_version_ui (priv->app));
        }
        if (gs_app_get_pixbuf (priv->app))
                gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image),
diff --git a/src/gs-app.c b/src/gs-app.c
index e23b04d..b859295 100644
--- a/src/gs-app.c
+++ b/src/gs-app.c
@@ -58,11 +58,13 @@ struct GsAppPrivate
        gchar                   *source;
        gchar                   *project_group;
        gchar                   *version;
+       gchar                   *version_ui;
        gchar                   *summary;
        gchar                   *description;
        GPtrArray               *screenshots;
        gchar                   *url;
        gchar                   *update_version;
+       gchar                   *update_version_ui;
        gchar                   *update_details;
        gchar                   *management_plugin;
        gint                     rating;
@@ -178,6 +180,12 @@ gs_app_to_string (GsApp *app)
                g_string_append_printf (str, "\tname:\t%s\n", priv->name);
        if (priv->version != NULL)
                g_string_append_printf (str, "\tversion:\t%s\n", priv->version);
+       if (priv->version_ui != NULL)
+               g_string_append_printf (str, "\tversion-ui:\t%s\n", priv->version_ui);
+       if (priv->update_version != NULL)
+               g_string_append_printf (str, "\tupdate-version:\t%s\n", priv->update_version);
+       if (priv->update_version_ui != NULL)
+               g_string_append_printf (str, "\tupdate-version-ui:\t%s\n", priv->update_version_ui);
        if (priv->summary != NULL)
                g_string_append_printf (str, "\tsummary:\t%s\n", priv->summary);
        if (priv->description != NULL)
@@ -515,23 +523,20 @@ gs_app_set_featured_pixbuf (GsApp *app, GdkPixbuf *pixbuf)
        app->priv->featured_pixbuf = g_object_ref (pixbuf);
 }
 
-/**
- * gs_app_get_version:
- */
-const gchar *
-gs_app_get_version (GsApp *app)
-{
-       g_return_val_if_fail (GS_IS_APP (app), NULL);
-       return app->priv->version;
-}
+typedef enum {
+       GS_APP_VERSION_FIXUP_RELEASE            = 1,
+       GS_APP_VERSION_FIXUP_DISTRO_SUFFIX      = 2,
+       GS_APP_VERSION_FIXUP_GIT_SUFFIX         = 4,
+       GS_APP_VERSION_FIXUP_LAST,
+} GsAppVersionFixup;
 
 /**
- * gs_app_get_pretty_version:
+ * gs_app_get_ui_version:
  *
  * convert 1:1.6.2-7.fc17 into "Version 1.6.2"
  **/
 static gchar *
-gs_app_get_pretty_version (const gchar *version)
+gs_app_get_ui_version (const gchar *version, guint64 flags)
 {
        guint i;
        gchar *new = NULL;
@@ -553,27 +558,106 @@ gs_app_get_pretty_version (const gchar *version)
 
        /* then remove any distro suffix */
        new = g_strdup (version);
-       f = g_strstr_len (new, -1, ".fc");
-       if (f != NULL)
-               *f= '\0';
+       if ((flags & GS_APP_VERSION_FIXUP_DISTRO_SUFFIX) > 0) {
+               f = g_strstr_len (new, -1, ".fc");
+               if (f != NULL)
+                       *f= '\0';
+       }
 
        /* then remove any release */
-       f = g_strrstr_len (new, -1, "-");
-       if (f != NULL)
-               *f= '\0';
+       if ((flags & GS_APP_VERSION_FIXUP_RELEASE) > 0) {
+               f = g_strrstr_len (new, -1, "-");
+               if (f != NULL)
+                       *f= '\0';
+       }
 
        /* then remove any git suffix */
-       f = g_strrstr_len (new, -1, ".2012");
-       if (f != NULL)
-               *f= '\0';
-       f = g_strrstr_len (new, -1, ".2013");
-       if (f != NULL)
-               *f= '\0';
+       if ((flags & GS_APP_VERSION_FIXUP_GIT_SUFFIX) > 0) {
+               f = g_strrstr_len (new, -1, ".2012");
+               if (f != NULL)
+                       *f= '\0';
+               f = g_strrstr_len (new, -1, ".2013");
+               if (f != NULL)
+                       *f= '\0';
+       }
 out:
        return new;
 }
 
 /**
+ * gs_app_ui_versions_invalidate:
+ */
+static void
+gs_app_ui_versions_invalidate (GsApp *app)
+{
+       GsAppPrivate *priv = app->priv;
+       g_free (priv->version_ui);
+       g_free (priv->update_version_ui);
+       priv->version_ui = NULL;
+       priv->update_version_ui = NULL;
+}
+
+/**
+ * gs_app_ui_versions_populate:
+ */
+static void
+gs_app_ui_versions_populate (GsApp *app)
+{
+       GsAppPrivate *priv = app->priv;
+       guint i;
+       guint64 flags[] = { GS_APP_VERSION_FIXUP_RELEASE |
+                           GS_APP_VERSION_FIXUP_DISTRO_SUFFIX |
+                           GS_APP_VERSION_FIXUP_GIT_SUFFIX,
+                           GS_APP_VERSION_FIXUP_DISTRO_SUFFIX |
+                           GS_APP_VERSION_FIXUP_GIT_SUFFIX,
+                           GS_APP_VERSION_FIXUP_DISTRO_SUFFIX,
+                           0 };
+
+       /* try each set of bitfields in order */
+       for (i = 0; flags[i] != 0; i++) {
+               priv->version_ui = gs_app_get_ui_version (priv->version, flags[i]);
+               priv->update_version_ui = gs_app_get_ui_version (priv->update_version, flags[i]);
+               if (g_strcmp0 (priv->version_ui, priv->update_version_ui) != 0) {
+                       g_object_notify (G_OBJECT (app), "version");
+                       g_signal_emit (app, signals[SIGNAL_STATE_CHANGED], 0);
+                       return;
+               }
+               gs_app_ui_versions_invalidate (app);
+       }
+
+       /* we tried, but failed */
+       priv->version_ui = g_strdup (priv->version);
+       priv->update_version_ui = g_strdup (priv->update_version);
+}
+
+/**
+ * gs_app_get_version:
+ */
+const gchar *
+gs_app_get_version (GsApp *app)
+{
+       g_return_val_if_fail (GS_IS_APP (app), NULL);
+       return app->priv->version;
+}
+
+/**
+ * gs_app_get_version_ui:
+ */
+const gchar *
+gs_app_get_version_ui (GsApp *app)
+{
+       g_return_val_if_fail (GS_IS_APP (app), NULL);
+
+       /* work out the two version numbers */
+       if (app->priv->version != NULL &&
+           app->priv->version_ui == NULL) {
+               gs_app_ui_versions_populate (app);
+       }
+
+       return app->priv->version_ui;
+}
+
+/**
  * gs_app_set_version:
  * @app:       A #GsApp instance
  * @version:   The version, e.g. "2:1.2.3.fc19"
@@ -586,7 +670,10 @@ gs_app_set_version (GsApp *app, const gchar *version)
 {
        g_return_if_fail (GS_IS_APP (app));
        g_free (app->priv->version);
-       app->priv->version = gs_app_get_pretty_version (version);
+       app->priv->version = g_strdup (version);
+       gs_app_ui_versions_invalidate (app);
+       g_object_notify (G_OBJECT (app), "version");
+       g_signal_emit (app, signals[SIGNAL_STATE_CHANGED], 0);
 }
 
 /**
@@ -689,6 +776,23 @@ gs_app_get_update_version (GsApp *app)
 }
 
 /**
+ * gs_app_get_update_version_ui:
+ */
+const gchar *
+gs_app_get_update_version_ui (GsApp *app)
+{
+       g_return_val_if_fail (GS_IS_APP (app), NULL);
+
+       /* work out the two version numbers */
+       if (app->priv->update_version != NULL &&
+           app->priv->update_version_ui == NULL) {
+               gs_app_ui_versions_populate (app);
+       }
+
+       return app->priv->update_version_ui;
+}
+
+/**
  * gs_app_set_update_version:
  */
 void
@@ -696,7 +800,10 @@ gs_app_set_update_version (GsApp *app, const gchar *update_version)
 {
        g_return_if_fail (GS_IS_APP (app));
        g_free (app->priv->update_version);
-       app->priv->update_version = gs_app_get_pretty_version (update_version);
+       app->priv->update_version = g_strdup (update_version);
+       gs_app_ui_versions_invalidate (app);
+       g_object_notify (G_OBJECT (app), "version");
+       g_signal_emit (app, signals[SIGNAL_STATE_CHANGED], 0);
 }
 
 /**
@@ -1062,10 +1169,12 @@ gs_app_finalize (GObject *object)
        g_free (priv->source);
        g_free (priv->project_group);
        g_free (priv->version);
+       g_free (priv->version_ui);
        g_free (priv->summary);
        g_free (priv->description);
        g_ptr_array_unref (priv->screenshots);
        g_free (priv->update_version);
+       g_free (priv->update_version_ui);
        g_free (priv->update_details);
        g_free (priv->management_plugin);
        g_hash_table_unref (priv->metadata);
diff --git a/src/gs-app.h b/src/gs-app.h
index 24a5587..18d2087 100644
--- a/src/gs-app.h
+++ b/src/gs-app.h
@@ -104,6 +104,7 @@ const gchar *gs_app_get_project_group       (GsApp          *app);
 void            gs_app_set_project_group       (GsApp          *app,
                                                 const gchar    *source);
 const gchar    *gs_app_get_version             (GsApp          *app);
+const gchar    *gs_app_get_version_ui          (GsApp          *app);
 void            gs_app_set_version             (GsApp          *app,
                                                 const gchar    *version);
 const gchar    *gs_app_get_summary             (GsApp          *app);
@@ -119,6 +120,7 @@ GPtrArray   *gs_app_get_screenshots         (GsApp          *app);
 void            gs_app_add_screenshot          (GsApp          *app,
                                                 GsScreenshot   *screenshot);
 const gchar    *gs_app_get_update_version      (GsApp          *app);
+const gchar    *gs_app_get_update_version_ui   (GsApp          *app);
 void            gs_app_set_update_version      (GsApp          *app,
                                                 const gchar    *update_version);
 const gchar    *gs_app_get_update_details      (GsApp          *app);
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index 9a7817b..d905590 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -39,6 +39,15 @@ gs_app_func (void)
 
        g_assert_cmpstr (gs_app_get_id (app), ==, "gnome-software");
 
+       /* check we clean up the version, but not at the expense of having
+        * the same string as the update version */
+       gs_app_set_version (app, "2.8.6-3.fc20");
+       gs_app_set_update_version (app, "2.8.6-4.fc20");
+       g_assert_cmpstr (gs_app_get_version (app), ==, "2.8.6-3.fc20");
+       g_assert_cmpstr (gs_app_get_update_version (app), ==, "2.8.6-4.fc20");
+       g_assert_cmpstr (gs_app_get_version_ui (app), ==, "2.8.6-3");
+       g_assert_cmpstr (gs_app_get_update_version_ui (app), ==, "2.8.6-4");
+
        g_object_unref (app);
 }
 
diff --git a/src/gs-shell-updates.c b/src/gs-shell-updates.c
index 64c48d2..ae146c4 100644
--- a/src/gs-shell-updates.c
+++ b/src/gs-shell-updates.c
@@ -226,7 +226,7 @@ gs_shell_updates_set_updates_description_ui (GsShellUpdates *shell_updates, GsAp
        } else {
                tmp = g_strdup_printf ("%s %s",
                                       gs_app_get_source (app),
-                                      gs_app_get_update_version (app));
+                                      gs_app_get_update_version_ui (app));
                gtk_window_set_title (GTK_WINDOW (widget), tmp);
                g_free (tmp);
        }
@@ -308,7 +308,7 @@ show_update_details (GsApp *app, GsShellUpdates *shell_updates)
                        gtk_widget_set_halign (label, GTK_ALIGN_START);
                        gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
                        gtk_box_pack_start (GTK_BOX (row), label, TRUE, TRUE, 0);
-                       label = gtk_label_new (gs_app_get_update_version (app_related));
+                       label = gtk_label_new (gs_app_get_update_version_ui (app_related));
                        g_object_set (label,
                                      "margin-left", 20,
                                      "margin-right", 20,


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