Branch data Line data Source code
1 : : /* 2 : : * gnome-keyring 3 : : * 4 : : * Copyright (C) 2011 Collabora Ltd. 5 : : * 6 : : * This program is free software; you can redistribute it and/or modify 7 : : * it under the terms of the GNU Lesser General Public License as 8 : : * published by the Free Software Foundation; either version 2.1 of 9 : : * the License, or (at your option) any later version. 10 : : * 11 : : * This program is distributed in the hope that it will be useful, but 12 : : * WITHOUT ANY WARRANTY; without even the implied warranty of 13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 : : * Lesser General Public License for more details. 15 : : * 16 : : * You should have received a copy of the GNU Lesser General Public 17 : : * License along with this program; if not, see <http://www.gnu.org/licenses/>. 18 : : * 19 : : * Author: Stef Walter <stefw@collabora.co.uk> 20 : : */ 21 : : 22 : : #include "config.h" 23 : : 24 : : #include "gcr-callback-output-stream.h" 25 : : 26 : : #include <glib/gi18n-lib.h> 27 : : 28 : : struct _GcrCallbackOutputStream { 29 : : GOutputStream parent; 30 : : GcrCallbackOutputFunc callback; 31 : : gpointer user_data; 32 : : GDestroyNotify destroy_func; 33 : : }; 34 : : 35 [ + + + - : 26 : G_DEFINE_TYPE (GcrCallbackOutputStream, _gcr_callback_output_stream, G_TYPE_OUTPUT_STREAM); + + ] 36 : : 37 : : static void 38 : 6 : _gcr_callback_output_stream_init (GcrCallbackOutputStream *self) 39 : : { 40 : : 41 : 6 : } 42 : : 43 : : static gssize 44 : 6 : _gcr_callback_output_stream_write (GOutputStream *stream, 45 : : const void *buffer, 46 : : gsize count, 47 : : GCancellable *cancellable, 48 : : GError **error) 49 : : { 50 : 6 : GcrCallbackOutputStream *self = GCR_CALLBACK_OUTPUT_STREAM (stream); 51 : : 52 [ - + ]: 6 : if (g_cancellable_set_error_if_cancelled (cancellable, error)) { 53 : 0 : return -1; 54 [ - + ]: 6 : } else if (self->callback == NULL) { 55 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED, 56 : : _("The stream was closed")); 57 : 0 : return -1; 58 : : } 59 : : 60 : 6 : return (self->callback) (buffer, count, cancellable, self->user_data, error); 61 : : } 62 : : 63 : : static gboolean 64 : 12 : _gcr_callback_output_stream_close (GOutputStream *stream, 65 : : GCancellable *cancellable, 66 : : GError **error) 67 : : { 68 : 12 : GcrCallbackOutputStream *self = GCR_CALLBACK_OUTPUT_STREAM (stream); 69 [ - + ]: 12 : if (g_cancellable_set_error_if_cancelled (cancellable, error)) { 70 : 0 : return FALSE; 71 [ + + ]: 12 : } else if (self->callback == NULL) { 72 : 6 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED, 73 : : _("The stream was closed")); 74 : 6 : return FALSE; 75 : : } 76 : : 77 [ - + ]: 6 : if (self->destroy_func != NULL) 78 : 0 : (self->destroy_func) (self->user_data); 79 : 6 : self->destroy_func = NULL; 80 : 6 : self->user_data = NULL; 81 : 6 : self->callback = NULL; 82 : : 83 : 6 : return TRUE; 84 : : } 85 : : 86 : : static void 87 : 6 : _gcr_callback_output_stream_dispose (GObject *obj) 88 : : { 89 : 6 : _gcr_callback_output_stream_close (G_OUTPUT_STREAM (obj), NULL, NULL); 90 : 6 : G_OBJECT_CLASS (_gcr_callback_output_stream_parent_class)->dispose (obj); 91 : 6 : } 92 : : 93 : : static void 94 : 1 : _gcr_callback_output_stream_class_init (GcrCallbackOutputStreamClass *klass) 95 : : { 96 : 1 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass); 97 : 1 : GOutputStreamClass *output_class = G_OUTPUT_STREAM_CLASS (klass); 98 : : 99 : 1 : gobject_class->dispose = _gcr_callback_output_stream_dispose; 100 : 1 : output_class->write_fn = _gcr_callback_output_stream_write; 101 : 1 : output_class->close_fn = _gcr_callback_output_stream_close; 102 : 1 : } 103 : : 104 : : /** 105 : : * _gcr_callback_output_stream_new: (skip) 106 : : * 107 : : * Create a new output stream which invokes the callback. 108 : : * 109 : : * Returns: (transfer full) (type Gcr.CallbackOutputStream): the new stream 110 : : */ 111 : : GOutputStream * 112 : 6 : _gcr_callback_output_stream_new (GcrCallbackOutputFunc callback, 113 : : gpointer user_data, 114 : : GDestroyNotify destroy_func) 115 : : { 116 : : GcrCallbackOutputStream *self; 117 : : 118 [ - + ]: 6 : g_return_val_if_fail (callback, NULL); 119 : : 120 : 6 : self = g_object_new (GCR_TYPE_CALLBACK_OUTPUT_STREAM, NULL); 121 : 6 : self->callback = callback; 122 : 6 : self->user_data = user_data; 123 : 6 : self->destroy_func = destroy_func; 124 : : 125 : 6 : return G_OUTPUT_STREAM (self); 126 : : }