about summary refs log tree commit diff stats
path: root/src/preferences.c
blob: 8b36f0598de926c5dbd4a862e5fbf346dd72720f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# Some OS-specific preliminaries for Soso.

# Memory layout
#
# 0x40000000 - 0x40001ffff - for ELF code+data
# 0x40002000 - 0x401ffffff - for heap
== code 0x40000000
== data 0x40001000

# Syscalls
#
# We don't have libc, so we need to know Soso's precise syscall layout.
# https://github.com/ozkl/soso/blob/master/kernel/syscalltable.h
== code

syscall_exit:  # status/ebx : int
    b8/copy-to-eax 8/imm32
    cd/syscall 0x80/imm8

syscall_read:  # fd/ebx : int, buf/ecx : addr, size/edx : int -> nbytes-or-error/eax : int
    b8/copy-to-eax 2/imm32
    cd/syscall 0x80/imm8
    c3/return

syscall_write:  # fd/ebx : int, buf/ecx : addr, size/edx : int -> nbytes-or-error/eax : int
    b8/copy-to-eax 3/imm32
    cd/syscall 0x80/imm8
    c3/return

syscall_open:  # filename/ebx : (addr kernel-string), flags/ecx : int -> fd-or-error/eax : int
    b8/copy-to-eax 0/imm32
    cd/syscall 0x80/imm8
    c3/return

syscall_close:  # fd/ebx : int -> status/eax
    b8/copy-to-eax 1/imm32
    cd/syscall 0x80/imm8
    c3/return

# anonymous mmap not implemented
374' href='#n374'>374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
/*
 * 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/>.
 *
 */

#include <stdlib.h>
#include <string.h>

#include <glib.h>
#ifdef HAVE_NCURSES_H
#include <ncurses.h>
#endif
#ifdef HAVE_NCURSES_NCURSES_H
#include <ncurses/ncurses.h>
#endif

#include "config.h"
#include "files.h"
#include "log.h"
#include "preferences.h"
#include "prof_autocomplete.h"

static gchar *prefs_loc;
static GKeyFile *prefs;
gint log_maxsize = 0;

static PAutocomplete login_ac;
static PAutocomplete boolean_choice_ac;

static void _save_prefs(void);

void
prefs_load(void)
{
    GError *err;

    log_info("Loading preferences");
    login_ac = p_autocomplete_new();
    prefs_loc = files_get_preferences_file();

    prefs = g_key_file_new();
    g_key_file_load_from_file(prefs, prefs_loc, G_KEY_FILE_KEEP_COMMENTS,
        NULL);

    // 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++) {
        p_autocomplete_add(login_ac, strdup(jids[i]));
    }

    for (i = 0; i < njids; i++) {
        free(jids[i]);
    }
    free(jids);

    err = NULL;
    log_maxsize = g_key_file_get_integer(prefs, "log", "maxsize", &err);
    if (err != NULL) {
        log_maxsize = 0;
        g_error_free(err);
    }

    boolean_choice_ac = p_autocomplete_new();
    p_autocomplete_add(boolean_choice_ac, strdup("on"));
    p_autocomplete_add(boolean_choice_ac, strdup("off"));
}

void
prefs_close(void)
{
    p_autocomplete_clear(login_ac);
    p_autocomplete_clear(boolean_choice_ac);
    g_key_file_free(prefs);
}

char *
prefs_find_login(char *prefix)
{
    return p_autocomplete_complete(login_ac, prefix);
}

void
prefs_reset_login_search(void)
{
    p_autocomplete_reset(login_ac);
}

char *
prefs_autocomplete_boolean_choice(char *prefix)
{
    return p_autocomplete_complete(boolean_choice_ac, prefix);
}

void
prefs_reset_boolean_choice(void)
{
    p_autocomplete_reset(boolean_choice_ac);
}

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);
    _save_prefs();
}

gchar *
prefs_get_theme(void)
{
    return g_key_file_get_string(prefs, "ui", "theme", NULL);
}

void
prefs_set_theme(gchar *value)
{
    g_key_file_set_string(prefs, "ui", "theme", value);
    _save_prefs();
}

gboolean
prefs_get_states(void)
{
    return g_key_file_get_boolean(prefs, "ui", "states", NULL);
}

void
prefs_set_states(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "states", value);
    _save_prefs();
}

gboolean
prefs_get_outtype(void)
{
    return g_key_file_get_boolean(prefs, "ui", "outtype", NULL);
}

void
prefs_set_outtype(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "outtype", value);
    _save_prefs();
}

gboolean
prefs_get_notify_typing(void)
{
    return g_key_file_get_boolean(prefs, "notifications", "typing", NULL);
}

void
prefs_set_notify_typing(gboolean value)
{
    g_key_file_set_boolean(prefs, "notifications", "typing", value);
    _save_prefs();
}

gboolean
prefs_get_notify_message(void)
{
    return g_key_file_get_boolean(prefs, "notifications", "message", NULL);
}

void
prefs_set_notify_message(gboolean value)
{
    g_key_file_set_boolean(prefs, "notifications", "message", value);
    _save_prefs();
}

gint
prefs_get_notify_remind(void)
{
    return g_key_file_get_integer(prefs, "notifications", "remind", NULL);
}

void
prefs_set_notify_remind(gint value)
{
    g_key_file_set_integer(prefs, "notifications", "remind", value);
    _save_prefs();
}

gint
prefs_get_max_log_size(void)
{
    if (log_maxsize < PREFS_MIN_LOG_SIZE)
        return PREFS_MAX_LOG_SIZE;
    else
        return log_maxsize;
}

void
prefs_set_max_log_size(gint value)
{
    log_maxsize = value;
    g_key_file_set_integer(prefs, "log", "maxsize", value);
    _save_prefs();
}

gint
prefs_get_priority(void)
{
    return g_key_file_get_integer(prefs, "jabber", "priority", NULL);
}

void
prefs_set_priority(gint value)
{
    g_key_file_set_integer(prefs, "jabber", "priority", value);
    _save_prefs();
}

gint
prefs_get_reconnect(void)
{
    return g_key_file_get_integer(prefs, "jabber", "reconnect", NULL);
}

void
prefs_set_reconnect(gint value)
{
    g_key_file_set_integer(prefs, "jabber", "reconnect", value);
    _save_prefs();
}

gint
prefs_get_autoping(void)
{
    return g_key_file_get_integer(prefs, "jabber", "autoping", NULL);
}

void
prefs_set_autoping(gint value)
{
    g_key_file_set_integer(prefs, "jabber", "autoping", value);
    _save_prefs();
}

gboolean
prefs_get_vercheck(void)
{
    return g_key_file_get_boolean(prefs, "ui", "vercheck", NULL);
}

void
prefs_set_vercheck(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "vercheck", value);
    _save_prefs();
}

gboolean
prefs_get_titlebarversion(void)
{
    return g_key_file_get_boolean(prefs, "ui", "titlebarversion", NULL);
}

void
prefs_set_titlebarversion(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "titlebarversion", value);
    _save_prefs();
}

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);
    _save_prefs();
}

gboolean
prefs_get_intype(void)
{
    return g_key_file_get_boolean(prefs, "ui", "intype", NULL);
}

void
prefs_set_intype(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "intype", value);
    _save_prefs();
}

gboolean
prefs_get_chlog(void)
{
    return g_key_file_get_boolean(prefs, "ui", "chlog", NULL);
}

void
prefs_set_chlog(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "chlog", value);
    _save_prefs();
}

gboolean
prefs_get_history(void)
{
    return g_key_file_get_boolean(prefs, "ui", "history", NULL);
}

void
prefs_set_history(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "history", value);
    _save_prefs();
}

gchar *
prefs_get_autoaway_mode(void)
{
    gchar *result = g_key_file_get_string(prefs, "autoaway", "mode", NULL);
    if (result == NULL) {
        return strdup("off");
    } else {
        return result;
    }
}

void
prefs_set_autoaway_mode(gchar *value)
{
    g_key_file_set_string(prefs, "autoaway", "mode", value);
    _save_prefs();
}

gint
prefs_get_autoaway_time(void)
{
    gint result = g_key_file_get_integer(prefs, "autoaway", "time", NULL);

    if (result == 0) {
        return 15;
    } else {
        return result;
    }
}

void
prefs_set_autoaway_time(gint value)
{
    g_key_file_set_integer(prefs, "autoaway", "time", value);
    _save_prefs();
}

gchar *
prefs_get_autoaway_message(void)
{
    return g_key_file_get_string(prefs, "autoaway", "message", NULL);
}

void
prefs_set_autoaway_message(gchar *value)
{
    if (value == NULL) {
        g_key_file_remove_key(prefs, "autoaway", "message", NULL);
    } else {
        g_key_file_set_string(prefs, "autoaway", "message", value);
    }
    _save_prefs();
}

gboolean
prefs_get_autoaway_check(void)
{
    return g_key_file_get_boolean(prefs, "autoaway", "check", NULL);
}

void
prefs_set_autoaway_check(gboolean value)
{
    g_key_file_set_boolean(prefs, "autoaway", "check", value);
    _save_prefs();
}

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;
    }
}

gboolean
prefs_get_showsplash(void)
{
    return g_key_file_get_boolean(prefs, "ui", "showsplash", NULL);
}

void
prefs_set_showsplash(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "showsplash", value);
    _save_prefs();
}

static void
_save_prefs(void)
{
    gsize g_data_size;
    char *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL);
    g_file_set_contents(prefs_loc, g_prefs_data, g_data_size, NULL);
}