Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-mime-filter-windows.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Authors: Jeffrey Stedfast <fejj@ximian.com>
4  *
5  *  Copyright 2002 Ximian, Inc. (www.ximian.com)
6  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "camel-charset-map.h"
33 #include "camel-mime-filter-windows.h"
34
35 #define d(x)
36 #define w(x)
37
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);
41
42 static CamelMimeFilterClass *parent_class = NULL;
43
44 CamelType
45 camel_mime_filter_windows_get_type (void)
46 {
47         static CamelType type = CAMEL_INVALID_TYPE;
48         
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,
55                                             NULL,
56                                             (CamelObjectInitFunc) camel_mime_filter_windows_init,
57                                             (CamelObjectFinalizeFunc) camel_mime_filter_windows_finalize);
58         }
59         
60         return type;
61 }
62
63 static void
64 camel_mime_filter_windows_finalize (CamelObject *o)
65 {
66         CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) o;
67         
68         g_free (windows->claimed_charset);
69 }
70
71 static void
72 camel_mime_filter_windows_init (CamelObject *o)
73 {
74         CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) o;
75         
76         windows->is_windows = FALSE;
77         windows->claimed_charset = NULL;
78 }
79
80 static void
81 filter_filter (CamelMimeFilter *filter, char *in, size_t len, size_t prespace,
82                char **out, size_t *outlen, size_t *outprespace)
83 {
84         CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) filter;
85         register unsigned char *inptr;
86         unsigned char *inend;
87         
88         if (!windows->is_windows) {
89                 inptr = (unsigned char *) in;
90                 inend = inptr + len;
91                 
92                 while (inptr < inend) {
93                         register unsigned char c = *inptr++;
94                         
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;
99                                 break;
100                         }
101                 }
102         }
103         
104         *out = in;
105         *outlen = len;
106         *outprespace = prespace;
107 }
108
109 static void 
110 filter_complete (CamelMimeFilter *filter, char *in, size_t len, size_t prespace,
111                  char **out, size_t *outlen, size_t *outprespace)
112 {
113         filter_filter (filter, in, len, prespace, out, outlen, outprespace);
114 }
115
116 static void
117 filter_reset (CamelMimeFilter *filter)
118 {
119         CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) filter;
120         
121         windows->is_windows = FALSE;
122 }
123
124 static void
125 camel_mime_filter_windows_class_init (CamelMimeFilterWindowsClass *klass)
126 {
127         CamelMimeFilterClass *filter_class = (CamelMimeFilterClass *) klass;
128         
129         parent_class = CAMEL_MIME_FILTER_CLASS (camel_type_get_global_classfuncs (camel_mime_filter_get_type ()));
130         
131         filter_class->reset = filter_reset;
132         filter_class->filter = filter_filter;
133         filter_class->complete = filter_complete;
134 }
135
136
137 /**
138  * camel_mime_filter_windows_new:
139  * @claimed_charset: ISO charset name
140  *
141  * Create a new #CamelMimeFilterWindows object that will analyse
142  * whether or not the text is really encoded in @claimed_charset.
143  *
144  * Returns a new #CamelMimeFilter object
145  **/
146 CamelMimeFilter *
147 camel_mime_filter_windows_new (const char *claimed_charset)
148 {
149         CamelMimeFilterWindows *new;
150         
151         g_return_val_if_fail (claimed_charset != NULL, NULL);
152         
153         new = CAMEL_MIME_FILTER_WINDOWS (camel_object_new (camel_mime_filter_windows_get_type ()));
154         
155         new->claimed_charset = g_strdup (claimed_charset);
156         
157         return CAMEL_MIME_FILTER (new);
158 }
159
160
161 /**
162  * camel_mime_filter_windows_is_windows_charset:
163  * @filter: a #CamelMimeFilterWindows object
164  *
165  * Get whether or not the textual content filtered by @filetr is
166  * really in a Microsoft Windows charset rather than the claimed ISO
167  * charset.
168  *
169  * Returns %TRUE if the text was found to be in a Microsoft Windows
170  * CP125x charset or %FALSE otherwise.
171  **/
172 gboolean
173 camel_mime_filter_windows_is_windows_charset (CamelMimeFilterWindows *filter)
174 {
175         g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), FALSE);
176         
177         return filter->is_windows;
178 }
179
180
181 /**
182  * camel_mime_filter_windows_real_charset:
183  * @filter: a #CamelMimeFilterWindows object
184  *
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
188  * equivalent).
189  *
190  * Returns the name of the actual charset
191  **/
192 const char *
193 camel_mime_filter_windows_real_charset (CamelMimeFilterWindows *filter)
194 {
195         g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), NULL);
196         
197         if (filter->is_windows)
198                 return camel_charset_iso_to_windows (filter->claimed_charset);
199         else
200                 return filter->claimed_charset;
201 }