* Fixes NB#76991, the send/receive refreshes the current folder as well
[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
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
46 /* Forbidden char arrays */
47 extern const gchar account_title_forbidden_chars[];
48 extern const gchar folder_name_forbidden_chars[];
49 extern const gchar user_name_forbidden_chars[];
50 extern const guint ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH;
51 extern const guint FOLDER_NAME_FORBIDDEN_CHARS_LENGTH;
52 extern const guint USER_NAME_FORBIDDEN_CHARS_LENGTH;
53
54 /**
55  * modest_text_utils_derived_subject:
56  * @subject: a string which contains the original subject
57  * @prefix: the prefix for the new subject (such as 'Re:' or 'Fwd:'),
58  *           must not be NULL
59  *
60  * create a 'derived' subject line for eg. replies and forwards 
61  * 
62  * Returns: a newly allocated string containing the resulting subject
63  * subject == NULL, then @prefix " " will be returned
64  */
65 gchar* modest_text_utils_derived_subject (const gchar *subject, 
66                                           const gchar* prefix);
67
68
69 /**
70  * modest_text_utils_quote:
71  * @text: a non-NULL string which contains the message to quote
72  * @from: a non-NULL  sender of the original message
73  * @content_type: the non-NULL content type for the quoting, e.g. "text/html"
74  * @signature: NULL or the signature to add
75  * @sent_date: sent date/time of the original message
76  * @attachments: a #GList of the attachments
77  * @limit: specifies the maximum characters per line in the quoted text
78  * 
79  * quote an existing message
80  * 
81  * Returns: a newly allocated string containing the quoted message
82  */
83 gchar* modest_text_utils_quote (const gchar *text, 
84                                 const gchar *content_type,
85                                 const gchar *signature,
86                                 const gchar *from,
87                                 const time_t sent_date, 
88                                 GList *attachments,
89                                 int limit);
90
91
92 /**
93  * modest_text_utils_cited_text:
94  * @from: sender of the message
95  * @sent_date: the sent date of the original message
96  * @text: the text of the original message
97  *
98  * cite the text in a message
99  * 
100  * Returns: a newly allocated string containing the cited text
101  */
102 gchar* modest_text_utils_cite (const gchar *text,
103                                const gchar *content_type,
104                                const gchar *signature,
105                                const gchar *from,
106                                time_t sent_date);
107
108 /**
109  * modest_text_utils_inlined_text
110  * @from: the non-NULL sender of the original message
111  * @sent_date: sent date/time of the original message
112  * @to: 
113  * @subject: 
114  * @text: 
115  *
116  * creates a new string with the "Original message" text prepended to
117  * the text passed as argument and some data of the header
118  * 
119  * Returns: a newly allocated string containing the quoted message
120  */
121 gchar*   modest_text_utils_inline (const gchar *text,
122                                    const gchar *content_type,
123                                    const gchar *signature,
124                                    const gchar *from,
125                                    time_t sent_date,
126                                    const gchar *to,
127                                    const gchar *subject);
128
129 /**
130  * modest_text_utils_remove_address
131  * @address_list: none-NULL string with a comma-separated list of email addresses
132  * @address: an specific e-mail address 
133  *
134  * remove a specific address from a list of email addresses; if @address
135  * is NULL, returns an unchanged @address_list
136  * 
137  * Returns: a newly allocated string containing the new list, or NULL
138  * in case of error or the original @address_list was NULL
139  */
140 gchar*   modest_text_utils_remove_address (const gchar *address_list, 
141                                            const gchar *address);
142
143 /**
144  * modest_text_utils_address_range_at_position:
145  * @address_list: non-NULL utf8 string containing a list of addresses
146  * @position: a gint
147  * @start: a gint pointer
148  * @end: a gint pointer
149  *
150  * Finds the start and end positions of the address at @position,
151  * in @recipients_list, a list of addresses in the format of a 
152  * recipient list in email. It stores the results in @start and
153  * @end
154  */
155 void     modest_text_utils_address_range_at_position (const gchar *recipients_list,
156                                                       guint position,
157                                                       guint *start,
158                                                       guint *end);
159
160 /**
161  * modest_text_utils_hyperlinkify_begin:
162  *
163  * begin a linkify block, compiling the caches to be reused.
164  */
165 void modest_text_utils_hyperlinkify_begin (void);
166
167 /**
168  * modest_text_utils_hyperlinkify_end:
169  *
170  * end a linkify block, freeing the caches to be reused.
171  */
172 void modest_text_utils_hyperlinkify_end (void);
173
174 /**
175  * modest_text_utils_convert_to_html:
176  * @txt: a string
177  *
178  * convert plain text (utf8) into html
179  * 
180  * Returns: a newly allocated string containing the html
181  */
182 gchar*  modest_text_utils_convert_to_html (const gchar *txt);
183
184 /**
185  * modest_text_utils_convert_to_html_body:
186  * @txt: a string
187  *
188  * convert plain text (utf8) into html without adding html headers.
189  * 
190  * Returns: a newly allocated string containing the html
191  */
192 gchar*  modest_text_utils_convert_to_html_body (const gchar *data, gssize n, gboolean hyperlinkify);
193
194
195 /**
196  * modest_text_utils_strftime:
197  * @s:
198  * @max:
199  * @fmt:
200  * @timet:
201  *
202  * this is just an alias for strftime(3), so we can use that without
203  * getting warning from gcc
204  * 
205  * Returns: a formatted string of max length @max in @s
206  */
207 size_t modest_text_utils_strftime(char *s, size_t max, const char  *fmt, time_t timet);
208
209
210
211 /**
212  * modest_text_utils_get_display_address:
213  * @address: original address (UTF8 string)
214  *
215  * make a 'display address' from an address:
216  * "Foo Bar &lt;foo@bar.cx&gt;" --&gt; "Foo Bar"
217  * ie. removes "&lt;...&gt;" parts
218  * the change is in-place; removes leading whitespace
219  * 
220  * NOTE: for optimization reasons, this function changes @address
221  * in-place
222  */
223 void modest_text_utils_get_display_address (gchar *address);
224
225 /**
226  * modest_text_utils_get_email_address:
227  * @full_address: original address (UTF8 string)
228  *
229  * make a 'foo@bar.cx' from an address:
230  * "Foo Bar <foo@bar.cx> (Bla)" --> "foo@bar.cx"
231  * If no "<...>" is found, then it returns the full
232  * strings.
233  * 
234  * Returns: a newly allocated string with the copy.
235  * 
236  * NULL in case of error or if address == NULL
237  */
238 gchar* modest_text_utils_get_email_address (const gchar *email_address);
239
240
241 /**
242  * modest_text_utils_get_subject_prefix_len:
243  * @subject: original subject (UTF8 string)
244  *
245  * determine the length of the "Re:/RE:/Fwd:" prefix in an e-mail address
246  * 
247  * Returns: the length of the  prefix, or 0 if there is none
248  */
249 gint modest_text_utils_get_subject_prefix_len (const gchar *subject);
250
251
252 /**
253  * modest_text_utils_utf8_strcmp:
254  * @s1: the first string
255  * @s2: the second string
256  * @insensitive: should the comparison be case-insensitive?
257  *
258  * a strcmp that is NULL-safe, can deal with UTF8 and case-insensitive comparison 
259  *
260  * Returns: an integer less than, equal to, or greater than zero if s1 is found,
261  * respectively, to be less than, to match, or be greater than s2.
262  */
263 gint modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive);
264
265
266
267 /**
268  * modest_text_utils_get_display_date:
269  * @date: the date to display
270  *
271  * get a string representation for a date.
272  * 
273  * Returns: the new display date, as a *static* string.
274  * This string should not be modified, and will change
275  * upon recalling this function. g_strdup it if you to
276  * do so.
277  * 
278  */
279 const gchar* modest_text_utils_get_display_date (time_t date);
280
281
282 /**
283  * modest_text_utils_get_display_size:
284  * @size: size in bytes
285  *
286  * get a string representation for a size in bytes.
287  * 
288  * Returns: the newly allocated display string for the
289  * size in bytes. must be freed.
290  */
291 gchar * modest_text_utils_get_display_size (guint64 size);
292
293
294
295 /**
296  * modest_text_utils_validate_domain_name:
297  * @email_address: a NULL-terminated string
298  * 
299  * validates the domain name passed as argument
300  * 
301  * Returns: TRUE if the domain name is valid, FALSE otherwise
302  **/
303 gboolean modest_text_utils_validate_domain_name (const gchar *domain);
304
305 /**
306  * modest_text_utils_validate_email_address:
307  * @email_address: a string
308  * @invalid_char_position: pointer to the position of the invalid
309  * character in case validation failed because of this, or %NULL.
310  * 
311  * validates the email address passed as argument
312  * 
313  * Returns: TRUE if the address is valid, FALSE otherwise
314  **/
315 gboolean     modest_text_utils_validate_email_address (const gchar *email_address, 
316                                                        const gchar **invalid_char_position);
317
318
319 /**
320  * modest_text_utils_validate_folder_name:
321  * @folder_name: a string
322  * 
323  * validates the folder name passed as argument. a 'valid folder name'
324  * is a name which should be valid on both Unix and Windows file systems.
325  * of course, this might be stricter than strictly needed in some cases,
326  * but it's better to err on the safe side.
327  * 
328  * Returns: TRUE if the folder name is valid, FALSE otherwise
329  **/
330 gboolean modest_text_utils_validate_folder_name (const gchar *folder_name);
331
332 /**
333  * modest_text_utils_validate_recipient:
334  * @recipient: a string
335  * @invalid_char_position: pointer to the position of the invalid char,
336  * if validation failed because there's an invalid char there, or %NULL.
337  *
338  * validates @recipient as a valid recipient field for header.
339  * It's different from modest_text_utils_validate_email_address()
340  * as it validates a whole recipient, and not only the part between
341  * the &lt; and &gt; symbols.
342  *
343  * Returns: %TRUE if the recipient is valid, FALSE otherwise
344  **/
345 gboolean     modest_text_utils_validate_recipient (const gchar *recipient,
346                                                    const gchar **invalid_char_position);
347
348 /**
349  * modest_text_utils_split_addresses_list:
350  * @addresses: a string
351  *
352  * obtains a GSList of addresses from a string of addresses
353  * in the format understood by email protocols
354  *
355  * Returns: a GSList of strings
356  **/
357 GSList      *modest_text_utils_split_addresses_list (const gchar *addresses);
358
359 /**
360  * modest_text_utils_get_addresses_indexes:
361  * @addresses: a string
362  * @start_indexes: a #GSList pointer
363  * @end_indexes: a #GSList pointer
364  *
365  * obtains two #GSList of @addresses with the range offsets of the addresses in
366  * the string
367  *
368  * Returns: a GSList of strings
369  **/
370 void         modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes);
371
372 /**
373  * modest_text_utils_get_color_string:
374  * @color: a #GdkColor
375  *
376  * Obtains a proper markup string for @color, in the format used
377  * by Pango and HTML.
378  *
379  * Returns: a newly allocated string
380  */
381 gchar *      modest_text_utils_get_color_string (GdkColor *color);
382
383 /**
384  * modest_text_utils_text_buffer_get_text:
385  * @buffer: a #GtkTextBuffer
386  *
387  * Obtains the contents of a @buffer in a string, replacing image
388  * pixbufs with blank spaces.
389  *
390  * Returns: a newly allocated UTF-8 string
391  */
392 gchar *      modest_text_utils_text_buffer_get_text (GtkTextBuffer *buffer);
393
394 typedef enum {
395         ACCOUNT_TITLE_FORBIDDEN_CHARS,
396         FOLDER_NAME_FORBIDDEN_CHARS,
397         USER_NAME_FORBIDDEN_NAMES,
398 } ModestTextUtilsForbiddenCharType;
399
400
401 /**
402  * modest_text_utils_is_forbidden_char:
403  * @character: some character
404  * @type: the type of forbidden char (see #ModestTextUtilsForbiddenCharType)
405  * 
406  * check whether the given character is 'forbidden'
407  *
408  * Returns: TRUE if it's forbidden, FALSE otherwise
409  */
410 gboolean     modest_text_utils_is_forbidden_char (const gchar character,
411                                                   ModestTextUtilsForbiddenCharType type);
412
413
414 #endif /* __MODEST_TEXT_UTILS_H__ */