[evolution-data-server] Introduce e_util_generate_uid()
- From: Milan Crha <mcrha src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-data-server] Introduce e_util_generate_uid()
- Date: Thu, 18 May 2017 14:51:50 +0000 (UTC)
commit 8e876b1df327e93025020a2793833d714b92280e
Author: Milan Crha <mcrha redhat com>
Date: Thu May 18 16:49:29 2017 +0200
Introduce e_util_generate_uid()
This is to be used to generate unique IDs without host name in the result
string. It replaces e_cal_component_gen_uid() and can be used for vCards
and other similar places as well.
.../backends/file/e-book-backend-file.c | 11 ++--
.../libedata-book/e-book-meta-backend.c | 57 +-------------------
.../backends/caldav/e-cal-backend-caldav.c | 2 +-
src/calendar/backends/file/e-cal-backend-file.c | 8 ++--
src/calendar/backends/http/e-cal-backend-http.c | 2 +-
.../backends/weather/e-cal-backend-weather.c | 2 +-
src/calendar/libecal/e-cal-client.c | 2 +-
src/calendar/libecal/e-cal-component.c | 42 +++------------
src/calendar/libecal/e-cal-component.h | 2 +
src/calendar/libecal/e-cal-util.c | 2 +-
src/calendar/libedata-cal/e-cal-meta-backend.c | 6 +-
src/libedataserver/e-data-server-util.c | 57 ++++++++++++++++++++
src/libedataserver/e-data-server-util.h | 2 +
13 files changed, 87 insertions(+), 108 deletions(-)
---
diff --git a/src/addressbook/backends/file/e-book-backend-file.c
b/src/addressbook/backends/file/e-book-backend-file.c
index 182c390..a25e5c1 100644
--- a/src/addressbook/backends/file/e-book-backend-file.c
+++ b/src/addressbook/backends/file/e-book-backend-file.c
@@ -634,11 +634,12 @@ maybe_transform_vcard_for_photo (EBookBackendFile *bf,
static gchar *
e_book_backend_file_create_unique_id (void)
{
- /* use a 32 counter and the 32 bit timestamp to make an id.
- * it's doubtful 2^32 id's will be created in a second, so we
- * should be okay. */
- static guint c = 0;
- return g_strdup_printf (PAS_ID_PREFIX "%08lX%08X", (glong) time (NULL), c++);
+ gchar *uid = e_util_generate_uid (), *prefixed;
+
+ prefixed = g_strconcat (PAS_ID_PREFIX, uid, NULL);
+ g_free (uid);
+
+ return prefixed;
}
static gchar *
diff --git a/src/addressbook/libedata-book/e-book-meta-backend.c
b/src/addressbook/libedata-book/e-book-meta-backend.c
index 9e99d2a..5690e70 100644
--- a/src/addressbook/libedata-book/e-book-meta-backend.c
+++ b/src/addressbook/libedata-book/e-book-meta-backend.c
@@ -1416,61 +1416,6 @@ ebmb_refresh_sync (EBookBackend *book_backend,
return success;
}
-/* Copied from e_cal_component_gen_uid() */
-static gchar *
-e_book_meta_backend_gen_uid (void)
-{
- gchar *iso, *ret;
- static const gchar *hostname;
- time_t t = time (NULL);
- struct tm stm;
- static gint serial;
-
- if (!hostname) {
-#ifndef G_OS_WIN32
- static gchar buffer[512];
-
- if ((gethostname (buffer, sizeof (buffer) - 1) == 0) &&
- (buffer[0] != 0))
- hostname = buffer;
- else
- hostname = "localhost";
-#else
- hostname = g_get_host_name ();
-#endif
- }
-
-#ifdef G_OS_WIN32
-#ifdef gmtime_r
-#undef gmtime_r
-#endif
-
-/* The gmtime() in Microsoft's C library is MT-safe */
-#define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
-#endif
-
- gmtime_r (&t, &stm);
- iso = g_strdup_printf ("%04d%02d%02dT%02d%02d%02dZ",
- (stm.tm_year + 1900),
- (stm.tm_mon + 1),
- stm.tm_mday,
- stm.tm_hour,
- stm.tm_min,
- stm.tm_sec);
-
- ret = g_strdup_printf (
- "%s-%d-%d-%d-%d@%s",
- iso,
- getpid (),
- getgid (),
- getppid (),
- serial++,
- hostname);
- g_free (iso);
-
- return ret;
-}
-
static gboolean
ebmb_create_contact_sync (EBookMetaBackend *meta_backend,
EBookCache *book_cache,
@@ -1492,7 +1437,7 @@ ebmb_create_contact_sync (EBookMetaBackend *meta_backend,
if (!uid) {
gchar *new_uid;
- new_uid = e_book_meta_backend_gen_uid ();
+ new_uid = e_util_generate_uid ();
if (!new_uid) {
g_propagate_error (error, e_data_book_create_error (E_DATA_BOOK_STATUS_INVALID_ARG,
NULL));
return FALSE;
diff --git a/src/calendar/backends/caldav/e-cal-backend-caldav.c
b/src/calendar/backends/caldav/e-cal-backend-caldav.c
index ee8ca33..3300542 100644
--- a/src/calendar/backends/caldav/e-cal-backend-caldav.c
+++ b/src/calendar/backends/caldav/e-cal-backend-caldav.c
@@ -1401,7 +1401,7 @@ ecb_caldav_get_free_busy_from_schedule_outbox_sync (ECalBackendCalDAV *cbdav,
comp = e_cal_component_new ();
e_cal_component_set_new_vtype (comp, E_CAL_COMPONENT_FREEBUSY);
- str = e_cal_component_gen_uid ();
+ str = e_util_generate_uid ();
e_cal_component_set_uid (comp, str);
g_free (str);
diff --git a/src/calendar/backends/file/e-cal-backend-file.c b/src/calendar/backends/file/e-cal-backend-file.c
index ee0fb89..34f70f0 100644
--- a/src/calendar/backends/file/e-cal-backend-file.c
+++ b/src/calendar/backends/file/e-cal-backend-file.c
@@ -600,7 +600,7 @@ check_dup_uid (ECalBackendFile *cbfile,
uid,
rid ? rid : ""));
- new_uid = e_cal_component_gen_uid ();
+ new_uid = e_util_generate_uid ();
e_cal_component_set_uid (comp, new_uid);
/* FIXME: I think we need to reset the SEQUENCE property and reset the
@@ -2247,7 +2247,7 @@ e_cal_backend_file_create_objects (ECalBackendSync *backend,
if (!comp_uid) {
gchar *new_uid;
- new_uid = e_cal_component_gen_uid ();
+ new_uid = e_util_generate_uid ();
if (!new_uid) {
g_slist_free_full (icalcomps, (GDestroyNotify) icalcomponent_free);
g_rec_mutex_unlock (&priv->idle_save_rmutex);
@@ -2613,7 +2613,7 @@ e_cal_backend_file_modify_objects (ECalBackendSync *backend,
if (split_icalcomp) {
gchar *new_uid;
- new_uid = e_cal_component_gen_uid ();
+ new_uid = e_util_generate_uid ();
icalcomponent_set_uid (split_icalcomp, new_uid);
g_free (new_uid);
@@ -3296,7 +3296,7 @@ e_cal_backend_file_receive_objects (ECalBackendSync *backend,
gchar *new_uid = NULL;
- new_uid = e_cal_component_gen_uid ();
+ new_uid = e_util_generate_uid ();
icalcomponent_set_uid (subcomp, new_uid);
g_free (new_uid);
} else {
diff --git a/src/calendar/backends/http/e-cal-backend-http.c b/src/calendar/backends/http/e-cal-backend-http.c
index 71f4844..a9e9b75 100644
--- a/src/calendar/backends/http/e-cal-backend-http.c
+++ b/src/calendar/backends/http/e-cal-backend-http.c
@@ -368,7 +368,7 @@ ecb_http_get_changes_sync (ECalMetaBackend *meta_backend,
icalcomponent_remove_component (vcalendar, subcomp);
if (!icalcomponent_get_first_property (subcomp, ICAL_UID_PROPERTY)) {
- gchar *new_uid = e_cal_component_gen_uid ();
+ gchar *new_uid = e_util_generate_uid ();
icalcomponent_set_uid (subcomp, new_uid);
g_free (new_uid);
}
diff --git a/src/calendar/backends/weather/e-cal-backend-weather.c
b/src/calendar/backends/weather/e-cal-backend-weather.c
index 3902293..0c2de55 100644
--- a/src/calendar/backends/weather/e-cal-backend-weather.c
+++ b/src/calendar/backends/weather/e-cal-backend-weather.c
@@ -499,7 +499,7 @@ create_weather (ECalBackendWeather *cbw,
e_cal_component_set_icalcomponent (cal_comp, ical_comp);
/* set uid */
- uid = e_cal_component_gen_uid ();
+ uid = e_util_generate_uid ();
e_cal_component_set_uid (cal_comp, uid);
g_free (uid);
diff --git a/src/calendar/libecal/e-cal-client.c b/src/calendar/libecal/e-cal-client.c
index e355eea..9b41bf1 100644
--- a/src/calendar/libecal/e-cal-client.c
+++ b/src/calendar/libecal/e-cal-client.c
@@ -3656,7 +3656,7 @@ e_cal_client_get_default_object_sync (ECalClient *client,
gchar *new_uid;
/* Make sure the UID is always unique. */
- new_uid = e_cal_component_gen_uid ();
+ new_uid = e_util_generate_uid ();
icalcomponent_set_uid (icalcomp, new_uid);
g_free (new_uid);
}
diff --git a/src/calendar/libecal/e-cal-component.c b/src/calendar/libecal/e-cal-component.c
index 65b530d..39e4b31 100644
--- a/src/calendar/libecal/e-cal-component.c
+++ b/src/calendar/libecal/e-cal-component.c
@@ -400,41 +400,13 @@ e_cal_component_init (ECalComponent *comp)
*
* Returns: A unique identifier string. Every time this function is called
* a different string is returned.
+ *
+ * Deprecated: Since 3.26, use e_util_generate_uid() instead
**/
gchar *
e_cal_component_gen_uid (void)
{
- gchar *iso, *ret;
- static const gchar *hostname;
- time_t t = time (NULL);
- static gint serial;
-
- if (!hostname) {
-#ifndef G_OS_WIN32
- static gchar buffer[512];
-
- if ((gethostname (buffer, sizeof (buffer) - 1) == 0) &&
- (buffer[0] != 0))
- hostname = buffer;
- else
- hostname = "localhost";
-#else
- hostname = g_get_host_name ();
-#endif
- }
-
- iso = isodate_from_time_t (t);
- ret = g_strdup_printf (
- "%s-%d-%d-%d-%d@%s",
- iso,
- getpid (),
- getgid (),
- getppid (),
- serial++,
- hostname);
- g_free (iso);
-
- return ret;
+ return e_util_generate_uid ();
}
/**
@@ -929,7 +901,7 @@ add_alarm (ECalComponent *comp,
remove_alarm_uid (alarm);
- new_auid = e_cal_component_gen_uid ();
+ new_auid = e_util_generate_uid ();
auid = set_alarm_uid (alarm, new_auid);
g_free (new_auid);
}
@@ -965,7 +937,7 @@ scan_alarm (ECalComponent *comp,
/* The component has no alarm UID property, so we create one. */
- new_auid = e_cal_component_gen_uid ();
+ new_auid = e_util_generate_uid ();
auid = set_alarm_uid (alarm, new_auid);
g_free (new_auid);
@@ -1020,7 +992,7 @@ ensure_mandatory_properties (ECalComponent *comp)
if (!priv->uid) {
gchar *uid;
- uid = e_cal_component_gen_uid ();
+ uid = e_util_generate_uid ();
priv->uid = icalproperty_new_uid (uid);
g_free (uid);
@@ -5712,7 +5684,7 @@ e_cal_component_alarm_new (void)
alarm->icalcomp = icalcomponent_new (ICAL_VALARM_COMPONENT);
- new_auid = e_cal_component_gen_uid ();
+ new_auid = e_util_generate_uid ();
alarm->uid = icalproperty_new_x (new_auid);
icalproperty_set_x_name (alarm->uid, EVOLUTION_ALARM_UID_PROPERTY);
icalcomponent_add_property (alarm->icalcomp, alarm->uid);
diff --git a/src/calendar/libecal/e-cal-component.h b/src/calendar/libecal/e-cal-component.h
index d8c2a05..eb84416 100644
--- a/src/calendar/libecal/e-cal-component.h
+++ b/src/calendar/libecal/e-cal-component.h
@@ -331,7 +331,9 @@ struct _ECalComponentClass {
GType e_cal_component_get_type (void);
+/* #ifndef EDS_DISABLE_DEPRECATED*/
gchar * e_cal_component_gen_uid (void);
+/*#endif / * EDS_DISABLE_DEPRECATED */
ECalComponent * e_cal_component_new (void);
ECalComponent * e_cal_component_new_from_string (const gchar *calobj);
diff --git a/src/calendar/libecal/e-cal-util.c b/src/calendar/libecal/e-cal-util.c
index a9426f8..c65cd1c 100644
--- a/src/calendar/libecal/e-cal-util.c
+++ b/src/calendar/libecal/e-cal-util.c
@@ -118,7 +118,7 @@ e_cal_util_new_component (icalcomponent_kind kind)
gchar *uid;
comp = icalcomponent_new (kind);
- uid = e_cal_component_gen_uid ();
+ uid = e_util_generate_uid ();
icalcomponent_set_uid (comp, uid);
g_free (uid);
dtstamp = icaltime_current_time_with_zone (icaltimezone_get_utc_timezone ());
diff --git a/src/calendar/libedata-cal/e-cal-meta-backend.c b/src/calendar/libedata-cal/e-cal-meta-backend.c
index 8e7aa02..57d1a45 100644
--- a/src/calendar/libedata-cal/e-cal-meta-backend.c
+++ b/src/calendar/libedata-cal/e-cal-meta-backend.c
@@ -1695,7 +1695,7 @@ ecmb_create_object_sync (ECalMetaBackend *meta_backend,
if (!uid) {
gchar *new_uid;
- new_uid = e_cal_component_gen_uid ();
+ new_uid = e_util_generate_uid ();
if (!new_uid) {
g_propagate_error (error, e_data_cal_create_error (InvalidObject, NULL));
return FALSE;
@@ -1994,7 +1994,7 @@ ecmb_modify_object_sync (ECalMetaBackend *meta_backend,
if (split_icalcomp) {
gchar *new_uid;
- new_uid = e_cal_component_gen_uid ();
+ new_uid = e_util_generate_uid ();
icalcomponent_set_uid (split_icalcomp, new_uid);
g_free (new_uid);
@@ -2448,7 +2448,7 @@ ecmb_receive_object_sync (ECalMetaBackend *meta_backend,
if (!id && method == ICAL_METHOD_PUBLISH) {
gchar *new_uid;
- new_uid = e_cal_component_gen_uid ();
+ new_uid = e_util_generate_uid ();
e_cal_component_set_uid (comp, new_uid);
g_free (new_uid);
diff --git a/src/libedataserver/e-data-server-util.c b/src/libedataserver/e-data-server-util.c
index 7cdae3e..cbb8cd3 100644
--- a/src/libedataserver/e-data-server-util.c
+++ b/src/libedataserver/e-data-server-util.c
@@ -3012,3 +3012,60 @@ e_util_unref_in_thread (gpointer object)
g_clear_error (&error);
}
+
+/**
+ * e_util_generate_uid:
+ *
+ * Generates a unique identificator, which can be used as part of
+ * the Message-ID header, or iCalendar component UID, or vCard UID.
+ * The resulting string doesn't contain any host name, it's
+ * a hexa-decimal string with no particular meaning.
+ *
+ * Free the returned string with g_free(), when no longer needed.
+ *
+ * Returns: (transfer full): generated unique identificator as
+ * a newly allocated string
+ *
+ * Since: 3.26
+ **/
+gchar *
+e_util_generate_uid (void)
+{
+ static volatile gint counter = 0;
+ gchar *uid;
+ GChecksum *checksum;
+
+ checksum = g_checksum_new (G_CHECKSUM_SHA1);
+
+ #define add_i64(_x) G_STMT_START { \
+ gint64 i64 = (_x); \
+ g_checksum_update (checksum, (const guchar *) &i64, sizeof (gint64)); \
+ } G_STMT_END
+
+ #define add_str(_x, _def) G_STMT_START { \
+ const gchar *str = (_x); \
+ if (!str) \
+ str = (_def); \
+ g_checksum_update (checksum, (const guchar *) str, strlen (str)); \
+ } G_STMT_END
+
+ add_i64 (g_get_monotonic_time ());
+ add_i64 (g_get_real_time ());
+ add_i64 (getpid ());
+ add_i64 (getgid ());
+ add_i64 (getppid ());
+ add_i64 (g_atomic_int_add (&counter, 1));
+
+ add_str (g_get_host_name (), "localhost");
+ add_str (g_get_user_name (), "user");
+ add_str (g_get_real_name (), "User");
+
+ #undef add_i64
+ #undef add_str
+
+ uid = g_strdup (g_checksum_get_string (checksum));
+
+ g_checksum_free (checksum);
+
+ return uid;
+}
diff --git a/src/libedataserver/e-data-server-util.h b/src/libedataserver/e-data-server-util.h
index 9dfb801..fab7589 100644
--- a/src/libedataserver/e-data-server-util.h
+++ b/src/libedataserver/e-data-server-util.h
@@ -284,6 +284,8 @@ gboolean e_util_get_source_oauth2_access_token_sync
void e_util_unref_in_thread (gpointer object);
+gchar * e_util_generate_uid (void);
+
G_END_DECLS
#endif /* E_DATA_SERVER_UTIL_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]