[nautilus/wip/ernestask/tasks: 13/15] Add attribute task class



commit 747cb0be0f5e60a5b497d754ee83023ad70f0424
Author: Ernestas Kulik <ernestask gnome org>
Date:   Tue Jun 27 20:52:34 2017 +0300

    Add attribute task class

 src/meson.build                     |    4 +-
 src/tasks/nautilus-attribute-task.c |  207 +++++++++++++++++++++++++++++++++++
 src/tasks/nautilus-attribute-task.h |   44 ++++++++
 3 files changed, 254 insertions(+), 1 deletions(-)
---
diff --git a/src/meson.build b/src/meson.build
index 4297951..d526797 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -270,7 +270,9 @@ libnautilus_sources = [
     'tasks/nautilus-extract-task.h',
     'tasks/nautilus-extract-task.c',
     'tasks/nautilus-deep-count-task.c',
-    'tasks/nautilus-deep-count-task.h'
+    'tasks/nautilus-deep-count-task.h',
+    'tasks/nautilus-attribute-task.c',
+    'tasks/nautilus-attribute-task.h'
 ]
 
 if get_option ('enable-tracker')
diff --git a/src/tasks/nautilus-attribute-task.c b/src/tasks/nautilus-attribute-task.c
new file mode 100644
index 0000000..0d8e867
--- /dev/null
+++ b/src/tasks/nautilus-attribute-task.c
@@ -0,0 +1,207 @@
+/* Copyright (C) 2017 Ernestas Kulik <ernestask gnome org>
+ *
+ * This file is part of Nautilus.
+ *
+ * Nautilus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Nautilus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Nautilus.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "nautilus-attribute-task.h"
+
+typedef struct
+{
+    GCancellable *cancellable;
+
+    GList *files;
+    const char *attributes;
+} NautilusAttributeTaskPrivate;
+
+static void iface_init (NautilusTaskInterface *iface);
+
+G_DEFINE_ABSTRACT_TYPE_WITH_CODE (NautilusAttributeTask, nautilus_attribute_task,
+                                  G_TYPE_OBJECT,
+                                  G_IMPLEMENT_INTERFACE (NAUTILUS_TYPE_TASK,
+                                                         iface_init)
+                                  G_ADD_PRIVATE (NautilusAttributeTask))
+
+enum
+{
+    PROP_FILES = 1,
+    PROP_ATTRIBUTES,
+    N_PROPERTIES
+};
+
+static GParamSpec *properties[N_PROPERTIES] = { NULL };
+
+static GCancellable *
+get_cancellable (NautilusTask *task)
+{
+    NautilusAttributeTask *self;
+    NautilusAttributeTaskPrivate *priv;
+
+    self = NAUTILUS_ATTRIBUTE_TASK (task);
+    priv = nautilus_attribute_task_get_instance_private (self);
+
+    return priv->cancellable;
+}
+
+static void
+query_info (gpointer data,
+            gpointer user_data)
+{
+    NautilusAttributeTask *self;
+    GObjectClass *object_class;
+    NautilusAttributeTaskClass *attribute_task_class;
+    NautilusFile *file;
+    NautilusAttributeTaskPrivate *priv;
+    g_autoptr (GFile) location = NULL;
+    GError *error = NULL;
+    g_autoptr (GFileInfo) info = NULL;
+
+    self = NAUTILUS_ATTRIBUTE_TASK (user_data);
+    object_class = G_OBJECT_GET_CLASS (self);
+    attribute_task_class = NAUTILUS_ATTRIBUTE_TASK_CLASS (object_class);
+    file = NAUTILUS_FILE (data);
+
+    if (!attribute_task_class->is_cache_invalid (self, file))
+    {
+        return;
+    }
+
+    priv = nautilus_attribute_task_get_instance_private (self);
+    location = nautilus_file_get_location (file);
+    info = g_file_query_info (location, priv->attributes,
+                              G_FILE_QUERY_INFO_NONE,
+                              priv->cancellable,
+                              &error);
+
+    attribute_task_class->update_cache (self,
+                                        file, g_object_ref (info),
+                                        error);
+}
+
+static void
+execute (NautilusTask *task)
+{
+    NautilusAttributeTask *self;
+    NautilusAttributeTaskPrivate *priv;
+
+    self = NAUTILUS_ATTRIBUTE_TASK (task);
+    priv = nautilus_attribute_task_get_instance_private (self);
+
+    g_list_foreach (priv->files, query_info, self);
+}
+
+static void
+iface_init (NautilusTaskInterface *iface)
+{
+    iface->get_cancellable = get_cancellable;
+    iface->execute = execute;
+}
+
+static gpointer
+file_copy_func (gconstpointer src,
+                gpointer      data)
+{
+    return nautilus_file_ref (NAUTILUS_FILE ((gpointer) src));
+}
+
+static void
+set_property (GObject      *object,
+              guint         property_id,
+              const GValue *value,
+              GParamSpec   *pspec)
+{
+    NautilusAttributeTask *self;
+    NautilusAttributeTaskPrivate *priv;
+
+    self = NAUTILUS_ATTRIBUTE_TASK (object);
+    priv = nautilus_attribute_task_get_instance_private (self);
+
+    switch (property_id)
+    {
+        case PROP_FILES:
+        {
+            GList *list;
+
+            list = (GList *) g_value_get_pointer (value);
+
+            g_list_free_full (priv->files,
+                              (GDestroyNotify) nautilus_file_unref);
+
+            priv->files = g_list_copy_deep (list, file_copy_func, NULL);
+        }
+        break;
+
+        case PROP_ATTRIBUTES:
+        {
+            g_free ((gpointer) priv->attributes);
+
+            priv->attributes = g_value_dup_string (value);
+        }
+        break;
+
+        default:
+        {
+        }
+    }
+}
+
+static void
+finalize (GObject *object)
+{
+    NautilusAttributeTask *self;
+    NautilusAttributeTaskPrivate *priv;
+
+    self = NAUTILUS_ATTRIBUTE_TASK (object);
+    priv = nautilus_attribute_task_get_instance_private (self);
+
+    g_clear_object (&priv->cancellable);
+    g_list_free_full (priv->files,
+                      (GDestroyNotify) nautilus_file_unref);
+    g_clear_pointer (&priv->attributes, g_free);
+}
+
+static void
+nautilus_attribute_task_class_init (NautilusAttributeTaskClass *klass)
+{
+    GObjectClass *object_class;
+
+    object_class = G_OBJECT_CLASS (klass);
+
+    object_class->set_property = set_property;
+    object_class->finalize = finalize;
+
+    properties[PROP_FILES] =
+        g_param_spec_pointer ("files", "Files", "Files",
+                              G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
+
+    properties[PROP_ATTRIBUTES] =
+        g_param_spec_string ("attributes", "Attributes", "Attributes",
+                             NULL,
+                             G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
+
+    g_object_class_install_properties (object_class, N_PROPERTIES, properties);
+}
+
+static void
+nautilus_attribute_task_init (NautilusAttributeTask *self)
+{
+    NautilusAttributeTaskPrivate *priv;
+
+    priv = nautilus_attribute_task_get_instance_private (self);
+
+    priv->cancellable = g_cancellable_new ();
+    priv->files = NULL;
+    priv->attributes = NULL;
+}
diff --git a/src/tasks/nautilus-attribute-task.h b/src/tasks/nautilus-attribute-task.h
new file mode 100644
index 0000000..6715e25
--- /dev/null
+++ b/src/tasks/nautilus-attribute-task.h
@@ -0,0 +1,44 @@
+/* Copyright (C) 2017 Ernestas Kulik <ernestask gnome org>
+ *
+ * This file is part of Nautilus.
+ *
+ * Nautilus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Nautilus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Nautilus.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NAUTILUS_ATTRIBUTE_TASK_H
+#define NAUTILUS_ATTRIBUTE_TASK_H
+
+#include "nautilus-task.h"
+
+#include "nautilus-file.h"
+
+#define NAUTILUS_TYPE_ATTRIBUTE_TASK (nautilus_attribute_task_get_type ())
+
+G_DECLARE_DERIVABLE_TYPE (NautilusAttributeTask, nautilus_attribute_task,
+                          NAUTILUS, ATTRIBUTE_TASK,
+                          GObject)
+
+struct _NautilusAttributeTaskClass
+{
+    GObjectClass parent_class;
+
+    gboolean (*is_cache_invalid) (NautilusAttributeTask *attribute_task,
+                                  NautilusFile          *file);
+    void     (*update_cache)     (NautilusAttributeTask *task,
+                                  NautilusFile          *file,
+                                  GFileInfo             *info,
+                                  GError                *error);
+};
+
+#endif


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