1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

preferences now uses autcomplete for login

This commit is contained in:
James Booth 2012-05-20 01:05:20 +01:00
parent 2080a61663
commit 6ca850bf0f

View File

@ -26,21 +26,19 @@
#include <glib.h> #include <glib.h>
#include "preferences.h" #include "preferences.h"
#include "prof_autocomplete.h"
static GString *prefs_loc; static GString *prefs_loc;
static GKeyFile *prefs; static GKeyFile *prefs;
// search logins list // search logins list
static GSList *logins = NULL; static PAutocomplete ac;
static GSList *last_found = NULL;
static gchar *search_str = NULL;
static void _save_prefs(void); static void _save_prefs(void);
static gint _compare_jids(gconstpointer a, gconstpointer b);
static gchar * _search_logins_from(GSList *curr);
void prefs_load(void) void prefs_load(void)
{ {
ac = p_autocomplete_new();
prefs_loc = g_string_new(getenv("HOME")); prefs_loc = g_string_new(getenv("HOME"));
g_string_append(prefs_loc, "/.profanity"); g_string_append(prefs_loc, "/.profanity");
@ -54,69 +52,18 @@ void prefs_load(void)
gsize i; gsize i;
for (i = 0; i < njids; i++) { for (i = 0; i < njids; i++) {
logins = g_slist_insert_sorted(logins, jids[i], _compare_jids); p_autocomplete_add(ac, jids[i]);
} }
} }
char * find_login(char *prefix) char * find_login(char *prefix)
{ {
gchar *found = NULL; return p_autocomplete_complete(ac, prefix);
if (!logins)
return NULL;
if (last_found == NULL) {
search_str = g_strdup(prefix);
found = _search_logins_from(logins);
return found;
} else {
found = _search_logins_from(g_slist_next(last_found));
if (found != NULL)
return found;
found = _search_logins_from(logins);
if (found != NULL)
return found;
reset_login_search();
return NULL;
}
} }
void reset_login_search(void) void reset_login_search(void)
{ {
last_found = NULL; p_autocomplete_reset(ac);
if (search_str != NULL) {
free(search_str);
search_str = NULL;
}
}
static gchar * _search_logins_from(GSList *curr)
{
while(curr) {
gchar *curr_jid = curr->data;
if (strncmp(curr_jid, search_str, strlen(search_str)) == 0) {
gchar *result = g_strdup(curr_jid);
last_found = curr;
return result;
}
curr = g_slist_next(curr);
}
return NULL;
}
static gint _compare_jids(gconstpointer a, gconstpointer b)
{
const gchar *str_a = (const gchar *) a;
const gchar *str_b = (const gchar *) b;
return g_strcmp0(str_a, str_b);
} }
gboolean prefs_get_beep(void) gboolean prefs_get_beep(void)