1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
5 * Copyright 2002 Ximian, Inc. (www.ximian.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 #include "camel-charset-map.h"
33 #include "camel-mime-filter-windows.h"
38 static void camel_mime_filter_windows_class_init (CamelMimeFilterWindowsClass *klass);
39 static void camel_mime_filter_windows_init (CamelObject *o);
40 static void camel_mime_filter_windows_finalize (CamelObject *o);
42 static CamelMimeFilterClass *parent_class = NULL;
45 camel_mime_filter_windows_get_type (void)
47 static CamelType type = CAMEL_INVALID_TYPE;
49 if (type == CAMEL_INVALID_TYPE) {
50 type = camel_type_register (camel_mime_filter_get_type (),
51 "CamelMimeFilterWindows",
52 sizeof (CamelMimeFilterWindows),
53 sizeof (CamelMimeFilterWindowsClass),
54 (CamelObjectClassInitFunc) camel_mime_filter_windows_class_init,
56 (CamelObjectInitFunc) camel_mime_filter_windows_init,
57 (CamelObjectFinalizeFunc) camel_mime_filter_windows_finalize);
64 camel_mime_filter_windows_finalize (CamelObject *o)
66 CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) o;
68 g_free (windows->claimed_charset);
72 camel_mime_filter_windows_init (CamelObject *o)
74 CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) o;
76 windows->is_windows = FALSE;
77 windows->claimed_charset = NULL;
81 filter_filter (CamelMimeFilter *filter, char *in, size_t len, size_t prespace,
82 char **out, size_t *outlen, size_t *outprespace)
84 CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) filter;
85 register unsigned char *inptr;
88 if (!windows->is_windows) {
89 inptr = (unsigned char *) in;
92 while (inptr < inend) {
93 register unsigned char c = *inptr++;
95 if (c >= 128 && c <= 159) {
96 w(g_warning ("Encountered Windows charset masquerading as %s",
97 windows->claimed_charset));
98 windows->is_windows = TRUE;
106 *outprespace = prespace;
110 filter_complete (CamelMimeFilter *filter, char *in, size_t len, size_t prespace,
111 char **out, size_t *outlen, size_t *outprespace)
113 filter_filter (filter, in, len, prespace, out, outlen, outprespace);
117 filter_reset (CamelMimeFilter *filter)
119 CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) filter;
121 windows->is_windows = FALSE;
125 camel_mime_filter_windows_class_init (CamelMimeFilterWindowsClass *klass)
127 CamelMimeFilterClass *filter_class = (CamelMimeFilterClass *) klass;
129 parent_class = CAMEL_MIME_FILTER_CLASS (camel_type_get_global_classfuncs (camel_mime_filter_get_type ()));
131 filter_class->reset = filter_reset;
132 filter_class->filter = filter_filter;
133 filter_class->complete = filter_complete;
138 * camel_mime_filter_windows_new:
139 * @claimed_charset: ISO charset name
141 * Create a new #CamelMimeFilterWindows object that will analyse
142 * whether or not the text is really encoded in @claimed_charset.
144 * Returns a new #CamelMimeFilter object
147 camel_mime_filter_windows_new (const char *claimed_charset)
149 CamelMimeFilterWindows *new;
151 g_return_val_if_fail (claimed_charset != NULL, NULL);
153 new = CAMEL_MIME_FILTER_WINDOWS (camel_object_new (camel_mime_filter_windows_get_type ()));
155 new->claimed_charset = g_strdup (claimed_charset);
157 return CAMEL_MIME_FILTER (new);
162 * camel_mime_filter_windows_is_windows_charset:
163 * @filter: a #CamelMimeFilterWindows object
165 * Get whether or not the textual content filtered by @filetr is
166 * really in a Microsoft Windows charset rather than the claimed ISO
169 * Returns %TRUE if the text was found to be in a Microsoft Windows
170 * CP125x charset or %FALSE otherwise.
173 camel_mime_filter_windows_is_windows_charset (CamelMimeFilterWindows *filter)
175 g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), FALSE);
177 return filter->is_windows;
182 * camel_mime_filter_windows_real_charset:
183 * @filter: a #CamelMimeFilterWindows object
185 * Get the name of the actual charset used to encode the textual
186 * content filtered by @filter (it will either be the original
187 * claimed_charset passed in at creation time or the Windows-CP125x
190 * Returns the name of the actual charset
193 camel_mime_filter_windows_real_charset (CamelMimeFilterWindows *filter)
195 g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), NULL);
197 if (filter->is_windows)
198 return camel_charset_iso_to_windows (filter->claimed_charset);
200 return filter->claimed_charset;