[Telepathy-commits] [telepathy-gabble/master] util: add gabble_generate_id, a wrapper for libuuid (if available) or a time-based ID (if not)

Simon McVittie simon.mcvittie at collabora.co.uk
Wed Mar 18 09:40:31 PDT 2009


---
 configure.ac    |    9 +++++++++
 src/Makefile.am |    4 ++--
 src/util.c      |   27 +++++++++++++++++++++++++++
 src/util.h      |    2 ++
 4 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/configure.ac b/configure.ac
index 40ce928..930e4dc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -195,6 +195,15 @@ PKG_CHECK_MODULES(SOUP, libsoup-2.4)
 AC_SUBST(SOUP_CFLAGS)
 AC_SUBST(SOUP_LIBS)
 
+PKG_CHECK_MODULES([UUID], [uuid], [HAVE_UUID=yes], [HAVE_UUID=no])
+AC_SUBST([UUID_CFLAGS])
+AC_SUBST([UUID_LIBS])
+if test x"$HAVE_UUID" = xyes; then
+  AC_DEFINE([HAVE_UUID], [1], [Define if libuuid is available])
+else
+  AC_MSG_WARN([libuuid not found, falling back to generating random IDs])
+fi
+
 AC_CHECK_FUNCS(getifaddrs memset select strndup setresuid setreuid strerror)
 
 AC_OUTPUT( Makefile \
diff --git a/src/Makefile.am b/src/Makefile.am
index 4a7aef4..eba1245 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -154,11 +154,11 @@ noinst_LTLIBRARIES = libgabble-convenience.la
 AM_CFLAGS = $(ERROR_CFLAGS) -I$(top_srcdir) -I$(top_builddir) \
 	    @DBUS_CFLAGS@ @GLIB_CFLAGS@ @LOUDMOUTH_CFLAGS@ \
 	    @HANDLE_LEAK_DEBUG_CFLAGS@ @TP_GLIB_CFLAGS@ \
-	    @SOUP_CFLAGS@ \
+	    @SOUP_CFLAGS@ @UUID_CFLAGS@ \
 	    -I $(top_srcdir)/lib -I $(top_builddir)/lib
 
 ALL_LIBS =  @DBUS_LIBS@ @GLIB_LIBS@ @LOUDMOUTH_LIBS@ @TP_GLIB_LIBS@ \
-	    @SOUP_LIBS@
+	    @SOUP_LIBS@ @UUID_LIBS@
 
 # Teach it how to make libgibber.la
 $(top_builddir)/lib/gibber/libgibber.la:
diff --git a/src/util.c b/src/util.c
index 811cb38..352ea1a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -29,6 +29,10 @@
 
 #include <telepathy-glib/handle-repo-dynamic.h>
 
+#ifdef HAVE_UUID
+# include <uuid.h>
+#endif
+
 #define DEBUG_FLAG GABBLE_DEBUG_JID
 
 #include "base64.h"
@@ -70,6 +74,29 @@ sha1_bin (const gchar *bytes,
   g_checksum_free (checksum);
 }
 
+gchar *
+gabble_generate_id (void)
+{
+#ifdef HAVE_UUID
+  /* generate random UUIDs */
+  uuid_t uu;
+  gchar *str;
+
+  str = g_new0 (gchar, 37);
+  uuid_generate_random (uu);
+  uuid_unparse_lower (uu, str);
+  return str;
+#else
+  /* generate from the time, a counter, and a random integer */
+  static gulong last = 0;
+  GTimeVal tv;
+
+  g_get_current_time (&tv);
+  return g_strdup_printf ("%lx.%lx/%lx/%x", tv.tv_sec, tv.tv_usec,
+      last++, g_random_int ());
+#endif
+}
+
 static void
 lm_message_node_add_nick (LmMessageNode *node, const gchar *nick)
 {
diff --git a/src/util.h b/src/util.h
index ff4e851..cc0d714 100644
--- a/src/util.h
+++ b/src/util.h
@@ -38,6 +38,8 @@ gchar *sha1_hex (const gchar *bytes, guint len);
 #define SHA1_HASH_SIZE 20
 void sha1_bin (const gchar *bytes, guint len, guchar out[SHA1_HASH_SIZE]);
 
+gchar *gabble_generate_id (void);
+
 void lm_message_node_add_own_nick (LmMessageNode *node,
     GabbleConnection *conn);
 void lm_message_node_unlink (LmMessageNode *orphan);
-- 
1.5.6.5




More information about the telepathy-commits mailing list