* src/modest-text-utils.[ch]:
[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
44 /**
45  * modest_text_utils_derived_subject:
46  * @subject: a string which contains the original subject
47  * @prefix: the prefix for the new subject (such as 'Re:' or 'Fwd:'),
48  *           must not be NULL
49  *
50  * create a 'derived' subject line for eg. replies and forwards 
51  * 
52  * Returns: a newly allocated string containing the resulting subject
53  * subject == NULL, then @prefix " " will be returned
54  */
55 gchar* modest_text_utils_derived_subject (const gchar *subject, 
56                                           const gchar* prefix);
57
58
59 /**
60  * modest_text_utils_quote:
61  * @text: a non-NULL string which contains the message to quote
62  * @from: a non-NULL  sender of the original message
63  * @content_type: the non-NULL content type for the quoting, e.g. "text/html"
64  * @signature: NULL or the signature to add
65  * @sent_date: sent date/time of the original message
66  * @attachments: a #GList of the attachments
67  * @limit: specifies the maximum characters per line in the quoted text
68  * 
69  * quote an existing message
70  * 
71  * Returns: a newly allocated string containing the quoted message
72  */
73 gchar* modest_text_utils_quote (const gchar *text, 
74                                 const gchar *content_type,
75                                 const gchar *signature,
76                                 const gchar *from,
77                                 const time_t sent_date, 
78                                 GList *attachments,
79                                 int limit);
80
81
82 /**
83  * modest_text_utils_cited_text:
84  * @from: sender of the message
85  * @sent_date: the sent date of the original message
86  * @text: the text of the original message
87  *
88  * cite the text in a message
89  * 
90  * Returns: a newly allocated string containing the cited text
91  */
92 gchar* modest_text_utils_cite (const gchar *text,
93                                const gchar *content_type,
94                                const gchar *signature,
95                                const gchar *from,
96                                time_t sent_date);
97
98 /**
99  * modest_text_utils_inlined_text
100  * @from: the non-NULL sender of the original message
101  * @sent_date: sent date/time of the original message
102  * @to: 
103  * @subject: 
104  * @text: 
105  *
106  * creates a new string with the "Original message" text prepended to
107  * the text passed as argument and some data of the header
108  * 
109  * Returns: a newly allocated string containing the quoted message
110  */
111 gchar*   modest_text_utils_inline (const gchar *text,
112                                    const gchar *content_type,
113                                    const gchar *signature,
114                                    const gchar *from,
115                                    time_t sent_date,
116                                    const gchar *to,
117                                    const gchar *subject);
118
119 /**
120  * modest_text_utils_remove_address
121  * @address_list: none-NULL string with a comma-separated list of email addresses
122  * @address: an specific e-mail address 
123  *
124  * remove a specific address from a list of email addresses; if @address
125  * is NULL, returns an unchanged @address_list
126  * 
127  * Returns: a newly allocated string containing the new list, or NULL
128  * in case of error or the original @address_list was NULL
129  */
130 gchar*   modest_text_utils_remove_address (const gchar *address_list, 
131                                            const gchar *address);
132
133 /**
134  * modest_text_utils_address_range_at_position:
135  * @address_list: utf8 string containing a list of addresses
136  * @position: a gint
137  * @start: a gint pointer
138  * @end: a gint pointer
139  *
140  * Finds the start and end positions of the address at @position,
141  * in @recipients_list, a list of addresses in the format of a 
142  * recipient list in email. It stores the results in @start and
143  * @end
144  */
145 void     modest_text_utils_address_range_at_position (const gchar *recipients_list,
146                                                       gint position,
147                                                       gint *start,
148                                                       gint *end);
149                                                       
150
151 /**
152  * modest_text_utils_convert_to_html:
153  * @txt: a string
154  *
155  * convert plain text (utf8) into html
156  * 
157  * Returns: a newly allocated string containing the html
158  */
159 gchar*  modest_text_utils_convert_to_html (const gchar *txt);
160
161 /**
162  * modest_text_utils_convert_to_html_body:
163  * @txt: a string
164  *
165  * convert plain text (utf8) into html without adding html headers.
166  * 
167  * Returns: a newly allocated string containing the html
168  */
169 gchar*  modest_text_utils_convert_to_html_body (const gchar *data);
170
171
172 /**
173  * modest_text_utils_strftime:
174  * @s:
175  * @max:
176  * @fmt:
177  * @timet:
178  *
179  * this is just an alias for strftime(3), so we can use that without
180  * getting warning from gcc
181  * 
182  * Returns: a formatted string of max length @max in @s
183  */
184 size_t modest_text_utils_strftime(char *s, size_t max, const char  *fmt, time_t timet);
185
186
187
188 /**
189  * modest_text_utils_get_display_address:
190  * @address: original address (UTF8 string)
191  *
192  * make a 'display address' from an address:
193  * "Foo Bar <foo@bar.cx> (Bla)" --> "Foo Bar"
194  * ie. removes "<...>" and "(...)" parts
195  * the change is in-place; removes leading/trailing whitespace
196  * 
197  * Returns: the new address of the string; this new string
198  * is _NOT_ newly allocated, so should not be freed. (remember
199  * the old address of the parameter if that one needs to be freed)
200  * 
201  * NULL in case of error or if address == NULL
202  */
203 gchar* modest_text_utils_get_display_address (gchar *address);
204
205 /**
206  * modest_text_utils_get_email_address:
207  * @full_address: original address (UTF8 string)
208  *
209  * make a 'foo@bar.cx' from an address:
210  * "Foo Bar <foo@bar.cx> (Bla)" --> "foo@bar.cx"
211  * If no "<...>" is found, then it returns the full
212  * strings.
213  * 
214  * Returns: a newly allocated string with the copy.
215  * 
216  * NULL in case of error or if address == NULL
217  */
218 gchar* modest_text_utils_get_email_address (const gchar *email_address);
219
220
221 /**
222  * modest_text_utils_get_subject_prefix_len:
223  * @subject: original subject (UTF8 string)
224  *
225  * determine the length of the "Re:/RE:/Fwd:" prefix in an e-mail address
226  * 
227  * Returns: the length of the  prefix, or 0 if there is none
228  */
229 gint modest_text_utils_get_subject_prefix_len (const gchar *subject);
230
231
232 /**
233  * modest_text_utils_utf8_strcmp:
234  * @s1: the first string
235  * @s2: the second string
236  * @insensitive: should the comparison be case-insensitive?
237  *
238  * a strcmp that is NULL-safe, can deal with UTF8 and case-insensitive comparison 
239  *
240  * Returns: an integer less than, equal to, or greater than zero if s1 is found,
241  * respectively, to be less than, to match, or be greater than s2.
242  */
243 gint modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive);
244
245
246
247 /**
248  * modest_text_utils_get_display_date:
249  * @date: the date to display
250  *
251  * get a string representation for a date.
252  * 
253  * Returns: the new display date, as a newly allocated string;
254  * free with g_free
255  */
256 gchar* modest_text_utils_get_display_date (time_t date);
257
258
259 /**
260  * modest_text_utils_get_display_size:
261  * @size: size in bytes
262  *
263  * get a string representation for a size in bytes.
264  * 
265  * Returns: the newly allocated display string for the
266  * size in bytes. must be freed.
267  */
268 gchar * modest_text_utils_get_display_size (guint64 size);
269
270
271 /**
272  * modest_text_utils_validate_email_address:
273  * @email_address: a string
274  * @invalid_char_position: pointer to the position of the invalid
275  * character in case validation failed because of this, or %NULL.
276  * 
277  * validates the email address passed as argument
278  * 
279  * Returns: TRUE if the address is valid, FALSE otherwise
280  **/
281 gboolean     modest_text_utils_validate_email_address (const gchar *email_address, 
282                                                        const gchar **invalid_char_position);
283
284 /**
285  * modest_text_utils_validate_recipient:
286  * @recipient: a string
287  * @invalid_char_position: pointer to the position of the invalid char,
288  * if validation failed because there's an invalid char there, or %NULL.
289  *
290  * validates @recipient as a valid recipient field for header.
291  * It's different from modest_text_utils_validate_email_address()
292  * as it validates a whole recipient, and not only the part between
293  * the &lt; and &gt; symbols.
294  *
295  * Returns: %TRUE if the recipient is valid, FALSE otherwise
296  **/
297 gboolean     modest_text_utils_validate_recipient (const gchar *recipient,
298                                                    const gchar **invalid_char_position);
299
300 /**
301  * modest_text_utils_split_addresses_list:
302  * @addresses: a string
303  *
304  * obtains a GSList of addresses from a string of addresses
305  * in the format understood by email protocols
306  *
307  * Returns: a GSList of strings
308  **/
309 GSList      *modest_text_utils_split_addresses_list (const gchar *addresses);
310
311 /**
312  * modest_text_utils_get_addresses_indexes:
313  * @addresses: a string
314  * @start_indexes: a #GSList pointer
315  * @end_indexes: a #GSList pointer
316  *
317  * obtains two #GSList of @addresses with the range offsets of the addresses in
318  * the string
319  *
320  * Returns: a GSList of strings
321  **/
322 void         modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes);
323
324 /**
325  * modest_text_utils_get_color_string:
326  * @color: a #GdkColor
327  *
328  * Obtains a proper markup string for @color, in the format used
329  * by Pango and HTML.
330  *
331  * Returns: a newly allocated string
332  */
333 gchar *      modest_text_utils_get_color_string (GdkColor *color);
334
335 /**
336  * modest_text_utils_text_buffer_get_text:
337  * @buffer: a #GtkTextBuffer
338  *
339  * Obtains the contents of a @buffer in a string, replacing image
340  * pixbufs with blank spaces.
341  *
342  * Returns: a newly allocated UTF-8 string
343  */
344 gchar *      modest_text_utils_text_buffer_get_text (GtkTextBuffer *buffer);
345
346 #endif /* __MODEST_TEXT_UTILS_H__ */