971bd23986411438dcd4ed10816428329f17c1bd
[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 #include <gtk/gtklabel.h>
41
42 #define _FM(str) dgettext("hildon-fm",str)
43 #define _CS(str) dgettext("hildon-common-strings",str)
44 #define _HL(str) dgettext("hildon-libs",str)
45 #define _MD(str) dgettext("maemo-af-desktop",str)
46 #define _AB(str) dgettext("osso-addressbook",str)
47 #define _KR(str) dgettext("ke-recv",str)
48 #define _UR(str) dgettext("osso-uri",str)
49
50 /* Forbidden char arrays */
51 extern const gchar account_title_forbidden_chars[];
52 extern const gchar folder_name_forbidden_chars[];
53 extern const gchar user_name_forbidden_chars[];
54 extern const guint ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH;
55 extern const guint FOLDER_NAME_FORBIDDEN_CHARS_LENGTH;
56 extern const guint USER_NAME_FORBIDDEN_CHARS_LENGTH;
57
58 /* It includes a white space as RFC 3676 Section 4.3 about usenet
59    message signatures defines */
60 #define MODEST_TEXT_UTILS_SIGNATURE_MARKER "-- "
61
62 /**
63  * modest_text_utils_derived_subject:
64  * @subject: a string which contains the original subject
65  * @prefix: the prefix for the new subject (such as 'Re:' or 'Fwd:'),
66  *           must not be NULL
67  *
68  * create a 'derived' subject line for eg. replies and forwards 
69  * 
70  * Returns: a newly allocated string containing the resulting subject
71  * subject == NULL, then @prefix " " will be returned
72  */
73 gchar* modest_text_utils_derived_subject (const gchar *subject, 
74                                           const gchar* prefix);
75
76
77 /**
78  * modest_text_utils_quote:
79  * @text: a non-NULL string which contains the message to quote
80  * @from: a non-NULL  sender of the original message
81  * @content_type: the non-NULL content type for the quoting, e.g. "text/html"
82  * @signature: NULL or the signature to add
83  * @sent_date: sent date/time of the original message
84  * @attachments: a #GList of the attachments
85  * @limit: specifies the maximum characters per line in the quoted text
86  * 
87  * quote an existing message
88  * 
89  * Returns: a newly allocated string containing the quoted message
90  */
91 gchar* modest_text_utils_quote (const gchar *text, 
92                                 const gchar *content_type,
93                                 const gchar *signature,
94                                 const gchar *from,
95                                 const time_t sent_date, 
96                                 GList *attachments,
97                                 int limit);
98
99
100 /**
101  * modest_text_utils_cited_text:
102  * @from: sender of the message
103  * @sent_date: the sent date of the original message
104  * @text: the text of the original message
105  *
106  * cite the text in a message
107  * 
108  * Returns: a newly allocated string containing the cited text
109  */
110 gchar* modest_text_utils_cite (const gchar *text,
111                                const gchar *content_type,
112                                const gchar *signature,
113                                const gchar *from,
114                                time_t sent_date);
115
116 /**
117  * modest_text_utils_inlined_text
118  * @from: the non-NULL sender of the original message
119  * @sent_date: sent date/time of the original message
120  * @to: 
121  * @subject: 
122  * @text: 
123  *
124  * creates a new string with the "Original message" text prepended to
125  * the text passed as argument and some data of the header
126  * 
127  * Returns: a newly allocated string containing the quoted message
128  */
129 gchar*   modest_text_utils_inline (const gchar *text,
130                                    const gchar *content_type,
131                                    const gchar *signature,
132                                    const gchar *from,
133                                    time_t sent_date,
134                                    const gchar *to,
135                                    const gchar *subject);
136
137 /**
138  * modest_text_utils_remove_address
139  * @address_list: non-NULL string with a comma-separated list of email addresses
140  * @address: an specific e-mail address 
141  *
142  * remove a specific address from a list of email addresses; if @address
143  * is NULL, returns an unchanged (but newly allocated) @address_list
144  * 
145  * Returns: a newly allocated string containing the new list, or NULL
146  * in case of error or the original @address_list was NULL
147  */
148 gchar*   modest_text_utils_remove_address (const gchar *address_list, 
149                                            const gchar *address);
150
151
152 /**
153  * modest_text_utils_remove_duplicate_addresses
154  * @address_list: non-NULL string with a comma-separated list of email addresses
155  *
156  * remove duplicate addresses from a list of email addresses
157  * 
158  * Returns: a newly allocated string containing the new list, or NULL
159  * in case of error or the original @address_list was NULL
160  */
161 gchar*   modest_text_utils_remove_duplicate_addresses (const gchar *address_list); 
162
163
164 /**
165  * modest_text_utils_address_range_at_position:
166  * @address_list: non-NULL utf8 string containing a list of addresses
167  * @position: a gint
168  * @start: a gint pointer
169  * @end: a gint pointer
170  *
171  * Finds the start and end positions of the address at @position,
172  * in @recipients_list, a list of addresses in the format of a 
173  * recipient list in email. It stores the results in @start and
174  * @end
175  */
176 void     modest_text_utils_address_range_at_position (const gchar *recipients_list,
177                                                       guint position,
178                                                       guint *start,
179                                                       guint *end);
180
181 /**
182  * modest_text_utils_hyperlinkify_begin:
183  *
184  * begin a linkify block, compiling the caches to be reused. Use it in mainloop.
185  */
186 void modest_text_utils_hyperlinkify_begin (void);
187
188 /**
189  * modest_text_utils_hyperlinkify_end:
190  *
191  * end a linkify block, freeing the caches to be reused. Use it in mainloop.
192  */
193 void modest_text_utils_hyperlinkify_end (void);
194
195 /**
196  * modest_text_utils_convert_to_html:
197  * @txt: a string
198  *
199  * convert plain text (utf8) into html
200  * 
201  * Returns: a newly allocated string containing the html
202  */
203 gchar*  modest_text_utils_convert_to_html (const gchar *txt);
204
205 /**
206  * modest_text_utils_convert_to_html_body:
207  * @txt: a string
208  *
209  * convert plain text (utf8) into html without adding html headers.
210  * 
211  * Returns: a newly allocated string containing the html
212  */
213 gchar*  modest_text_utils_convert_to_html_body (const gchar *data, gssize n, gboolean hyperlinkify);
214
215
216 /**
217  * modest_text_utils_strftime:
218  * @s:
219  * @max:
220  * @fmt:
221  * @timet:
222  *
223  * this is just an alias for strftime(3), so we can use that without
224  * getting warning from gcc
225  * 
226  * Returns: a formatted string of max length @max in @s
227  */
228 size_t modest_text_utils_strftime(char *s, size_t max, const char  *fmt, time_t timet);
229
230 /**
231  * modest_text_utils_hyperlinkify:
232  * @string_buffer: buffer where we replace uri strings with links
233  *
234  * Replace uri's with links in the buffer. This is required that the document
235  * do not contain linkified links already.
236  */
237 void modest_text_utils_hyperlinkify (GString *string_buffer);
238
239 /**
240  * modest_text_utils_get_display_address:
241  * @address: original address (UTF8 string)
242  *
243  * make a 'display address' from an address:
244  * "Foo Bar &lt;foo@bar.cx&gt;" --&gt; "Foo Bar"
245  * ie. removes "&lt;...&gt;" parts
246  * the change is in-place; removes leading whitespace
247  * 
248  * NOTE: for optimization reasons, this function changes @address
249  * in-place
250  */
251 void modest_text_utils_get_display_address (gchar *address);
252
253 /**
254  * modest_text_utils_get_display_addresses:
255  * @addresses: a list of comma-separated addresses
256  *
257  * Transforms a list of email addresses in a list of recipients,
258  * replacing each plain email address by the correspondent display
259  * address.
260  *
261  * Returns: a newly allocated string, that must be freed by the caller
262  **/
263 gchar *modest_text_utils_get_display_addresses (const gchar *addresses);
264
265
266 /**
267  * modest_text_utils_get_email_address:
268  * @full_address: original address (UTF8 string)
269  *
270  * make a 'foo@bar.cx' from an address:
271  * "Foo Bar <foo@bar.cx> (Bla)" --> "foo@bar.cx"
272  * If no "<...>" is found, then it returns the full
273  * strings.
274  * 
275  * Returns: a newly allocated string with the copy.
276  * 
277  * NULL in case of error or if address == NULL
278  */
279 gchar* modest_text_utils_get_email_address (const gchar *email_address);
280
281
282 /**
283  * modest_text_utils_get_subject_prefix_len:
284  * @subject: original subject (UTF8 string)
285  *
286  * determine the length of the "Re:/RE:/Fwd:" prefix in an e-mail address
287  * 
288  * Returns: the length of the  prefix, or 0 if there is none
289  */
290 gint modest_text_utils_get_subject_prefix_len (const gchar *subject);
291
292
293 /**
294  * modest_text_utils_utf8_strcmp:
295  * @s1: the first string
296  * @s2: the second string
297  * @insensitive: should the comparison be case-insensitive?
298  *
299  * a strcmp that is NULL-safe, can deal with UTF8 and case-insensitive comparison 
300  *
301  * Returns: an integer less than, equal to, or greater than zero if s1 is found,
302  * respectively, to be less than, to match, or be greater than s2.
303  */
304 gint modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive);
305
306
307
308 /**
309  * modest_text_utils_get_display_date:
310  * @date: the date to display
311  *
312  * get a string representation for a date.
313  * 
314  * Returns: the new display date, as a *static* string.
315  * This string should not be modified, and will change
316  * upon recalling this function. g_strdup it if you to
317  * do so.
318  * 
319  */
320 const gchar* modest_text_utils_get_display_date (time_t date);
321
322
323 /**
324  * modest_text_utils_get_display_size:
325  * @size: size in bytes
326  *
327  * get a string representation for a size in bytes.
328  * 
329  * Returns: the newly allocated display string for the
330  * size in bytes. must be freed.
331  */
332 gchar * modest_text_utils_get_display_size (guint64 size);
333
334
335
336 /**
337  * modest_text_utils_validate_domain_name:
338  * @email_address: a NULL-terminated string
339  * 
340  * validates the domain name passed as argument
341  * 
342  * Returns: TRUE if the domain name is valid, FALSE otherwise
343  **/
344 gboolean modest_text_utils_validate_domain_name (const gchar *domain);
345
346 /**
347  * modest_text_utils_validate_email_address:
348  * @email_address: a string
349  * @invalid_char_position: pointer to the position of the invalid
350  * character in case validation failed because of this, or %NULL.
351  * 
352  * validates the email address passed as argument
353  * 
354  * Returns: TRUE if the address is valid, FALSE otherwise
355  **/
356 gboolean     modest_text_utils_validate_email_address (const gchar *email_address, 
357                                                        const gchar **invalid_char_position);
358
359
360 /**
361  * modest_text_utils_validate_folder_name:
362  * @folder_name: a string
363  * 
364  * validates the folder name passed as argument. a 'valid folder name'
365  * is a name which should be valid on both Unix and Windows file systems.
366  * of course, this might be stricter than strictly needed in some cases,
367  * but it's better to err on the safe side.
368  * 
369  * Returns: TRUE if the folder name is valid, FALSE otherwise
370  **/
371 gboolean modest_text_utils_validate_folder_name (const gchar *folder_name);
372
373 /**
374  * modest_text_utils_validate_recipient:
375  * @recipient: a string
376  * @invalid_char_position: pointer to the position of the invalid char,
377  * if validation failed because there's an invalid char there, or %NULL.
378  *
379  * validates @recipient as a valid recipient field for header.
380  * It's different from modest_text_utils_validate_email_address()
381  * as it validates a whole recipient, and not only the part between
382  * the &lt; and &gt; symbols.
383  *
384  * Returns: %TRUE if the recipient is valid, FALSE otherwise
385  **/
386 gboolean     modest_text_utils_validate_recipient (const gchar *recipient,
387                                                    const gchar **invalid_char_position);
388
389 /**
390  * modest_text_utils_split_addresses_list:
391  * @addresses: a string
392  *
393  * obtains a GSList of addresses from a string of addresses
394  * in the format understood by email protocols
395  *
396  * Returns: a newly allocated GSList of strings
397  **/
398 GSList      *modest_text_utils_split_addresses_list (const gchar *addresses);
399
400
401 /**
402  * modest_text_utils_get_addresses_indexes:
403  * @addresses: a string
404  * @start_indexes: a #GSList pointer
405  * @end_indexes: a #GSList pointer
406  *
407  * obtains two #GSList of @addresses with the range offsets of the addresses in
408  * the string
409  *
410  * Returns: a GSList of strings
411  **/
412 void         modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes);
413
414 /**
415  * modest_text_utils_address_with_standard_length:
416  * @recipients_list: a string
417  *
418  * obtains the list of recipients, but making sure that lines are not longer than 1000 chars
419  *
420  * Returns: a newly allocated string
421  */
422 gchar *      modest_text_utils_address_with_standard_length (const gchar *recipients_list);
423
424 /**
425  * modest_text_utils_get_color_string:
426  * @color: a #GdkColor
427  *
428  * Obtains a proper markup string for @color, in the format used
429  * by Pango and HTML.
430  *
431  * Returns: a newly allocated string
432  */
433 gchar *      modest_text_utils_get_color_string (GdkColor *color);
434
435 /**
436  * modest_text_utils_text_buffer_get_text:
437  * @buffer: a #GtkTextBuffer
438  *
439  * Obtains the contents of a @buffer in a string, replacing image
440  * pixbufs with blank spaces.
441  *
442  * Returns: a newly allocated UTF-8 string
443  */
444 gchar *      modest_text_utils_text_buffer_get_text (GtkTextBuffer *buffer);
445
446 typedef enum {
447         ACCOUNT_TITLE_FORBIDDEN_CHARS,
448         FOLDER_NAME_FORBIDDEN_CHARS,
449         USER_NAME_FORBIDDEN_NAMES,
450 } ModestTextUtilsForbiddenCharType;
451
452 /**
453  * modest_text_utils_label_get_selection:
454  * @label: a #GtkLabel
455  *
456  * Obtain the current selection of @label
457  *
458  * Returns: a string with current selection, or %NULL if no selection in @label
459  */
460 gchar *      modest_text_utils_label_get_selection (GtkLabel *label);
461
462 /**
463  * modest_text_utils_is_forbidden_char:
464  * @character: some character
465  * @type: the type of forbidden char (see #ModestTextUtilsForbiddenCharType)
466  * 
467  * check whether the given character is 'forbidden'
468  *
469  * Returns: TRUE if it's forbidden, FALSE otherwise
470  */
471 gboolean     modest_text_utils_is_forbidden_char (const gchar character,
472                                                   ModestTextUtilsForbiddenCharType type);
473
474 /**
475  * modest_text_utils_buffer_selection_is_valid:
476  * @buffer: a #GtkTextBuffer
477  *
478  * Checks if @buffer contains a valid selection for cut/copy. This means it's
479  * not empty, and no images are in the selection.
480  *
481  * Returns: %TRUE if there's a valid selection, false otherwise.
482  */
483 gboolean     modest_text_utils_buffer_selection_is_valid (GtkTextBuffer *buffer);
484
485 /**
486  * modest_text_utils_escape_mnemonics:
487  * @text: a string
488  *
489  * obtains the representation of text, but escaping mnemonics (we duplicate _)
490  *
491  * Returns: a newly allocated string
492  */
493 gchar *modest_text_utils_escape_mnemonics (const gchar *text);
494
495 /**
496  * modest_text_utils_simplify_recipients:
497  * @recipients: a list of recipients
498  *
499  * returns a list of simplified recipients:
500  *   * a@b <a@b> converted to a@b
501  *   * NULL converted to ""
502  *
503  * It's mainly intended for printing in screen addresses, but it can
504  * also be used for reply/forward.
505  */
506 gchar *modest_text_utils_simplify_recipients (const gchar *recipient);
507
508 /**
509  * modest_text_utils_remove_duplicate_addresses_list
510  * @address_list: non-NULL #GSList of email addresses
511  *
512  * remove duplicate addresses from a list of email addresses
513  *
514  * Returns: a list without the duplicate addresses or NULL in case of
515  * error or the original @address_list was NULL
516  */
517 GSList *modest_text_utils_remove_duplicate_addresses_list (GSList *address_list);
518
519 /**
520  * modest_text_utils_get_secure_header:
521  * @value: the value of a mail header
522  * @header: the header that we're evaluating
523  *
524  * This function returns the secure value for a header. Basically it
525  * avoids DoS attacks caused by specially malformed headers like for
526  * example. From:From:From...From: some@mail.com
527  *
528  * Returns: returns the secured header
529  **/
530 gchar * modest_text_utils_get_secure_header (const gchar *value, const gchar *header);
531
532 /**
533  * modest_text_utils_quote_names:
534  * @recipients: a list of valid email addresses separated by ',' or ';'
535  *
536  * This function quotes the name part of an email address if it's not
537  * quoted and if it exists. For example
538  * aaa@bbb.com -> aaa@bbb.com
539  * "my name" <aaa@bbb.com> -> "my name" <aaa@bbb.com>
540  * my name aaa@bbb.com -> "my name" aaa@bbb.com
541  *
542  * It even supports things like
543  * my, name <aaa@bbb.com>, aaa@ccc.com -> "my, name" <aaa@bbb.com>; aaa@ccc.com
544  *
545  * Returns: a newly allocated string with the quoted email addresses
546  **/
547 gchar * modest_text_utils_quote_names (const gchar *recipients);
548
549 #endif /* __MODEST_TEXT_UTILS_H__ */