File: gmime-disposition.c

package info (click to toggle)
gmime2 2.0.14-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,092 kB
  • ctags: 3,177
  • sloc: ansic: 29,973; sh: 8,234; makefile: 413; perl: 6
file content (260 lines) | stat: -rw-r--r-- 6,437 bytes parent folder | download
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2001-2004 Ximian, Inc. (www.ximian.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 Street #330, Boston, MA 02111-1307, USA.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <ctype.h>
#include <string.h>

#include "gmime-disposition.h"


static int
param_equal (gconstpointer v, gconstpointer v2)
{
	return g_strcasecmp ((const char *) v, (const char *) v2) == 0;
}

static guint
param_hash (gconstpointer key)
{
	const char *p = key;
	guint h = tolower (*p);
	
	if (h)
		for (p += 1; *p != '\0'; p++)
			h = (h << 5) - h + tolower (*p);
	
	return h;
}


/**
 * g_mime_disposition_new:
 * @disposition: disposition header (and params)
 *
 * Creates a new GMimeDisposition object.
 *
 * Returns a new disposition object
 **/
GMimeDisposition *
g_mime_disposition_new (const char *disposition)
{
	GMimeDisposition *new;
	GMimeParam *param;
	const char *inptr;
	char *value;
	
	new = g_new (GMimeDisposition, 1);
	if (!disposition) {
		new->disposition = g_strdup (GMIME_DISPOSITION_ATTACHMENT);
		new->params = NULL;
		new->param_hash = NULL;
		
		return new;
	}
	
	/* get content disposition part */
	for (inptr = disposition; *inptr && *inptr != ';'; inptr++); /* find ; or \0 */
	value = g_strndup (disposition, (unsigned) (inptr - disposition));
	g_strstrip (value);
	
	new->disposition = value;
	
	/* parse the parameters, if any */
	if (*inptr++ == ';' && *inptr) {
		param = new->params = g_mime_param_new_from_string (inptr);
		new->param_hash = g_hash_table_new (param_hash, param_equal);
		while (param) {
			g_hash_table_insert (new->param_hash, param->name, param);
			param = param->next;
		}
	} else {
		new->params = NULL;
		new->param_hash = NULL;
	}
	
	return new;
}


/**
 * g_mime_disposition_destroy:
 * @disposition: disposition object
 *
 * Destroy the disposition object.
 **/
void
g_mime_disposition_destroy (GMimeDisposition *disposition)
{
	if (disposition) {
		g_free (disposition->disposition);
		if (disposition->params)
			g_mime_param_destroy (disposition->params);
		if (disposition->param_hash)
			g_hash_table_destroy (disposition->param_hash);
		g_free (disposition);
	}
}


/**
 * g_mime_disposition_set:
 * @disposition: disposition object
 * @value: disposition value
 *
 * Sets the disposition to @value which may be one of
 * GMIME_DISPOSITION_ATTACHMENT or GMIME_DISPOSITION_INLINE or, by your
 * choice, any other string which would indicate how the MIME part
 * should be displayed by the MUA.
 **/
void
g_mime_disposition_set (GMimeDisposition *disposition, const char *value)
{
	g_return_if_fail (disposition != NULL);
	g_return_if_fail (value != NULL);
	
	g_free (disposition->disposition);
	disposition->disposition = g_strdup (value);
}


/**
 * g_mime_disposition_get:
 * @disposition: disposition object
 *
 * Gets the disposition or %NULL on fail.
 *
 * Returns the disposition string which is probably one of
 * GMIME_DISPOSITION_ATTACHMENT or GMIME_DISPOSITION_INLINE.
 **/
const char *
g_mime_disposition_get (GMimeDisposition *disposition)
{
	g_return_val_if_fail (disposition != NULL, NULL);
	
	return disposition->disposition;
}


/**
 * g_mime_disposition_add_parameter:
 * @disposition: disposition object
 * @attribute: parameter name
 * @value: parameter value
 *
 * Adds a new parameter of name @name and value @value to the
 * disposition.
 **/
void
g_mime_disposition_add_parameter (GMimeDisposition *disposition, const char *attribute, const char *value)
{
	GMimeParam *param = NULL;
	
	g_return_if_fail (disposition != NULL);
	g_return_if_fail (attribute != NULL);
	g_return_if_fail (value != NULL);
	
	if (disposition->params) {
		param = g_hash_table_lookup (disposition->param_hash, attribute);
		if (param) {
			g_free (param->value);
			param->value = g_strdup (value);
		}
	} else {
		/* hash table may not be initialized */
		if (!disposition->param_hash)
			disposition->param_hash = g_hash_table_new (param_hash, param_equal);
	}
	
	if (param == NULL) {
		param = g_mime_param_new (attribute, value);
		disposition->params = g_mime_param_append_param (disposition->params, param);
		g_hash_table_insert (disposition->param_hash, param->name, param);
	}
}


/**
 * g_mime_disposition_get_parameter:
 * @disposition: disposition object
 * @attribute: parameter name
 *
 * Gets the value of the parameter @attribute, or %NULL on fail.
 *
 * Returns the value of the parameter of name @attribute.
 **/
const char *
g_mime_disposition_get_parameter (GMimeDisposition *disposition, const char *attribute)
{
	GMimeParam *param;
	
	g_return_val_if_fail (disposition != NULL, NULL);
	g_return_val_if_fail (attribute != NULL, NULL);
	
	if (!disposition->param_hash)
		return NULL;
	
	param = g_hash_table_lookup (disposition->param_hash, attribute);
	
	if (param)
		return param->value;
	else
		return NULL;
}


/**
 * g_mime_disposition_header:
 * @disposition: disposition object
 * @fold: fold header if needed
 *
 * Allocates a string buffer containing the Content-Disposition header
 * represented by the disposition object @disposition.
 *
 * Returns a string containing the disposition header
 **/
char *
g_mime_disposition_header (GMimeDisposition *disposition, gboolean fold)
{
	GString *string;
	char *header, *buf;
	
	g_return_val_if_fail (disposition != NULL, NULL);
	
	/* we need to have this so wrapping is correct */
	string = g_string_new ("Content-Disposition: ");
	
	g_string_append (string, disposition->disposition);
	g_mime_param_write_to_string (disposition->params, fold, string);
	
	header = string->str;
	g_string_free (string, FALSE);
	
	buf = header + strlen ("Content-Disposition: ");
	memmove (header, buf, strlen (buf) + 1);
	
	return header;
}