| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <gio/gio.h> |
| 25 |
|
| 26 |
#include "dex-cancellable.h" |
| 27 |
#include "dex-future-private.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
typedef struct _DexCancellable |
| 41 |
{ |
| 42 |
DexFuture parent_instance; |
| 43 |
GCancellable *cancellable; |
| 44 |
gulong handler; |
| 45 |
} DexCancellable; |
| 46 |
|
| 47 |
typedef struct _DexCancellableClass |
| 48 |
{ |
| 49 |
DexFutureClass parent_class; |
| 50 |
} DexCancellableClass; |
| 51 |
|
| 52 |
DEX_DEFINE_FINAL_TYPE (DexCancellable, dex_cancellable, DEX_TYPE_FUTURE) |
| 53 |
|
| 54 |
static void |
| 55 |
dex_cancellable_finalize (DexObject *object) |
| 56 |
{ |
| 57 |
DexCancellable *self = (DexCancellable *)object; |
| 58 |
|
| 59 |
if (self->handler) |
| 60 |
g_cancellable_disconnect (self->cancellable, self->handler); |
| 61 |
|
| 62 |
g_clear_object (&self->cancellable); |
| 63 |
|
| 64 |
DEX_OBJECT_CLASS (dex_cancellable_parent_class)->finalize (object); |
| 65 |
} |
| 66 |
|
| 67 |
static void |
| 68 |
dex_cancellable_class_init (DexCancellableClass *cancellable_class) |
| 69 |
{ |
| 70 |
DexObjectClass *object_class = DEX_OBJECT_CLASS (cancellable_class); |
| 71 |
|
| 72 |
object_class->finalize = dex_cancellable_finalize; |
| 73 |
} |
| 74 |
|
| 75 |
static void |
| 76 |
dex_cancellable_init (DexCancellable *cancellable) |
| 77 |
{ |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
DexCancellable * |
| 88 |
dex_cancellable_new (void) |
| 89 |
{ |
| 90 |
return (DexCancellable *)dex_object_create_instance (DEX_TYPE_CANCELLABLE); |
| 91 |
} |
| 92 |
|
| 93 |
static void |
| 94 |
dex_cancellable_cancelled_cb (GCancellable *cancellable, |
| 95 |
DexWeakRef *wr) |
| 96 |
{ |
| 97 |
DexCancellable *self; |
| 98 |
|
| 99 |
g_assert (G_IS_CANCELLABLE (cancellable)); |
| 100 |
g_assert (wr != NULL); |
| 101 |
|
| 102 |
if ((self = dex_weak_ref_get (wr))) |
| 103 |
{ |
| 104 |
self->handler = 0; |
| 105 |
dex_future_complete (DEX_FUTURE (self), |
| 106 |
NULL, |
| 107 |
g_error_new_literal (G_IO_ERROR, |
| 108 |
G_IO_ERROR_CANCELLED, |
| 109 |
"Operation cancelled")); |
| 110 |
dex_clear (&self); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
weak_ref_free (gpointer data) |
| 116 |
{ |
| 117 |
dex_weak_ref_clear (data); |
| 118 |
g_free (data); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
DexFuture * |
| 132 |
dex_cancellable_new_from_cancellable (GCancellable *cancellable) |
| 133 |
{ |
| 134 |
DexWeakRef *wr; |
| 135 |
DexCancellable *ret; |
| 136 |
|
| 137 |
g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), NULL); |
| 138 |
|
| 139 |
ret = dex_cancellable_new (); |
| 140 |
|
| 141 |
wr = g_new0 (DexWeakRef, 1); |
| 142 |
dex_weak_ref_init (wr, ret); |
| 143 |
|
| 144 |
ret->cancellable = g_object_ref (cancellable); |
| 145 |
ret->handler = g_cancellable_connect (cancellable, |
| 146 |
G_CALLBACK (dex_cancellable_cancelled_cb), |
| 147 |
wr, weak_ref_free); |
| 148 |
|
| 149 |
return DEX_FUTURE (ret); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
void |
| 163 |
dex_cancellable_cancel (DexCancellable *cancellable) |
| 164 |
{ |
| 165 |
g_return_if_fail (DEX_IS_CANCELLABLE (cancellable)); |
| 166 |
|
| 167 |
dex_future_complete (DEX_FUTURE (cancellable), |
| 168 |
NULL, |
| 169 |
g_error_new_literal (G_IO_ERROR, |
| 170 |
G_IO_ERROR_CANCELLED, |
| 171 |
"Operation cancelled")); |
| 172 |
} |