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