Extending test-client-custom-summary to try e_book_client_get_contacts_uids()
[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 (C) 1999-2008 Novell, Inc. (www.novell.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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "camel-charset-map.h"
32 #include "camel-mime-filter-windows.h"
33
34 #define CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE(obj) \
35         (G_TYPE_INSTANCE_GET_PRIVATE \
36         ((obj), CAMEL_TYPE_MIME_FILTER_WINDOWS, CamelMimeFilterWindowsPrivate))
37
38 #define d(x)
39 #define w(x)
40
41 struct _CamelMimeFilterWindowsPrivate {
42         gboolean is_windows;
43         gchar *claimed_charset;
44 };
45
46 G_DEFINE_TYPE (CamelMimeFilterWindows, camel_mime_filter_windows, CAMEL_TYPE_MIME_FILTER)
47
48 static void
49 mime_filter_windows_finalize (GObject *object)
50 {
51         CamelMimeFilterWindowsPrivate *priv;
52
53         priv = CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE (object);
54
55         g_free (priv->claimed_charset);
56
57         /* Chain up to parent's finalize() method. */
58         G_OBJECT_CLASS (camel_mime_filter_windows_parent_class)->finalize (object);
59 }
60
61 static void
62 mime_filter_windows_filter (CamelMimeFilter *mime_filter,
63                             const gchar *in,
64                             gsize len,
65                             gsize prespace,
66                             gchar **out,
67                             gsize *outlen,
68                             gsize *outprespace)
69 {
70         CamelMimeFilterWindowsPrivate *priv;
71         register guchar *inptr;
72         guchar *inend;
73
74         priv = CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE (mime_filter);
75
76         if (!priv->is_windows) {
77                 inptr = (guchar *) in;
78                 inend = inptr + len;
79
80                 while (inptr < inend) {
81                         register guchar c = *inptr++;
82
83                         if (c >= 128 && c <= 159) {
84                                 w (g_warning (
85                                         "Encountered Windows "
86                                         "charset masquerading as %s",
87                                         priv->claimed_charset));
88                                 priv->is_windows = TRUE;
89                                 break;
90                         }
91                 }
92         }
93
94         *out = (gchar *) in;
95         *outlen = len;
96         *outprespace = prespace;
97 }
98
99 static void
100 mime_filter_windows_complete (CamelMimeFilter *mime_filter,
101                               const gchar *in,
102                               gsize len,
103                               gsize prespace,
104                               gchar **out,
105                               gsize *outlen,
106                               gsize *outprespace)
107 {
108         mime_filter_windows_filter (
109                 mime_filter, in, len, prespace,
110                 out, outlen, outprespace);
111 }
112
113 static void
114 mime_filter_windows_reset (CamelMimeFilter *mime_filter)
115 {
116         CamelMimeFilterWindowsPrivate *priv;
117
118         priv = CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE (mime_filter);
119
120         priv->is_windows = FALSE;
121 }
122
123 static void
124 camel_mime_filter_windows_class_init (CamelMimeFilterWindowsClass *class)
125 {
126         GObjectClass *object_class;
127         CamelMimeFilterClass *mime_filter_class;
128
129         g_type_class_add_private (class, sizeof (CamelMimeFilterWindowsPrivate));
130
131         object_class = G_OBJECT_CLASS (class);
132         object_class->finalize = mime_filter_windows_finalize;
133
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;
138 }
139
140 static void
141 camel_mime_filter_windows_init (CamelMimeFilterWindows *filter)
142 {
143         filter->priv = CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE (filter);
144 }
145
146 /**
147  * camel_mime_filter_windows_new:
148  * @claimed_charset: ISO charset name
149  *
150  * Create a new #CamelMimeFilterWindows object that will analyse
151  * whether or not the text is really encoded in @claimed_charset.
152  *
153  * Returns: a new #CamelMimeFilter object
154  **/
155 CamelMimeFilter *
156 camel_mime_filter_windows_new (const gchar *claimed_charset)
157 {
158         CamelMimeFilter *filter;
159         CamelMimeFilterWindowsPrivate *priv;
160
161         g_return_val_if_fail (claimed_charset != NULL, NULL);
162
163         filter = g_object_new (CAMEL_TYPE_MIME_FILTER_WINDOWS, NULL);
164         priv = CAMEL_MIME_FILTER_WINDOWS_GET_PRIVATE (filter);
165
166         priv->claimed_charset = g_strdup (claimed_charset);
167
168         return filter;
169 }
170
171 /**
172  * camel_mime_filter_windows_is_windows_charset:
173  * @filter: a #CamelMimeFilterWindows object
174  *
175  * Get whether or not the textual content filtered by @filter is
176  * really in a Microsoft Windows charset rather than the claimed ISO
177  * charset.
178  *
179  * Returns: %TRUE if the text was found to be in a Microsoft Windows
180  * CP125x charset or %FALSE otherwise.
181  **/
182 gboolean
183 camel_mime_filter_windows_is_windows_charset (CamelMimeFilterWindows *filter)
184 {
185         g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), FALSE);
186
187         return filter->priv->is_windows;
188 }
189
190 /**
191  * camel_mime_filter_windows_real_charset:
192  * @filter: a #CamelMimeFilterWindows object
193  *
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
197  * equivalent).
198  *
199  * Returns: the name of the actual charset
200  **/
201 const gchar *
202 camel_mime_filter_windows_real_charset (CamelMimeFilterWindows *filter)
203 {
204         const gchar *charset;
205
206         g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), NULL);
207
208         charset = filter->priv->claimed_charset;
209
210         if (filter->priv->is_windows)
211                 charset = camel_charset_iso_to_windows (charset);
212
213         return charset;
214 }