ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / libgnomevfs / gnome-vfs-context.c
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.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
43 /* This is a token Context to return in situations
44  * where we don't normally have a context: eg, during sync calls
45  */
46 const GnomeVFSContext sync_context = {NULL};
47
48 /**
49  * gnome_vfs_context_new:
50  * 
51  * Creates a new context and cancellation object. Must be called
52  * from the main glib event loop.
53  *
54  * Return value: a newly allocated #GnomeVFSContext
55  **/
56 GnomeVFSContext*
57 gnome_vfs_context_new (void)
58 {
59         GnomeVFSContext *ctx;
60
61         GNOME_VFS_ASSERT_PRIMARY_THREAD;
62
63         ctx = g_new0(GnomeVFSContext, 1);
64
65         ctx->cancellation = gnome_vfs_cancellation_new();
66  
67         return ctx;
68 }
69
70 /**
71  * gnome_vfs_context_free:
72  * @ctx: context to be freed
73  *
74  * Free @ctx and destroy the associated #GnomeVFSCancellation.
75  **/
76 void
77 gnome_vfs_context_free (GnomeVFSContext *ctx)
78 {
79         g_return_if_fail(ctx != NULL);
80   
81         gnome_vfs_cancellation_destroy(ctx->cancellation);
82         
83         g_free(ctx);
84 }
85
86 /**
87  * gnome_vfs_context_get_cancellation:
88  * @ctx: context to get the #GnomeVFSCancellation from
89  *
90  * Retrieve the #GnomeVFSCancellation associated with @ctx.
91  *
92  * Return value: @ctx 's #GnomeVFSCancellation
93  **/
94 GnomeVFSCancellation*
95 gnome_vfs_context_get_cancellation (const GnomeVFSContext *ctx)
96 {
97         g_return_val_if_fail(ctx != NULL, NULL);
98         return ctx->cancellation;
99 }
100
101 /**
102  * gnome_vfs_context_peek_current:
103  *
104  * Get the currently active context. It shouldn't be
105  * manipulated but can be compared to context's the module
106  * holds to determine whether they are active.
107  *
108  * Return value: the currently active #GnomeVFSContext
109  **/
110 const GnomeVFSContext *
111 gnome_vfs_context_peek_current            (void)
112 {
113         const GnomeVFSContext *ret;
114         
115         _gnome_vfs_get_current_context ((GnomeVFSContext **)&ret);
116
117         /* If the context is NULL, then this must be a synchronous call */
118         if (ret == NULL) {
119                 ret = &sync_context;
120         }
121
122         return ret;
123 }
124
125 /**
126  * gnome_vfs_context_check_cancellation_current:
127  * 
128  * Check to see if the currently active context has been cancelled.
129  *
130  * Return value: %TRUE if the currently active context has been cancelled, otherwise %FALSE
131  **/
132 gboolean
133 gnome_vfs_context_check_cancellation_current (void)
134 {
135         const GnomeVFSContext *current_ctx;
136
137         current_ctx = gnome_vfs_context_peek_current ();
138
139         if (current_ctx == &sync_context) {
140                 return FALSE;
141         } else if (current_ctx != NULL) {
142                 return gnome_vfs_cancellation_check (gnome_vfs_context_get_cancellation (current_ctx));
143         } else {
144                 return FALSE;
145         }       
146 }