Python plugin: Add Folder to MessageInfo objects
[claws.git] / src / common / utils.h
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The code of the g_utf8_substring function below is owned by
19  * Matthias Clasen <matthiasc@src.gnome.org>/<mclasen@redhat.com>
20  * and is got from GLIB 2.30
21  *
22  */
23
24 #ifndef __UTILS_H__
25 #define __UTILS_H__
26
27 #ifdef HAVE_CONFIG_H
28 #include "claws-features.h"
29 #endif
30
31 #ifdef HAVE_BACKTRACE
32 #include <execinfo.h>
33 #endif
34
35 #include <glib.h>
36 #include <glib-object.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <dirent.h>
43 #include <time.h>
44 #if HAVE_ALLOCA_H
45 #  include <alloca.h>
46 #endif
47 #if HAVE_WCHAR_H
48 #  include <wchar.h>
49 #endif
50
51 /* The Hurd doesn't have this limit */
52 #ifndef PATH_MAX
53   #define PATH_MAX 4196
54 #endif
55
56 #ifdef G_OS_WIN32
57
58 #define fsync _commit
59
60 #define pipe(phandles)  _pipe (phandles, 4096, _O_BINARY)
61 #endif
62 /* Wrappers for C library function that take pathname arguments. */
63 #  include <glib/gstdio.h>
64
65 /* why is this sometimes undefined !? */
66 #ifndef G_MAXOFFSET
67 typedef gint64 goffset;
68 #define G_MINOFFSET     G_MININT64
69 #define G_MAXOFFSET     G_MAXINT64
70 #endif
71
72 #ifndef HAVE_U32_TYPEDEF
73   #undef u32        /* maybe there is a macro with this name */
74   typedef guint32 u32;
75   #define HAVE_U32_TYPEDEF
76 #endif
77
78 #if !GLIB_CHECK_VERSION(2, 25, 0)
79 # ifdef G_OS_WIN32
80         typedef _g_stat_struct GStatBuf;
81 # else
82         typedef struct stat GStatBuf;
83 # endif
84 #endif
85
86 #ifndef BIG_ENDIAN_HOST
87   #if (G_BYTE_ORDER == G_BIG_ENDIAN)
88     #define BIG_ENDIAN_HOST 1
89   #endif
90 #endif
91
92 #define CHDIR_RETURN_IF_FAIL(dir) \
93 { \
94         if (change_dir(dir) < 0) return; \
95 }
96
97 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
98 { \
99         if (change_dir(dir) < 0) return val; \
100 }
101
102 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
103 { \
104         if (change_dir(dir) < 0) { \
105                 code \
106                 return val; \
107         } \
108 }
109
110 #define Xalloca(ptr, size, iffail) \
111 { \
112         if ((ptr = alloca(size)) == NULL) { \
113                 g_warning("can't allocate memory\n"); \
114                 iffail; \
115         } \
116 }
117
118 #define Xstrdup_a(ptr, str, iffail) \
119 { \
120         gchar *__tmp; \
121  \
122         if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
123                 g_warning("can't allocate memory\n"); \
124                 iffail; \
125         } else \
126                 strcpy(__tmp, str); \
127  \
128         ptr = __tmp; \
129 }
130
131 #define Xstrndup_a(ptr, str, len, iffail) \
132 { \
133         gchar *__tmp; \
134  \
135         if ((__tmp = alloca(len + 1)) == NULL) { \
136                 g_warning("can't allocate memory\n"); \
137                 iffail; \
138         } else { \
139                 strncpy(__tmp, str, len); \
140                 __tmp[len] = '\0'; \
141         } \
142  \
143         ptr = __tmp; \
144 }
145
146 #define Xstrcat_a(ptr, str1, str2, iffail) \
147 { \
148         gchar *__tmp; \
149         gint len1, len2; \
150  \
151         len1 = strlen(str1); \
152         len2 = strlen(str2); \
153         if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
154                 g_warning("can't allocate memory\n"); \
155                 iffail; \
156         } else { \
157                 memcpy(__tmp, str1, len1); \
158                 memcpy(__tmp + len1, str2, len2 + 1); \
159         } \
160  \
161         ptr = __tmp; \
162 }
163
164 #define AUTORELEASE_STR(str, iffail) \
165 { \
166         gchar *__str; \
167         Xstrdup_a(__str, str, iffail); \
168         g_free(str); \
169         str = __str; \
170 }
171
172 #define FILE_OP_ERROR(file, func) \
173 { \
174         g_printerr("%s: ", file); \
175         fflush(stderr); \
176         perror(func); \
177 }
178
179 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
180
181 /* from NetworkManager */
182 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
183 #define print_backtrace()                                               \
184 G_STMT_START                                                            \
185 {                                                                       \
186         void *_call_stack[512];                                         \
187         int  _call_stack_size;                                          \
188         char **_symbols;                                                \
189         _call_stack_size = backtrace (_call_stack,                      \
190                                       G_N_ELEMENTS (_call_stack));      \
191         _symbols = backtrace_symbols (_call_stack, _call_stack_size);   \
192         if (_symbols != NULL)                                           \
193         {                                                               \
194                 int _i;                                                 \
195                 _i = 0;                                                 \
196                 g_print ("traceback:\n");                               \
197                 while (_i < _call_stack_size)                           \
198                 {                                                       \
199                         g_print ("%d:\t%s\n", _i, _symbols[_i]);        \
200                         _i++;                                           \
201                 }                                                       \
202                 free (_symbols);                                        \
203         }                                                               \
204 }                                                                       \
205 G_STMT_END
206 #else
207 #define print_backtrace()                                               \
208 G_STMT_START                                                            \
209 {                                                                       \
210 }                                                                       \
211 G_STMT_END
212 #endif
213
214
215 #define cm_return_val_if_fail(expr,val) G_STMT_START {                  \
216         if (!(expr)) {                                                  \
217                 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
218                 print_backtrace();                                      \
219                 g_print("\n");                                          \
220                 return val;                                             \
221         }                                                               \
222 } G_STMT_END
223
224 #define cm_return_if_fail(expr) G_STMT_START {                          \
225         if (!(expr)) {                                                  \
226                 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
227                 print_backtrace();                                      \
228                 g_print("\n");                                          \
229                 return;                                                 \
230         }                                                               \
231 } G_STMT_END
232
233 #ifndef MIN
234         #define MIN(a, b) ((a) < (b) ? (a) : (b))
235 #endif
236 #ifndef MAX
237         #define MAX(a, b) ((a) > (b) ? (a) : (b))
238 #endif
239
240 #ifdef __cplusplus
241 extern "C" {
242 #endif
243
244 typedef gpointer (*GNodeMapFunc)        (gpointer nodedata, gpointer data);
245
246 /* debug functions */
247 void debug_set_mode             (gboolean mode);
248 gboolean debug_get_mode         (void);
249
250 #ifndef __CYGWIN__
251 #define debug_print \
252         debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
253         debug_print_real
254 #else
255   /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
256 #define debug_print \
257         debug_print_real("%s:%d:", __FILE__, __LINE__), \
258         debug_print_real
259 #endif
260
261 /* for macro expansion */
262 #define Str(x)  #x
263 #define Xstr(x) Str(x)
264
265
266 /* System related stuff.  */
267
268 gboolean superuser_p (void);
269
270 /* List utilities. */
271
272 GSList *slist_copy_deep         (GSList         *list,
273                                  GCopyFunc       func);
274
275 /* String utilities.  */
276
277 void list_free_strings          (GList          *list);
278 void slist_free_strings         (GSList         *list);
279 void slist_free_strings_full    (GSList         *list);
280
281 void hash_free_strings          (GHashTable     *table);
282
283 gint str_case_equal             (gconstpointer   v,
284                                  gconstpointer   v2);
285 guint str_case_hash             (gconstpointer   key);
286
287 void ptr_array_free_strings     (GPtrArray      *array);
288
289 /* number-string conversion */
290 gint to_number                  (const gchar *nstr);
291 gchar *itos_buf                 (gchar       *nstr,
292                                  gint         n);
293 gchar *itos                     (gint         n);
294 gchar *to_human_readable        (goffset      size);
295
296 /* alternative string functions */
297 gint strcmp2            (const gchar    *s1,
298                          const gchar    *s2);
299 gchar *strstr2          (const gchar    *s1,
300                          const gchar    *s2);
301 gint path_cmp           (const gchar    *s1,
302                          const gchar    *s2);
303 gchar *strretchomp      (gchar          *str);
304 gchar *strtailchomp     (gchar          *str,
305                          gchar           tail_char);
306 gchar *strcrchomp       (gchar          *str);
307 gint file_strip_crs     (const gchar    *file);
308 gchar *strcasestr       (const gchar    *haystack,
309                          const gchar    *needle);
310 gchar *strncasestr      (const gchar    *haystack,
311                          gint            haystack_len,
312                          const gchar    *needle);
313 gpointer my_memmem      (gconstpointer   haystack,
314                          size_t          haystacklen,
315                          gconstpointer   needle,
316                          size_t          needlelen);
317 gchar *strncpy2         (gchar          *dest,
318                          const gchar    *src,
319                          size_t          n);
320
321 gboolean is_next_nonascii       (const gchar *s);
322 gint get_next_word_len          (const gchar *s);
323
324 /* functions for string parsing */
325 gint subject_compare                    (const gchar    *s1,
326                                          const gchar    *s2);
327 gint subject_compare_for_sort           (const gchar    *s1,
328                                          const gchar    *s2);
329 void trim_subject                       (gchar          *str);
330 void eliminate_parenthesis              (gchar          *str,
331                                          gchar           op,
332                                          gchar           cl);
333 void extract_parenthesis                (gchar          *str,
334                                          gchar           op,
335                                          gchar           cl);
336
337 void extract_quote                      (gchar          *str,
338                                          gchar           quote_chr);
339 gchar *escape_internal_quotes           (gchar          *str,
340                                          gchar           quote_chr);
341 void eliminate_address_comment          (gchar          *str);
342 gchar *strchr_with_skip_quote           (const gchar    *str,
343                                          gint            quote_chr,
344                                          gint            c);
345 void extract_address                    (gchar          *str);
346 void extract_list_id_str                (gchar          *str);
347
348 GSList *address_list_append             (GSList         *addr_list,
349                                          const gchar    *str);
350 GSList *address_list_append_with_comments(GSList        *addr_list,
351                                          const gchar    *str);
352 GSList *references_list_prepend         (GSList         *msgid_list,
353                                          const gchar    *str);
354 GSList *references_list_append          (GSList         *msgid_list,
355                                          const gchar    *str);
356 GSList *newsgroup_list_append           (GSList         *group_list,
357                                          const gchar    *str);
358
359 GList *add_history                      (GList          *list,
360                                          const gchar    *str);
361
362 void remove_return                      (gchar          *str);
363 void remove_space                       (gchar          *str);
364 void unfold_line                        (gchar          *str);
365 void subst_char                         (gchar          *str,
366                                          gchar           orig,
367                                          gchar           subst);
368 void subst_chars                        (gchar          *str,   
369                                          gchar          *orig, 
370                                          gchar          subst);
371 void subst_for_filename                 (gchar          *str);
372 void subst_for_shellsafe_filename       (gchar          *str);
373 gboolean is_ascii_str                   (const gchar    *str);
374 gint get_quote_level                    (const gchar    *str,
375                                          const gchar    *quote_chars);
376 gint check_line_length                  (const gchar    *str,
377                                          gint            max_chars,
378                                          gint           *line);
379
380 gchar **strsplit_with_quote             (const gchar    *str,
381                                          const gchar    *delim,
382                                          gint            max_tokens);
383
384 gchar *get_abbrev_newsgroup_name        (const gchar    *group,
385                                          gint            len);
386 gchar *trim_string                      (const gchar    *str,
387                                          gint            len);
388
389 GList *uri_list_extract_filenames       (const gchar    *uri_list);
390 gboolean is_uri_string                  (const gchar    *str);
391 gchar *get_uri_path                     (const gchar    *uri);
392 gint get_uri_len                        (const gchar    *str);
393 void decode_uri                         (gchar          *decoded_uri,
394                                          const gchar    *encoded_uri);
395 void decode_uri_with_plus               (gchar          *decoded_uri, 
396                                          const gchar    *encoded_uri, 
397                                          gboolean        with_plus);
398 gint scan_mailto_url                    (const gchar    *mailto,
399                                          gchar         **from,
400                                          gchar         **to,
401                                          gchar         **cc,
402                                          gchar         **bcc,
403                                          gchar         **subject,
404                                          gchar         **body,
405                                          gchar         ***attach,
406                                          gchar         **inreplyto);
407
408 /* return static strings */
409 const gchar *get_home_dir               (void);
410 const gchar *get_rc_dir                 (void);
411 void  set_rc_dir                        (const gchar *dir);
412 gboolean rc_dir_is_alt                  (void);
413 const gchar *get_mail_base_dir          (void);
414 const gchar *get_news_cache_dir         (void);
415 const gchar *get_imap_cache_dir         (void);
416 const gchar *get_mime_tmp_dir           (void);
417 const gchar *get_template_dir           (void);
418 const gchar *get_plugin_dir             (void);
419 const gchar *get_tmp_dir                (void);
420 const gchar *get_locale_dir             (void);
421 gchar *get_tmp_file                     (void);
422 const gchar *get_domain_name            (void);
423 const gchar *get_desktop_file(void);
424 #ifdef G_OS_WIN32
425 const gchar *get_themes_dir             (void);
426 const gchar *get_cert_file              (void);
427 #endif
428 /* file / directory handling */
429 off_t get_file_size             (const gchar    *file);
430 off_t get_file_size_as_crlf     (const gchar    *file);
431
432 time_t get_file_mtime           (const gchar *file);
433
434 gboolean file_exist             (const gchar    *file,
435                                  gboolean        allow_fifo);
436 gboolean is_relative_filename   (const gchar *file);
437 gboolean is_dir_exist           (const gchar    *dir);
438 gboolean is_file_entry_exist    (const gchar    *file);
439 gboolean dirent_is_regular_file (struct dirent  *d);
440
441 #define is_file_exist(file)             file_exist(file, FALSE)
442 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
443
444 gint change_dir                 (const gchar    *dir);
445 gint make_dir                   (const gchar    *dir);
446 gint make_dir_hier              (const gchar    *dir);
447 gint remove_all_files           (const gchar    *dir);
448 gint remove_numbered_files      (const gchar    *dir,
449                                  guint           first,
450                                  guint           last);
451 gint remove_numbered_files_not_in_list(const gchar *dir,
452                                        GSList *numberlist);
453 gint remove_all_numbered_files  (const gchar    *dir);
454 gint remove_dir_recursive       (const gchar    *dir);
455 gint append_file                (const gchar    *src,
456                                  const gchar    *dest,
457                                  gboolean        keep_backup);
458 gint rename_force               (const gchar    *oldpath,
459                                  const gchar    *newpath);
460 gint copy_file                  (const gchar    *src,
461                                  const gchar    *dest,
462                                  gboolean        keep_backup);
463 gint move_file                  (const gchar    *src,
464                                  const gchar    *dest,
465                                  gboolean        overwrite);
466 gint copy_dir                   (const gchar    *src,
467                                  const gchar    *dest);
468 gint copy_file_part_to_fp       (FILE           *fp,
469                                  off_t           offset,
470                                  size_t          length,
471                                  FILE           *dest_fp);
472 gint copy_file_part             (FILE           *fp,
473                                  off_t           offset,
474                                  size_t          length,
475                                  const gchar    *dest);
476
477 gchar *canonicalize_str         (const gchar    *str);
478 gint canonicalize_file          (const gchar    *src,
479                                  const gchar    *dest);
480 gint canonicalize_file_replace  (const gchar    *file);
481
482 gchar *normalize_newlines       (const gchar    *str);
483
484 gchar *get_outgoing_rfc2822_str (FILE           *fp);
485
486 gint change_file_mode_rw        (FILE           *fp,
487                                  const gchar    *file);
488 FILE *my_tmpfile                (void);
489 FILE *get_tmpfile_in_dir        (const gchar    *dir,
490                                  gchar         **filename);
491 FILE *str_open_as_stream        (const gchar    *str);
492 gint str_write_to_file          (const gchar    *str,
493                                  const gchar    *file);
494 gchar *file_read_to_str         (const gchar    *file);
495 gchar *file_read_stream_to_str  (FILE           *fp);
496 gchar *file_read_to_str_no_recode(const gchar *file);
497 gchar *file_read_stream_to_str_no_recode(FILE *fp);
498
499 char *fgets_crlf(char *buf, int size, FILE *stream);
500
501 /* process execution */
502 gint execute_command_line       (const gchar    *cmdline,
503                                  gboolean        async);
504 gchar *get_command_output       (const gchar    *cmdline);
505
506 /* open URI with external browser */
507 gint open_uri(const gchar *uri, const gchar *cmdline);
508 /* open file with text editor */
509 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
510
511 /* time functions */
512 time_t remote_tzoffset_sec      (const gchar    *zone);
513 time_t tzoffset_sec             (time_t         *now);
514 gchar *tzoffset                 (time_t         *now);
515 void get_rfc822_date            (gchar          *buf,
516                                  gint            len);
517
518 size_t fast_strftime            (gchar                  *buf, 
519                                  gint                    buflen, 
520                                  const gchar            *format, 
521                                  struct tm              *lt);
522
523 /* debugging */
524 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
525 const char * debug_srcname (const char *file);
526
527 /* subject threading */
528 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
529 void subject_table_insert(GHashTable *subject_table, gchar * subject,
530                           void * data);
531 void subject_table_remove(GHashTable *subject_table, gchar * subject);
532 void utils_free_regex(void);
533 gint subject_get_prefix_length (const gchar *subject);
534
535 /* quoting recognition */
536 const gchar * line_has_quote_char       (const gchar *str,
537                                          const gchar *quote_chars);
538
539 gint g_int_compare      (gconstpointer a, gconstpointer b);
540
541 gchar *generate_msgid           (gchar *buf, gint len, gchar *user_addr);
542 gchar *generate_mime_boundary   (const gchar *prefix);
543
544 gint quote_cmd_argument(gchar * result, guint size,
545                         const gchar * path);
546 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
547
548 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
549 void get_hex_str(gchar *out, guchar ch);
550
551 /* auto pointer for containers that support GType system */
552
553 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
554 typedef struct AutoPointer      GAuto;
555 GType g_auto_pointer_register           (void);
556 GAuto *g_auto_pointer_new               (gpointer pointer);
557 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
558                                          GFreeFunc free);
559 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
560 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
561 void g_auto_pointer_free                (GAuto *auto_ptr);
562 void replace_returns                    (gchar *str);
563 gboolean get_uri_part   (const gchar *start,
564                          const gchar *scanpos,
565                          const gchar **bp,
566                          const gchar **ep,
567                          gboolean hdr);
568 gchar *make_uri_string  (const gchar *bp,
569                          const gchar *ep);
570 gboolean get_email_part (const gchar *start, 
571                          const gchar *scanpos,
572                          const gchar **bp, 
573                          const gchar **ep,
574                          gboolean hdr);
575 gchar *make_email_string(const gchar *bp,
576                          const gchar *ep);
577 gchar *make_http_string (const gchar *bp,
578                          const gchar *ep);
579
580 gchar *mailcap_get_command_for_type(const gchar *type, 
581                                     const gchar *file_to_open);
582 void mailcap_update_default        (const gchar *type,
583                                     const gchar *command);
584
585 gboolean file_is_email(const gchar *filename);
586 gboolean sc_g_list_bigger(GList *list, gint max);
587 gboolean sc_g_slist_bigger(GSList *list, gint max);
588
589 int claws_unlink(const gchar *filename);
590
591 GMutex *cm_mutex_new(void);
592 void cm_mutex_free(GMutex *mutex);
593
594 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
595
596 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
597
598 #if !GLIB_CHECK_VERSION(2, 30, 0)
599 gchar   *g_utf8_substring         (const gchar *p,
600                                    glong        start_pos,
601                                    glong        end_pos) G_GNUC_MALLOC;
602 #endif
603
604 #ifdef __cplusplus
605 }
606 #endif
607
608 #endif /* __UTILS_H__ */