1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU Lesser General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
19 * Authors: Rodrigo Moya <rodrigo@ximian.com>
33 #include <glib-object.h>
35 #include "e-data-server-util.h"
38 * e_get_user_cache_dir:
40 * Returns a base directory in which to store user-specific,
41 * non-essential cached data for Evolution or Evolution-Data-Server.
43 * The returned string is owned by libedataserver and must not be
46 * Returns: base directory for user-specific, non-essential data
51 e_get_user_cache_dir (void)
53 static gchar *dirname = NULL;
55 if (G_UNLIKELY (dirname == NULL)) {
56 const gchar *cache_dir = g_get_user_cache_dir ();
57 dirname = g_build_filename (cache_dir, "evolution", NULL);
58 g_mkdir_with_parents (dirname, 0700);
65 * e_get_user_config_dir:
67 * Returns a base directory in which to store user-specific configuration
68 * information for Evolution or Evolution-Data-Server.
70 * The returned string is owned by libedataserver and must not be
73 * Returns: base directory for user-specific configuration information
78 e_get_user_config_dir (void)
80 static gchar *dirname = NULL;
82 if (G_UNLIKELY (dirname == NULL)) {
83 const gchar *config_dir = g_get_user_config_dir ();
84 dirname = g_build_filename (config_dir, "evolution", NULL);
85 g_mkdir_with_parents (dirname, 0700);
92 * e_get_user_data_dir:
94 * Returns a base directory in which to store user-specific data for
95 * Evolution or Evolution-Data-Server.
97 * The returned string is owned by libedataserver and must not be
100 * Returns: base directory for user-specific data
105 e_get_user_data_dir (void)
107 static gchar *dirname = NULL;
109 if (G_UNLIKELY (dirname == NULL)) {
110 const gchar *data_dir = g_get_user_data_dir ();
111 dirname = g_build_filename (data_dir, "evolution", NULL);
112 g_mkdir_with_parents (dirname, 0700);
119 * e_util_strdup_strip:
120 * @string: (allow-none): a string value, or %NULL
122 * Duplicates @string and strips off any leading or trailing whitespace.
123 * The resulting string is returned unless it is empty or %NULL, in which
124 * case the function returns %NULL.
126 * Free the returned string with g_free().
128 * Returns: a newly-allocated, stripped copy of @string, or %NULL
133 e_util_strdup_strip (const gchar *string)
137 duplicate = g_strdup (string);
138 if (duplicate != NULL) {
139 g_strstrip (duplicate);
140 if (*duplicate == '\0') {
151 * @haystack: The string to search in.
152 * @needle: The string to search for.
154 * Find the first instance of @needle in @haystack, ignoring case for
155 * bytes that are ASCII characters.
157 * Returns: A pointer to the start of @needle in @haystack, or NULL if
158 * @needle is not found.
161 e_util_strstrcase (const gchar *haystack,
164 /* find the needle in the haystack neglecting case */
168 g_return_val_if_fail (haystack != NULL, NULL);
169 g_return_val_if_fail (needle != NULL, NULL);
171 len = strlen (needle);
172 if (len > strlen (haystack))
176 return (gchar *) haystack;
178 for (ptr = haystack; *(ptr + len - 1) != '\0'; ptr++)
179 if (!g_ascii_strncasecmp (ptr, needle, len))
180 return (gchar *) ptr;
186 * e_util_unicode_get_utf8:
187 * @text: The string to take the UTF-8 character from.
188 * @out: The location to store the UTF-8 character in.
190 * Get a UTF-8 character from the beginning of @text.
192 * Returns: A pointer to the next character in @text after @out.
195 e_util_unicode_get_utf8 (const gchar *text,
198 g_return_val_if_fail (text != NULL, NULL);
199 g_return_val_if_fail (out != NULL, NULL);
201 *out = g_utf8_get_char (text);
202 return (*out == (gunichar) -1) ? NULL : g_utf8_next_char (text);
206 * e_util_utf8_strstrcase:
207 * @haystack: The string to search in.
208 * @needle: The string to search for.
210 * Find the first instance of @needle in @haystack, ignoring case. (No
211 * proper case folding or decomposing is done.) Both @needle and
212 * @haystack are UTF-8 strings.
214 * Returns: A pointer to the first instance of @needle in @haystack, or
215 * %NULL if no match is found, or if either of the strings are
216 * not legal UTF-8 strings.
219 e_util_utf8_strstrcase (const gchar *haystack,
222 gunichar *nuni, unival;
226 if (haystack == NULL)
232 if (strlen (needle) == 0)
235 if (strlen (haystack) == 0)
238 nuni = g_alloca (sizeof (gunichar) * strlen (needle));
241 for (p = e_util_unicode_get_utf8 (needle, &unival);
243 p = e_util_unicode_get_utf8 (p, &unival)) {
244 nuni[nlen++] = g_unichar_tolower (unival);
246 /* NULL means there was illegal utf-8 sequence */
250 for (p = e_util_unicode_get_utf8 (o, &unival);
252 p = e_util_unicode_get_utf8 (p, &unival)) {
254 sc = g_unichar_tolower (unival);
255 /* We have valid stripped gchar */
259 while (npos < nlen) {
260 q = e_util_unicode_get_utf8 (q, &unival);
261 if (!q || !unival) return NULL;
262 sc = g_unichar_tolower (unival);
263 if (sc != nuni[npos]) break;
277 stripped_char (gunichar ch)
284 utype = g_unichar_type (ch);
287 case G_UNICODE_CONTROL:
288 case G_UNICODE_FORMAT:
289 case G_UNICODE_UNASSIGNED:
290 case G_UNICODE_SPACING_MARK:
294 /* Convert to lowercase, fall through */
295 ch = g_unichar_tolower (ch);
296 case G_UNICODE_LOWERCASE_LETTER:
297 if ((dlen = g_unichar_fully_decompose (ch, FALSE, decomp, 4))) {
308 * e_util_utf8_strstrcasedecomp:
309 * @haystack: The string to search in.
310 * @needle: The string to search for.
312 * Find the first instance of @needle in @haystack, where both @needle
313 * and @haystack are UTF-8 strings. Both strings are stripped and
314 * decomposed for comparison, and case is ignored.
316 * Returns: A pointer to the first instance of @needle in @haystack, or
317 * %NULL if either of the strings are not legal UTF-8 strings.
320 e_util_utf8_strstrcasedecomp (const gchar *haystack,
328 if (haystack == NULL)
334 if (strlen (needle) == 0)
337 if (strlen (haystack) == 0)
340 nuni = g_alloca (sizeof (gunichar) * strlen (needle));
343 for (p = e_util_unicode_get_utf8 (needle, &unival);
345 p = e_util_unicode_get_utf8 (p, &unival)) {
347 sc = stripped_char (unival);
352 /* NULL means there was illegal utf-8 sequence */
354 /* If everything is correct, we have decomposed,
355 * lowercase, stripped needle */
360 for (p = e_util_unicode_get_utf8 (o, &unival);
362 p = e_util_unicode_get_utf8 (p, &unival)) {
364 sc = stripped_char (unival);
366 /* We have valid stripped gchar */
370 while (npos < nlen) {
371 q = e_util_unicode_get_utf8 (q, &unival);
372 if (!q || !unival) return NULL;
373 sc = stripped_char (unival);
374 if ((!sc) || (sc != nuni[npos])) break;
389 * e_util_utf8_strcasecmp:
390 * @s1: a UTF-8 string
391 * @s2: another UTF-8 string
393 * Compares two UTF-8 strings using approximate case-insensitive ordering.
395 * Returns: < 0 if @s1 compares before @s2, 0 if they compare equal,
396 * > 0 if @s1 compares after @s2
399 e_util_utf8_strcasecmp (const gchar *s1,
402 gchar *folded_s1, *folded_s2;
405 g_return_val_if_fail (s1 != NULL && s2 != NULL, -1);
407 if (strcmp (s1, s2) == 0)
410 folded_s1 = g_utf8_casefold (s1, -1);
411 folded_s2 = g_utf8_casefold (s2, -1);
413 retval = g_utf8_collate (folded_s1, folded_s2);
422 * e_util_utf8_remove_accents:
423 * @str: a UTF-8 string, or %NULL
425 * Returns a newly-allocated copy of @str with accents removed.
427 * Returns: a newly-allocated string
432 e_util_utf8_remove_accents (const gchar *str)
440 res = g_utf8_normalize (str, -1, G_NORMALIZE_NFD);
442 return g_strdup (str);
444 for (i = 0, j = 0; res[i]; i++) {
445 if ((guchar) res[i] != 0xCC || res[i + 1] == 0) {
459 * e_util_utf8_make_valid:
460 * @str: a UTF-8 string
462 * Returns a newly-allocated copy of @str, with invalid characters
463 * replaced by Unicode replacement characters (U+FFFD).
465 * Returns: a newly-allocated string
470 e_util_utf8_make_valid (const gchar *str)
472 return e_util_utf8_data_make_valid (str, strlen (str));
476 * e_util_utf8_data_make_valid:
477 * @data: UTF-8 binary data
478 * @data_bytes: length of the binary data
480 * Returns a newly-allocated NULL-terminated string with invalid characters
481 * replaced by Unicode replacement characters (U+FFFD).
483 * Returns: a newly-allocated string
488 e_util_utf8_data_make_valid (const gchar *data,
491 /* almost identical copy of glib's _g_utf8_make_valid() */
493 const gchar *remainder, *invalid;
494 gint remaining_bytes, valid_bytes;
496 g_return_val_if_fail (data != NULL, NULL);
499 remainder = (gchar *) data,
500 remaining_bytes = data_bytes;
502 while (remaining_bytes != 0) {
503 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
505 valid_bytes = invalid - remainder;
508 string = g_string_sized_new (remaining_bytes);
510 g_string_append_len (string, remainder, valid_bytes);
511 /* append U+FFFD REPLACEMENT CHARACTER */
512 g_string_append (string, "\357\277\275");
514 remaining_bytes -= valid_bytes + 1;
515 remainder = invalid + 1;
519 return g_strndup ((gchar *) data, data_bytes);
521 g_string_append (string, remainder);
523 g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
525 return g_string_free (string, FALSE);
529 * e_util_utf8_normalize:
530 * @str: a UTF-8 string
532 * Normalizes @str by making it all lower case and removing any accents from it.
534 * Returns: The normalized version of @str, or %NULL if @str was not valid UTF-8
539 e_util_utf8_normalize (const gchar *str)
542 gchar *normal, *casefolded = NULL;
547 if (!g_utf8_validate (str, -1, NULL)) {
548 valid = e_util_utf8_make_valid (str);
552 normal = e_util_utf8_remove_accents (str);
554 casefolded = g_utf8_casefold (normal, -1);
563 * e_util_ensure_gdbus_string:
564 * @str: a possibly invalid UTF-8 string, or %NULL
565 * @gdbus_str: return location for the corrected string
567 * If @str is a valid UTF-8 string, the function returns @str and does
568 * not set @gdbus_str.
570 * If @str is an invalid UTF-8 string, the function calls
571 * e_util_utf8_make_valid() and points @gdbus_str to the newly-allocated,
572 * valid UTF-8 string, and also returns it. The caller should free the
573 * string pointed to by @gdbus_str with g_free().
575 * If @str is %NULL, the function returns an empty string and does not
578 * Admittedly, the function semantics are a little awkward. The example
579 * below illustrates the easiest way to cope with the @gdbus_str argument:
583 * const gchar *trusted_utf8;
584 * gchar *allocated = NULL;
586 * trusted_utf8 = e_util_ensure_gdbus_string (untrusted_utf8, &allocated);
588 * Do stuff with trusted_utf8, then clear it.
590 * trusted_utf8 = NULL;
592 * g_free (allocated);
597 * Returns: a valid UTF-8 string
602 e_util_ensure_gdbus_string (const gchar *str,
605 g_return_val_if_fail (gdbus_str != NULL, NULL);
612 if (g_utf8_validate (str, -1, NULL))
615 *gdbus_str = e_util_utf8_make_valid (str);
622 * @string: The string array to store the result in.
623 * @max: The size of array @s.
624 * @fmt: The formatting to use on @tm.
625 * @tm: The time value to format.
627 * This function is a wrapper around the strftime (3) function, which
628 * converts the %l and %k (12h and 24h) format variables
631 * Returns: The number of characters placed in @s.
634 e_strftime (gchar *string,
639 #ifndef HAVE_LKSTRFTIME
640 gchar *c, *ffmt, *ff;
644 g_return_val_if_fail (string != NULL, 0);
645 g_return_val_if_fail (fmt != NULL, 0);
646 g_return_val_if_fail (tm != NULL, 0);
648 #ifdef HAVE_LKSTRFTIME
649 ret = strftime (string, max, fmt, tm);
651 ffmt = g_strdup (fmt);
653 while ((c = strstr (ff, "%l")) != NULL) {
659 while ((c = strstr (ff, "%k")) != NULL) {
665 /* The Microsoft strftime () doesn't have %e either */
667 while ((c = strstr (ff, "%e")) != NULL) {
673 ret = strftime (string, max, ffmt, tm);
677 if (ret == 0 && max > 0)
685 * @string: The string array to store the result in.
686 * @max: The size of array @s.
687 * @fmt: The formatting to use on @tm.
688 * @tm: The time value to format.
690 * The UTF-8 equivalent of e_strftime ().
692 * Returns: The number of characters placed in @s.
695 e_utf8_strftime (gchar *string,
701 gchar *locale_fmt, *buf;
703 g_return_val_if_fail (string != NULL, 0);
704 g_return_val_if_fail (fmt != NULL, 0);
705 g_return_val_if_fail (tm != NULL, 0);
707 locale_fmt = g_locale_from_utf8 (fmt, -1, NULL, &sz, NULL);
711 ret = e_strftime (string, max, locale_fmt, tm);
717 buf = g_locale_to_utf8 (string, ret, NULL, &sz, NULL);
724 gchar *tmp = buf + max - 1;
725 tmp = g_utf8_find_prev_char (buf, tmp);
732 memcpy (string, buf, sz);
743 * @thread: A #GThread pointer
745 * Returns a 64-bit integer hopefully uniquely identifying the
746 * thread. To be used in debugging output and logging only.
747 * The returned value is just a cast of a pointer to the 64-bit integer.
749 * There is no guarantee that calling e_util_gthread_id () on one
750 * thread first and later after that thread has dies on another won't
751 * return the same integer.
753 * On Linux and Win32, known to really return a unique id for each
754 * thread existing at a certain time. No guarantee that ids won't be
755 * reused after a thread has terminated, though.
757 * Returns: A 64-bit integer.
762 e_util_gthread_id (GThread *thread)
764 #if GLIB_SIZEOF_VOID_P == 8
766 return (guint64) thread;
768 return (gint) thread;
772 /* This only makes a filename safe for usage as a filename.
773 * It still may have shell meta-characters in it. */
775 /* This code is rather misguided and mostly pointless, but can't be
776 * changed because of backward compatibility, I guess.
778 * It replaces some perfectly safe characters like '%' with an
779 * underscore. (Recall that on Unix, the only bytes not allowed in a
780 * file name component are '\0' and '/'.) On the other hand, the UTF-8
781 * for a printable non-ASCII Unicode character (that thus consists of
782 * several very nonprintable non-ASCII bytes) is let through as
783 * such. But those bytes are of course also allowed in filenames, so
784 * it doesn't matter as such...
787 e_filename_make_safe (gchar *string)
792 const gchar *unsafe_chars = " /'\"`&();|<>$%{}!\\:*?#";
794 const gchar *unsafe_chars = " /'\"`&();|<>$%{}!#";
797 g_return_if_fail (string != NULL);
802 c = g_utf8_get_char (p);
804 p = g_utf8_next_char (p);
805 /* I wonder what this code is supposed to actually
806 * achieve, and whether it does that as currently
809 if (!g_unichar_isprint (c) ||
810 (c < 0xff && strchr (unsafe_chars, c & 0xff))) {
818 * e_filename_mkdir_encoded:
819 * @basepath: base path of a file name; this is left unchanged
820 * @fileprefix: prefix for the filename; this is encoded
821 * @filename: file name to use; this is encoded; can be %NULL
822 * @fileindex: used when @filename is NULL, then the filename
823 * is generated as "file" + fileindex
825 * Creates a local path constructed from @basepath / @fileprefix + "-" + @filename,
826 * and makes sure the path @basepath exists. If creation of
827 * the path fails, then NULL is returned.
829 * Returns: Full local path like g_build_filename() except that @fileprefix
830 * and @filename are encoded to create a proper file elements for
831 * a file system. Free returned pointer with g_free().
836 e_filename_mkdir_encoded (const gchar *basepath,
837 const gchar *fileprefix,
838 const gchar *filename,
841 gchar *elem1, *elem2, *res, *fn;
843 g_return_val_if_fail (basepath != NULL, NULL);
844 g_return_val_if_fail (*basepath != 0, NULL);
845 g_return_val_if_fail (fileprefix != NULL, NULL);
846 g_return_val_if_fail (*fileprefix != 0, NULL);
847 g_return_val_if_fail (!filename || *filename, NULL);
849 if (g_mkdir_with_parents (basepath, 0700) < 0)
852 elem1 = g_strdup (fileprefix);
854 elem2 = g_strdup (filename);
856 elem2 = g_strdup_printf ("file%d", fileindex);
858 e_filename_make_safe (elem1);
859 e_filename_make_safe (elem2);
861 fn = g_strconcat (elem1, "-", elem2, NULL);
863 res = g_build_filename (basepath, fn, NULL);
873 * e_util_slist_to_strv:
874 * @strings: (element-type utf8): a #GSList of strings (const gchar *)
876 * Convert list of strings into NULL-terminates array of strings.
878 * Returns: (transfer full): Newly allocated %NULL-terminated array of strings.
879 * Returned pointer should be freed with g_strfreev().
881 * Note: Pair function for this is e_util_strv_to_slist().
886 e_util_slist_to_strv (const GSList *strings)
891 array = g_ptr_array_sized_new (g_slist_length ((GSList *) strings) + 1);
893 for (iter = strings; iter; iter = iter->next) {
894 const gchar *str = iter->data;
897 g_ptr_array_add (array, g_strdup (str));
900 /* NULL-terminated */
901 g_ptr_array_add (array, NULL);
903 return (gchar **) g_ptr_array_free (array, FALSE);
907 * e_util_strv_to_slist:
908 * @strv: a NULL-terminated array of strings (const gchar *)
910 * Convert NULL-terminated array of strings to a list of strings.
912 * Returns: (transfer full) (element-type utf8): Newly allocated #GSList of
913 * newly allocated strings. The returned pointer should be freed with
914 * e_util_free_string_slist().
916 * Note: Pair function for this is e_util_slist_to_strv().
921 e_util_strv_to_slist (const gchar * const *strv)
923 GSList *slist = NULL;
929 for (ii = 0; strv[ii]; ii++) {
930 slist = g_slist_prepend (slist, g_strdup (strv[ii]));
933 return g_slist_reverse (slist);
937 * e_util_copy_string_slist:
938 * @copy_to: (element-type utf8) (allow-none): Where to copy; can be %NULL
939 * @strings: (element-type utf8): #GSList of strings to be copied
941 * Copies #GSList of strings at the end of @copy_to.
943 * Returns: (transfer full) (element-type utf8): New head of @copy_to.
944 * Returned pointer can be freed with e_util_free_string_slist().
948 * Deprecated: 3.8: Use g_slist_copy_deep() instead, and optionally
949 * g_slist_concat() to concatenate the copied list
950 * to another #GSList.
953 e_util_copy_string_slist (GSList *copy_to,
954 const GSList *strings)
958 copied_list = g_slist_copy_deep (
959 (GSList *) strings, (GCopyFunc) g_strdup, NULL);
961 return g_slist_concat (copy_to, copied_list);
965 * e_util_copy_object_slist:
966 * @copy_to: (element-type GObject) (allow-none): Where to copy; can be %NULL
967 * @objects: (element-type GObject): #GSList of #GObject<!-- -->s to be copied
969 * Copies #GSList of #GObject<!-- -->s at the end of @copy_to.
971 * Returns: (transfer full) (element-type GObject): New head of @copy_to.
972 * Returned pointer can be freed with e_util_free_object_slist().
976 * Deprecated: 3.8: Use g_slist_copy_deep() instead, and optionally
977 * g_slist_concat() to concatenate the copied list
978 * to another #GSList.
981 e_util_copy_object_slist (GSList *copy_to,
982 const GSList *objects)
986 copied_list = g_slist_copy_deep (
987 (GSList *) objects, (GCopyFunc) g_object_ref, NULL);
989 return g_slist_concat (copy_to, copied_list);
993 * e_util_free_string_slist:
994 * @strings: (element-type utf8): a #GSList of strings (gchar *)
996 * Frees memory previously allocated by e_util_strv_to_slist().
1000 * Deprecated: 3.8: Use g_slist_free_full() instead.
1003 e_util_free_string_slist (GSList *strings)
1005 g_slist_free_full (strings, (GDestroyNotify) g_free);
1009 * e_util_free_object_slist:
1010 * @objects: (element-type GObject): a #GSList of #GObject<!-- -->s
1012 * Calls g_object_unref() on each member of @objects and then frees
1013 * also @objects itself.
1017 * Deprecated: 3.8: Use g_slist_free_full() instead.
1020 e_util_free_object_slist (GSList *objects)
1022 g_slist_free_full (objects, (GDestroyNotify) g_object_unref);
1026 * e_util_free_nullable_object_slist:
1027 * @objects: (element-type GObject): a #GSList of nullable #GObject<!-- -->s
1029 * Calls g_object_unref() on each member of @objects if non-%NULL and then frees
1030 * also @objects itself.
1035 e_util_free_nullable_object_slist (GSList *objects)
1038 for (l = objects; l; l = l->next) {
1040 g_object_unref (l->data);
1042 g_slist_free (objects);
1047 * @src_queue: a source #GQueue
1048 * @dst_queue: a destination #GQueue
1050 * Transfers the contents of @src_queue to the tail of @dst_queue.
1051 * When the operation is complete, @src_queue will be empty.
1056 e_queue_transfer (GQueue *src_queue,
1059 g_return_if_fail (src_queue != NULL);
1060 g_return_if_fail (dst_queue != NULL);
1062 dst_queue->head = g_list_concat (dst_queue->head, src_queue->head);
1063 dst_queue->tail = g_list_last (dst_queue->head);
1064 dst_queue->length += src_queue->length;
1066 src_queue->head = NULL;
1067 src_queue->tail = NULL;
1068 src_queue->length = 0;
1071 /* Helper for e_file_recursive_delete() */
1073 file_recursive_delete_thread (GSimpleAsyncResult *simple,
1075 GCancellable *cancellable)
1077 GError *error = NULL;
1079 e_file_recursive_delete_sync (G_FILE (object), cancellable, &error);
1082 g_simple_async_result_take_error (simple, error);
1086 * e_file_recursive_delete_sync:
1087 * @file: a #GFile to delete
1088 * @cancellable: optional #GCancellable object, or %NULL
1089 * @error: return location for a #GError, or %NULL
1091 * Deletes @file. If @file is a directory, its contents are deleted
1092 * recursively before @file itself is deleted. The recursive delete
1093 * operation will stop on the first error.
1095 * If @cancellable is not %NULL, then the operation can be cancelled
1096 * by triggering the cancellable object from another thread. If the
1097 * operation was cancelled, the error #G_IO_ERROR_CANCELLED will be
1100 * Returns: %TRUE if the file was deleted, %FALSE otherwise
1105 e_file_recursive_delete_sync (GFile *file,
1106 GCancellable *cancellable,
1109 GFileEnumerator *file_enumerator;
1110 GFileInfo *file_info;
1111 GFileType file_type;
1112 gboolean success = TRUE;
1113 GError *local_error = NULL;
1115 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1117 file_type = g_file_query_file_type (
1118 file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, cancellable);
1120 /* If this is not a directory, delete like normal. */
1121 if (file_type != G_FILE_TYPE_DIRECTORY)
1122 return g_file_delete (file, cancellable, error);
1124 /* Note, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS is critical here
1125 * so we only delete files inside the directory being deleted. */
1126 file_enumerator = g_file_enumerate_children (
1127 file, G_FILE_ATTRIBUTE_STANDARD_NAME,
1128 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1129 cancellable, error);
1131 if (file_enumerator == NULL)
1134 file_info = g_file_enumerator_next_file (
1135 file_enumerator, cancellable, &local_error);
1137 while (file_info != NULL) {
1141 name = g_file_info_get_name (file_info);
1143 /* Here's the recursive part. */
1144 child = g_file_get_child (file, name);
1145 success = e_file_recursive_delete_sync (
1146 child, cancellable, error);
1147 g_object_unref (child);
1149 g_object_unref (file_info);
1154 file_info = g_file_enumerator_next_file (
1155 file_enumerator, cancellable, &local_error);
1158 if (local_error != NULL) {
1159 g_propagate_error (error, local_error);
1163 g_object_unref (file_enumerator);
1168 /* The directory should be empty now. */
1169 return g_file_delete (file, cancellable, error);
1173 * e_file_recursive_delete:
1174 * @file: a #GFile to delete
1175 * @io_priority: the I/O priority of the request
1176 * @cancellable: optional #GCancellable object, or %NULL
1177 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1178 * @user_data: data to pass to the callback function
1180 * Asynchronously deletes @file. If @file is a directory, its contents
1181 * are deleted recursively before @file itself is deleted. The recursive
1182 * delete operation will stop on the first error.
1184 * If @cancellable is not %NULL, then the operation can be cancelled
1185 * by triggering the cancellable object before the operation finishes.
1187 * When the operation is finished, @callback will be called. You can then
1188 * call e_file_recursive_delete_finish() to get the result of the operation.
1193 e_file_recursive_delete (GFile *file,
1195 GCancellable *cancellable,
1196 GAsyncReadyCallback callback,
1199 GSimpleAsyncResult *simple;
1201 g_return_if_fail (G_IS_FILE (file));
1203 simple = g_simple_async_result_new (
1204 G_OBJECT (file), callback, user_data,
1205 e_file_recursive_delete);
1207 g_simple_async_result_set_check_cancellable (simple, cancellable);
1209 g_simple_async_result_run_in_thread (
1210 simple, file_recursive_delete_thread,
1211 io_priority, cancellable);
1213 g_object_unref (simple);
1217 * e_file_recursive_delete_finish:
1218 * @file: a #GFile to delete
1219 * @result: a #GAsyncResult
1220 * @error: return location for a #GError, or %NULL
1222 * Finishes the operation started with e_file_recursive_delete().
1224 * If the operation was cancelled, the error #G_IO_ERROR_CANCELLED will be
1227 * Returns: %TRUE if the file was deleted, %FALSE otherwise
1232 e_file_recursive_delete_finish (GFile *file,
1233 GAsyncResult *result,
1236 GSimpleAsyncResult *simple;
1238 g_return_val_if_fail (
1239 g_simple_async_result_is_valid (
1240 result, G_OBJECT (file), e_file_recursive_delete), FALSE);
1242 simple = G_SIMPLE_ASYNC_RESULT (result);
1244 /* Assume success unless a GError is set. */
1245 return !g_simple_async_result_propagate_error (simple, error);
1249 * e_binding_transform_enum_value_to_nick:
1250 * @binding: a #GBinding
1251 * @source_value: a #GValue whose type is derived from #G_TYPE_ENUM
1252 * @target_value: a #GValue of type #G_TYPE_STRING
1253 * @not_used: not used
1255 * Transforms an enumeration value to its corresponding nickname.
1257 * Returns: %TRUE if the enum value has a corresponding nickname
1262 e_binding_transform_enum_value_to_nick (GBinding *binding,
1263 const GValue *source_value,
1264 GValue *target_value,
1267 GEnumClass *enum_class;
1268 GEnumValue *enum_value;
1270 gboolean success = FALSE;
1272 g_return_val_if_fail (G_IS_BINDING (binding), FALSE);
1274 enum_class = g_type_class_peek (G_VALUE_TYPE (source_value));
1275 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), FALSE);
1277 value = g_value_get_enum (source_value);
1278 enum_value = g_enum_get_value (enum_class, value);
1279 if (enum_value != NULL) {
1280 g_value_set_string (target_value, enum_value->value_nick);
1288 * e_binding_transform_enum_nick_to_value:
1289 * @binding: a #GBinding
1290 * @source_value: a #GValue of type #G_TYPE_STRING
1291 * @target_value: a #GValue whose type is derived from #G_TYPE_ENUM
1292 * @not_used: not used
1294 * Transforms an enumeration nickname to its corresponding value.
1296 * Returns: %TRUE if the enum nickname has a corresponding value
1301 e_binding_transform_enum_nick_to_value (GBinding *binding,
1302 const GValue *source_value,
1303 GValue *target_value,
1306 GEnumClass *enum_class;
1307 GEnumValue *enum_value;
1308 const gchar *string;
1309 gboolean success = FALSE;
1311 g_return_val_if_fail (G_IS_BINDING (binding), FALSE);
1313 enum_class = g_type_class_peek (G_VALUE_TYPE (target_value));
1314 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), FALSE);
1316 string = g_value_get_string (source_value);
1317 enum_value = g_enum_get_value_by_nick (enum_class, string);
1318 if (enum_value != NULL) {
1319 g_value_set_enum (target_value, enum_value->value);
1327 * e_enum_from_string:
1328 * @enum_type: The enum type
1329 * @string: The string containing the enum value or nick
1330 * @enum_value: A return location to store the result
1332 * Fetches the appropriate enumeration value for @string in the given
1333 * enum type @type and stores the result in @enum_value
1335 * Returns: %TRUE if the string was a valid name or nick
1336 * for the given @type, %FALSE if the conversion failed.
1341 e_enum_from_string (GType enum_type,
1342 const gchar *string,
1345 GEnumClass *enum_class;
1349 gboolean retval = TRUE;
1351 g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), FALSE);
1352 g_return_val_if_fail (string != NULL, FALSE);
1354 value = g_ascii_strtoull (string, &endptr, 0);
1355 if (endptr != string)
1356 /* parsed a number */
1357 *enum_value = value;
1359 enum_class = g_type_class_ref (enum_type);
1360 ev = g_enum_get_value_by_name (enum_class, string);
1362 ev = g_enum_get_value_by_nick (enum_class, string);
1365 *enum_value = ev->value;
1369 g_type_class_unref (enum_class);
1377 * @enum_type: An enum type
1378 * @enum_value: The enum value to convert
1380 * Converts an enum value to a string using strings from the GType system.
1382 * Returns: the string representing @eval
1387 e_enum_to_string (GType enum_type,
1390 GEnumClass *enum_class;
1391 const gchar *string = NULL;
1394 enum_class = g_type_class_ref (enum_type);
1396 g_return_val_if_fail (enum_class != NULL, NULL);
1398 for (i = 0; i < enum_class->n_values; i++) {
1399 if (enum_value == enum_class->values[i].value) {
1400 string = enum_class->values[i].value_nick;
1405 g_type_class_unref (enum_class);
1413 * #EAsyncClosure provides a simple way to run an asynchronous function
1414 * synchronously without blocking a running #GMainLoop or using threads.
1416 * 1) Create an #EAsyncClosure with e_async_closure_new().
1418 * 2) Call the asynchronous function passing e_async_closure_callback() as
1419 * the #GAsyncReadyCallback argument and the #EAsyncClosure as the data
1422 * 3) Call e_async_closure_wait() and collect the #GAsyncResult.
1424 * 4) Call the corresponding asynchronous "finish" function, passing the
1425 * #GAsyncResult returned by e_async_closure_wait().
1427 * 5) If needed, repeat steps 2-4 for additional asynchronous functions
1428 * using the same #EAsyncClosure.
1430 * 6) Finally, free the #EAsyncClosure with e_async_closure_free().
1434 struct _EAsyncClosure {
1436 GMainContext *context;
1437 GAsyncResult *result;
1441 * e_async_closure_new:
1443 * Creates a new #EAsyncClosure for use with asynchronous functions.
1445 * Returns: a new #EAsyncClosure
1450 e_async_closure_new (void)
1452 EAsyncClosure *closure;
1454 closure = g_slice_new0 (EAsyncClosure);
1455 closure->context = g_main_context_new ();
1456 closure->loop = g_main_loop_new (closure->context, FALSE);
1458 g_main_context_push_thread_default (closure->context);
1464 * e_async_closure_wait:
1465 * @closure: an #EAsyncClosure
1467 * Call this function immediately after starting an asynchronous operation.
1468 * The function waits for the asynchronous operation to complete and returns
1469 * its #GAsyncResult to be passed to the operation's "finish" function.
1471 * This function can be called repeatedly on the same #EAsyncClosure to
1472 * easily string together multiple asynchronous operations.
1474 * Returns: (transfer none): a #GAsyncResult which is owned by the closure
1479 e_async_closure_wait (EAsyncClosure *closure)
1481 g_return_val_if_fail (closure != NULL, NULL);
1483 g_main_loop_run (closure->loop);
1485 return closure->result;
1489 * e_async_closure_free:
1490 * @closure: an #EAsyncClosure
1492 * Frees the @closure and the resources it holds.
1497 e_async_closure_free (EAsyncClosure *closure)
1499 g_return_if_fail (closure != NULL);
1501 g_main_context_pop_thread_default (closure->context);
1503 g_main_loop_unref (closure->loop);
1504 g_main_context_unref (closure->context);
1506 if (closure->result != NULL)
1507 g_object_unref (closure->result);
1509 g_slice_free (EAsyncClosure, closure);
1513 * e_async_closure_callback:
1514 * @object: a #GObject or %NULL, it is not used by the function at all
1515 * @result: a #GAsyncResult
1516 * @closure: an #EAsyncClosure
1518 * Pass this function as the #GAsyncReadyCallback argument of an asynchronous
1519 * function, and the #EAsyncClosure as the data argument.
1521 * This causes e_async_closure_wait() to terminate and return @result.
1526 e_async_closure_callback (GObject *object,
1527 GAsyncResult *result,
1530 EAsyncClosure *real_closure;
1532 g_return_if_fail (G_IS_ASYNC_RESULT (result));
1533 g_return_if_fail (closure != NULL);
1535 real_closure = closure;
1537 /* Replace any previous result. */
1538 if (real_closure->result != NULL)
1539 g_object_unref (real_closure->result);
1540 real_closure->result = g_object_ref (result);
1542 g_main_loop_quit (real_closure->loop);
1547 #include <windows.h>
1549 static const gchar *prefix = NULL;
1550 static const gchar *cp_prefix;
1552 static const gchar *localedir;
1553 static const gchar *extensiondir;
1554 static const gchar *imagesdir;
1555 static const gchar *ui_uidir;
1557 static HMODULE hmodule;
1558 G_LOCK_DEFINE_STATIC (mutex);
1560 /* Silence gcc with a prototype. Yes, this is silly. */
1561 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
1563 LPVOID lpvReserved);
1565 /* Minimal DllMain that just tucks away the DLL's HMODULE */
1567 DllMain (HINSTANCE hinstDLL,
1571 switch (fdwReason) {
1572 case DLL_PROCESS_ATTACH:
1580 e_util_replace_prefix (const gchar *configure_time_prefix,
1581 const gchar *runtime_prefix,
1582 const gchar *configure_time_path)
1584 gchar *c_t_prefix_slash;
1587 c_t_prefix_slash = g_strconcat (configure_time_prefix, "/", NULL);
1589 if (runtime_prefix &&
1590 g_str_has_prefix (configure_time_path, c_t_prefix_slash)) {
1591 retval = g_strconcat (
1593 configure_time_path + strlen (configure_time_prefix),
1596 retval = g_strdup (configure_time_path);
1598 g_free (c_t_prefix_slash);
1604 replace_prefix (const gchar *runtime_prefix,
1605 const gchar *configure_time_path)
1607 return e_util_replace_prefix (
1608 E_DATA_SERVER_PREFIX, runtime_prefix, configure_time_path);
1618 if (prefix != NULL) {
1623 /* This requires that the libedataserver DLL is installed in $bindir */
1624 full_pfx = g_win32_get_package_installation_directory_of_module (hmodule);
1625 cp_pfx = g_win32_locale_filename_from_utf8 (full_pfx);
1627 prefix = g_strdup (full_pfx);
1628 cp_prefix = g_strdup (cp_pfx);
1633 localedir = replace_prefix (cp_prefix, E_DATA_SERVER_LOCALEDIR);
1634 extensiondir = replace_prefix (prefix, E_DATA_SERVER_EXTENSIONDIR);
1635 imagesdir = replace_prefix (prefix, E_DATA_SERVER_IMAGESDIR);
1636 ui_uidir = replace_prefix (prefix, E_DATA_SERVER_UI_UIDIR);
1641 #include "libedataserver-private.h" /* For prototypes */
1643 #define GETTER_IMPL(varbl) \
1649 #define PRIVATE_GETTER(varbl) \
1651 _libedataserver_get_##varbl (void) \
1654 #define PUBLIC_GETTER(varbl) \
1656 e_util_get_##varbl (void) \
1659 PRIVATE_GETTER (extensiondir)
1660 PRIVATE_GETTER (imagesdir)
1661 PRIVATE_GETTER (ui_uidir)
1663 PUBLIC_GETTER (prefix)
1664 PUBLIC_GETTER (cp_prefix)
1665 PUBLIC_GETTER (localedir)
1667 #endif /* G_OS_WIN32 */
1669 static gint default_dbus_timeout = -1;
1672 * e_data_server_util_set_dbus_call_timeout:
1673 * @timeout_msec: default timeout for D-Bus calls in miliseconds
1675 * Sets default timeout, in milliseconds, for calls of g_dbus_proxy_call()
1678 * -1 means the default value as set by D-Bus itself.
1679 * G_MAXINT means no timeout at all.
1681 * Default value is set also by configure option --with-dbus-call-timeout=ms
1682 * and -1 is used when not set.
1686 * Deprecated: 3.8: This value is not used anywhere.
1689 e_data_server_util_set_dbus_call_timeout (gint timeout_msec)
1691 default_dbus_timeout = timeout_msec;
1695 * e_data_server_util_get_dbus_call_timeout:
1697 * Returns the value set by e_data_server_util_set_dbus_call_timeout().
1699 * Returns: the D-Bus call timeout in milliseconds
1703 * Deprecated: 3.8: This value is not used anywhere.
1706 e_data_server_util_get_dbus_call_timeout (void)
1708 return default_dbus_timeout;
1712 * e_named_parameters_new:
1714 * Creates a new instance of an #ENamedParameters. This should be freed
1715 * with e_named_parameters_free(), when no longer needed. Names are
1716 * compared case insensitively.
1718 * The structure is not thread safe, if the caller requires thread safety,
1719 * then it should provide it on its own.
1721 * Returns: newly allocated #ENamedParameters
1726 e_named_parameters_new (void)
1728 return (ENamedParameters *) g_ptr_array_new_with_free_func (g_free);
1732 * e_named_parameters_new_strv:
1733 * @strv: NULL-terminated string array to be used as a content of a newly
1734 * created #ENamedParameters
1736 * Creates a new instance of an #ENamedParameters, with initial content
1737 * being taken from @strv. This should be freed with e_named_parameters_free(),
1738 * when no longer needed. Names are compared case insensitively.
1740 * The structure is not thread safe, if the caller requires thread safety,
1741 * then it should provide it on its own.
1743 * Returns: newly allocated #ENamedParameters
1748 e_named_parameters_new_strv (const gchar * const *strv)
1750 ENamedParameters *parameters;
1753 g_return_val_if_fail (strv != NULL, NULL);
1755 parameters = e_named_parameters_new ();
1756 for (ii = 0; strv[ii]; ii++) {
1757 g_ptr_array_add ((GPtrArray *) parameters, g_strdup (strv[ii]));
1764 * e_named_parameters_free:
1765 * @parameters: an #ENamedParameters
1767 * Frees an instance of #ENamedParameters, previously allocated
1768 * with e_named_parameters_new(). Function does nothing, if
1769 * @parameters is %NULL.
1774 e_named_parameters_free (ENamedParameters *parameters)
1779 g_ptr_array_unref ((GPtrArray *) parameters);
1783 * e_named_parameters_clear:
1784 * @parameters: an #ENamedParameters
1786 * Removes all stored parameters from @parameters.
1791 e_named_parameters_clear (ENamedParameters *parameters)
1794 g_return_if_fail (parameters != NULL);
1796 array = (GPtrArray *) parameters;
1799 g_ptr_array_remove_range (array, 0, array->len);
1803 * e_named_parameters_assign:
1804 * @parameters: an #ENamedParameters to assign values to
1805 * @from: (allow-none): an #ENamedParameters to get values from, or %NULL
1807 * Makes content of the @parameters the same as @from.
1808 * Functions clears content of @parameters if @from is %NULL.
1813 e_named_parameters_assign (ENamedParameters *parameters,
1814 const ENamedParameters *from)
1816 g_return_if_fail (parameters != NULL);
1818 e_named_parameters_clear (parameters);
1822 GPtrArray *from_array = (GPtrArray *) from;
1824 for (ii = 0; ii < from_array->len; ii++) {
1826 (GPtrArray *) parameters,
1827 g_strdup (from_array->pdata[ii]));
1833 get_parameter_index (const ENamedParameters *parameters,
1839 g_return_val_if_fail (parameters != NULL, -1);
1840 g_return_val_if_fail (name != NULL, -1);
1842 name_len = strlen (name);
1844 array = (GPtrArray *) parameters;
1846 for (ii = 0; ii < array->len; ii++) {
1847 const gchar *name_and_value = g_ptr_array_index (array, ii);
1849 if (name_and_value == NULL)
1852 if (name_and_value[name_len] != ':')
1855 if (g_ascii_strncasecmp (name_and_value, name, name_len) == 0)
1863 * e_named_parameters_set:
1864 * @parameters: an #ENamedParameters
1865 * @name: name of a parameter to set
1866 * @value: (allow-none): value to set, or %NULL to unset
1868 * Sets parameter named @name to value @value. If @value is NULL,
1869 * then the parameter is removed. @value can be an empty string.
1871 * Note: There is a restriction on parameter names, it cannot be empty or
1872 * contain a colon character (':'), otherwise it can be pretty much anything.
1877 e_named_parameters_set (ENamedParameters *parameters,
1883 gchar *name_and_value;
1885 g_return_if_fail (parameters != NULL);
1886 g_return_if_fail (name != NULL);
1887 g_return_if_fail (strchr (name, ':') == NULL);
1888 g_return_if_fail (*name != '\0');
1890 array = (GPtrArray *) parameters;
1892 index = get_parameter_index (parameters, name);
1895 g_ptr_array_remove_index (array, index);
1899 name_and_value = g_strconcat (name, ":", value, NULL);
1901 g_free (array->pdata[index]);
1902 array->pdata[index] = name_and_value;
1904 g_ptr_array_add (array, name_and_value);
1909 * e_named_parameters_get:
1910 * @parameters: an #ENamedParameters
1911 * @name: name of a parameter to get
1913 * Returns current value of a parameter with name @name. If not such
1914 * exists, then returns %NULL.
1916 * Returns: value of a parameter named @name, or %NULL.
1921 e_named_parameters_get (const ENamedParameters *parameters,
1925 const gchar *name_and_value;
1927 g_return_val_if_fail (parameters != NULL, NULL);
1928 g_return_val_if_fail (name != NULL, NULL);
1930 index = get_parameter_index (parameters, name);
1934 name_and_value = g_ptr_array_index ((GPtrArray *) parameters, index);
1936 return name_and_value + strlen (name) + 1;
1940 * e_named_parameters_test:
1941 * @parameters: an #ENamedParameters
1942 * @name: name of a parameter to test
1943 * @value: value to test
1944 * @case_sensitively: whether to compare case sensitively
1946 * Compares current value of parameter named @name with given @value
1947 * and returns whether they are equal, either case sensitively or
1948 * insensitively, based on @case_sensitively argument. Function
1949 * returns %FALSE, if no such parameter exists.
1951 * Returns: Whether parameter of given name has stored value of given value.
1956 e_named_parameters_test (const ENamedParameters *parameters,
1959 gboolean case_sensitively)
1961 const gchar *stored_value;
1963 g_return_val_if_fail (parameters != NULL, FALSE);
1964 g_return_val_if_fail (name != NULL, FALSE);
1965 g_return_val_if_fail (value != NULL, FALSE);
1967 stored_value = e_named_parameters_get (parameters, name);
1971 if (case_sensitively)
1972 return strcmp (stored_value, value) == 0;
1974 return g_ascii_strcasecmp (stored_value, value) == 0;
1978 * e_named_parameters_to_strv:
1979 * @parameters: an #ENamedParameters
1981 * Returns: (transfer full): Contents of @parameters as a null-terminated strv
1986 e_named_parameters_to_strv (const ENamedParameters *parameters)
1988 GPtrArray *array = (GPtrArray *) parameters;
1989 GPtrArray *ret = g_ptr_array_new ();
1993 for (i = 0; i < array->len; i++) {
1994 g_ptr_array_add (ret, g_strdup (array->pdata[i]));
1998 g_ptr_array_add (ret, NULL);
2000 return (gchar **) g_ptr_array_free (ret, FALSE);
2003 static ENamedParameters *
2004 e_named_parameters_ref (ENamedParameters *params)
2006 return (ENamedParameters *) g_ptr_array_ref ((GPtrArray *) params);
2010 e_named_parameters_unref (ENamedParameters *params)
2012 g_ptr_array_unref ((GPtrArray *) params);
2015 G_DEFINE_BOXED_TYPE (
2018 e_named_parameters_ref,
2019 e_named_parameters_unref);