2012-05-09 22:29:48 -04:00
|
|
|
/*
|
|
|
|
* preferences.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 James Booth <boothj5@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of Profanity.
|
|
|
|
*
|
|
|
|
* Profanity is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Profanity 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-05-09 22:37:24 -04:00
|
|
|
#include <stdlib.h>
|
2012-05-10 17:18:08 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-05-09 22:29:48 -04:00
|
|
|
#include <glib.h>
|
|
|
|
|
2012-05-10 18:51:06 -04:00
|
|
|
#include "preferences.h"
|
|
|
|
|
2012-05-10 05:02:03 -04:00
|
|
|
static GString *prefs_loc;
|
2012-05-10 04:55:55 -04:00
|
|
|
static GKeyFile *prefs;
|
2012-05-09 22:29:48 -04:00
|
|
|
|
2012-05-10 17:44:23 -04:00
|
|
|
// search logins list
|
|
|
|
static GSList *logins = NULL;
|
2012-05-10 18:11:48 -04:00
|
|
|
static GSList *last_found = NULL;
|
|
|
|
static gchar *search_str = NULL;
|
2012-05-10 17:44:23 -04:00
|
|
|
|
2012-05-10 05:12:02 -04:00
|
|
|
static void _save_prefs(void);
|
2012-05-10 17:44:23 -04:00
|
|
|
static gint _compare_jids(gconstpointer a, gconstpointer b);
|
2012-05-10 18:11:48 -04:00
|
|
|
static gchar * _search_logins_from(GSList *curr);
|
2012-05-10 05:02:03 -04:00
|
|
|
|
2012-05-09 22:29:48 -04:00
|
|
|
void prefs_load(void)
|
|
|
|
{
|
2012-05-10 05:02:03 -04:00
|
|
|
prefs_loc = g_string_new(getenv("HOME"));
|
2012-05-09 22:37:24 -04:00
|
|
|
g_string_append(prefs_loc, "/.profanity");
|
2012-05-09 22:29:48 -04:00
|
|
|
|
2012-05-10 04:55:55 -04:00
|
|
|
prefs = g_key_file_new();
|
|
|
|
g_key_file_load_from_file(prefs, prefs_loc->str, G_KEY_FILE_NONE, NULL);
|
2012-05-10 17:44:23 -04:00
|
|
|
|
|
|
|
// create the logins searchable list for autocompletion
|
|
|
|
gsize njids;
|
|
|
|
gchar **jids =
|
|
|
|
g_key_file_get_string_list(prefs, "connections", "logins", &njids, NULL);
|
|
|
|
|
|
|
|
gsize i;
|
|
|
|
for (i = 0; i < njids; i++) {
|
2012-05-10 18:51:06 -04:00
|
|
|
logins = g_slist_insert_sorted(logins, jids[i], _compare_jids);
|
2012-05-10 17:44:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-10 18:51:06 -04:00
|
|
|
char * find_login(char *prefix)
|
2012-05-10 18:11:48 -04:00
|
|
|
{
|
|
|
|
gchar *found = NULL;
|
|
|
|
|
|
|
|
if (!logins)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (last_found == NULL) {
|
2012-05-10 18:51:06 -04:00
|
|
|
search_str = g_strdup(prefix);
|
2012-05-10 18:11:48 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-05-10 18:51:06 -04:00
|
|
|
reset_login_search();
|
2012-05-10 18:11:48 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-10 18:51:06 -04:00
|
|
|
void reset_login_search(void)
|
2012-05-10 18:11:48 -04:00
|
|
|
{
|
|
|
|
last_found = NULL;
|
2012-05-10 18:51:06 -04:00
|
|
|
if (search_str != NULL) {
|
2012-05-10 18:11:48 -04:00
|
|
|
free(search_str);
|
2012-05-10 18:51:06 -04:00
|
|
|
search_str = NULL;
|
|
|
|
}
|
2012-05-10 18:11:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-10 17:44:23 -04:00
|
|
|
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);
|
2012-05-09 22:29:48 -04:00
|
|
|
}
|
2012-05-10 04:55:55 -04:00
|
|
|
|
|
|
|
gboolean prefs_get_beep(void)
|
|
|
|
{
|
|
|
|
return g_key_file_get_boolean(prefs, "ui", "beep", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void prefs_set_beep(gboolean value)
|
|
|
|
{
|
|
|
|
g_key_file_set_boolean(prefs, "ui", "beep", value);
|
2012-05-10 05:02:03 -04:00
|
|
|
_save_prefs();
|
2012-05-10 04:55:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean prefs_get_flash(void)
|
|
|
|
{
|
|
|
|
return g_key_file_get_boolean(prefs, "ui", "flash", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void prefs_set_flash(gboolean value)
|
|
|
|
{
|
|
|
|
g_key_file_set_boolean(prefs, "ui", "flash", value);
|
2012-05-10 05:02:03 -04:00
|
|
|
_save_prefs();
|
|
|
|
}
|
|
|
|
|
2012-05-10 17:18:08 -04:00
|
|
|
void prefs_add_login(const char *jid)
|
|
|
|
{
|
|
|
|
gsize njids;
|
|
|
|
gchar **jids =
|
|
|
|
g_key_file_get_string_list(prefs, "connections", "logins", &njids, NULL);
|
|
|
|
|
|
|
|
// no logins remembered yet
|
|
|
|
if (jids == NULL) {
|
|
|
|
njids = 1;
|
|
|
|
jids = (gchar**) g_malloc(sizeof(gchar *) * 2);
|
|
|
|
jids[0] = g_strdup(jid);
|
|
|
|
jids[1] = NULL;
|
|
|
|
g_key_file_set_string_list(prefs, "connections", "logins",
|
|
|
|
(const gchar * const *)jids, njids);
|
|
|
|
_save_prefs();
|
|
|
|
g_strfreev(jids);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
gsize i;
|
|
|
|
for (i = 0; i < njids; i++) {
|
|
|
|
if (strcmp(jid, jids[i]) == 0) {
|
|
|
|
g_strfreev(jids);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// jid not found, add to the list
|
|
|
|
jids = (gchar **) g_realloc(jids, (sizeof(gchar *) * (njids+2)));
|
|
|
|
jids[njids] = g_strdup(jid);
|
|
|
|
njids++;
|
|
|
|
jids[njids] = NULL;
|
|
|
|
g_key_file_set_string_list(prefs, "connections", "logins",
|
|
|
|
(const gchar * const *)jids, njids);
|
|
|
|
_save_prefs();
|
|
|
|
g_strfreev(jids);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-10 05:12:02 -04:00
|
|
|
static void _save_prefs(void)
|
2012-05-10 05:02:03 -04:00
|
|
|
{
|
|
|
|
gsize g_data_size;
|
|
|
|
char *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL);
|
|
|
|
g_file_set_contents(prefs_loc->str, g_prefs_data, g_data_size, NULL);
|
2012-05-10 04:55:55 -04:00
|
|
|
}
|