about summary refs log tree commit diff stats
path: root/src/plugins/callbacks.c
blob: 3d5d6e4882d9f06279e9e3c2a1bc09115c105830 (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
pre { 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 */
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - linux/print-file.mu</title>
<meta name="Generator" content="Vim/8.1">
<meta name="plugin-version" content="vim8.1_v1">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; }
body { font-size:12pt; font-family: monospace; color
/*
 * callbacks.c
 * vim: expandtab:ts=4:sts=4:sw=4
 *
 * Copyright (C) 2012 - 2019 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 <https://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

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

#include "command/cmd_defs.h"
#include "command/cmd_ac.h"
#include "plugins/callbacks.h"
#include "plugins/plugins.h"
#include "tools/autocomplete.h"
#include "tools/parser.h"
#include "ui/ui.h"
#include "ui/window_list.h"

static GHashTable* p_commands = NULL;
static GHashTable* p_timed_functions = NULL;
static GHashTable* p_window_callbacks = NULL;

static void
_free_window_callback(PluginWindowCallback* window_callback)
{
    if (window_callback->callback_destroy) {
        window_callback->callback_destroy(window_callback->callback);
    }
    free(window_callback);
}

static void
_free_window_callbacks(GHashTable* window_callbacks)
{
    g_hash_table_destroy(window_callbacks);
}

static void
_free_command_help(CommandHelp* help)
{
    int i = 0;
    while (help->tags[i] != NULL) {
        free(help->tags[i++]);
    }

    i = 0;
    while (help->synopsis[i] != NULL) {
        free(help->synopsis[i++]);
    }

    free(help->desc);

    i = 0;
    while (help->args[i] != NULL && help->args[i][0] != NULL) {
        free(help->args[i][0]);
        free(help->args[i][1]);
        i++;
    }

    i = 0;
    while (help->examples[i] != NULL) {
        free(help->examples[i++]);
    }

    free(help);
}

static void
_free_command(PluginCommand* command)
{
    if (command->callback_destroy) {
        command->callback_destroy(command->callback);
    }
    free(command->command_name);

    _free_command_help(command->help);

    free(command);
}

static void
_free_command_hash(GHashTable* command_hash)
{
    g_hash_table_destroy(command_hash);
}

static void
_free_timed_function(PluginTimedFunction* timed_function)
{
    if (timed_function->callback_destroy) {
        timed_function->callback_destroy(timed_function->callback);
    }

    g_timer_destroy(timed_function->timer);

    free(timed_function);
}

static void
_free_timed_function_list(GList* timed_functions)
{
    g_list_free_full(timed_functions, (GDestroyNotify)_free_timed_function);
}

void
callbacks_init(void)
{
    p_commands = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_free_command_hash);
    p_timed_functions = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_free_timed_function_list);
    p_window_callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_free_window_callbacks);
}

void
callbacks_remove(const char* const plugin_name)
{
    GHashTable* command_hash = g_hash_table_lookup(p_commands, plugin_name);
    if (command_hash) {
        GList* commands = g_hash_table_get_keys(command_hash);
        GList* curr = commands;
        while (curr) {
            char* command = curr->data;
            cmd_ac_remove(command);
            cmd_ac_remove_help(&command[1]);
            curr = g_list_next(curr);
        }
        g_list_free(commands);
    }

    g_hash_table_remove(p_commands, plugin_name);
    g_hash_table_remove(p_timed_functions, plugin_name);

    GHashTable* tag_to_win_cb_hash = g_hash_table_lookup(p_window_callbacks, plugin_name);
    if (tag_to_win_cb_hash) {
        GList* tags = g_hash_table_get_keys(tag_to_win_cb_hash);
        GList* curr = tags;
        while (curr) {
            wins_close_plugin(curr->data);
            curr = g_list_next(curr);
        }
        g_list_free(tags);
    }

    g_hash_table_remove(p_window_callbacks, plugin_name);
}

void
callbacks_close(void)
{
    g_hash_table_destroy(p_commands);
    g_hash_table_destroy(p_timed_functions);
    g_hash_table_destroy(p_window_callbacks);
}

void
callbacks_add_command(const char* const plugin_name, PluginCommand* command)
{
    GHashTable* command_hash = g_hash_table_lookup(p_commands, plugin_name);
    if (command_hash) {
        g_hash_table_insert(command_hash, strdup(command->command_name), command);
    } else {
        command_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_free_command);
        g_hash_table_insert(command_hash, strdup(command->command_name), command);
        g_hash_table_insert(p_commands, strdup(plugin_name), command_hash);
    }
    cmd_ac_add(command->command_name);
    cmd_ac_add_help(&command->command_name[1]);
}

void
callbacks_add_timed(const char* const plugin_name, PluginTimedFunction* timed_function)
{
    GList* timed_function_list = g_hash_table_lookup(p_timed_functions, plugin_name);
    if (timed_function_list) {
        // we assign this so we dont get: -Werror=unused-result
        timed_function_list = g_list_append(timed_function_list, timed_function);
    } else {
        timed_function_list = g_list_append(timed_function_list, timed_function);
        g_hash_table_insert(p_timed_functions, strdup(plugin_name), timed_function_list);
    }
}

gboolean
callbacks_win_exists(const char* const plugin_name, const char* tag)
{
    GHashTable* window_callbacks = g_hash_table_lookup(p_window_callbacks, plugin_name);
    if (window_callbacks) {
        PluginWindowCallback* cb = g_hash_table_lookup(window_callbacks, tag);
        if (cb) {
            return TRUE;
        }
    }

    return FALSE;
}

void
callbacks_remove_win(const char* const plugin_name, const char* const tag)
{
    GHashTable* window_callbacks = g_hash_table_lookup(p_window_callbacks, plugin_name);
    if (window_callbacks) {
        g_hash_table_remove(window_callbacks, tag);
    }
}

void
callbacks_add_window_handler(const char* const plugin_name, const char* tag, PluginWindowCallback* window_callback)
{
    GHashTable* window_callbacks = g_hash_table_lookup(p_window_callbacks, plugin_name);
    if (window_callbacks) {
        g_hash_table_insert(window_callbacks, strdup(tag), window_callback);
    } else {
        window_callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_free_window_callback);
        g_hash_table_insert(window_callbacks, strdup(tag), window_callback);
        g_hash_table_insert(p_window_callbacks, strdup(plugin_name), window_callbacks);
    }
}

void*
callbacks_get_window_handler(const char* tag)
{
    if (p_window_callbacks) {
        GList* window_callback_hashes = g_hash_table_get_values(p_window_callbacks);
        GList* curr_hash = window_callback_hashes;
        while (curr_hash) {
            GHashTable* window_callback_hash = curr_hash->data;
            PluginWindowCallback* callback = g_hash_table_lookup(window_callback_hash, tag);
            if (callback) {
                g_list_free(window_callback_hashes);
                return callback;
            }

            curr_hash = g_list_next(curr_hash);
        }

        g_list_free(window_callback_hashes);
        return NULL;
    } else {
        return NULL;
    }
}

gboolean
plugins_run_command(const char* const input)
{
    gchar** split = g_strsplit(input, " ", -1);

    GList* command_hashes = g_hash_table_get_values(p_commands);
    GList* curr_hash = command_hashes;
    while (curr_hash) {
        GHashTable* command_hash = curr_hash->data;

        PluginCommand* command = g_hash_table_lookup(command_hash, split[0]);
        if (command) {
            gboolean result;
            gchar** args = parse_args_with_freetext(input, command->min_args, command->max_args, &result);
            if (result == FALSE) {
                ui_invalid_command_usage(command->command_name, NULL);
                g_strfreev(split);
                g_list_free(command_hashes);
                return TRUE;
            } else {
                command->callback_exec(command, args);
                g_strfreev(split);
                g_strfreev(args);
                g_list_free(command_hashes);
                return TRUE;
            }
        }

        curr_hash = g_list_next(curr_hash);
    }

    g_list_free(command_hashes);
    g_strfreev(split);
    return FALSE;
}

CommandHelp*
plugins_get_help(const char* const cmd)
{
    GList* command_hashes = g_hash_table_get_values(p_commands);
    GList* curr_hash = command_hashes;
    while (curr_hash) {
        GHashTable* command_hash = curr_hash->data;

        PluginCommand* command = g_hash_table_lookup(command_hash, cmd);
        if (command) {
            g_list_free(command_hashes);
            return command->help;
        }

        curr_hash = g_list_next(curr_hash);
    }

    g_list_free(command_hashes);

    return NULL;
}

void
plugins_run_timed(void)
{
    GList* timed_functions_lists = g_hash_table_get_values(p_timed_functions);

    GList* curr_list = timed_functions_lists;
    while (curr_list) {
        GList* timed_function_list = curr_list->data;
        GList* curr = timed_function_list;
        while (curr) {
            PluginTimedFunction* timed_function = curr->data;

            gdouble elapsed = g_timer_elapsed(timed_function->timer, NULL);

            if (timed_function->interval_seconds > 0 && elapsed >= timed_function->interval_seconds) {
                timed_function->callback_exec(timed_function);
                g_timer_start(timed_function->timer);
            }

            curr = g_list_next(curr);
        }
        curr_list = g_list_next(curr_list);
    }

    g_list_free(timed_functions_lists);
}

GList*
plugins_get_command_names(void)
{
    GList* result = NULL;

    GList* command_hashes = g_hash_table_get_values(p_commands);
    GList* curr_hash = command_hashes;
    while (curr_hash) {
        GHashTable* command_hash = curr_hash->data;
        GList* commands = g_hash_table_get_keys(command_hash);
        GList* curr = commands;
        while (curr) {
            char* command = curr->data;
            result = g_list_append(result, command);
            curr = g_list_next(curr);
        }
        g_list_free(commands);
        curr_hash = g_list_next(curr_hash);
    }

    g_list_free(command_hashes);

    return result;
}