* Fixes NB@63545
[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
44 /* Forbidden char arrays */
45 extern const gchar account_title_forbidden_chars[];
46 extern const gchar folder_name_forbidden_chars[];
47 extern const gchar user_name_forbidden_chars[];
48 extern const guint ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH;
49 extern const guint FOLDER_NAME_FORBIDDEN_CHARS_LENGTH;
50 extern const guint USER_NAME_FORBIDDEN_CHARS_LENGTH;
51
52 /**
53  * modest_text_utils_derived_subject:
54  * @subject: a string which contains the original subject
55  * @prefix: the prefix for the new subject (such as 'Re:' or 'Fwd:'),
56  *           must not be NULL
57  *
58  * create a 'derived' subject line for eg. replies and forwards 
59  * 
60  * Returns: a newly allocated string containing the resulting subject
61  * subject == NULL, then @prefix " " will be returned
62  */
63 gchar* modest_text_utils_derived_subject (const gchar *subject, 
64                                           const gchar* prefix);
65
66
67 /**
68  * modest_text_utils_quote:
69  * @text: a non-NULL string which contains the message to quote
70  * @from: a non-NULL  sender of the original message
71  * @content_type: the non-NULL content type for the quoting, e.g. "text/html"
72  * @signature: NULL or the signature to add
73  * @sent_date: sent date/time of the original message
74  * @attachments: a #GList of the attachments
75  * @limit: specifies the maximum characters per line in the quoted text
76  * 
77  * quote an existing message
78  * 
79  * Returns: a newly allocated string containing the quoted message
80  */
81 gchar* modest_text_utils_quote (const gchar *text, 
82                                 const gchar *content_type,
83                                 const gchar *signature,
84                                 const gchar *from,
85                                 const time_t sent_date, 
86                                 GList *attachments,
87                                 int limit);
88
89
90 /**
91  * modest_text_utils_cited_text:
92  * @from: sender of the message
93  * @sent_date: the sent date of the original message
94  * @text: the text of the original message
95  *
96  * cite the text in a message
97  * 
98  * Returns: a newly allocated string containing the cited text
99  */
100 gchar* modest_text_utils_cite (const gchar *text,
101                                const gchar *content_type,
102                                const gchar *signature,
103                                const gchar *from,
104                                time_t sent_date);
105
106 /**
107  * modest_text_utils_inlined_text
108  * @from: the non-NULL sender of the original message
109  * @sent_date: sent date/time of the original message
110  * @to: 
111  * @subject: 
112  * @text: 
113  *
114  * creates a new string with the "Original message" text prepended to
115  * the text passed as argument and some data of the header
116  * 
117  * Returns: a newly allocated string containing the quoted message
118  */
119 gchar*   modest_text_utils_inline (const gchar *text,
120                                    const gchar *content_type,
121                                    const gchar *signature,
122                                    const gchar *from,
123                                    time_t sent_date,
124                                    const gchar *to,
125                                    const gchar *subject);
126
127 /**
128  * modest_text_utils_remove_address
129  * @address_list: none-NULL string with a comma-separated list of email addresses
130  * @address: an specific e-mail address 
131  *
132  * remove a specific address from a list of email addresses; if @address
133  * is NULL, returns an unchanged @address_list
134  * 
135  * Returns: a newly allocated string containing the new list, or NULL
136  * in case of error or the original @address_list was NULL
137  */
138 gchar*   modest_text_utils_remove_address (const gchar *address_list, 
139                                            const gchar *address);
140
141 /**
142  * modest_text_utils_address_range_at_position:
143  * @address_list: utf8 string containing a list of addresses
144  * @position: a gint
145  * @start: a gint pointer
146  * @end: a gint pointer
147  *
148  * Finds the start and end positions of the address at @position,
149  * in @recipients_list, a list of addresses in the format of a 
150  * recipient list in email. It stores the results in @start and
151  * @end
152  */
153 void     modest_text_utils_address_range_at_position (const gchar *recipients_list,
154                                                       gint position,
155                                                       gint *start,
156                                                       gint *end);
157                                                       
158
159 /**
160  * modest_text_utils_convert_to_html:
161  * @txt: a string
162  *
163  * convert plain text (utf8) into html
164  * 
165  * Returns: a newly allocated string containing the html
166  */
167 gchar*  modest_text_utils_convert_to_html (const gchar *txt);
168
169 /**
170  * modest_text_utils_convert_to_html_body:
171  * @txt: a string
172  *
173  * convert plain text (utf8) into html without adding html headers.
174  * 
175  * Returns: a newly allocated string containing the html
176  */
177 gchar*  modest_text_utils_convert_to_html_body (const gchar *data);
178
179
180 /**
181  * modest_text_utils_strftime:
182  * @s:
183  * @max:
184  * @fmt:
185  * @timet:
186  *
187  * this is just an alias for strftime(3), so we can use that without
188  * getting warning from gcc
189  * 
190  * Returns: a formatted string of max length @max in @s
191  */
192 size_t modest_text_utils_strftime(char *s, size_t max, const char  *fmt, time_t timet);
193
194
195
196 /**
197  * modest_text_utils_get_display_address:
198  * @address: original address (UTF8 string)
199  *
200  * make a 'display address' from an address:
201  * "Foo Bar <foo@bar.cx> (Bla)" --> "Foo Bar"
202  * ie. removes "<...>" and "(...)" parts
203  * the change is in-place; removes leading/trailing whitespace
204  * 
205  * Returns: the new address of the string; this new string
206  * is _NOT_ newly allocated, so should not be freed. (remember
207  * the old address of the parameter if that one needs to be freed)
208  * 
209  * NULL in case of error or if address == NULL
210  */
211 gchar* modest_text_utils_get_display_address (gchar *address);
212
213 /**
214  * modest_text_utils_get_email_address:
215  * @full_address: original address (UTF8 string)
216  *
217  * make a 'foo@bar.cx' from an address:
218  * "Foo Bar <foo@bar.cx> (Bla)" --> "foo@bar.cx"
219  * If no "<...>" is found, then it returns the full
220  * strings.
221  * 
222  * Returns: a newly allocated string with the copy.
223  * 
224  * NULL in case of error or if address == NULL
225  */
226 gchar* modest_text_utils_get_email_address (const gchar *email_address);
227
228
229 /**
230  * modest_text_utils_get_subject_prefix_len:
231  * @subject: original subject (UTF8 string)
232  *
233  * determine the length of the "Re:/RE:/Fwd:" prefix in an e-mail address
234  * 
235  * Returns: the length of the  prefix, or 0 if there is none
236  */
237 gint modest_text_utils_get_subject_prefix_len (const gchar *subject);
238
239
240 /**
241  * modest_text_utils_utf8_strcmp:
242  * @s1: the first string
243  * @s2: the second string
244  * @insensitive: should the comparison be case-insensitive?
245  *
246  * a strcmp that is NULL-safe, can deal with UTF8 and case-insensitive comparison 
247  *
248  * Returns: an integer less than, equal to, or greater than zero if s1 is found,
249  * respectively, to be less than, to match, or be greater than s2.
250  */
251 gint modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive);
252
253
254
255 /**
256  * modest_text_utils_get_display_date:
257  * @date: the date to display
258  *
259  * get a string representation for a date.
260  * 
261  * Returns: the new display date, as a newly allocated string;
262  * free with g_free
263  */
264 gchar* modest_text_utils_get_display_date (time_t date);
265
266
267 /**
268  * modest_text_utils_get_display_size:
269  * @size: size in bytes
270  *
271  * get a string representation for a size in bytes.
272  * 
273  * Returns: the newly allocated display string for the
274  * size in bytes. must be freed.
275  */
276 gchar * modest_text_utils_get_display_size (guint64 size);
277
278
279
280 /**
281  * modest_text_utils_validate_domain_name:
282  * @email_address: a NULL-terminated string
283  * 
284  * validates the domain name passed as argument
285  * 
286  * Returns: TRUE if the domain name is valid, FALSE otherwise
287  **/
288 gboolean modest_text_utils_validate_domain_name (const gchar *domain);
289
290 /**
291  * modest_text_utils_validate_email_address:
292  * @email_address: a string
293  * @invalid_char_position: pointer to the position of the invalid
294  * character in case validation failed because of this, or %NULL.
295  * 
296  * validates the email address passed as argument
297  * 
298  * Returns: TRUE if the address is valid, FALSE otherwise
299  **/
300 gboolean     modest_text_utils_validate_email_address (const gchar *email_address, 
301                                                        const gchar **invalid_char_position);
302
303
304 /**
305  * modest_text_utils_validate_folder_name:
306  * @folder_name: a string
307  * 
308  * validates the folder name passed as argument. a 'valid folder name'
309  * is a name which should be valid on both Unix and Windows file systems.
310  * of course, this might be stricter than strictly needed in some cases,
311  * but it's better to err on the safe side.
312  * 
313  * Returns: TRUE if the folder name is valid, FALSE otherwise
314  **/
315 gboolean modest_text_utils_validate_folder_name (const gchar *folder_name);
316
317 /**
318  * modest_text_utils_validate_recipient:
319  * @recipient: a string
320  * @invalid_char_position: pointer to the position of the invalid char,
321  * if validation failed because there's an invalid char there, or %NULL.
322  *
323  * validates @recipient as a valid recipient field for header.
324  * It's different from modest_text_utils_validate_email_address()
325  * as it validates a whole recipient, and not only the part between
326  * the &lt; and &gt; symbols.
327  *
328  * Returns: %TRUE if the recipient is valid, FALSE otherwise
329  **/
330 gboolean     modest_text_utils_validate_recipient (const gchar *recipient,
331                                                    const gchar **invalid_char_position);
332
333 /**
334  * modest_text_utils_split_addresses_list:
335  * @addresses: a string
336  *
337  * obtains a GSList of addresses from a string of addresses
338  * in the format understood by email protocols
339  *
340  * Returns: a GSList of strings
341  **/
342 GSList      *modest_text_utils_split_addresses_list (const gchar *addresses);
343
344 /**
345  * modest_text_utils_get_addresses_indexes:
346  * @addresses: a string
347  * @start_indexes: a #GSList pointer
348  * @end_indexes: a #GSList pointer
349  *
350  * obtains two #GSList of @addresses with the range offsets of the addresses in
351  * the string
352  *
353  * Returns: a GSList of strings
354  **/
355 void         modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes);
356
357 /**
358  * modest_text_utils_get_color_string:
359  * @color: a #GdkColor
360  *
361  * Obtains a proper markup string for @color, in the format used
362  * by Pango and HTML.
363  *
364  * Returns: a newly allocated string
365  */
366 gchar *      modest_text_utils_get_color_string (GdkColor *color);
367
368 /**
369  * modest_text_utils_text_buffer_get_text:
370  * @buffer: a #GtkTextBuffer
371  *
372  * Obtains the contents of a @buffer in a string, replacing image
373  * pixbufs with blank spaces.
374  *
375  * Returns: a newly allocated UTF-8 string
376  */
377 gchar *      modest_text_utils_text_buffer_get_text (GtkTextBuffer *buffer);
378
379 typedef enum {
380         ACCOUNT_TITLE_FORBIDDEN_CHARS,
381         FOLDER_NAME_FORBIDDEN_CHARS,
382         USER_NAME_FORBIDDEN_NAMES,
383 } ModestTextUtilsForbiddenCharType;
384
385 gboolean     modest_text_utils_is_forbidden_char (const gchar character,
386                                                   ModestTextUtilsForbiddenCharType type);
387
388 #endif /* __MODEST_TEXT_UTILS_H__ */