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