[gnome-software/wip/hughsie/screenshot-last-modified: 2/3] Only trust the screenshot cache for 30 days



commit 5c50e4ae5f1651f9861c03b255131187a1242ccc
Author: Richard Hughes <richard hughsie com>
Date:   Wed Nov 9 10:53:24 2016 +0000

    Only trust the screenshot cache for 30 days
    
    Also add a GSetting to allow admins to control how old the cache should get
    before checking for a newer image on the server.
    
    Inspired by a patch from Joaquim Rocha <jrocha endlessm com>, many thanks.

 data/org.gnome.software.gschema.xml |   10 ++++++++++
 src/gs-screenshot-image.c           |   20 ++++++++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)
---
diff --git a/data/org.gnome.software.gschema.xml b/data/org.gnome.software.gschema.xml
index 63c9369..321d163 100644
--- a/data/org.gnome.software.gschema.xml
+++ b/data/org.gnome.software.gschema.xml
@@ -63,6 +63,16 @@
       <default>0</default>
       <summary>The last update timestamp</summary>
     </key>
+    <key name="screenshot-cache-age-maximum" type="u">
+      <default>2592000</default>
+      <summary>The age in seconds to verify the upstream screenshot is still valid</summary>
+      <description>
+        Choosing a larger value will mean less round-trips to the remote server
+        but updates to the screenshots may take longer to show to the user.
+        A value of 0 means to never check the server if the image already exists
+        in the cache.
+      </description>
+    </key>
     <key name="review-server" type="s">
       <default>'https://odrs.gnome.org/1.0/reviews/api'</default>
       <summary>The server to use for application reviews</summary>
diff --git a/src/gs-screenshot-image.c b/src/gs-screenshot-image.c
index 01fbcf4..8541442 100644
--- a/src/gs-screenshot-image.c
+++ b/src/gs-screenshot-image.c
@@ -45,6 +45,7 @@ struct _GsScreenshotImage
        GtkWidget       *image1;
        GtkWidget       *image2;
        GtkWidget       *label_error;
+       GSettings       *settings;
        SoupSession     *session;
        SoupMessage     *message;
        gchar           *filename;
@@ -379,10 +380,23 @@ gs_screenshot_image_load_async (GsScreenshotImage *ssimg,
                return;
        }
 
-       /* does local file already exist */
+       /* does local file already exist and has recently been downloaded */
        if (g_file_test (ssimg->filename, G_FILE_TEST_EXISTS)) {
+               guint64 age_max;
+               g_autoptr(GFile) file = NULL;
+
+               /* show the image we have in cache while we're checking for the
+                * new screenshot (which probably won't have changed) */
                as_screenshot_show_image (ssimg);
-               return;
+
+               /* verify the cache age against the maximum allowed */
+               age_max = g_settings_get_uint (ssimg->settings,
+                                              "screenshot-cache-age-maximum");
+               file = g_file_new_for_path (ssimg->filename);
+               if (age_max > 0 && gs_utils_get_file_age (file) < age_max) {
+                       g_debug ("image new enough, not re-requesting from server");
+                       return;
+               }
        }
 
        /* can we load a blurred smaller version of this straight away */
@@ -460,6 +474,7 @@ gs_screenshot_image_destroy (GtkWidget *widget)
        }
        g_clear_object (&ssimg->screenshot);
        g_clear_object (&ssimg->session);
+       g_clear_object (&ssimg->settings);
 
        g_clear_pointer (&ssimg->filename, g_free);
 
@@ -472,6 +487,7 @@ gs_screenshot_image_init (GsScreenshotImage *ssimg)
        AtkObject *accessible;
 
        ssimg->use_desktop_background = TRUE;
+       ssimg->settings = g_settings_new ("org.gnome.software");
 
        gtk_widget_set_has_window (GTK_WIDGET (ssimg), FALSE);
        gtk_widget_init_template (GTK_WIDGET (ssimg));


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