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