about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-10-07 00:16:58 +0100
committerJames Booth <boothj5@gmail.com>2013-10-07 00:16:58 +0100
commit383d91ec36b7b4afad79dffe2049dabbe2ec93fa (patch)
tree79a12d39dad2503221efe0cdd7f3fcff3bcb9cc0 /src
parent9bea1ce83096bd0a8fe17647f4635adb7bdac904 (diff)
downloadprofani-tty-383d91ec36b7b4afad79dffe2049dabbe2ec93fa.tar.gz
Added p_contact_create_display_string
Diffstat (limited to 'src')
-rw-r--r--src/contact.c25
-rw-r--r--src/contact.h1
-rw-r--r--src/ui/core.c46
3 files changed, 34 insertions, 38 deletions
diff --git a/src/contact.c b/src/contact.c
index 9cc992b2..0b955548 100644
--- a/src/contact.c
+++ b/src/contact.c
@@ -166,6 +166,31 @@ p_contact_name_or_jid(const PContact contact)
     }
 }
 
+char *
+p_contact_create_display_string(const PContact contact, const char * const resource)
+{
+    GString *result_str = g_string_new("");
+
+    // use nickname if exists
+    if (contact->name != NULL) {
+        g_string_append(result_str, contact->name);
+    } else {
+        g_string_append(result_str, contact->barejid);
+    }
+
+    // add resource if not default provided by profanity
+    if (strcmp(resource, "__prof_default") != 0) {
+        g_string_append(result_str, " (");
+        g_string_append(result_str, resource);
+        g_string_append(result_str, ")");
+    }
+
+    char *result = result_str->str;
+    g_string_free(result_str, FALSE);
+
+    return result;
+}
+
 static Resource *
 _highest_presence(Resource *first, Resource *second)
 {
diff --git a/src/contact.h b/src/contact.h
index e71e0f7d..998e5f8b 100644
--- a/src/contact.h
+++ b/src/contact.h
@@ -55,5 +55,6 @@ void p_contact_set_groups(const PContact contact, GSList *groups);
 GSList * p_contact_groups(const PContact contact);
 gboolean p_contact_in_group(const PContact contact, const char * const group);
 gboolean p_contact_subscribed(const PContact contact);
+char * p_contact_create_display_string(const PContact contact, const char * const resource);
 
 #endif
diff --git a/src/ui/core.c b/src/ui/core.c
index 43e95ab7..6a6c15c4 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -377,36 +377,20 @@ void
 ui_contact_online(const char * const barejid, const char * const resource,
     const char * const show, const char * const status, GDateTime *last_activity)
 {
-    Jid *jid = jid_create_from_bare_and_resource(barejid, resource);
     PContact contact = roster_get_contact(barejid);
-    GString *display_str = g_string_new("");
-
-    // use nickname if exists
-    if (p_contact_name(contact) != NULL) {
-        g_string_append(display_str, p_contact_name(contact));
-    } else {
-        g_string_append(display_str, barejid);
-    }
-
-    // add resource if not default provided by profanity
-    if (strcmp(jid->resourcepart, "__prof_default") != 0) {
-        g_string_append(display_str, " (");
-        g_string_append(display_str, jid->resourcepart);
-        g_string_append(display_str, ")");
-    }
+    char *display_str = p_contact_create_display_string(contact, resource);
 
     ProfWin *console = wins_get_console();
-    win_show_status_string(console, display_str->str, show, status, last_activity,
+    win_show_status_string(console, display_str, show, status, last_activity,
         "++", "online");
 
     ProfWin *window = wins_get_by_recipient(barejid);
     if (window != NULL) {
-        win_show_status_string(window, display_str->str, show, status,
+        win_show_status_string(window, display_str, show, status,
             last_activity, "++", "online");
     }
 
-    jid_destroy(jid);
-    g_string_free(display_str, TRUE);
+    free(display_str);
 
     if (wins_is_current(console)) {
         wins_refresh_current();
@@ -421,34 +405,20 @@ ui_contact_offline(const char * const from, const char * const show,
 {
     Jid *jidp = jid_create(from);
     PContact contact = roster_get_contact(jidp->barejid);
-    GString *display_str = g_string_new("");
-
-    // use nickname if exists
-    if (p_contact_name(contact) != NULL) {
-        g_string_append(display_str, p_contact_name(contact));
-    } else {
-        g_string_append(display_str, jidp->barejid);
-    }
-
-    // add resource if not default provided by profanity
-    if (strcmp(jidp->resourcepart, "__prof_default") != 0) {
-        g_string_append(display_str, " (");
-        g_string_append(display_str, jidp->resourcepart);
-        g_string_append(display_str, ")");
-    }
+    char *display_str = p_contact_create_display_string(contact, jidp->resourcepart);
 
     ProfWin *console = wins_get_console();
-    win_show_status_string(console, display_str->str, show, status, NULL, "--",
+    win_show_status_string(console, display_str, show, status, NULL, "--",
         "offline");
 
     ProfWin *window = wins_get_by_recipient(jidp->barejid);
     if (window != NULL) {
-        win_show_status_string(window, display_str->str, show, status, NULL, "--",
+        win_show_status_string(window, display_str, show, status, NULL, "--",
             "offline");
     }
 
     jid_destroy(jidp);
-    g_string_free(display_str, TRUE);
+    free(display_str);
 
     if (wins_is_current(console)) {
         wins_refresh_current();