[polari] room: Split out polari_util_get_basenick() helper



commit 2b45e62f6d3e9e46120b1a0d255dc06e1062e376
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Sep 17 14:22:15 2015 +0200

    room: Split out polari_util_get_basenick() helper
    
    Stripping down a nick to a common base for different variants isn't
    just useful for getting highlights while not using one's regular nick,
    but also to group different nicks that supposedly identify the same
    person, so split out a utility method.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=755133

 src/Makefile-lib.am   |    2 ++
 src/lib/polari-room.c |   12 ++----------
 src/lib/polari-util.c |   43 +++++++++++++++++++++++++++++++++++++++++++
 src/lib/polari-util.h |   24 ++++++++++++++++++++++++
 4 files changed, 71 insertions(+), 10 deletions(-)
---
diff --git a/src/Makefile-lib.am b/src/Makefile-lib.am
index 68ce5d3..c76b1a9 100644
--- a/src/Makefile-lib.am
+++ b/src/Makefile-lib.am
@@ -4,12 +4,14 @@ libpolari_headers = \
        lib/polari-drag-helper.h \
        lib/polari-fixed-size-frame.h \
        lib/polari-room.h \
+       lib/polari-util.h \
        $(NULL)
 
 libpolari_sources = \
        lib/polari-drag-helper.c \
        lib/polari-fixed-size-frame.c \
        lib/polari-room.c \
+       lib/polari-util.c \
        $(NULL)
 
 libpolari_1_0_la_SOURCES = $(libpolari_headers) $(libpolari_sources)
diff --git a/src/lib/polari-room.c b/src/lib/polari-room.c
index f23daa8..8ddbaa7 100644
--- a/src/lib/polari-room.c
+++ b/src/lib/polari-room.c
@@ -19,6 +19,7 @@
 #include <string.h>
 
 #include "polari-room.h"
+#include "polari-util.h"
 
 typedef struct _PolariRoomPrivate PolariRoomPrivate;
 
@@ -257,8 +258,6 @@ update_self_nick (PolariRoom *room)
 {
   TpConnection *conn;
   TpContact *self;
-  const char *nick;
-  int len;
 
   PolariRoomPrivate *priv = room->priv;
 
@@ -270,14 +269,7 @@ update_self_nick (PolariRoom *room)
   conn = tp_channel_get_connection (room->priv->channel);
   self = tp_connection_get_self_contact (conn);
 
-  nick = tp_contact_get_alias (self);
-  for (len = 0; g_ascii_isalnum(nick[len]); len++)
-    ;
-
-  if (len > 0)
-    priv->self_nick = g_strndup (nick, len);
-  else
-    priv->self_nick = g_strdup (nick);
+  priv->self_nick = polari_util_get_basenick (tp_contact_get_alias (self));
 }
 
 static void
diff --git a/src/lib/polari-util.c b/src/lib/polari-util.c
new file mode 100644
index 0000000..4da11ba
--- /dev/null
+++ b/src/lib/polari-util.c
@@ -0,0 +1,43 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.";
+ */
+
+#include "polari-util.h"
+
+#include <glib.h>
+
+/**
+ * polari_util_get_basenick:
+ * @nick: (transfer none): the original nick
+ *
+ * Returns: (transfer full): the "base nick" of @nick, which can be used to
+ *   group nicks that likely belong to the same person (e.g. "nick-away" or
+ *   "nick|bbl")
+ */
+char *
+polari_util_get_basenick (const char *nick)
+{
+  int len;
+
+  for (len = 0; g_ascii_isalnum(nick[len]); len++)
+    ;
+
+  if (len > 0)
+    return g_strndup (nick, len);
+  else
+    return g_strdup (nick);
+}
diff --git a/src/lib/polari-util.h b/src/lib/polari-util.h
new file mode 100644
index 0000000..73888c5
--- /dev/null
+++ b/src/lib/polari-util.h
@@ -0,0 +1,24 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.";
+ */
+
+#ifndef __POLARI_UTIL_H__
+#define __POLARI_UTIL_H__
+
+char *polari_util_get_basenick (const char *nick);
+
+#endif /* __POLARI_UTIL_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]