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