[totem/wip/hadess/more-save-file-fixes: 4/5] save-file: Only enable "Make available offline" if stream not already saved



commit dc4e083945e00d0df0eef7a938d04cecf22f6632
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Feb 7 11:14:17 2019 +0100

    save-file: Only enable "Make available offline" if stream not already saved
    
    In totem_save_file_download_filename(), check if there's already a saved
    file for that stream, so as not to offer saving it again.
    
    Closes: #295

 src/plugins/save-file/totem-save-file.c | 52 +++++++++++++++++++++++++++++++--
 1 file changed, 50 insertions(+), 2 deletions(-)
---
diff --git a/src/plugins/save-file/totem-save-file.c b/src/plugins/save-file/totem-save-file.c
index b76ad5d03..102954ae4 100644
--- a/src/plugins/save-file/totem-save-file.c
+++ b/src/plugins/save-file/totem-save-file.c
@@ -51,6 +51,8 @@ typedef struct {
        char        *name;
        gboolean     is_tmp;
 
+       GCancellable *cancellable;
+
        GSimpleAction *action;
 } TotemSaveFilePluginPrivate;
 
@@ -329,12 +331,45 @@ out:
        g_clear_object (&file);
 }
 
+static void
+cache_file_exists_cb (GObject      *source_object,
+                     GAsyncResult *res,
+                     gpointer      user_data)
+{
+       TotemSaveFilePlugin *pi;
+       GFileInfo *info;
+       GError *error = NULL;
+       char *path;
+
+       path = g_file_get_path (G_FILE (source_object));
+       info = g_file_query_info_finish (G_FILE (source_object), res, &error);
+       if (!info) {
+               if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+                       pi = user_data;
+                       g_simple_action_set_enabled (G_SIMPLE_ACTION (pi->priv->action), TRUE);
+
+                       g_debug ("Enabling offline save, as '%s' for '%s'",
+                                path, pi->priv->mrl);
+               }
+               g_free (path);
+               g_error_free (error);
+               return;
+       }
+       g_object_unref (info);
+
+       pi = user_data;
+       g_debug ("Not enabling offline save, as '%s' already exists for '%s'",
+                path, pi->priv->mrl);
+       g_free (path);
+}
+
 static void
 totem_save_file_download_filename (GObject    *gobject,
                                   GParamSpec *pspec,
                                   TotemSaveFilePlugin *pi)
 {
-       char *filename;
+       char *filename, *cache_path;
+       GFile *file;
 
        /* We're already ready to copy it */
        if (pi->priv->cache_mrl != NULL)
@@ -354,7 +389,14 @@ totem_save_file_download_filename (GObject    *gobject,
 
        g_debug ("Cache MRL: '%s', name: '%s'", pi->priv->cache_mrl, pi->priv->name);
 
-       g_simple_action_set_enabled (G_SIMPLE_ACTION (pi->priv->action), TRUE);
+       cache_path = checksum_path_for_mrl (pi->priv->mrl);
+       file = g_file_new_for_path (cache_path);
+       g_free (cache_path);
+
+       g_file_query_info_async (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
+                                G_FILE_QUERY_INFO_NONE, 0, pi->priv->cancellable,
+                                cache_file_exists_cb, pi);
+       g_object_unref (file);
 }
 
 static void
@@ -369,6 +411,7 @@ impl_activate (PeasActivatable *plugin)
 
        priv->totem = g_object_get_data (G_OBJECT (plugin), "object");
        priv->bvw = totem_object_get_video_widget (priv->totem);
+       priv->cancellable = g_cancellable_new ();
 
        g_signal_connect (priv->totem,
                          "file-opened",
@@ -416,6 +459,11 @@ impl_deactivate (PeasActivatable *plugin)
 
        totem_object_empty_menu_section (priv->totem, "save-placeholder");
 
+       if (priv->cancellable) {
+               g_cancellable_cancel (priv->cancellable);
+               g_clear_object (&priv->cancellable);
+       }
+
        priv->totem = NULL;
        priv->bvw = NULL;
 


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