about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-12-04 00:16:42 +0000
committerJames Booth <boothj5@gmail.com>2014-12-04 00:16:42 +0000
commit5314e5970358f2864ce8dbc8e6d95f67738ffcec (patch)
treed829ae74e7c6ebe88e44fb089286ec26137c9411
parenteeb6e6b8fcef375e70e27b1459b1d90e22b483e5 (diff)
downloadprofani-tty-5314e5970358f2864ce8dbc8e6d95f67738ffcec.tar.gz
Added /resource autocompletion
-rw-r--r--src/command/command.c47
-rw-r--r--src/contact.c23
-rw-r--r--src/contact.h3
3 files changed, 72 insertions, 1 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 8d3f4969..7dbdeed4 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -95,6 +95,7 @@ static char * _kick_autocomplete(char *input, int *size);
 static char * _ban_autocomplete(char *input, int *size);
 static char * _affiliation_autocomplete(char *input, int *size);
 static char * _role_autocomplete(char *input, int *size);
+static char * _resource_autocomplete(char *input, int *size);
 
 GHashTable *commands = NULL;
 
@@ -1092,6 +1093,7 @@ static Autocomplete form_field_multi_ac;
 static Autocomplete occupants_ac;
 static Autocomplete occupants_default_ac;
 static Autocomplete time_ac;
+static Autocomplete resource_ac;
 
 /*
  * Initialise command autocompleter and history
@@ -1426,6 +1428,14 @@ cmd_init(void)
     autocomplete_add(time_ac, "seconds");
     autocomplete_add(time_ac, "off");
 
+    time_ac = autocomplete_new();
+    autocomplete_add(time_ac, "minutes");
+    autocomplete_add(time_ac, "seconds");
+
+    resource_ac = autocomplete_new();
+    autocomplete_add(resource_ac, "set");
+    autocomplete_add(resource_ac, "off");
+
     cmd_history_init();
 }
 
@@ -1480,6 +1490,7 @@ cmd_uninit(void)
     autocomplete_free(occupants_ac);
     autocomplete_free(occupants_default_ac);
     autocomplete_free(time_ac);
+    autocomplete_free(resource_ac);
 }
 
 gboolean
@@ -1651,6 +1662,7 @@ cmd_reset_autocomplete()
     autocomplete_reset(occupants_ac);
     autocomplete_reset(occupants_default_ac);
     autocomplete_reset(time_ac);
+    autocomplete_reset(resource_ac);
 
     if (ui_current_win_type() == WIN_MUC_CONFIG) {
         ProfWin *window = wins_get_current();
@@ -1659,6 +1671,14 @@ cmd_reset_autocomplete()
         }
     }
 
+    if (ui_current_win_type() == WIN_CHAT) {
+        char *recipient = ui_current_recipient();
+        PContact contact = roster_get_contact(recipient);
+        if (contact) {
+            p_contact_resource_ac_reset(contact);
+        }
+    }
+
     bookmark_autocomplete_reset();
 }
 
@@ -1959,6 +1979,7 @@ _cmd_complete_parameters(char *input, int *size)
     g_hash_table_insert(ac_funcs, "/ban",           _ban_autocomplete);
     g_hash_table_insert(ac_funcs, "/affiliation",   _affiliation_autocomplete);
     g_hash_table_insert(ac_funcs, "/role",          _role_autocomplete);
+    g_hash_table_insert(ac_funcs, "/resource",      _resource_autocomplete);
 
     char parsed[*size+1];
     i = 0;
@@ -2406,6 +2427,32 @@ _theme_autocomplete(char *input, int *size)
 }
 
 static char *
+_resource_autocomplete(char *input, int *size)
+{
+    char *found = NULL;
+
+    ProfWin *current = wins_get_current();
+    if (current && current->type == WIN_CHAT) {
+        char *recipient = ui_current_recipient();
+        PContact contact = roster_get_contact(recipient);
+        if (contact) {
+            Autocomplete ac = p_contact_resource_ac(contact);
+            found = autocomplete_param_with_ac(input, size, "/resource set", ac, FALSE);
+            if (found != NULL) {
+                return found;
+            }
+        }
+    }
+
+    found = autocomplete_param_with_ac(input, size, "/resource", resource_ac, FALSE);
+    if (found != NULL) {
+        return found;
+    }
+
+    return NULL;
+}
+
+static char *
 _form_autocomplete(char *input, int *size)
 {
     char *found = NULL;
diff --git a/src/contact.c b/src/contact.c
index e0853f72..f16f1679 100644
--- a/src/contact.c
+++ b/src/contact.c
@@ -41,6 +41,7 @@
 #include "contact.h"
 #include "common.h"
 #include "resource.h"
+#include "tools/autocomplete.h"
 
 struct p_contact_t {
     char *barejid;
@@ -51,6 +52,7 @@ struct p_contact_t {
     gboolean pending_out;
     GDateTime *last_activity;
     GHashTable *available_resources;
+    Autocomplete resource_ac;
 };
 
 PContact
@@ -85,6 +87,8 @@ p_contact_new(const char * const barejid, const char * const name,
     contact->available_resources = g_hash_table_new_full(g_str_hash, g_str_equal, free,
         (GDestroyNotify)resource_destroy);
 
+    contact->resource_ac = autocomplete_new();
+
     return contact;
 }
 
@@ -131,7 +135,10 @@ p_contact_groups(const PContact contact)
 gboolean
 p_contact_remove_resource(PContact contact, const char * const resource)
 {
-    return g_hash_table_remove(contact->available_resources, resource);
+    gboolean result = g_hash_table_remove(contact->available_resources, resource);
+    autocomplete_remove(contact->resource_ac, resource);
+
+    return result;
 }
 
 void
@@ -152,6 +159,7 @@ p_contact_free(PContact contact)
         }
 
         g_hash_table_destroy(contact->available_resources);
+        autocomplete_free(contact->resource_ac);
         free(contact);
     }
 }
@@ -373,6 +381,7 @@ void
 p_contact_set_presence(const PContact contact, Resource *resource)
 {
     g_hash_table_replace(contact->available_resources, strdup(resource->name), resource);
+    autocomplete_add(contact->resource_ac, strdup(resource->name));
 }
 
 void
@@ -402,3 +411,15 @@ p_contact_set_last_activity(const PContact contact, GDateTime *last_activity)
         contact->last_activity = g_date_time_ref(last_activity);
     }
 }
+
+Autocomplete
+p_contact_resource_ac(const PContact contact)
+{
+    return contact->resource_ac;
+}
+
+void
+p_contact_resource_ac_reset(const PContact contact)
+{
+    autocomplete_reset(contact->resource_ac);
+}
\ No newline at end of file
diff --git a/src/contact.h b/src/contact.h
index 03829e7f..17a3b210 100644
--- a/src/contact.h
+++ b/src/contact.h
@@ -36,6 +36,7 @@
 #define CONTACT_H
 
 #include "resource.h"
+#include "tools/autocomplete.h"
 
 typedef struct p_contact_t *PContact;
 
@@ -68,5 +69,7 @@ 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);
+Autocomplete p_contact_resource_ac(const PContact contact);
+void p_contact_resource_ac_reset(const PContact contact);
 
 #endif