1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* gnome-vfs-context.c - context VFS modules can use to communicate with gnome-vfs proper |
---|
3 | |
---|
4 | Copyright (C) 1999 Free Software Foundation |
---|
5 | |
---|
6 | The Gnome Library is free software; you can redistribute it and/or |
---|
7 | modify it under the terms of the GNU Library General Public License as |
---|
8 | published by the Free Software Foundation; either version 2 of the |
---|
9 | License, or (at your option) any later version. |
---|
10 | |
---|
11 | The Gnome Library is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | Library General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU Library General Public |
---|
17 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, USA. |
---|
20 | |
---|
21 | Author: Havoc Pennington <hp@redhat.com> */ |
---|
22 | |
---|
23 | #include <config.h> |
---|
24 | #include "gnome-vfs-context.h" |
---|
25 | |
---|
26 | #include "gnome-vfs-backend-private.h" |
---|
27 | #include "gnome-vfs-cancellation.h" |
---|
28 | #include "gnome-vfs-private-utils.h" |
---|
29 | #include "gnome-vfs-utils.h" |
---|
30 | #include <stdio.h> |
---|
31 | |
---|
32 | #if 1 |
---|
33 | #define DEBUG_MSG (x) printf x |
---|
34 | #else |
---|
35 | #define DEBUG_MSG (x) |
---|
36 | #endif |
---|
37 | |
---|
38 | |
---|
39 | struct GnomeVFSContext { |
---|
40 | GnomeVFSCancellation *cancellation; |
---|
41 | |
---|
42 | guint refcount; |
---|
43 | }; |
---|
44 | |
---|
45 | /* This is a token Context to return in situations |
---|
46 | * where we don't normally have a context: eg, during sync calls |
---|
47 | */ |
---|
48 | const GnomeVFSContext sync_context = {NULL, 1}; |
---|
49 | |
---|
50 | GnomeVFSContext* |
---|
51 | gnome_vfs_context_new (void) |
---|
52 | { |
---|
53 | GnomeVFSContext *ctx; |
---|
54 | |
---|
55 | GNOME_VFS_ASSERT_PRIMARY_THREAD; |
---|
56 | |
---|
57 | ctx = g_new0(GnomeVFSContext, 1); |
---|
58 | |
---|
59 | ctx->cancellation = gnome_vfs_cancellation_new(); |
---|
60 | |
---|
61 | ctx->refcount = 1; |
---|
62 | |
---|
63 | return ctx; |
---|
64 | } |
---|
65 | |
---|
66 | void |
---|
67 | gnome_vfs_context_ref (GnomeVFSContext *ctx) |
---|
68 | { |
---|
69 | g_return_if_fail(ctx != NULL); |
---|
70 | |
---|
71 | /* FIXME: this function should be removed in Gnome 2.0 GnomeVFS */ |
---|
72 | g_warning ("Warning call to deprecated function '%s'\n", G_GNUC_FUNCTION); |
---|
73 | |
---|
74 | ctx->refcount += 1; |
---|
75 | } |
---|
76 | |
---|
77 | /* Note: _unref should be replaced with a _free function in the gnome 2.0 platform */ |
---|
78 | void |
---|
79 | gnome_vfs_context_unref (GnomeVFSContext *ctx) |
---|
80 | { |
---|
81 | g_return_if_fail(ctx != NULL); |
---|
82 | g_return_if_fail(ctx->refcount > 0); |
---|
83 | |
---|
84 | if (ctx->refcount == 1) { |
---|
85 | gnome_vfs_cancellation_destroy(ctx->cancellation); |
---|
86 | |
---|
87 | g_free(ctx); |
---|
88 | } else { |
---|
89 | ctx->refcount -= 1; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | GnomeVFSCancellation* |
---|
94 | gnome_vfs_context_get_cancellation (const GnomeVFSContext *ctx) |
---|
95 | { |
---|
96 | g_return_val_if_fail(ctx != NULL, NULL); |
---|
97 | return ctx->cancellation; |
---|
98 | } |
---|
99 | |
---|
100 | const GnomeVFSContext * |
---|
101 | gnome_vfs_context_peek_current (void) |
---|
102 | { |
---|
103 | const GnomeVFSContext *ret; |
---|
104 | |
---|
105 | gnome_vfs_backend_get_current_context ((GnomeVFSContext **)&ret); |
---|
106 | |
---|
107 | /* If the context is NULL, then this must be a synchronous call */ |
---|
108 | if (ret == NULL) { |
---|
109 | ret = &sync_context; |
---|
110 | } |
---|
111 | |
---|
112 | return ret; |
---|
113 | } |
---|
114 | |
---|
115 | gboolean |
---|
116 | gnome_vfs_context_check_cancellation_current (void) |
---|
117 | { |
---|
118 | const GnomeVFSContext *current_ctx; |
---|
119 | |
---|
120 | current_ctx = gnome_vfs_context_peek_current (); |
---|
121 | |
---|
122 | if (current_ctx == &sync_context) { |
---|
123 | return FALSE; |
---|
124 | } else if (current_ctx != NULL) { |
---|
125 | return gnome_vfs_cancellation_check (gnome_vfs_context_get_cancellation (current_ctx)); |
---|
126 | } else { |
---|
127 | return FALSE; |
---|
128 | } |
---|
129 | } |
---|