* fix for NB#67837, NB#63633
[modest] / src / modest-text-utils.h
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30
31 /* modest-text-utils.h */
32
33 #ifndef __MODEST_TEXT_UTILS_H__
34 #define __MODEST_TEXT_UTILS_H__
35
36 #include <time.h>
37 #include <glib.h>
38 #include <gdk/gdkcolor.h>
39 #include <gtk/gtktextbuffer.h>
40
41 #define _FM(str) dgettext("hildon-fm",str)
42 #define _CS(str) dgettext("hildon-common-strings",str)
43 #define _HL(str) dgettext("hildon-libs",str)
44
45 /* Forbidden char arrays */
46 extern const gchar account_title_forbidden_chars[];
47 extern const gchar folder_name_forbidden_chars[];
48 extern const gchar user_name_forbidden_chars[];
49 extern const guint ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH;
50 extern const guint FOLDER_NAME_FORBIDDEN_CHARS_LENGTH;
51 extern const guint USER_NAME_FORBIDDEN_CHARS_LENGTH;
52
53 /**
54  * modest_text_utils_derived_subject:
55  * @subject: a string which contains the original subject
56  * @prefix: the prefix for the new subject (such as 'Re:' or 'Fwd:'),
57  *           must not be NULL
58  *
59  * create a 'derived' subject line for eg. replies and forwards 
60  * 
61  * Returns: a newly allocated string containing the resulting subject
62  * subject == NULL, then @prefix " " will be returned
63  */
64 gchar* modest_text_utils_derived_subject (const gchar *subject, 
65                                           const gchar* prefix);
66
67
68 /**
69  * modest_text_utils_quote:
70  * @text: a non-NULL string which contains the message to quote
71  * @from: a non-NULL  sender of the original message
72  * @content_type: the non-NULL content type for the quoting, e.g. "text/html"
73  * @signature: NULL or the signature to add
74  * @sent_date: sent date/time of the original message
75  * @attachments: a #GList of the attachments
76  * @limit: specifies the maximum characters per line in the quoted text
77  * 
78  * quote an existing message
79  * 
80  * Returns: a newly allocated string containing the quoted message
81  */
82 gchar* modest_text_utils_quote (const gchar *text, 
83                                 const gchar *content_type,
84                                 const gchar *signature,
85                                 const gchar *from,
86                                 const time_t sent_date, 
87                                 GList *attachments,
88                                 int limit);
89
90
91 /**
92  * modest_text_utils_cited_text:
93  * @from: sender of the message
94  * @sent_date: the sent date of the original message
95  * @text: the text of the original message
96  *
97  * cite the text in a message
98  * 
99  * Returns: a newly allocated string containing the cited text
100  */
101 gchar* modest_text_utils_cite (const gchar *text,
102                                const gchar *content_type,
103                                const gchar *signature,
104                                const gchar *from,
105                                time_t sent_date);
106
107 /**
108  * modest_text_utils_inlined_text
109  * @from: the non-NULL sender of the original message
110  * @sent_date: sent date/time of the original message
111  * @to: 
112  * @subject: 
113  * @text: 
114  *
115  * creates a new string with the "Original message" text prepended to
116  * the text passed as argument and some data of the header
117  * 
118  * Returns: a newly allocated string containing the quoted message
119  */
120 gchar*   modest_text_utils_inline (const gchar *text,
121                                    const gchar *content_type,
122                                    const gchar *signature,
123                                    const gchar *from,
124                                    time_t sent_date,
125                                    const gchar *to,
126                                    const gchar *subject);
127
128 /**
129  * modest_text_utils_remove_address
130  * @address_list: none-NULL string with a comma-separated list of email addresses
131  * @address: an specific e-mail address 
132  *
133  * remove a specific address from a list of email addresses; if @address
134  * is NULL, returns an unchanged @address_list
135  * 
136  * Returns: a newly allocated string containing the new list, or NULL
137  * in case of error or the original @address_list was NULL
138  */
139 gchar*   modest_text_utils_remove_address (const gchar *address_list, 
140                                            const gchar *address);
141
142 /**
143  * modest_text_utils_address_range_at_position:
144  * @address_list: utf8 string containing a list of addresses
145  * @position: a gint
146  * @start: a gint pointer
147  * @end: a gint pointer
148  *
149  * Finds the start and end positions of the address at @position,
150  * in @recipients_list, a list of addresses in the format of a 
151  * recipient list in email. It stores the results in @start and
152  * @end
153  */
154 void     modest_text_utils_address_range_at_position (const gchar *recipients_list,
155                                                       gint position,
156                                                       gint *start,
157                                                       gint *end);
158                                                       
159
160 /**
161  * modest_text_utils_convert_to_html:
162  * @txt: a string
163  *
164  * convert plain text (utf8) into html
165  * 
166  * Returns: a newly allocated string containing the html
167  */
168 gchar*  modest_text_utils_convert_to_html (const gchar *txt);
169
170 /**
171  * modest_text_utils_convert_to_html_body:
172  * @txt: a string
173  *
174  * convert plain text (utf8) into html without adding html headers.
175  * 
176  * Returns: a newly allocated string containing the html
177  */
178 gchar*  modest_text_utils_convert_to_html_body (const gchar *data);
179
180
181 /**
182  * modest_text_utils_strftime:
183  * @s:
184  * @max:
185  * @fmt:
186  * @timet:
187  *
188  * this is just an alias for strftime(3), so we can use that without
189  * getting warning from gcc
190  * 
191  * Returns: a formatted string of max length @max in @s
192  */
193 size_t modest_text_utils_strftime(char *s, size_t max, const char  *fmt, time_t timet);
194
195
196
197 /**
198  * modest_text_utils_get_display_address:
199  * @address: original address (UTF8 string)
200  *
201  * make a 'display address' from an address:
202  * "Foo Bar <foo@bar.cx> (Bla)" --> "Foo Bar"
203  * ie. removes "<...>" and "(...)" parts
204  * the change is in-place; removes leading/trailing whitespace
205  * 
206  * Returns: the new address of the string; this new string
207  * is _NOT_ newly allocated, so should not be freed. (remember
208  * the old address of the parameter if that one needs to be freed)
209  * 
210  * NULL in case of error or if address == NULL
211  */
212 gchar* modest_text_utils_get_display_address (gchar *address);
213
214 /**
215  * modest_text_utils_get_email_address:
216  * @full_address: original address (UTF8 string)
217  *
218  * make a 'foo@bar.cx' from an address:
219  * "Foo Bar <foo@bar.cx> (Bla)" --> "foo@bar.cx"
220  * If no "<...>" is found, then it returns the full
221  * strings.
222  * 
223  * Returns: a newly allocated string with the copy.
224  * 
225  * NULL in case of error or if address == NULL
226  */
227 gchar* modest_text_utils_get_email_address (const gchar *email_address);
228
229
230 /**
231  * modest_text_utils_get_subject_prefix_len:
232  * @subject: original subject (UTF8 string)
233  *
234  * determine the length of the "Re:/RE:/Fwd:" prefix in an e-mail address
235  * 
236  * Returns: the length of the  prefix, or 0 if there is none
237  */
238 gint modest_text_utils_get_subject_prefix_len (const gchar *subject);
239
240
241 /**
242  * modest_text_utils_utf8_strcmp:
243  * @s1: the first string
244  * @s2: the second string
245  * @insensitive: should the comparison be case-insensitive?
246  *
247  * a strcmp that is NULL-safe, can deal with UTF8 and case-insensitive comparison 
248  *
249  * Returns: an integer less than, equal to, or greater than zero if s1 is found,
250  * respectively, to be less than, to match, or be greater than s2.
251  */
252 gint modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive);
253
254
255
256 /**
257  * modest_text_utils_get_display_date:
258  * @date: the date to display
259  *
260  * get a string representation for a date.
261  * 
262  * Returns: the new display date, as a newly allocated string;
263  * free with g_free
264  */
265 gchar* modest_text_utils_get_display_date (time_t date);
266
267
268 /**
269  * modest_text_utils_get_display_size:
270  * @size: size in bytes
271  *
272  * get a string representation for a size in bytes.
273  * 
274  * Returns: the newly allocated display string for the
275  * size in bytes. must be freed.
276  */
277 gchar * modest_text_utils_get_display_size (guint64 size);
278
279
280
281 /**
282  * modest_text_utils_validate_domain_name:
283  * @email_address: a NULL-terminated string
284  * 
285  * validates the domain name passed as argument
286  * 
287  * Returns: TRUE if the domain name is valid, FALSE otherwise
288  **/
289 gboolean modest_text_utils_validate_domain_name (const gchar *domain);
290
291 /**
292  * modest_text_utils_validate_email_address:
293  * @email_address: a string
294  * @invalid_char_position: pointer to the position of the invalid
295  * character in case validation failed because of this, or %NULL.
296  * 
297  * validates the email address passed as argument
298  * 
299  * Returns: TRUE if the address is valid, FALSE otherwise
300  **/
301 gboolean     modest_text_utils_validate_email_address (const gchar *email_address, 
302                                                        const gchar **invalid_char_position);
303
304
305 /**
306  * modest_text_utils_validate_folder_name:
307  * @folder_name: a string
308  * 
309  * validates the folder name passed as argument. a 'valid folder name'
310  * is a name which should be valid on both Unix and Windows file systems.
311  * of course, this might be stricter than strictly needed in some cases,
312  * but it's better to err on the safe side.
313  * 
314  * Returns: TRUE if the folder name is valid, FALSE otherwise
315  **/
316 gboolean modest_text_utils_validate_folder_name (const gchar *folder_name);
317
318 /**
319  * modest_text_utils_validate_recipient:
320  * @recipient: a string
321  * @invalid_char_position: pointer to the position of the invalid char,
322  * if validation failed because there's an invalid char there, or %NULL.
323  *
324  * validates @recipient as a valid recipient field for header.
325  * It's different from modest_text_utils_validate_email_address()
326  * as it validates a whole recipient, and not only the part between
327  * the &lt; and &gt; symbols.
328  *
329  * Returns: %TRUE if the recipient is valid, FALSE otherwise
330  **/
331 gboolean     modest_text_utils_validate_recipient (const gchar *recipient,
332                                                    const gchar **invalid_char_position);
333
334 /**
335  * modest_text_utils_split_addresses_list:
336  * @addresses: a string
337  *
338  * obtains a GSList of addresses from a string of addresses
339  * in the format understood by email protocols
340  *
341  * Returns: a GSList of strings
342  **/
343 GSList      *modest_text_utils_split_addresses_list (const gchar *addresses);
344
345 /**
346  * modest_text_utils_get_addresses_indexes:
347  * @addresses: a string
348  * @start_indexes: a #GSList pointer
349  * @end_indexes: a #GSList pointer
350  *
351  * obtains two #GSList of @addresses with the range offsets of the addresses in
352  * the string
353  *
354  * Returns: a GSList of strings
355  **/
356 void         modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes);
357
358 /**
359  * modest_text_utils_get_color_string:
360  * @color: a #GdkColor
361  *
362  * Obtains a proper markup string for @color, in the format used
363  * by Pango and HTML.
364  *
365  * Returns: a newly allocated string
366  */
367 gchar *      modest_text_utils_get_color_string (GdkColor *color);
368
369 /**
370  * modest_text_utils_text_buffer_get_text:
371  * @buffer: a #GtkTextBuffer
372  *
373  * Obtains the contents of a @buffer in a string, replacing image
374  * pixbufs with blank spaces.
375  *
376  * Returns: a newly allocated UTF-8 string
377  */
378 gchar *      modest_text_utils_text_buffer_get_text (GtkTextBuffer *buffer);
379
380 typedef enum {
381         ACCOUNT_TITLE_FORBIDDEN_CHARS,
382         FOLDER_NAME_FORBIDDEN_CHARS,
383         USER_NAME_FORBIDDEN_NAMES,
384 } ModestTextUtilsForbiddenCharType;
385
386 gboolean     modest_text_utils_is_forbidden_char (const gchar character,
387                                                   ModestTextUtilsForbiddenCharType type);
388
389 #endif /* __MODEST_TEXT_UTILS_H__ */