[gnome-software/wip/rancell/apt] Refresh appstream index if needed
- From: William Hua <williamhua src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software/wip/rancell/apt] Refresh appstream index if needed
- Date: Wed, 16 Mar 2016 19:15:04 +0000 (UTC)
commit 3c720785c99c4f56a8fb6a4b86632a4e5d056417
Author: William Hua <william hua canonical com>
Date: Wed Mar 16 12:25:53 2016 -0400
Refresh appstream index if needed
src/plugins/gs-plugin-appstream.c | 71 +++++++++++++++++++++++++++++++++++++
1 files changed, 71 insertions(+), 0 deletions(-)
---
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index 4f47533..ea6d8c2 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -26,6 +26,7 @@
#include <gs-utils.h>
#include <gs-plugin.h>
#include <gs-plugin-loader.h>
+#include <gs-application.h>
#include "gs-appstream.h"
@@ -151,6 +152,73 @@ gs_plugin_appstream_get_origins_hash (GPtrArray *array)
return origins;
}
+#define APP_INFO_PATH "/var/lib/app-info"
+#define REFRESH_TIMEOUT 1000
+
+static gboolean
+needs_refresh (GsPlugin *plugin)
+{
+ g_autoptr(GDir) dir = g_dir_open (APP_INFO_PATH, 0, NULL);
+
+ return dir == NULL || g_dir_read_name (dir) == NULL;
+}
+
+static void
+refreshed_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GsPlugin *plugin = user_data;
+ GsPluginLoader *loader = GS_PLUGIN_LOADER (source_object);
+
+ if (gs_plugin_loader_refresh_finish (loader, res, NULL))
+ gs_plugin_updates_changed (plugin);
+}
+
+static void
+start_refresh (GsPlugin *plugin)
+{
+ GApplication *application;
+ GsPluginLoader *loader;
+
+ application = g_application_get_default ();
+
+ loader = gs_application_get_plugin_loader (GS_APPLICATION (application));
+
+ gs_plugin_loader_refresh_async (loader,
+ 0,
+ GS_PLUGIN_REFRESH_FLAGS_UPDATES,
+ NULL,
+ refreshed_cb,
+ plugin);
+}
+
+static gboolean
+ask_refresh (gpointer user_data)
+{
+ GApplication *application;
+ GtkWindow *parent;
+ GtkWidget *dialog;
+
+ application = g_application_get_default ();
+
+ parent = gtk_application_get_active_window (GTK_APPLICATION (application));
+
+ 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 (user_data);
+
+ gtk_widget_destroy (dialog);
+
+ return G_SOURCE_REMOVE;
+}
+
/**
* gs_plugin_appstream_startup:
*
@@ -174,6 +242,9 @@ gs_plugin_appstream_startup (GsPlugin *plugin, GError **error)
ptask = as_profile_start_literal (plugin->profile, "appstream::startup");
+ if (needs_refresh (plugin))
+ gdk_threads_add_timeout (REFRESH_TIMEOUT, ask_refresh, plugin);
+
/* Parse the XML */
if (g_getenv ("GNOME_SOFTWARE_PREFER_LOCAL") != NULL) {
as_store_set_add_flags (plugin->priv->store,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]