[gnome-software/wip/william/cherry-pick-4: 16/23] Refresh appstream index if needed



commit f0b5a75a3e9074d590dcd510c3d31434c3d0ff05
Author: William Hua <william hua canonical com>
Date:   Thu Jun 2 15:53:11 2016 -0400

    Refresh appstream index if needed

 src/gs-application.c              |   84 ++++++++++++++++++++++++++++++++++++-
 src/gs-plugin.h                   |    1 +
 src/plugins/gs-plugin-appstream.c |   15 +++++++
 3 files changed, 99 insertions(+), 1 deletions(-)
---
diff --git a/src/gs-application.c b/src/gs-application.c
index a68947c..065298b 100644
--- a/src/gs-application.c
+++ b/src/gs-application.c
@@ -247,15 +247,97 @@ gs_application_dbus_unregister (GApplication    *application,
 }
 
 static void
+refreshed_cb (GObject      *source_object,
+             GAsyncResult *res,
+             gpointer      user_data)
+{
+       GsPluginLoader *loader = GS_PLUGIN_LOADER (source_object);
+
+       if (gs_plugin_loader_refresh_finish (loader, res, NULL)) {
+               gs_plugin_loader_refresh_async (loader,
+                                               0,
+                                               GS_PLUGIN_REFRESH_FLAGS_UI,
+                                               NULL,
+                                               NULL,
+                                               NULL);
+       }
+}
+
+static void
+start_refresh (GsApplication *app)
+{
+       g_action_group_activate_action (G_ACTION_GROUP (app),
+                                       "set-mode",
+                                       g_variant_new_string ("updates"));
+
+       gs_plugin_loader_refresh_async (gs_application_get_plugin_loader (app),
+                                       0,
+                                       GS_PLUGIN_REFRESH_FLAGS_METADATA |
+                                       GS_PLUGIN_REFRESH_FLAGS_PAYLOAD |
+                                       GS_PLUGIN_REFRESH_FLAGS_UI,
+                                       NULL,
+                                       refreshed_cb,
+                                       app);
+}
+
+#define APP_INFO_PATH "/var/lib/app-info"
+
+static gboolean
+needs_refresh (void)
+{
+       g_autoptr(GDir) dir = g_dir_open (APP_INFO_PATH, 0, NULL);
+
+       return dir == NULL || g_dir_read_name (dir) == NULL;
+}
+
+static gboolean
+ask_refresh (gpointer user_data)
+{
+       GsApplication *app = user_data;
+       GtkWindow *parent;
+       GtkWidget *dialog;
+
+       parent = gtk_application_get_active_window (GTK_APPLICATION (app));
+
+       if (gtk_widget_is_visible (GTK_WIDGET (parent))) {
+               dialog = gtk_message_dialog_new (parent,
+                                                GTK_DIALOG_MODAL |
+                                                GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                GTK_MESSAGE_QUESTION,
+                                                GTK_BUTTONS_YES_NO,
+                                                _("An update is needed to show all installable apps. 
Download now?"));
+
+               if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
+                       start_refresh (app);
+
+               gtk_widget_destroy (dialog);
+       }
+
+       return G_SOURCE_REMOVE;
+}
+
+static void
+first_run_dialog_destroyed_cb (GtkWidget *object,
+                              gpointer   user_data)
+{
+       g_signal_handlers_disconnect_by_data (object, user_data);
+
+       if (needs_refresh ())
+               gdk_threads_add_idle (ask_refresh, user_data);
+}
+
+static void
 gs_application_show_first_run_dialog (GsApplication *app)
 {
        GtkWidget *dialog;
 
        if (g_settings_get_boolean (app->settings, "first-run") == TRUE) {
                dialog = gs_first_run_dialog_new ();
+               g_signal_connect (dialog, "destroy", G_CALLBACK (first_run_dialog_destroyed_cb), app);
                gs_shell_modal_dialog_present (app->shell, GTK_DIALOG (dialog));
                g_settings_set_boolean (app->settings, "first-run", FALSE);
-       }
+       } else if (needs_refresh ())
+               gdk_threads_add_idle (ask_refresh, app);
 }
 
 static void
diff --git a/src/gs-plugin.h b/src/gs-plugin.h
index 230c4a1..a474569 100644
--- a/src/gs-plugin.h
+++ b/src/gs-plugin.h
@@ -195,6 +195,7 @@ typedef enum {
        GS_PLUGIN_REFRESH_FLAGS_METADATA                = 1 << 0,
        GS_PLUGIN_REFRESH_FLAGS_PAYLOAD                 = 1 << 1,
        GS_PLUGIN_REFRESH_FLAGS_INTERACTIVE             = 1 << 2,
+       GS_PLUGIN_REFRESH_FLAGS_UI                      = 1 << 3,
        /*< private >*/
        GS_PLUGIN_REFRESH_FLAGS_LAST
 } GsPluginRefreshFlags;
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index 244f223..edb4a45 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -216,6 +216,21 @@ gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error)
        return TRUE;
 }
 
+gboolean
+gs_plugin_refresh (GsPlugin              *plugin,
+                  guint                  cache_age,
+                  GsPluginRefreshFlags   flags,
+                  GCancellable          *cancellable,
+                  GError               **error)
+{
+       if (flags & GS_PLUGIN_REFRESH_FLAGS_UI) {
+               gs_plugin_setup (plugin, cancellable, error);
+               gs_plugin_updates_changed (plugin);
+       }
+
+       return TRUE;
+}
+
 static gboolean
 gs_plugin_refine_from_id (GsPlugin *plugin,
                          GsApp *app,


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