aboutsummaryrefslogtreecommitdiffstats
path: root/camel/gmime-content-field.c
blob: bec9721e0a742a4589f52d914516b52496d38b10 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* mime-content_field.c : mime content type field utilities  */

/* 
 *
 * Author : 
 *  Bertrand Guiheneuf <bertrand@helixcode.com>
 *
 * Copyright 1999, 2000 HelixCode (http://www.helixcode.com) .
 *
 * 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 "gmime-content-field.h"
#include "string-utils.h"
#include "camel-log.h"
#include <string.h>
#include "hash-table-utils.h"

/**
 * gmime_content_field_new: Creates a new GMimeContentField object
 * @type: mime type
 * @subtype: mime subtype 
 * 
 * Creates a GMimeContentField object and initialize it with
 * a mime type and a mime subtype. For example, 
 * gmime_content_field_new ("application", "postcript");
 * will create a content field with complete mime type 
 * "application/postscript"
 * 
 * Return value: The newly created GMimeContentField object
 **/
GMimeContentField *
gmime_content_field_new (const gchar *type, const gchar *subtype)
{
    GMimeContentField *ctf;

    ctf = g_new (GMimeContentField, 1);
    ctf->type = g_strdup (type);
    ctf->subtype = g_strdup (subtype);
    ctf->parameters =  g_hash_table_new (g_strcase_hash, g_strcase_equal);
    ctf->ref = 1;

    return ctf;
} 


static void
_free_parameter (gpointer name, gpointer value, gpointer user_data)
{
    g_free (name);
    g_free (value);
}

/**
 * gmime_content_field_free: free a GMimeContentField object
 * @content_field: GMimeContentField object
 * 
 * This method destroys the object and should be used very carefully.
 * Use gmime_content_field_unref instead.
 *
 **/
void 
gmime_content_field_free (GMimeContentField *content_field)
{
    if (!content_field) return;

    g_hash_table_foreach (content_field->parameters, _free_parameter, NULL);
    g_free (content_field->type);
    g_free (content_field->subtype);
    g_hash_table_destroy (content_field->parameters);
    g_free (content_field);
}

/**
 * gmime_content_field_ref: add a reference to a GMimeContentField object
 * @content_field: GMimeContentField object
 * 
 * Tell a GMimeContentField object that something holds a reference
 * on him. This, coupled with the corresponding 
 * gmime_content_field_unref() method allow several 
 * objects to use the same GMimeContentField object.
 **/
void 
gmime_content_field_ref (GMimeContentField *content_field)
{
    content_field->ref += 1;
}

/**
 * gmime_content_field_unref: remove a reference to a GMimeContentField object
 * @content_field: GMimeContentField object
 * 
 * Tell a GMimeContentField object that something which 
 * was holding a reference to him does not need it anymore.
 * When no more reference exist, the GMimeContentField object
 * is freed using gmime_content_field_free().
 *
 **/
void 
gmime_content_field_unref (GMimeContentField *content_field)
{
    if (!content_field) return;
    
    content_field->ref -= 1;
    if (content_field->ref <= 0)
        gmime_content_field_free (content_field);
}



/**
 * gmime_content_field_set_parameter: set a parameter for a GMimeContentField object
 * @content_field: content field
 * @attribute: parameter name 
 * @value: paramteter value
 * 
 * set a parameter (called attribute in RFC 2045) of a content field. Meaningfull
 * or valid parameters name depend on the content type object. For example, 
 * gmime_content_field_set_parameter (cf, "charset", "us-ascii");
 * will make sense for a "text/plain" content field but not for a 
 * "image/gif". This routine does not check parameter validity.
 **/
void 
gmime_content_field_set_parameter (GMimeContentField *content_field, const gchar *attribute, const gchar *value)
{
    gboolean attribute_exists;
    gchar *old_attribute;
    gchar *old_value;
    CAMEL_LOG_FULL_DEBUG ("GMimeContentField:: Entering set_parameter\n");
    CAMEL_LOG_FULL_DEBUG ("GMimeContentField:: set_parameter content_field=%p name=%s, value=%s\n", content_field, attribute, value);
    attribute_exists = g_hash_table_lookup_extended (content_field->parameters, 
                             attribute, 
                             (gpointer *) &old_attribute,
                             (gpointer *) &old_value);
    /** CHECK THAT : is normal to free pointers before insertion ? **/
    if (attribute_exists) {
        g_free (old_value);
        g_free (old_attribute);
    } 
        
    g_hash_table_insert (content_field->parameters, g_strdup (attribute), g_strdup (value));
    CAMEL_LOG_FULL_DEBUG ("GMimeContentField:: Leaving set_parameter\n");
}


/**
 * _print_parameter: print a parameter/value pair to a stream as described in RFC 2045
 * @name: name of the parameter
 * @value: value of the parameter
 * @user_data: CamelStream object to write the text to.
 * 
 * 
 **/
static void
_print_parameter (gpointer name, gpointer value, gpointer user_data)
{
    CamelStream *stream = (CamelStream *)user_data;
    
    camel_stream_write_strings (stream, 
                    "; \n    ", 
                    (gchar *)name, 
                    "=", 
                    (gchar *)value,
                    NULL);
    
}

/**
 * gmime_content_field_write_to_stream: write a mime content type to a stream
 * @content_field: content type object
 * @stream: the stream
 * 
 * 
 **/
void
gmime_content_field_write_to_stream (GMimeContentField *content_field, CamelStream *stream)
{
    if (!content_field) return;

    g_assert (stream);
    if (content_field->type) {
        camel_stream_write_strings (stream, "Content-Type: ", content_field->type, NULL);
        if (content_field->subtype) {
            camel_stream_write_strings (stream, "/", content_field->subtype, NULL);
        }
        /* print all parameters */
        g_hash_table_foreach (content_field->parameters, _print_parameter, stream);
        camel_stream_write_string (stream, "\n");
    } else CAMEL_LOG_FULL_DEBUG ("GMimeContentField::write_to_stream no mime type found\n");
}

/**
 * gmime_content_field_get_mime_type: return the mime type of the content field object
 * @content_field: content field object
 * 
 * A RFC 2045 content type field contains the mime type in the
 * form "type/subtype" (example : "application/postscript") and some
 * parameters (attribute/value pairs). This routine returns the mime type 
 * in a gchar object. 
 * 
 * Return value: the mime type in the form "type/subtype" or NULL if not defined.
 **/
gchar * 
gmime_content_field_get_mime_type (GMimeContentField *content_field)
{
    gchar *mime_type;

    if (!content_field->type) return NULL;

    if (content_field->subtype) 
        mime_type = g_strdup_printf ("%s/%s", content_field->type, content_field->subtype);
    else 
        mime_type = g_strdup (content_field->type);
    return mime_type;
}

static void
___debug_print_parameter (gpointer name, gpointer value, gpointer user_data)
{
    
    printf ("****** parameter \"%s\"=\"%s\"\n", (gchar *)name, (gchar *)value);
    
}

/**
 * gmime_content_field_get_parameter: return the value of a mime type parameter
 * @content_field: content field object
 * @name: name of the parameter
 * 
 * Returns the value of a parameter contained in the content field 
 * object. The content type is formed of a mime type, a mime subtype,
 * and a parameter list. Each parameter is a name/value pair. This 
 * routine returns the value assiciated to a given name. 
 * When the parameter does not exist, NULL is returned. 
 * 
 * Return value: parameter value, or NULL if not found.
 **/
const gchar *
gmime_content_field_get_parameter (GMimeContentField *content_field, const gchar *name)
{
    const gchar *parameter;
    const gchar *old_name;
    gboolean parameter_exists;

    CAMEL_LOG_FULL_DEBUG ("Entering GMimeContentField::get_parameter content_field =%p\n", content_field);
    g_assert (content_field->parameters);
    g_assert (name);
    CAMEL_LOG_FULL_DEBUG ("GMimeContentField::get_parameter looking for parameter \"%s\"\n", name);
    /* parameter = (const gchar *)g_hash_table_lookup (content_field->parameters, name); */
    parameter_exists = g_hash_table_lookup_extended (content_field->parameters, 
                             name, 
                             (gpointer *) &old_name,
                             (gpointer *) &parameter);
    if (!parameter_exists) {
        CAMEL_LOG_FULL_DEBUG ("GMimeContentField::get_parameter, parameter not found\n");
        g_hash_table_foreach (content_field->parameters, ___debug_print_parameter, NULL);
        return NULL;
    }
    CAMEL_LOG_FULL_DEBUG ("Leaving GMimeContentField::get_parameter\n");
    return parameter;
}




/**
 * gmime_content_field_construct_from_string: construct a ContentType object by parsing a string.
 *
 * @content_field: content type object to construct 
 * @string: string containing the content type field 
 * 
 * Parse a string containing a content type field as defined in
 * RFC 2045, and construct the corresponding ContentType object.
 * The string is not modified and not used in the ContentType 
 * object. It can and must be freed by the calling part.
 **/
void
gmime_content_field_construct_from_string (GMimeContentField *content_field, const gchar *string)
{
    gint first, len;
    gchar *str;
    gint i=0;
    gchar *type, *subtype;
    gchar *param_name, *param_value;
    gboolean param_end;
    
    CAMEL_LOG_TRACE ( "GMimeContentField::construct_from_string, entering\n");
    g_assert (string);
    g_assert (content_field);
 
    g_free (content_field->type);   
    g_free (content_field->subtype);
    
    
    first = 0;
    len = strlen (string);
    if (!len) { 
        CAMEL_LOG_FULL_DEBUG ( "GMimeContentField::construct_from_string, leaving\n");
        return;
    }
    CAMEL_LOG_FULL_DEBUG ("GMimeContentField::construct_from_string, All checks done\n");
    CAMEL_LOG_FULL_DEBUG ("GMimeContentField::construct_from_string the complete header is\n"
                  "-------------------\n%s\n-------------------\n", string);
    /* find the type */
    while ( (i<len) && (!strchr ("/;", string[i])) ) i++;
    
    if (i == 0) { 
        CAMEL_LOG_FULL_DEBUG ( "GMimeContentField::construct_from_string, leaving\n");
        return;
    }
    
    type = g_strndup (string, i);
    string_trim (type, " \t\"", STRING_TRIM_STRIP_TRAILING | STRING_TRIM_STRIP_LEADING);
    content_field->type = type;
    CAMEL_LOG_TRACE ( "GMimeContentField::construct_from_string, Found mime type : \"%s\"\n", type); 
    if (i >= len-1) {
        content_field->subtype = NULL;
        
        CAMEL_LOG_FULL_DEBUG ( "GMimeContentField::construct_from_string  only found the type leaving\n");
        return;         
    }
    
    first = i+1;
    /* find the subtype, if any */
    if (string[i++] == '/') {
        while ( (i<len) && (string[i] != ';') ) i++;
        if (i != first) {
            subtype = g_strndup (string+first, i-first);
            string_trim (subtype, " \t\"", STRING_TRIM_STRIP_TRAILING | STRING_TRIM_STRIP_LEADING);
            content_field->subtype = subtype;
            CAMEL_LOG_TRACE ( "GMimeContentField::construct_from_string, Found mime subtype: \"%s\"\n", subtype);
            if (i >= len-1) { 
                CAMEL_LOG_FULL_DEBUG ( "GMimeContentField::construct_from_string found the subtype but no parameter, leaving\n");
                return;
            }
        }
    }
    first = i+1;

    /* parse parameters list */
    param_end = FALSE;
    do {
        while ( (i<len) && (string[i] != '=') ) i++;
        if ((i == len) || (i==first)) param_end = TRUE;
        else {
            /* we have found parameter name */
            param_name = g_strndup (string+first, i-first);
            string_trim (param_name, " \"", STRING_TRIM_STRIP_TRAILING | STRING_TRIM_STRIP_LEADING);
            i++;
            first = i;
            /* Let's find parameter value */
            while ( (i<len) && (string[i] != ';') ) i++;
            if (i != first) param_value = g_strndup (string+first, i-first);
            else param_value = g_strdup ("");
            CAMEL_LOG_TRACE ( "GMimeContentField::construct_from_string, Found mime parameter \"%s\"=\"%s\"\n", param_name, param_value);
            string_trim (param_value, " \t\"", STRING_TRIM_STRIP_TRAILING | STRING_TRIM_STRIP_LEADING);
            gmime_content_field_set_parameter (content_field, param_name, param_value);
            g_free (param_name);
            g_free (param_value);
            i++;
            first = i;
        }
    } while ((!param_end) && (first < len));


}