aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-medium.c
blob: b6a3ffc9fce81836bbd25e7577d34720cf5f4dd0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camelMedium.c : Abstract class for a medium */


/* 
 *
 * Copyright (C) 1999 Bertrand Guiheneuf <Bertrand.Guiheneuf@inria.fr> .
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
#include <config.h>
#include "camel-medium.h"
#include <stdio.h>
#include "gmime-content-field.h"
#include "string-utils.h"
#include "camel-log.h"
#include "gmime-utils.h"
#include "camel-simple-data-wrapper.h"





static CamelDataWrapperClass *parent_class=NULL;

/* Returns the class for a CamelMedium */
#define CM_CLASS(so) CAMEL_MEDIUM_CLASS (GTK_OBJECT(so)->klass)

static void _add_header (CamelMedium *medium, gchar *header_name, gchar *header_value);
static void _remove_header (CamelMedium *medium, const gchar *header_name);
static const gchar *_get_header (CamelMedium *medium, const gchar *header_name);

static CamelDataWrapper *_get_content_object(CamelMedium *medium);
static void _set_content_object(CamelMedium *medium, CamelDataWrapper *content);

static void _finalize (GtkObject *object);

static void
camel_medium_class_init (CamelMediumClass *camel_medium_class)
{
    CamelDataWrapperClass *camel_data_wrapper_class = CAMEL_DATA_WRAPPER_CLASS (camel_medium_class);
    GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (camel_data_wrapper_class);

    parent_class = gtk_type_class (camel_data_wrapper_get_type ());
    
    /* virtual method definition */
    camel_medium_class->add_header = _add_header;
    camel_medium_class->remove_header = _remove_header;
    camel_medium_class->get_header = _get_header;
    
    
    
    /* virtual method overload */
    //  camel_data_wrapper_class->write_to_stream = _write_to_stream;
    //camel_data_wrapper_class->construct_from_stream = _construct_from_stream;

    gtk_object_class->finalize = _finalize;
}

static void
camel_medium_init (gpointer   object,  gpointer   klass)
{
    CamelMedium *camel_medium = CAMEL_MEDIUM (object);
    
    camel_medium->headers =  g_hash_table_new (g_str_hash, g_str_equal);
}




GtkType
camel_medium_get_type (void)
{
    static GtkType camel_medium_type = 0;
    
    if (!camel_medium_type) {
        GtkTypeInfo camel_medium_info = 
        {
            "CamelMedium",
            sizeof (CamelMedium),
            sizeof (CamelMediumClass),
            (GtkClassInitFunc) camel_medium_class_init,
            (GtkObjectInitFunc) camel_medium_init,
                /* reserved_1 */ NULL,
                /* reserved_2 */ NULL,
            (GtkClassInitFunc) NULL,
        };
        
        camel_medium_type = gtk_type_unique (camel_data_wrapper_get_type (), &camel_medium_info);
    }
    
    return camel_medium_type;
}


static void           
_finalize (GtkObject *object)
{
    CamelMedium *medium = CAMEL_MEDIUM (object);


    CAMEL_LOG_FULL_DEBUG ("Entering CamelMedium::finalize\n");

    if (medium->headers) {
        g_hash_table_destroy (medium->headers);
    }

    GTK_OBJECT_CLASS (parent_class)->finalize (object);
    CAMEL_LOG_FULL_DEBUG ("Leaving CamelMedium::finalize\n");
}



/* **** */

static void
_add_header (CamelMedium *medium, gchar *header_name, gchar *header_value)
{
    gboolean header_exists;
    gchar *old_header_name;
    gchar *old_header_value;
    

    header_exists = g_hash_table_lookup_extended (medium->headers, header_name, 
                              (gpointer *) &old_header_name,
                              (gpointer *) &old_header_value);
    if (header_exists) {
        g_free (old_header_name);
        g_free (old_header_value);
    }
    
    g_hash_table_insert (medium->headers, header_name, header_value);
}


void
camel_medium_add_header (CamelMedium *medium, gchar *header_name, gchar *header_value)
{
    CM_CLASS(medium)->add_header(medium, header_name, header_value);
}


/* **** */


static void
_remove_header (CamelMedium *medium, const gchar *header_name)
{
    
    gboolean header_exists;
    gchar *old_header_name;
    gchar *old_header_value;
    
    header_exists = g_hash_table_lookup_extended (medium->headers, header_name, 
                              (gpointer *) &old_header_name,
                              (gpointer *) &old_header_value);
    if (header_exists) {
        g_free (old_header_name);
        g_free (old_header_value);
    }
    
    g_hash_table_remove (medium->headers, header_name);
    
}

void
camel_medium_remove_header (CamelMedium *medium, const gchar *header_name)
{
    CM_CLASS(medium)->remove_header(medium, header_name);
}


/* **** */


static const gchar *
_get_header (CamelMedium *medium, const gchar *header_name)
{
    
    gchar *old_header_name;
    gchar *old_header_value;
    gchar *header_value;
    
    header_value = (gchar *)g_hash_table_lookup (medium->headers, header_name);
    return header_value;
}

const gchar *
camel_medium_get_header (CamelMedium *medium, const gchar *header_name)
{
    return CM_CLASS(medium)->get_header (medium, header_name);
}


/* **** */


static CamelDataWrapper *
_get_content_object (CamelMedium *medium)
{
    return medium->content;
    
}


CamelDataWrapper *
camel_medium_get_content_object (CamelMedium *medium)
{
    return CM_CLASS(medium)->get_content_object (medium);
}


/* **** */


static void
_set_content_object (CamelMedium *medium, CamelDataWrapper *content)
{
    GMimeContentField *object_content_field;

    CAMEL_LOG_FULL_DEBUG ("Entering CamelMedium::set_content_object\n");
    if (medium->content) {
        CAMEL_LOG_FULL_DEBUG ("CamelMedium::set_content_object unreferencing old content object\n");
        gtk_object_unref (GTK_OBJECT (medium->content));
    }
    gtk_object_ref (GTK_OBJECT (content));
    medium->content = content;
    
}

void 
camel_medium_set_content_object (CamelMedium *medium, CamelDataWrapper *content)
{
    CM_CLASS(medium)->set_content_object (medium, content);
}


/* **** */