[gnome-software/wip/kalev/more-origin-popover: 2/3] trivial: Break out two functions to GsApp



commit 42d7d730dc81f14c0b66134834ec19b85a329ebc
Author: Kalev Lember <klember redhat com>
Date:   Thu Sep 27 19:07:15 2018 +0200

    trivial: Break out two functions to GsApp

 lib/gs-app.c                | 83 +++++++++++++++++++++++++++++++++++++++++++++
 lib/gs-app.h                |  4 ++-
 src/gs-details-page.c       | 18 ++--------
 src/gs-origin-popover-row.c | 29 +++-------------
 4 files changed, 92 insertions(+), 42 deletions(-)
---
diff --git a/lib/gs-app.c b/lib/gs-app.c
index c40564bb..eade7a03 100644
--- a/lib/gs-app.c
+++ b/lib/gs-app.c
@@ -53,6 +53,7 @@
 
 #include "gs-app-collation.h"
 #include "gs-app-private.h"
+#include "gs-os-release.h"
 #include "gs-plugin.h"
 #include "gs-utils.h"
 
@@ -4471,4 +4472,86 @@ gs_app_new_from_unique_id (const gchar *unique_id)
        return app;
 }
 
+/**
+ * gs_app_get_origin_ui:
+ * @app: a #GsApp
+ *
+ * Gets the package origin that's suitable for UI use.
+ *
+ * Returns: The package origin for UI use
+ *
+ * Since: 3.32
+ **/
+gchar *
+gs_app_get_origin_ui (GsApp *app)
+{
+       /* use the distro name for official packages */
+       if (gs_app_has_quirk (app, AS_APP_QUIRK_PROVENANCE)) {
+               g_autoptr(GsOsRelease) os_release = gs_os_release_new (NULL);
+               if (os_release != NULL)
+                       return g_strdup (gs_os_release_get_name (os_release));
+       }
+
+       /* use "Local file" rather than the filename for local files */
+       if (gs_app_get_state (app) == AS_APP_STATE_AVAILABLE_LOCAL) {
+               /* TRANSLATORS: this is a locally downloaded package */
+               return g_strdup (_("Local file"));
+       }
+
+       /* fall back to origin */
+       return g_strdup (gs_app_get_origin (app));
+}
+
+/**
+ * gs_app_get_packaging_format:
+ * @app: a #GsApp
+ *
+ * Gets the packaging format, e.g. 'RPM' or 'Flatpak'.
+ *
+ * Returns: The packaging format
+ *
+ * Since: 3.32
+ **/
+gchar *
+gs_app_get_packaging_format (GsApp *app)
+{
+       AsBundleKind bundle_kind;
+       const gchar *bundle_kind_ui;
+       const gchar *packaging_format;
+
+       /* does the app have packaging format set? */
+       packaging_format = gs_app_get_metadata_item (app, "GnomeSoftware::PackagingFormat");
+       if (packaging_format != NULL)
+               return g_strdup (packaging_format);
+
+       /* fall back to bundle kind */
+       bundle_kind = gs_app_get_bundle_kind (app);
+       switch (bundle_kind) {
+       case AS_BUNDLE_KIND_LIMBA:
+               bundle_kind_ui = "Limba";
+               break;
+       case AS_BUNDLE_KIND_FLATPAK:
+               bundle_kind_ui = "Flatpak";
+               break;
+       case AS_BUNDLE_KIND_SNAP:
+               bundle_kind_ui = "Snap";
+               break;
+       case AS_BUNDLE_KIND_PACKAGE:
+               bundle_kind_ui = "Package";
+               break;
+       case AS_BUNDLE_KIND_CABINET:
+               bundle_kind_ui = "Cabinet";
+               break;
+       case AS_BUNDLE_KIND_APPIMAGE:
+               bundle_kind_ui = "AppImage";
+               break;
+       default:
+               g_warning ("unhandled bundle kind %s", as_bundle_kind_to_string (bundle_kind));
+               bundle_kind_ui = as_bundle_kind_to_string (bundle_kind);
+       }
+
+       g_assert (bundle_kind_ui != NULL);
+       return g_strdup (bundle_kind_ui);
+}
+
 /* vim: set noexpandtab: */
diff --git a/lib/gs-app.h b/lib/gs-app.h
index f05779a2..daf39592 100644
--- a/lib/gs-app.h
+++ b/lib/gs-app.h
@@ -1,7 +1,7 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  *
  * Copyright (C) 2013-2018 Richard Hughes <richard hughsie com>
- * Copyright (C) 2014-2015 Kalev Lember <klember redhat com>
+ * Copyright (C) 2014-2018 Kalev Lember <klember redhat com>
  *
  * Licensed under the GNU General Public License Version 2
  *
@@ -325,6 +325,8 @@ void                 gs_app_remove_quirk            (GsApp          *app,
                                                 AsAppQuirk      quirk);
 gboolean        gs_app_is_installed            (GsApp          *app);
 gboolean        gs_app_is_updatable            (GsApp          *app);
+gchar          *gs_app_get_origin_ui           (GsApp          *app);
+gchar          *gs_app_get_packaging_format    (GsApp          *app);
 G_END_DECLS
 
 #endif /* __GS_APP_H */
diff --git a/src/gs-details-page.c b/src/gs-details-page.c
index ea1c086f..d74aec6e 100644
--- a/src/gs-details-page.c
+++ b/src/gs-details-page.c
@@ -886,11 +886,10 @@ gs_details_page_get_alternates_cb (GObject *source_object,
        GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (source_object);
        g_autoptr(GError) error = NULL;
        g_autoptr(GsAppList) list = NULL;
-       g_autoptr(GsOsRelease) os_release = NULL;
        GtkWidget *origin_box;
        GtkWidget *origin_button_label;
        GtkWidget *origin_popover_list_box;
-       const gchar *origin_ui = NULL;
+       g_autofree gchar *origin_ui = NULL;
 
        origin_box = GTK_WIDGET (gtk_builder_get_object (self->builder, "origin_box"));
        origin_button_label = GTK_WIDGET (gtk_builder_get_object (self->builder, "origin_button_label"));
@@ -923,20 +922,7 @@ gs_details_page_get_alternates_cb (GObject *source_object,
                gtk_container_add (GTK_CONTAINER (origin_popover_list_box), row);
        }
 
-       /* use the distro name for official packages */
-       if (gs_app_has_quirk (self->app, AS_APP_QUIRK_PROVENANCE)) {
-               os_release = gs_os_release_new (NULL);
-               if (os_release != NULL)
-                       origin_ui = gs_os_release_get_name (os_release);
-       } else if (gs_app_get_state (self->app) == AS_APP_STATE_AVAILABLE_LOCAL) {
-               /* TRANSLATORS: this is a locally downloaded package */
-               origin_ui = _("Local file");
-       }
-
-       /* fall back to origin */
-       if (origin_ui == NULL)
-               origin_ui = gs_app_get_origin (self->app);
-
+       origin_ui = gs_app_get_origin_ui (self->app);
        if (origin_ui != NULL)
                gtk_label_set_text (GTK_LABEL (origin_button_label), origin_ui);
        else
diff --git a/src/gs-origin-popover-row.c b/src/gs-origin-popover-row.c
index 8b70f013..cd80b828 100644
--- a/src/gs-origin-popover-row.c
+++ b/src/gs-origin-popover-row.c
@@ -43,28 +43,14 @@ static void
 refresh_ui (GsOriginPopoverRow *row)
 {
        GsOriginPopoverRowPrivate *priv = gs_origin_popover_row_get_instance_private (row);
-       const gchar *origin_ui = NULL;
-       const gchar *packaging_format;
+       g_autofree gchar *origin_ui = NULL;
+       g_autofree gchar *packaging_format = NULL;
        g_autofree gchar *url = NULL;
-       g_autoptr(GsOsRelease) os_release = NULL;
 
        g_assert (GS_IS_ORIGIN_POPOVER_ROW (row));
        g_assert (GS_IS_APP (priv->app));
 
-       /* use the distro name for official packages */
-       if (gs_app_has_quirk (priv->app, AS_APP_QUIRK_PROVENANCE)) {
-               os_release = gs_os_release_new (NULL);
-               if (os_release != NULL)
-                       origin_ui = gs_os_release_get_name (os_release);
-       } else if (gs_app_get_state (priv->app) == AS_APP_STATE_AVAILABLE_LOCAL) {
-               /* TRANSLATORS: this is a locally downloaded package */
-               origin_ui = _("Local file");
-       }
-
-       /* fall back to origin */
-       if (origin_ui == NULL)
-               origin_ui = gs_app_get_origin (priv->app);
-
+       origin_ui = gs_app_get_origin_ui (priv->app);
        if (origin_ui != NULL) {
                gtk_label_set_text (GTK_LABEL (priv->name_label), origin_ui);
        }
@@ -85,14 +71,7 @@ refresh_ui (GsOriginPopoverRow *row)
                gtk_widget_hide (priv->url_box);
        }
 
-       packaging_format = gs_app_get_metadata_item (priv->app, "GnomeSoftware::PackagingFormat");
-       if (packaging_format == NULL) {
-               AsBundleKind bundle_kind;
-               bundle_kind = gs_app_get_bundle_kind (priv->app);
-               if (bundle_kind != AS_BUNDLE_KIND_UNKNOWN)
-                       packaging_format = as_bundle_kind_to_string (bundle_kind);
-       }
-
+       packaging_format = gs_app_get_packaging_format (priv->app);
        if (packaging_format != NULL) {
                gtk_label_set_text (GTK_LABEL (priv->format_label), packaging_format);
                gtk_widget_show (priv->format_box);


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