1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Added window handling api

This commit is contained in:
James Booth 2013-09-09 01:24:05 +01:00
parent e657ec94ba
commit e639f4cea6
18 changed files with 335 additions and 2 deletions

View File

@ -1229,6 +1229,10 @@ cmd_execute_default(const char * const inp)
}
break;
case WIN_PLUGIN:
plugins_win_process_line(recipient, inp);
break;
default:
break;
}
@ -2540,6 +2544,7 @@ _cmd_status(gchar **args, struct cmd_help_t help)
}
break;
case WIN_CONSOLE:
case WIN_PLUGIN:
if (usr != NULL) {
usr_jid = roster_barejid_from_name(usr);
if (usr_jid == NULL) {
@ -2616,6 +2621,7 @@ _cmd_info(gchar **args, struct cmd_help_t help)
}
break;
case WIN_CONSOLE:
case WIN_PLUGIN:
if (usr != NULL) {
usr_jid = roster_barejid_from_name(usr);
if (usr_jid == NULL) {
@ -2669,6 +2675,7 @@ _cmd_caps(gchar **args, struct cmd_help_t help)
break;
case WIN_CHAT:
case WIN_CONSOLE:
case WIN_PLUGIN:
if (args[0] != NULL) {
Jid *jid = jid_create(args[0]);
@ -2745,6 +2752,7 @@ _cmd_software(gchar **args, struct cmd_help_t help)
break;
case WIN_CHAT:
case WIN_CONSOLE:
case WIN_PLUGIN:
if (args[0] != NULL) {
Jid *jid = jid_create(args[0]);
@ -3039,7 +3047,7 @@ _cmd_tiny(gchar **args, struct cmd_help_t help)
ui_current_error_line(error->str);
}
g_string_free(error, TRUE);
} else if (win_type != WIN_CONSOLE) {
} else if (win_type != WIN_CONSOLE && win_type != WIN_PLUGIN) {
char *tiny = tinyurl_get(url);
if (tiny != NULL) {

View File

@ -28,6 +28,7 @@
#include "plugins/callbacks.h"
#include "profanity.h"
#include "ui/notifier.h"
#include "ui/windows.h"
#include "ui/ui.h"
void
@ -124,3 +125,43 @@ api_log_error(const char *message)
{
log_error("%s", message);
}
int
api_win_exists(char *tag)
{
return (wins_get_by_recipient(tag) != NULL);
}
void
api_win_create(char *tag, void *callback,
void(*callback_func)(PluginWindowCallback *window_callback, char *tag, const char * const line))
{
PluginWindowCallback *window = malloc(sizeof(PluginWindowCallback));
window->callback = callback;
window->callback_func = callback_func;
callbacks_add_window_handler(tag, window);
wins_new(tag, WIN_PLUGIN);
}
void
api_win_focus(char *tag)
{
ProfWin *win = wins_get_by_recipient(tag);
int num = wins_get_num(win);
ui_switch_win(num);
}
void
api_win_process_line(char *tag, const char * const line)
{
PluginWindowCallback *window = callbacks_get_window_handler(tag);
window->callback_func(window, tag, line);
}
void
api_win_show(char *tag, char *line)
{
ProfWin *window = wins_get_by_recipient(tag);
win_print_time(window, '-');
wprintw(window->win, "%s\n", line);
}

View File

@ -42,4 +42,11 @@ void api_log_info(const char *message);
void api_log_warning(const char *message);
void api_log_error(const char *message);
int api_win_exists(char *tag);
void api_win_create(char *tag, void *callback,
void(*callback_func)(PluginWindowCallback *window_callback, char *tag, char *line));
void api_win_focus(char *tag);
void api_win_process_line(char *tag, const char * const line);
void api_win_show(char *tag, char *line);
#endif

View File

@ -37,6 +37,10 @@ typedef struct timed_wrapper_t {
void(*func)(void);
} TimedWrapper;
typedef struct window_wrapper_t {
void(*func)(char *tag, char *line);
} WindowWrapper;
static void
c_api_cons_alert(void)
{
@ -111,6 +115,38 @@ c_api_log_error(const char *message)
api_log_error(message);
}
int
c_api_win_exists(char *tag)
{
return api_win_exists(tag);
}
void
c_api_win_create(char *tag, void(*callback)(char *tag, char *line))
{
WindowWrapper *wrapper = malloc(sizeof(WindowWrapper));
wrapper->func = callback;
api_win_create(tag, wrapper, c_window_callback);
}
void
c_api_win_focus(char *tag)
{
api_win_focus(tag);
}
void
c_api_win_process_line(char *tag, char *line)
{
api_win_process_line(tag, line);
}
void
c_api_win_show(char *tag, char *line)
{
api_win_show(tag, line);
}
void
c_command_callback(PluginCommand *command, gchar **args)
{
@ -127,6 +163,14 @@ c_timed_callback(PluginTimedFunction *timed_function)
f();
}
void
c_window_callback(PluginWindowCallback *window_callback, char *tag, char *line)
{
WindowWrapper *wrapper = window_callback->callback;
void(*f)(char *tag, char *line) = wrapper->func;
f(tag, line);
}
void
c_api_init(void)
{
@ -141,4 +185,9 @@ c_api_init(void)
prof_log_info = c_api_log_info;
prof_log_warning = c_api_log_warning;
prof_log_error = c_api_log_error;
prof_win_exists = c_api_win_exists;
prof_win_create = c_api_win_create;
prof_win_focus = c_api_win_focus;
prof_win_process_line = c_api_win_process_line;
prof_win_show = c_api_win_show;
}

View File

@ -26,3 +26,4 @@ void c_api_init(void);
void c_command_callback(PluginCommand *command, gchar **args);
void c_timed_callback(PluginTimedFunction *timed_function);
void c_window_callback(PluginWindowCallback *window_callback, char *tag, char *line);

View File

@ -32,6 +32,7 @@
static GSList *p_commands = NULL;
static GSList *p_timed_functions = NULL;
static GHashTable *p_window_callbacks = NULL;
void
callbacks_add_command(PluginCommand *command)
@ -46,6 +47,26 @@ callbacks_add_timed(PluginTimedFunction *timed_function)
p_timed_functions = g_slist_append(p_timed_functions, timed_function);
}
void
callbacks_add_window_handler(char *tag, PluginWindowCallback *window_callback)
{
if (p_window_callbacks == NULL) {
p_window_callbacks = g_hash_table_new(g_str_hash, g_str_equal);
}
g_hash_table_insert(p_window_callbacks, tag, window_callback);
}
void *
callbacks_get_window_handler(char *tag)
{
if (p_window_callbacks != NULL) {
return g_hash_table_lookup(p_window_callbacks, tag);
} else {
return NULL;
}
}
gboolean
plugins_run_command(const char * const input)
{

View File

@ -43,7 +43,14 @@ typedef struct p_timed_function {
GTimer *timer;
} PluginTimedFunction;
typedef struct p_window_input_callback {
void *callback;
void (*callback_func)(struct p_window_input_callback *window_callback, char *tag, const char * const line);
} PluginWindowCallback;
void callbacks_add_command(PluginCommand *command);
void callbacks_add_timed(PluginTimedFunction *timed_function);
void callbacks_add_window_handler(char *tag, PluginWindowCallback *window_callback);
void * callbacks_get_window_handler(char *tag);
#endif

View File

@ -27,6 +27,7 @@
#include "config/preferences.h"
#include "log.h"
#include "plugins/callbacks.h"
#include "plugins/api.h"
#include "plugins/plugins.h"
#include "plugins/python_plugins.h"
#include "plugins/python_api.h"
@ -113,6 +114,12 @@ plugins_get_lang_string(ProfPlugin *plugin)
}
}
void
plugins_win_process_line(char *win, const char * const line)
{
api_win_process_line(win, line);
}
void
plugins_on_start(void)
{

View File

@ -77,4 +77,5 @@ gboolean plugins_run_command(const char * const cmd);
void plugins_run_timed(void);
gchar * plugins_get_dir(void);
void plugins_win_process_line(char *win, const char * const line);
#endif

View File

@ -22,6 +22,7 @@
#include <stdlib.h>
#include "plugins/profapi.h"
#include "plugins/callbacks.h"
void (*prof_cons_alert)(void) = NULL;
@ -43,3 +44,9 @@ void (*prof_log_debug)(const char *message) = NULL;
void (*prof_log_info)(const char *message) = NULL;
void (*prof_log_warning)(const char *message) = NULL;
void (*prof_log_error)(const char *message) = NULL;
int (*prof_win_exists)(PROF_WIN_TAG win) = NULL;
void (*prof_win_create)(PROF_WIN_TAG win, void(*input_handler)(PROF_WIN_TAG win, char *line)) = NULL;
void (*prof_win_focus)(PROF_WIN_TAG win) = NULL;
void (*prof_win_process_line)(PROF_WIN_TAG win, char *line) = NULL;
void (*prof_win_show)(PROF_WIN_TAG win, char *line) = NULL;

View File

@ -23,6 +23,8 @@
#ifndef PROF_API_H
#define PROF_API_H
typedef char* PROF_WIN_TAG;
void (*prof_cons_alert)(void);
void (*prof_cons_show)(const char * const message);
@ -43,4 +45,10 @@ void (*prof_log_info)(const char *message);
void (*prof_log_warning)(const char *message);
void (*prof_log_error)(const char *message);
int (*prof_win_exists)(PROF_WIN_TAG win);
void (*prof_win_create)(PROF_WIN_TAG win, void(*input_handler)(PROF_WIN_TAG win, char *line));
void (*prof_win_focus)(PROF_WIN_TAG win);
void (*prof_win_process_line)(PROF_WIN_TAG win, char *line);
void (*prof_win_show)(PROF_WIN_TAG win, char *line);
#endif

View File

@ -171,6 +171,79 @@ python_api_log_error(PyObject *self, PyObject *args)
return Py_BuildValue("");
}
static PyObject *
python_api_win_exists(PyObject *self, PyObject *args)
{
char *tag = NULL;
if (!PyArg_ParseTuple(args, "s", &tag)) {
return Py_BuildValue("");
}
if (api_win_exists(tag)) {
return Py_BuildValue("i", 1);
} else {
return Py_BuildValue("i", 0);
}
}
static PyObject *
python_api_win_create(PyObject *self, PyObject *args)
{
char *tag = NULL;
PyObject *p_callback = NULL;
if (!PyArg_ParseTuple(args, "sO", &tag, &p_callback)) {
return Py_BuildValue("");
}
if (p_callback && PyCallable_Check(p_callback)) {
api_win_create(tag, p_callback, python_window_callback);
}
return Py_BuildValue("");
}
static PyObject *
python_api_win_focus(PyObject *self, PyObject *args)
{
char *tag = NULL;
if (!PyArg_ParseTuple(args, "s", &tag)) {
return Py_BuildValue("");
}
api_win_focus(tag);
return Py_BuildValue("");
}
static PyObject *
python_api_win_process_line(PyObject *self, PyObject *args)
{
char *tag = NULL;
char *line = NULL;
if (!PyArg_ParseTuple(args, "ss", &tag, &line)) {
return Py_BuildValue("");
}
api_win_process_line(tag, line);
return Py_BuildValue("");
}
static PyObject *
python_api_win_show(PyObject *self, PyObject *args)
{
char *tag = NULL;
char *line = NULL;
if (!PyArg_ParseTuple(args, "ss", &tag, &line)) {
return Py_BuildValue("");
}
api_win_show(tag, line);
return Py_BuildValue("");
}
void
python_command_callback(PluginCommand *command, gchar **args)
{
@ -218,6 +291,20 @@ python_timed_callback(PluginTimedFunction *timed_function)
PyObject_CallObject(timed_function->callback, NULL);
}
void
python_window_callback(PluginWindowCallback *window_callback, char *tag, char *line)
{
PyObject *p_args = NULL;
p_args = Py_BuildValue("ss", tag, line);
PyObject_CallObject(window_callback->callback, p_args);
Py_XDECREF(p_args);
if (PyErr_Occurred()) {
PyErr_Print();
PyErr_Clear();
}
}
static PyMethodDef apiMethods[] = {
{ "cons_alert", python_api_cons_alert, METH_NOARGS, "Highlight the console window in the status bar." },
{ "cons_show", python_api_cons_show, METH_VARARGS, "Print a line to the console." },
@ -230,6 +317,11 @@ static PyMethodDef apiMethods[] = {
{ "log_info", python_api_log_info, METH_VARARGS, "Log an info message" },
{ "log_warning", python_api_log_warning, METH_VARARGS, "Log a warning message" },
{ "log_error", python_api_log_error, METH_VARARGS, "Log an error message" },
{ "win_exists", python_api_win_exists, METH_VARARGS, "Determine whether a window exists." },
{ "win_create", python_api_win_create, METH_VARARGS, "Create a new window." },
{ "win_focus", python_api_win_focus, METH_VARARGS, "Focus a window." },
{ "win_show", python_api_win_show, METH_VARARGS, "Show text in the window." },
{ "win_process_line", python_api_win_process_line, METH_VARARGS, "Send a line of input to a window." },
{ NULL, NULL, 0, NULL }
};

View File

@ -29,5 +29,6 @@ void python_shutdown(void);
void python_command_callback(PluginCommand *command, gchar **args);
void python_timed_callback(PluginTimedFunction *timed_function);
void python_window_callback(PluginWindowCallback *window_callback, char *tag, char *line);
#endif

View File

@ -151,6 +151,57 @@ ruby_api_log_error(VALUE self, VALUE v_message)
return Qnil;
}
static VALUE
ruby_api_win_exists(VALUE self, VALUE v_tag)
{
char *tag = STR2CSTR(v_tag);
if (api_win_exists(tag)) {
return Qtrue;
} else {
return Qfalse;
}
}
static VALUE
ruby_api_win_create(VALUE self, VALUE v_tag, VALUE v_callback)
{
char *tag = STR2CSTR(v_tag);
api_win_create(tag, (void*)v_callback, ruby_window_callback);
return Qnil;
}
static VALUE
ruby_api_win_focus(VALUE self, VALUE v_tag)
{
char *tag = STR2CSTR(v_tag);
api_win_focus(tag);
return Qnil;
}
static VALUE
ruby_api_win_process_line(VALUE self, VALUE v_tag, VALUE v_line)
{
char *tag = STR2CSTR(v_tag);
char *line = STR2CSTR(v_line);
api_win_process_line(tag, line);
return Qnil;
}
static VALUE
ruby_api_win_show(VALUE self, VALUE v_tag, VALUE v_line)
{
char *tag = STR2CSTR(v_tag);
char *line = STR2CSTR(v_line);
api_win_show(tag, line);
return Qnil;
}
void
ruby_command_callback(PluginCommand *command, gchar **args)
{
@ -187,6 +238,14 @@ ruby_timed_callback(PluginTimedFunction *timed_function)
rb_funcall((VALUE)timed_function->callback, rb_intern("call"), 0);
}
void
ruby_window_callback(PluginWindowCallback *window_callback, char *tag, char *line)
{
rb_funcall((VALUE)window_callback->callback, rb_intern("call"), 2,
rb_str_new2(tag), rb_str_new2(line));
}
static VALUE prof_module;
void
@ -205,4 +264,9 @@ ruby_api_init(void)
rb_define_module_function(prof_module, "log_info", RUBY_METHOD_FUNC(ruby_api_log_info), 1);
rb_define_module_function(prof_module, "log_warning", RUBY_METHOD_FUNC(ruby_api_log_warning), 1);
rb_define_module_function(prof_module, "log_error", RUBY_METHOD_FUNC(ruby_api_log_error), 1);
rb_define_module_function(prof_module, "win_exists", RUBY_METHOD_FUNC(ruby_api_win_exists), 1);
rb_define_module_function(prof_module, "win_create", RUBY_METHOD_FUNC(ruby_api_win_create), 2);
rb_define_module_function(prof_module, "win_focus", RUBY_METHOD_FUNC(ruby_api_win_focus), 1);
rb_define_module_function(prof_module, "win_process_line", RUBY_METHOD_FUNC(ruby_api_win_process_line), 2);
rb_define_module_function(prof_module, "win_show", RUBY_METHOD_FUNC(ruby_api_win_show), 2);
}

View File

@ -29,5 +29,6 @@ void ruby_shutdown(void);
void ruby_command_callback(PluginCommand *command, gchar **args);
void ruby_timed_callback(PluginTimedFunction *timed_function);
void ruby_window_callback(PluginWindowCallback *window_callback, char *tag, char *line);
#endif

View File

@ -41,7 +41,8 @@ typedef enum {
WIN_CHAT,
WIN_MUC,
WIN_PRIVATE,
WIN_DUCK
WIN_DUCK,
WIN_PLUGIN
} win_type_t;
typedef struct prof_win_t {

View File

@ -383,6 +383,7 @@ wins_create_summary(void)
GString *priv_string;
GString *muc_string;
GString *duck_string;
GString *plugin_string;
switch (window->type)
{
@ -455,7 +456,21 @@ wins_create_summary(void)
g_string_free(duck_string, TRUE);
break;
case WIN_PLUGIN:
plugin_string = g_string_new("");
g_string_printf(plugin_string, "%d: %s plugin", ui_index, window->from);
if (window->unread > 0) {
GString *plugin_unread = g_string_new("");
g_string_printf(plugin_unread, ", %d unread", window->unread);
g_string_append(plugin_string, plugin_unread->str);
g_string_free(plugin_unread, TRUE);
}
result = g_slist_append(result, strdup(plugin_string->str));
g_string_free(plugin_string, TRUE);
break;
default:
break;
}

View File

@ -23,6 +23,8 @@
#ifndef WINDOWS_H
#define WINDOWS_H
#include "ui/window.h"
void wins_init(void);
ProfWin * wins_get_console(void);
ProfWin * wins_get_current(void);