1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
5 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.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.
31 #include "camel-charset-map.h"
32 #include "camel-mime-filter-windows.h"
34 #define CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE(obj) \
35 (G_TYPE_INSTANCE_GET_PRIVATE \
36 ((obj), CAMEL_TYPE_MIME_FILTER_WINDOWS, CamelMimeFilterWindowsPrivate))
41 struct _CamelMimeFilterWindowsPrivate {
43 gchar *claimed_charset;
46 G_DEFINE_TYPE (CamelMimeFilterWindows, camel_mime_filter_windows, CAMEL_TYPE_MIME_FILTER)
49 mime_filter_windows_finalize (GObject *object)
51 CamelMimeFilterWindowsPrivate *priv;
53 priv = CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE (object);
55 g_free (priv->claimed_charset);
57 /* Chain up to parent's finalize() method. */
58 G_OBJECT_CLASS (camel_mime_filter_windows_parent_class)->finalize (object);
62 mime_filter_windows_filter (CamelMimeFilter *mime_filter,
70 CamelMimeFilterWindowsPrivate *priv;
71 register guchar *inptr;
74 priv = CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE (mime_filter);
76 if (!priv->is_windows) {
77 inptr = (guchar *) in;
80 while (inptr < inend) {
81 register guchar c = *inptr++;
83 if (c >= 128 && c <= 159) {
85 "Encountered Windows "
86 "charset masquerading as %s",
87 priv->claimed_charset));
88 priv->is_windows = TRUE;
96 *outprespace = prespace;
100 mime_filter_windows_complete (CamelMimeFilter *mime_filter,
108 mime_filter_windows_filter (
109 mime_filter, in, len, prespace,
110 out, outlen, outprespace);
114 mime_filter_windows_reset (CamelMimeFilter *mime_filter)
116 CamelMimeFilterWindowsPrivate *priv;
118 priv = CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE (mime_filter);
120 priv->is_windows = FALSE;
124 camel_mime_filter_windows_class_init (CamelMimeFilterWindowsClass *class)
126 GObjectClass *object_class;
127 CamelMimeFilterClass *mime_filter_class;
129 g_type_class_add_private (class, sizeof (CamelMimeFilterWindowsPrivate));
131 object_class = G_OBJECT_CLASS (class);
132 object_class->finalize = mime_filter_windows_finalize;
134 mime_filter_class = CAMEL_MIME_FILTER_CLASS (class);
135 mime_filter_class->filter = mime_filter_windows_filter;
136 mime_filter_class->complete = mime_filter_windows_complete;
137 mime_filter_class->reset = mime_filter_windows_reset;
141 camel_mime_filter_windows_init (CamelMimeFilterWindows *filter)
143 filter->priv = CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE (filter);
147 * camel_mime_filter_windows_new:
148 * @claimed_charset: ISO charset name
150 * Create a new #CamelMimeFilterWindows object that will analyse
151 * whether or not the text is really encoded in @claimed_charset.
153 * Returns: a new #CamelMimeFilter object
156 camel_mime_filter_windows_new (const gchar *claimed_charset)
158 CamelMimeFilter *filter;
159 CamelMimeFilterWindowsPrivate *priv;
161 g_return_val_if_fail (claimed_charset != NULL, NULL);
163 filter = g_object_new (CAMEL_TYPE_MIME_FILTER_WINDOWS, NULL);
164 priv = CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE (filter);
166 priv->claimed_charset = g_strdup (claimed_charset);
172 * camel_mime_filter_windows_is_windows_charset:
173 * @filter: a #CamelMimeFilterWindows object
175 * Get whether or not the textual content filtered by @filter is
176 * really in a Microsoft Windows charset rather than the claimed ISO
179 * Returns: %TRUE if the text was found to be in a Microsoft Windows
180 * CP125x charset or %FALSE otherwise.
183 camel_mime_filter_windows_is_windows_charset (CamelMimeFilterWindows *filter)
185 g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), FALSE);
187 return filter->priv->is_windows;
191 * camel_mime_filter_windows_real_charset:
192 * @filter: a #CamelMimeFilterWindows object
194 * Get the name of the actual charset used to encode the textual
195 * content filtered by @filter (it will either be the original
196 * claimed_charset passed in at creation time or the Windows-CP125x
199 * Returns: the name of the actual charset
202 camel_mime_filter_windows_real_charset (CamelMimeFilterWindows *filter)
204 const gchar *charset;
206 g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), NULL);
208 charset = filter->priv->claimed_charset;
210 if (filter->priv->is_windows)
211 charset = camel_charset_iso_to_windows (charset);