Commit d47565f5 authored by Ernestas Kulik's avatar Ernestas Kulik 🦑

Add task class

parent c6a27528
......@@ -264,7 +264,9 @@ libnautilus_sources = [
'nautilus-tag-manager.c',
'nautilus-tag-manager.h',
'nautilus-favorite-directory.c',
'nautilus-favorite-directory.h'
'nautilus-favorite-directory.h',
'nautilus-task.c',
'nautilus-task.h'
]
nautilus_deps = [
......
#include "nautilus-task.h"
typedef struct
{
GCancellable *cancellable;
gpointer task_data;
GDestroyNotify task_data_destroy;
} NautilusTaskPrivate;
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (NautilusTask, nautilus_task,
G_TYPE_OBJECT)
enum
{
PROP_CANCELLABLE = 1,
N_PROPERTIES
};
static GParamSpec *properties[N_PROPERTIES] = { NULL };
static void
set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
switch (property_id)
{
case PROP_CANCELLABLE:
{
NautilusTask *self;
NautilusTaskPrivate *priv;
self = NAUTILUS_TASK (object);
priv = nautilus_task_get_instance_private (self);
if (G_UNLIKELY (priv->cancellable) != NULL)
{
g_clear_object (&priv->cancellable);
}
priv->cancellable = g_value_dup_object (value);
}
break;
default:
{
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
}
static void
finalize (GObject *object)
{
NautilusTask *self;
NautilusTaskPrivate *priv;
self = NAUTILUS_TASK (object);
priv = nautilus_task_get_instance_private (self);
g_clear_object (&priv->cancellable);
if (priv->task_data != NULL && priv->task_data_destroy != NULL)
{
priv->task_data_destroy (priv->task_data);
}
G_OBJECT_CLASS (nautilus_task_parent_class)->finalize (object);
}
static void
nautilus_task_class_init (NautilusTaskClass *klass)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (klass);
object_class->set_property = set_property;
object_class->finalize = finalize;
properties[PROP_CANCELLABLE] =
g_param_spec_object ("cancellable", "Cancellable", "Cancellable",
G_TYPE_CANCELLABLE,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME);
g_object_class_install_properties (object_class, N_PROPERTIES, properties);
}
static void
nautilus_task_init (NautilusTask *self)
{
}
GCancellable *
nautilus_task_get_cancellable (NautilusTask *task)
{
NautilusTaskPrivate *priv;
g_return_val_if_fail (NAUTILUS_TASK (task), NULL);
priv = nautilus_task_get_instance_private (task);
return g_object_ref (priv->cancellable);
}
void
nautilus_task_execute (NautilusTask *task)
{
NautilusTaskClass *klass;
g_return_if_fail (NAUTILUS_IS_TASK (task));
klass = NAUTILUS_TASK_GET_CLASS (task);
g_return_if_fail (klass->execute != NULL);
klass->execute (task);
}
gpointer
nautilus_task_get_task_data (NautilusTask *task)
{
NautilusTaskPrivate *priv;
g_return_val_if_fail (NAUTILUS_IS_TASK (task), NULL);
priv = nautilus_task_get_instance_private (task);
return priv->task_data;
}
void
nautilus_task_set_task_data (NautilusTask *task,
gpointer task_data,
GDestroyNotify task_data_destroy)
{
NautilusTaskPrivate *priv;
g_return_if_fail (NAUTILUS_IS_TASK (task));
priv = nautilus_task_get_instance_private (task);
if (priv->task_data != NULL && priv->task_data_destroy != NULL)
{
priv->task_data_destroy (priv->task_data);
}
priv->task_data = task_data;
priv->task_data_destroy = task_data_destroy;
}
/* 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 2 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_TASK_H
#define NAUTILUS_TASK_H
#include <gio/gio.h>
#include <glib-object.h>
#define NAUTILUS_TYPE_TASK (nautilus_task_get_type ())
G_DECLARE_DERIVABLE_TYPE (NautilusTask, nautilus_task,
NAUTILUS, TASK,
GObject)
typedef void (*NautilusTaskCallback) (NautilusTask *task,
gpointer user_data);
struct _NautilusTaskClass
{
GObjectClass parent_class;
void (*execute) (NautilusTask *task);
};
GCancellable *nautilus_task_get_cancellable (NautilusTask *task);
void nautilus_task_execute (NautilusTask *task);
gpointer nautilus_task_get_task_data (NautilusTask *task);
void nautilus_task_set_task_data (NautilusTask *task,
gpointer task_data,
GDestroyNotify task_data_destroy);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment