Rewritten Cache Manager to better match its W32 original behaviour
[captive.git] / src / libcaptive / cc / privatebcb.c
1 /* $Id$
2  * reactos Cache Manager (Cc*) PrivateBcb structure of libcaptive
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
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; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "privatebcb.h" /* self */
23 #include "privatebcb-priv.h"    /* self */
24 #include <glib-object.h>
25 #include "captive/macros.h"
26 #include <glib/ghash.h>
27
28
29 /* CONFIG: */
30
31 #define CAPTIVE_PUBLIC_BCB_NODETYPECODE 0xDE45  /* FIXME: W32 undocumented */
32
33
34 static GHashTable *CaptivePrivateBcbObject_hash;
35
36 static void CaptivePrivateBcbObject_hash_init(void)
37 {
38         if (CaptivePrivateBcbObject_hash)
39                 return;
40         CaptivePrivateBcbObject_hash=g_hash_table_new(
41                         g_direct_hash,  /* hash_func */
42                         g_direct_equal);        /* key_equal_func */
43 }
44
45 static gpointer captive_private_bcb_object_parent_class=NULL;
46
47
48 static void captive_private_bcb_object_finalize(CaptivePrivateBcbObject *captive_private_bcb_object)
49 {
50         g_return_if_fail(captive_private_bcb_object!=NULL);
51
52         if (captive_private_bcb_object->SharedCacheMap) {
53                 g_object_unref(captive_private_bcb_object->SharedCacheMap);
54                 captive_private_bcb_object->SharedCacheMap=NULL;
55                 }
56         if (captive_private_bcb_object->PublicBcb) {
57                 g_free(captive_private_bcb_object->PublicBcb);
58                 captive_private_bcb_object->PublicBcb=NULL;
59                 }
60
61         G_OBJECT_CLASS(captive_private_bcb_object_parent_class)->finalize((GObject *)captive_private_bcb_object);
62 }
63
64
65 static void captive_private_bcb_object_class_init(CaptivePrivateBcbObjectClass *class)
66 {
67 GObjectClass *gobject_class=G_OBJECT_CLASS(class);
68
69         captive_private_bcb_object_parent_class=g_type_class_ref(G_TYPE_OBJECT);
70         gobject_class->finalize=(void (*)(GObject *object))captive_private_bcb_object_finalize;
71 }
72
73
74 static void captive_private_bcb_object_init(CaptivePrivateBcbObject *captive_private_bcb_object)
75 {
76 PUBLIC_BCB *PublicBcb;
77
78         captive_new(PublicBcb);
79         PublicBcb->NodeTypeCode=CAPTIVE_PUBLIC_BCB_NODETYPECODE;
80         PublicBcb->NodeByteSize=sizeof(*PublicBcb);     /* we have no extensions there */
81         PublicBcb->MappedLength=0;
82         PublicBcb->MappedFileOffset.QuadPart=0;
83         captive_private_bcb_object->PublicBcb=PublicBcb;
84         CaptivePrivateBcbObject_hash_init();
85         g_hash_table_insert(CaptivePrivateBcbObject_hash,PublicBcb,captive_private_bcb_object);
86 }
87
88
89 GType captive_private_bcb_object_get_type(void)
90 {
91 static GType captive_private_bcb_object_type=0;
92
93         if (!captive_private_bcb_object_type) {
94 static const GTypeInfo captive_private_bcb_object_info={
95                                 sizeof(CaptivePrivateBcbObjectClass),
96                                 NULL,   /* base_init */
97                                 NULL,   /* base_finalize */
98                                 (GClassInitFunc)captive_private_bcb_object_class_init,
99                                 NULL,   /* class_finalize */
100                                 NULL,   /* class_data */
101                                 sizeof(CaptivePrivateBcbObject),
102                                 5,      /* n_preallocs */
103                                 (GInstanceInitFunc)captive_private_bcb_object_init,
104                                 };
105
106                 captive_private_bcb_object_type=g_type_register_static(G_TYPE_OBJECT,
107                                 "CaptivePrivateBcbObject",&captive_private_bcb_object_info,G_TYPE_FLAG_ABSTRACT);
108                 }
109
110         return captive_private_bcb_object_type;
111 }
112
113 void _captive_private_bcb_object_connect_SharedCacheMap
114                 (CaptivePrivateBcbObject *captive_private_bcb_object,CaptiveSharedCacheMapObject *SharedCacheMap)
115 {
116         g_return_if_fail(CAPTIVE_PRIVATE_BCB_IS_OBJECT(captive_private_bcb_object));
117         g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(SharedCacheMap));
118         g_return_if_fail(captive_private_bcb_object->SharedCacheMap==NULL);
119
120         g_object_ref(SharedCacheMap);
121         captive_private_bcb_object->SharedCacheMap=SharedCacheMap;
122 }
123
124 PUBLIC_BCB *captive_private_bcb_object_get_PublicBcb(CaptivePrivateBcbObject *captive_private_bcb_object)
125 {
126         g_return_val_if_fail(CAPTIVE_PRIVATE_BCB_IS_OBJECT(captive_private_bcb_object),NULL);
127
128         return captive_private_bcb_object->PublicBcb;
129 }
130
131 CaptiveSharedCacheMapObject *captive_private_bcb_object_get_SharedCacheMap(CaptivePrivateBcbObject *captive_private_bcb_object)
132 {
133         g_return_val_if_fail(CAPTIVE_PRIVATE_BCB_IS_OBJECT(captive_private_bcb_object),NULL);
134         g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_private_bcb_object->SharedCacheMap),NULL);
135
136         return captive_private_bcb_object->SharedCacheMap;
137 }
138
139 CaptivePrivateBcbObject *captive_PublicBcb_to_PrivateBcbObject(const PUBLIC_BCB *PublicBcb)
140 {
141 CaptivePrivateBcbObject *captive_private_bcb_object;
142
143         g_return_val_if_fail(PublicBcb!=NULL,NULL);
144
145         CaptivePrivateBcbObject_hash_init();
146         captive_private_bcb_object=g_hash_table_lookup(CaptivePrivateBcbObject_hash,PublicBcb);
147         g_return_val_if_fail(CAPTIVE_PRIVATE_BCB_IS_OBJECT(captive_private_bcb_object),NULL);
148
149         return captive_private_bcb_object;
150 }