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