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