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