1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2012 Jonathan Matthew <jonathan@d14n.org>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* The Rhythmbox authors hereby grant permission for non-GPL compatible
11
* GStreamer plugins to be used and distributed together with GStreamer
12
* and Rhythmbox. This permission is above and beyond the permissions granted
13
* by the GPL license by which Rhythmbox is covered. If you modify this code
14
* you may extend this exception to your version of the code, but you are not
15
* obligated to do so. If you do not wish to do so, delete this exception
16
* statement from your version.
18
* This program is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* You should have received a copy of the GNU General Public License
24
* along with this program; if not, write to the Free Software
25
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
31
#include <lib/rb-async-copy.h>
32
#include <lib/rb-debug.h>
35
* SECTION:rb-async-copy
36
* @short_description: performs asynchronous file copies (like g_file_copy_async)
41
static void rb_async_copy_class_init (RBAsyncCopyClass *klass);
42
static void rb_async_copy_init (RBAsyncCopy *copy);
44
struct _RBAsyncCopyPrivate
52
RBAsyncCopyCallback callback;
53
gpointer callback_data;
54
GDestroyNotify destroy_data;
56
RBAsyncCopyProgressCallback progress;
57
gpointer progress_data;
58
GDestroyNotify destroy_progress_data;
61
G_DEFINE_TYPE (RBAsyncCopy, rb_async_copy, G_TYPE_OBJECT);
64
progress_cb (goffset current_num_bytes, goffset total_bytes, gpointer data)
66
RBAsyncCopy *copy = RB_ASYNC_COPY (data);
68
if (copy->priv->progress)
69
copy->priv->progress (copy, current_num_bytes, total_bytes, copy->priv->progress_data);
73
copy_cb (GObject *src, GAsyncResult *res, gpointer data)
75
RBAsyncCopy *copy = RB_ASYNC_COPY (data);
78
result = g_file_copy_finish (G_FILE (src), res, ©->priv->error);
80
rb_debug ("copy finished: %s", (result == FALSE) ? copy->priv->error->message : "ok");
81
copy->priv->callback (copy, result, copy->priv->callback_data);
85
* rb_async_copy_start:
86
* @copy: a #RBAsyncCopy
88
* @dest: destination URI
89
* @callback: completion callback
90
* @user_data: data for completion callback
91
* @destroy_data: destroy function for user_data
93
* Starts copying @src to @dest, calling @callback on completion or error.
96
rb_async_copy_start (RBAsyncCopy *copy,
99
RBAsyncCopyCallback callback,
101
GDestroyNotify destroy_data)
103
g_assert (copy->priv->src == NULL);
105
copy->priv->cancel = g_cancellable_new ();
107
copy->priv->callback = callback;
108
copy->priv->callback_data = user_data;
109
copy->priv->destroy_data = destroy_data;
111
copy->priv->src = g_file_new_for_commandline_arg (src);
112
copy->priv->dest = g_file_new_for_commandline_arg (dest);
114
g_file_copy_async (copy->priv->src,
126
* rb_async_copy_cancel:
127
* @copy: a #RBAsyncCopy
129
* Cancels the loading operation, ensuring that the callback
130
* will not be called again.
133
rb_async_copy_cancel (RBAsyncCopy *copy)
135
g_cancellable_cancel (copy->priv->cancel);
139
* rb_async_copy_set_callback:
140
* @copy: a #RBAsyncCopy
141
* @callback: the progress callback
142
* @user_data: data to pass to the callback
143
* @destroy_data: function to call to destroy user_data
145
* Sets the progress callback for the copy. The callback will
146
* be called periodically while the copy is proceeding.
149
rb_async_copy_set_progress (RBAsyncCopy *copy,
150
RBAsyncCopyProgressCallback callback,
152
GDestroyNotify destroy_data)
154
g_assert (copy->priv->progress == NULL);
155
g_assert (copy->priv->src == NULL);
157
copy->priv->progress = callback;
158
copy->priv->progress_data = user_data;
159
copy->priv->destroy_progress_data = destroy_data;
163
* rb_async_copy_get_error:
164
* @copy: a #RBAsyncCopy
166
* If an error has occurred that prevents the copy from proceeding,
167
* this function will return a #GError, otherwise NULL.
169
* Return value: copy error or NULL
172
rb_async_copy_get_error (RBAsyncCopy *copy)
174
if (copy->priv->error)
175
return g_error_copy (copy->priv->error);
182
* Creates and returns a new #RBAsyncCopy instance.
184
* Return value: #RBAsyncCopy instance
187
rb_async_copy_new (void)
189
return RB_ASYNC_COPY (g_object_new (RB_TYPE_ASYNC_COPY, NULL));
193
impl_finalize (GObject *object)
195
RBAsyncCopy *copy = RB_ASYNC_COPY (object);
197
g_clear_error (©->priv->error);
199
if (copy->priv->cancel) {
200
g_object_unref (copy->priv->cancel);
201
copy->priv->cancel = NULL;
204
if (copy->priv->src) {
205
g_object_unref (copy->priv->src);
206
copy->priv->src = NULL;
208
if (copy->priv->dest) {
209
g_object_unref (copy->priv->dest);
210
copy->priv->dest = NULL;
213
if (copy->priv->destroy_data) {
214
copy->priv->destroy_data (copy->priv->callback_data);
216
if (copy->priv->destroy_progress_data) {
217
copy->priv->destroy_progress_data (copy->priv->progress_data);
220
G_OBJECT_CLASS (rb_async_copy_parent_class)->finalize (object);
224
rb_async_copy_init (RBAsyncCopy *copy)
226
copy->priv = G_TYPE_INSTANCE_GET_PRIVATE (copy, RB_TYPE_ASYNC_COPY, RBAsyncCopyPrivate);
230
rb_async_copy_class_init (RBAsyncCopyClass *klass)
232
GObjectClass *object_class = G_OBJECT_CLASS (klass);
234
object_class->finalize = impl_finalize;
236
g_type_class_add_private (klass, sizeof (RBAsyncCopyPrivate));