1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
31 /* modest-text-utils.h */
33 #ifndef __MODEST_TEXT_UTILS_H__
34 #define __MODEST_TEXT_UTILS_H__
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)
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")
71 #define _CS_ILLEGAL_CHARACTERS_ENTERED _CS("ckdg_ib_illegal_characters_entered")
72 #define _CS_CANNOT_ZOOM_HERE _CS("ckct_ib_cannot_zoom_here")
73 #define _CS_MAX_ZOOM_LEVEL_REACHED _CS("ckct_ib_max_zoom_level_reached")
74 #define _CS_MIN_ZOOM_LEVEL_REACHED _CS("ckct_ib_min_zoom_level_reached")
75 #define _CS_MAXIMUM_CHARACTERS_REACHED _CS("ckdg_ib_maximum_characters_reached")
76 #define _CS_FOLDER_ALREADY_EXISTS _CS("ckdg_ib_folder_already_exists")
77 #define _CS_SET_PASSWORD_INCORRECT _CS("ecdg_ib_set_password_incorrect")
78 #define _CS_UNABLE_TO_PASTE_HERE _CS("ckct_ib_unable_to_paste_here")
79 #define _CS_UNABLE_TO_OPEN_FILE_NOT_FOUND _CS("sfil_ni_unable_to_open_file_not_found")
80 #define _CS_UNABLE_TO_RENAME _CS("ckdg_ib_unable_to_rename")
81 #define _CS_UNABLE_TO_DELETE _CS("ckdg_ib_unable_to_delete")
82 #define _CS_NOTHING_TO_SORT _CS("ckdg_ib_nothing_to_sort")
83 #define _CS_NOT_ENOUGH_MEMORY _CS("sfil_ni_not_enough_memory")
84 #define _CS_FOLDER_ALREADY_EXISTS _CS("ckdg_ib_folder_already_exists")
85 #define _CS_PASTING _CS("ckct_nw_pasting")
86 #define _CS_GETTING_ITEMS _CS("mcen_ib_getting_items")
87 #define _CS_COPIED _CS("ecoc_ib_edwin_copied")
88 #define _CS_UNABLE_TO_SEND _CS("sfil_ib_unable_to_send")
89 #define _CS_FIND_REP_ENTER_TEXT _CS("ecdg_ib_find_rep_enter_text")
90 #define _CS_UPDATING _CS("ckdg_pb_updating")
91 #define _CS_SAVED _CS("sfil_ib_saved")
93 #define _HL_SAVE _("Save")
94 #define _HL_YES _("Yes")
95 #define _HL_NO _("Yes")
96 #define _HL_VIEW _("View")
97 #define _HL_TITLE_SORT _("Sort")
98 #define _HL_TITLE_NEW_FOLDER _("New folder")
99 #define _HL_TITLE_RENAME_FOLDER _HL("Rename folder")
100 #define _HL_RENAME_NAME _("Name")
101 #define _HL_24H_TIME _("%H:%M")
102 #define _HL_DATE _("%m/%d/%Y")
103 #define _HL_WEEK _("%A")
104 #define _HL_DATE_MEDIUM _("%e %B %Y")
105 #define _HL_12H_TIME_PM _("%l:%M pm")
106 #define _HL_12H_TIME_AM _("%l:%M am")
107 #define _HL_DONE _("Done")
108 #define _HL_DELETE _("Delete")
109 #define _HL_MOVE _("Move")
110 #define _HL_RENAME _("Rename")
111 #define _HL_IB_FIND_COMPLETE _("Search complete")
112 #define _HL_IB_FIND_NO_MATCHES _("No matches")
113 #define _HL_IB_ZOOM _("Zoom %d")
115 #define _CS_ILLEGAL_CHARACTERS_ENTERED _("Illegal characters entered")
116 #define _CS_CANNOT_ZOOM_HERE _("Cannot zoom here")
117 #define _CS_MAX_ZOOM_LEVEL_REACHED _("Max. zoom level reached")
118 #define _CS_MIN_ZOOM_LEVEL_REACHED _("Min. zoom level reached")
119 #define _CS_MAXIMUM_CHARACTERS_REACHED _("Maximum characters reached")
120 #define _CS_FOLDER_ALREADY_EXISTS _("Folder already exists")
121 #define _CS_SET_PASSWORD_INCORRECT _("Password incorrect")
122 #define _CS_UNABLE_TO_PASTE_HERE _("Unable to paste here")
123 #define _CS_UNABLE_TO_OPEN_FILE_NOT_FOUND _("File not found")
124 #define _CS_UNABLE_TO_RENAME _("Unable to rename")
125 #define _CS_UNABLE_TO_DELETE _("Unable to delete")
126 #define _CS_NOTHING_TO_SORT _("Nothing to sort")
127 #define _CS_NOT_ENOUGH_MEMORY _("Not enough memory")
128 #define _CS_FOLDER_ALREADY_EXISTS _("Folder already exists")
129 #define _CS_PASTING _("Pasting")
130 #define _CS_GETTING_ITEMS _("Getting items")
131 #define _CS_COPIED _("Copied")
132 #define _CS_UNABLE_TO_SEND _("Unable to send")
133 #define _CS_FIND_REP_ENTER_TEXT _("Enter text to search")
134 #define _CS_UPDATING _("Updating...")
135 #define _CS_SAVED _("Saved")
138 /* Forbidden char arrays */
139 extern const gchar account_title_forbidden_chars[];
140 extern const gchar folder_name_forbidden_chars[];
141 extern const gchar user_name_forbidden_chars[];
142 extern const guint ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH;
143 extern const guint FOLDER_NAME_FORBIDDEN_CHARS_LENGTH;
144 extern const guint USER_NAME_FORBIDDEN_CHARS_LENGTH;
146 /* It includes a white space as RFC 3676 Section 4.3 about usenet
147 message signatures defines */
148 #define MODEST_TEXT_UTILS_SIGNATURE_MARKER "-- "
151 * modest_text_utils_derived_subject:
152 * @subject: a string which contains the original subject
153 * @is_reply: whether the derived subject is for a reply or a forward message
155 * create a 'derived' subject line for eg. replies and forwards. Note
156 * that this function will use the localized versions of "Re" and
157 * "Fw", unless one of these two versions was already included. For
158 * example replying to an email in Finish would work as:
160 * "some subject" -> "VS: some subject"
161 * "VS: some subject" -> "VS: some subject"
162 * "Re: some subject" -> "Re: some subject"
163 * "Fw: some subject" -> "VS: Fw: some subject"
165 * Returns: a newly allocated string containing the resulting subject
167 gchar* modest_text_utils_derived_subject (const gchar *subject,
172 * modest_text_utils_quote:
173 * @text: a non-NULL string which contains the message to quote
174 * @from: a non-NULL sender of the original message
175 * @content_type: the non-NULL content type for the quoting, e.g. "text/html"
176 * @signature: NULL or the signature to add
177 * @sent_date: sent date/time of the original message
178 * @attachments: a #GList of the attachments
179 * @limit: specifies the maximum characters per line in the quoted text
181 * quote an existing message
183 * Returns: a newly allocated string containing the quoted message
185 gchar* modest_text_utils_quote (const gchar *text,
186 const gchar *content_type,
187 const gchar *signature,
189 const time_t sent_date,
195 * modest_text_utils_cited_text:
196 * @from: sender of the message
197 * @sent_date: the sent date of the original message
198 * @text: the text of the original message
200 * cite the text in a message
202 * Returns: a newly allocated string containing the cited text
204 gchar* modest_text_utils_cite (const gchar *text,
205 const gchar *content_type,
206 const gchar *signature,
211 * modest_text_utils_inlined_text
212 * @from: the non-NULL sender of the original message
213 * @sent_date: sent date/time of the original message
218 * creates a new string with the "Original message" text prepended to
219 * the text passed as argument and some data of the header
221 * Returns: a newly allocated string containing the quoted message
223 gchar* modest_text_utils_inline (const gchar *text,
224 const gchar *content_type,
225 const gchar *signature,
229 const gchar *subject);
232 * modest_text_utils_remove_address
233 * @address_list: non-NULL string with a comma-separated list of email addresses
234 * @address: an specific e-mail address
236 * remove a specific address from a list of email addresses; if @address
237 * is NULL, returns an unchanged (but newly allocated) @address_list
239 * Returns: a newly allocated string containing the new list, or NULL
240 * in case of error or the original @address_list was NULL
242 gchar* modest_text_utils_remove_address (const gchar *address_list,
243 const gchar *address);
247 * modest_text_utils_remove_duplicate_addresses
248 * @address_list: non-NULL string with a comma-separated list of email addresses
250 * remove duplicate addresses from a list of email addresses
252 * Returns: a newly allocated string containing the new list, or NULL
253 * in case of error or the original @address_list was NULL
255 gchar* modest_text_utils_remove_duplicate_addresses (const gchar *address_list);
259 * modest_text_utils_address_range_at_position:
260 * @address_list: non-NULL utf8 string containing a list of addresses
262 * @start: a gint pointer
263 * @end: a gint pointer
265 * Finds the start and end positions of the address at @position,
266 * in @recipients_list, a list of addresses in the format of a
267 * recipient list in email. It stores the results in @start and
270 void modest_text_utils_address_range_at_position (const gchar *recipients_list,
276 * modest_text_utils_hyperlinkify_begin:
278 * begin a linkify block, compiling the caches to be reused. Use it in mainloop.
280 void modest_text_utils_hyperlinkify_begin (void);
283 * modest_text_utils_hyperlinkify_end:
285 * end a linkify block, freeing the caches to be reused. Use it in mainloop.
287 void modest_text_utils_hyperlinkify_end (void);
290 * modest_text_utils_convert_to_html:
293 * convert plain text (utf8) into html
295 * Returns: a newly allocated string containing the html
297 gchar* modest_text_utils_convert_to_html (const gchar *txt);
300 * modest_text_utils_convert_to_html_body:
303 * convert plain text (utf8) into html without adding html headers.
305 * Returns: a newly allocated string containing the html
307 gchar* modest_text_utils_convert_to_html_body (const gchar *data, gssize n, gboolean hyperlinkify);
311 * modest_text_utils_strftime:
317 * this is just an alias for strftime(3), so we can use that without
318 * getting warning from gcc
320 * Returns: a formatted string of max length @max in @s
322 size_t modest_text_utils_strftime(char *s, size_t max, const char *fmt, time_t timet);
325 * modest_text_utils_hyperlinkify:
326 * @string_buffer: buffer where we replace uri strings with links
328 * Replace uri's with links in the buffer. This is required that the document
329 * do not contain linkified links already.
331 void modest_text_utils_hyperlinkify (GString *string_buffer);
334 * modest_text_utils_get_display_address:
335 * @address: original address (UTF8 string)
337 * make a 'display address' from an address:
338 * "Foo Bar <foo@bar.cx>" --> "Foo Bar"
339 * ie. removes "<...>" parts
340 * the change is in-place; removes leading whitespace
342 * NOTE: for optimization reasons, this function changes @address
345 void modest_text_utils_get_display_address (gchar *address);
348 * modest_text_utils_get_display_addresses:
349 * @addresses: a list of comma-separated addresses
351 * Transforms a list of email addresses in a list of recipients,
352 * replacing each plain email address by the correspondent display
355 * Returns: a newly allocated string, that must be freed by the caller
357 gchar *modest_text_utils_get_display_addresses (const gchar *addresses);
361 * modest_text_utils_get_email_address:
362 * @full_address: original address (UTF8 string)
364 * make a 'foo@bar.cx' from an address:
365 * "Foo Bar <foo@bar.cx> (Bla)" --> "foo@bar.cx"
366 * If no "<...>" is found, then it returns the full
369 * Returns: a newly allocated string with the copy.
371 * NULL in case of error or if address == NULL
373 gchar* modest_text_utils_get_email_address (const gchar *email_address);
377 * modest_text_utils_get_subject_prefix_len:
378 * @subject: original subject (UTF8 string)
380 * determine the length of the "Re:/RE:/Fwd:" prefix in an e-mail address
382 * Returns: the length of the prefix, or 0 if there is none
384 gint modest_text_utils_get_subject_prefix_len (const gchar *subject);
388 * modest_text_utils_utf8_strcmp:
389 * @s1: the first string
390 * @s2: the second string
391 * @insensitive: should the comparison be case-insensitive?
393 * a strcmp that is NULL-safe, can deal with UTF8 and case-insensitive comparison
395 * Returns: an integer less than, equal to, or greater than zero if s1 is found,
396 * respectively, to be less than, to match, or be greater than s2.
398 gint modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive);
403 * modest_text_utils_get_display_date:
404 * @date: the date to display
406 * get a string representation for a date.
408 * Returns: the new display date, as a *static* string.
409 * This string should not be modified, and will change
410 * upon recalling this function. g_strdup it if you to
414 const gchar* modest_text_utils_get_display_date (time_t date);
418 * modest_text_utils_get_display_size:
419 * @size: size in bytes
421 * get a string representation for a size in bytes.
423 * Returns: the newly allocated display string for the
424 * size in bytes. must be freed.
426 gchar * modest_text_utils_get_display_size (guint64 size);
431 * modest_text_utils_validate_domain_name:
432 * @email_address: a NULL-terminated string
434 * validates the domain name passed as argument
436 * Returns: TRUE if the domain name is valid, FALSE otherwise
438 gboolean modest_text_utils_validate_domain_name (const gchar *domain);
441 * modest_text_utils_validate_email_address:
442 * @email_address: a string
443 * @invalid_char_position: pointer to the position of the invalid
444 * character in case validation failed because of this, or %NULL.
446 * validates the email address passed as argument
448 * Returns: TRUE if the address is valid, FALSE otherwise
450 gboolean modest_text_utils_validate_email_address (const gchar *email_address,
451 const gchar **invalid_char_position);
455 * modest_text_utils_validate_folder_name:
456 * @folder_name: a string
458 * validates the folder name passed as argument. a 'valid folder name'
459 * is a name which should be valid on both Unix and Windows file systems.
460 * of course, this might be stricter than strictly needed in some cases,
461 * but it's better to err on the safe side.
463 * Returns: TRUE if the folder name is valid, FALSE otherwise
465 gboolean modest_text_utils_validate_folder_name (const gchar *folder_name);
468 * modest_text_utils_validate_recipient:
469 * @recipient: a string
470 * @invalid_char_position: pointer to the position of the invalid char,
471 * if validation failed because there's an invalid char there, or %NULL.
473 * validates @recipient as a valid recipient field for header.
474 * It's different from modest_text_utils_validate_email_address()
475 * as it validates a whole recipient, and not only the part between
476 * the < and > symbols.
478 * Returns: %TRUE if the recipient is valid, FALSE otherwise
480 gboolean modest_text_utils_validate_recipient (const gchar *recipient,
481 const gchar **invalid_char_position);
484 * modest_text_utils_split_addresses_list:
485 * @addresses: a string
487 * obtains a GSList of addresses from a string of addresses
488 * in the format understood by email protocols
490 * Returns: a newly allocated GSList of strings
492 GSList *modest_text_utils_split_addresses_list (const gchar *addresses);
495 * modest_text_utils_join_addresses:
496 * @from: comma separated string of addresses
497 * @to: comma separated string of addresses
498 * @cc: comma separated string of addresses
499 * @bcc: comma separated string of addresses
501 * joins all the addresses in a single comma-separated string
503 * Returns: a newly allocated string with a list of addresses
505 gchar *modest_text_utils_join_addresses (const gchar *from,
511 * modest_text_utils_get_addresses_indexes:
512 * @addresses: a string
513 * @start_indexes: a #GSList pointer
514 * @end_indexes: a #GSList pointer
516 * obtains two #GSList of @addresses with the range offsets of the addresses in
519 * Returns: a GSList of strings
521 void modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes);
524 * modest_text_utils_address_with_standard_length:
525 * @recipients_list: a string
527 * obtains the list of recipients, but making sure that lines are not longer than 1000 chars
529 * Returns: a newly allocated string
531 gchar * modest_text_utils_address_with_standard_length (const gchar *recipients_list);
534 * modest_text_utils_get_color_string:
535 * @color: a #GdkColor
537 * Obtains a proper markup string for @color, in the format used
540 * Returns: a newly allocated string
542 gchar * modest_text_utils_get_color_string (GdkColor *color);
545 * modest_text_utils_text_buffer_get_text:
546 * @buffer: a #GtkTextBuffer
548 * Obtains the contents of a @buffer in a string, replacing image
549 * pixbufs with blank spaces.
551 * Returns: a newly allocated UTF-8 string
553 gchar * modest_text_utils_text_buffer_get_text (GtkTextBuffer *buffer);
556 ACCOUNT_TITLE_FORBIDDEN_CHARS,
557 FOLDER_NAME_FORBIDDEN_CHARS,
558 USER_NAME_FORBIDDEN_NAMES,
559 } ModestTextUtilsForbiddenCharType;
562 * modest_text_utils_label_get_selection:
563 * @label: a #GtkLabel
565 * Obtain the current selection of @label
567 * Returns: a string with current selection, or %NULL if no selection in @label
569 gchar * modest_text_utils_label_get_selection (GtkLabel *label);
572 * modest_text_utils_is_forbidden_char:
573 * @character: some character
574 * @type: the type of forbidden char (see #ModestTextUtilsForbiddenCharType)
576 * check whether the given character is 'forbidden'
578 * Returns: TRUE if it's forbidden, FALSE otherwise
580 gboolean modest_text_utils_is_forbidden_char (const gchar character,
581 ModestTextUtilsForbiddenCharType type);
584 * modest_text_utils_buffer_selection_is_valid:
585 * @buffer: a #GtkTextBuffer
587 * Checks if @buffer contains a valid selection for cut/copy. This means it's
588 * not empty, and no images are in the selection.
590 * Returns: %TRUE if there's a valid selection, false otherwise.
592 gboolean modest_text_utils_buffer_selection_is_valid (GtkTextBuffer *buffer);
595 * modest_text_utils_escape_mnemonics:
598 * obtains the representation of text, but escaping mnemonics (we duplicate _)
600 * Returns: a newly allocated string
602 gchar *modest_text_utils_escape_mnemonics (const gchar *text);
605 * modest_text_utils_simplify_recipients:
606 * @recipients: a list of recipients
608 * returns a list of simplified recipients:
609 * * a@b <a@b> converted to a@b
610 * * NULL converted to ""
612 * It's mainly intended for printing in screen addresses, but it can
613 * also be used for reply/forward.
615 gchar *modest_text_utils_simplify_recipients (const gchar *recipient);
618 * modest_text_utils_remove_duplicate_addresses_list
619 * @address_list: non-NULL #GSList of email addresses
621 * remove duplicate addresses from a list of email addresses
623 * Returns: a list without the duplicate addresses or NULL in case of
624 * error or the original @address_list was NULL
626 GSList *modest_text_utils_remove_duplicate_addresses_list (GSList *address_list);
629 * modest_text_utils_get_secure_header:
630 * @value: the value of a mail header
631 * @header: the header that we're evaluating
633 * This function returns the secure value for a header. Basically it
634 * avoids DoS attacks caused by specially malformed headers like for
635 * example. From:From:From...From: some@mail.com
637 * Returns: returns the secured header
639 gchar * modest_text_utils_get_secure_header (const gchar *value, const gchar *header);
642 * modest_text_utils_quote_names:
643 * @recipients: a list of valid email addresses separated by ',' or ';'
645 * This function quotes the name part of an email address if it's not
646 * quoted and if it exists. For example
647 * aaa@bbb.com -> aaa@bbb.com
648 * "my name" <aaa@bbb.com> -> "my name" <aaa@bbb.com>
649 * my name aaa@bbb.com -> "my name" aaa@bbb.com
651 * It even supports things like
652 * my, name <aaa@bbb.com>, aaa@ccc.com -> "my, name" <aaa@bbb.com>; aaa@ccc.com
654 * Returns: a newly allocated string with the quoted email addresses
656 gchar * modest_text_utils_quote_names (const gchar *recipients);
658 #endif /* __MODEST_TEXT_UTILS_H__ */