1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00
irssi/src/fe-common/core/printtext.c

404 lines
9.9 KiB
C
Raw Normal View History

/*
printtext.c : irssi
Copyright (C) 1999-2000 Timo Sirainen
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "module.h"
#include "module-formats.h"
#include "modules.h"
#include "signals.h"
#include "commands.h"
#include "settings.h"
#include "levels.h"
#include "servers.h"
#include "themes.h"
#include "windows.h"
#include "printtext.h"
static int beep_msg_level, beep_when_away;
static int timestamps, msgs_timestamps;
static int timestamp_timeout;
static int signal_gui_print_text;
static int signal_print_text_stripped;
static int signal_print_text;
static int signal_print_text_finished;
static int signal_print_format;
static void print_line(TEXT_DEST_REC *dest, const char *text);
void printbeep(void)
{
signal_emit_id(signal_gui_print_text, 6, active_win, NULL, NULL,
GINT_TO_POINTER(PRINTFLAG_BEEP), "", MSGLEVEL_NEVER);
}
static void printformat_module_dest(const char *module, TEXT_DEST_REC *dest,
int formatnum, va_list va)
{
char *arglist[MAX_FORMAT_PARAMS];
char buffer[DEFAULT_FORMAT_ARGLIST_SIZE];
FORMAT_REC *formats;
THEME_REC *theme;
char *str;
theme = dest->window->theme == NULL ? current_theme :
dest->window->theme;
formats = g_hash_table_lookup(default_formats, module);
format_read_arglist(va, &formats[formatnum],
arglist, sizeof(arglist)/sizeof(char *),
buffer, sizeof(buffer));
signal_emit_id(signal_print_format, 5, theme, module,
dest, GINT_TO_POINTER(formatnum), arglist);
str = format_get_text_theme_args(theme, module, dest, formatnum, va);
if (*str != '\0') print_line(dest, str);
g_free(str);
}
void printformat_module_args(const char *module, void *server,
const char *target, int level,
int formatnum, va_list va)
{
TEXT_DEST_REC dest;
format_create_dest(&dest, server, target, level, NULL);
printformat_module_dest(module, &dest, formatnum, va);
}
void printformat_module(const char *module, void *server, const char *target, int level, int formatnum, ...)
{
va_list va;
va_start(va, formatnum);
printformat_module_args(module, server, target, level, formatnum, va);
va_end(va);
}
void printformat_module_window_args(const char *module, WINDOW_REC *window,
int level, int formatnum, va_list va)
{
TEXT_DEST_REC dest;
format_create_dest(&dest, NULL, NULL, level, window);
printformat_module_dest(module, &dest, formatnum, va);
}
void printformat_module_window(const char *module, WINDOW_REC *window,
int level, int formatnum, ...)
{
va_list va;
va_start(va, formatnum);
printformat_module_window_args(module, window, level, formatnum, va);
va_end(va);
}
static void print_line(TEXT_DEST_REC *dest, const char *text)
{
char *str, *tmp;
g_return_if_fail(dest != NULL);
g_return_if_fail(text != NULL);
tmp = format_get_line_start(current_theme, dest);
str = format_add_linestart(text, tmp);
g_free_not_null(tmp);
/* send the plain text version for logging etc.. */
tmp = strip_codes(str);
signal_emit_id(signal_print_text_stripped, 2, dest, tmp);
g_free(tmp);
signal_emit_id(signal_print_text, 2, dest, str);
g_free(str);
}
/* append string to `out', expand newlines. */
static void printtext_append_str(TEXT_DEST_REC *dest, GString *out, const char *str)
{
while (*str != '\0') {
if (*str != '\n')
g_string_append_c(out, *str);
else {
print_line(dest, out->str);
g_string_truncate(out, 0);
}
str++;
}
}
static char *printtext_get_args(TEXT_DEST_REC *dest, const char *str, va_list va)
{
GString *out;
char *ret;
out = g_string_new(NULL);
for (; *str != '\0'; str++) {
if (*str != '%') {
g_string_append_c(out, *str);
continue;
}
if (*++str == '\0')
break;
/* standard parameters */
switch (*str) {
case 's': {
char *s = (char *) va_arg(va, char *);
if (s && *s) printtext_append_str(dest, out, s);
break;
}
case 'd': {
int d = (int) va_arg(va, int);
g_string_sprintfa(out, "%d", d);
break;
}
case 'f': {
double f = (double) va_arg(va, double);
g_string_sprintfa(out, "%0.2f", f);
break;
}
case 'u': {
unsigned int d = (unsigned int) va_arg(va, unsigned int);
g_string_sprintfa(out, "%u", d);
break;
}
case 'l': {
long d = (long) va_arg(va, long);
if (*++str != 'd' && *str != 'u') {
g_string_sprintfa(out, "%ld", d);
str--;
} else {
if (*str == 'd')
g_string_sprintfa(out, "%ld", d);
else
g_string_sprintfa(out, "%lu", d);
}
break;
}
default:
if (!format_expand_styles(out, *str, dest)) {
g_string_append_c(out, '%');
g_string_append_c(out, *str);
}
break;
}
}
ret = out->str;
g_string_free(out, FALSE);
return ret;
}
/* Write text to target - convert color codes */
void printtext(void *server, const char *target, int level, const char *text, ...)
{
TEXT_DEST_REC dest;
char *str;
va_list va;
g_return_if_fail(text != NULL);
format_create_dest(&dest, server, target, level, NULL);
va_start(va, text);
str = printtext_get_args(&dest, text, va);
va_end(va);
print_line(&dest, str);
g_free(str);
}
void printtext_window(WINDOW_REC *window, int level, const char *text, ...)
{
TEXT_DEST_REC dest;
char *str;
va_list va;
g_return_if_fail(text != NULL);
format_create_dest(&dest, NULL, NULL, level,
window != NULL ? window : active_win);
va_start(va, text);
str = printtext_get_args(&dest, text, va);
va_end(va);
print_line(&dest, str);
g_free(str);
}
#define show_timestamp(level) \
((level & (MSGLEVEL_NEVER|MSGLEVEL_LASTLOG)) == 0 && \
(timestamps || (msgs_timestamps && ((level) & MSGLEVEL_MSGS))))
static char *get_timestamp(TEXT_DEST_REC *dest)
{
struct tm *tm;
time_t t;
int diff;
if (!show_timestamp(dest->level))
return NULL;
t = time(NULL);
if (timestamp_timeout > 0) {
diff = t - dest->window->last_timestamp;
dest->window->last_timestamp = t;
if (diff < timestamp_timeout)
return NULL;
}
tm = localtime(&t);
return format_get_text_theme(NULL, MODULE_NAME, dest, IRCTXT_TIMESTAMP,
tm->tm_year+1900,
tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
static char *get_server_tag(TEXT_DEST_REC *dest)
{
SERVER_REC *server;
server = dest->server;
if (server == NULL || servers == NULL || servers->next == NULL ||
(dest->window->active != NULL && dest->window->active->server == server))
return NULL;
return format_get_text_theme(NULL, MODULE_NAME, dest,
IRCTXT_SERVERTAG, server->tag);
}
static void msg_beep_check(SERVER_REC *server, int level)
{
if (level != 0 && (level & MSGLEVEL_NOHILIGHT) == 0 &&
(beep_msg_level & level) &&
(beep_when_away || (server != NULL && !server->usermode_away))) {
printbeep();
}
}
static char *fix_line_start(TEXT_DEST_REC *dest, const char *text)
{
char *timestamp, *servertag;
char *linestart, *str;
timestamp = get_timestamp(dest);
servertag = get_server_tag(dest);
if (timestamp == NULL && servertag == NULL)
return g_strdup(text);
linestart = g_strconcat(timestamp != NULL ? timestamp : "",
servertag, NULL);
str = format_add_linestart(text, linestart);
g_free(linestart);
g_free_not_null(timestamp);
g_free_not_null(servertag);
return str;
}
static void sig_print_text(TEXT_DEST_REC *dest, const char *text)
{
char *str;
g_return_if_fail(dest != NULL);
g_return_if_fail(text != NULL);
msg_beep_check(dest->server, dest->level);
dest->window->last_line = time(NULL);
format_newline(dest->window);
str = fix_line_start(dest, text);
format_send_to_gui(dest, str);
g_free(str);
signal_emit_id(signal_print_text_finished, 1, dest->window);
}
void printtext_multiline(void *server, const char *target, int level,
const char *format, const char *text)
{
char **lines, **tmp;
g_return_if_fail(format != NULL);
g_return_if_fail(text != NULL);
lines = g_strsplit(text, "\n", -1);
for (tmp = lines; *tmp != NULL; tmp++)
printtext(NULL, NULL, MSGLEVEL_NEVER, format, *tmp);
g_strfreev(lines);
}
static void sig_gui_dialog(const char *type, const char *text)
{
char *format;
if (g_strcasecmp(type, "warning") == 0)
format = _("%_Warning:%_ %s");
else if (g_strcasecmp(type, "error") == 0)
format = _("%_Error:%_ %s");
else
format = "%s";
printtext_multiline(NULL, NULL, MSGLEVEL_NEVER, format, text);
}
static void read_settings(void)
{
timestamps = settings_get_bool("timestamps");
timestamp_timeout = settings_get_int("timestamp_timeout");
msgs_timestamps = settings_get_bool("msgs_timestamps");
beep_msg_level = level2bits(settings_get_str("beep_on_msg"));
beep_when_away = settings_get_bool("beep_when_away");
}
void printtext_init(void)
{
settings_add_int("misc", "timestamp_timeout", 0);
signal_gui_print_text = signal_get_uniq_id("gui print text");
signal_print_text_stripped = signal_get_uniq_id("print text stripped");
signal_print_text = signal_get_uniq_id("print text");
signal_print_text_finished = signal_get_uniq_id("print text finished");
signal_print_format = signal_get_uniq_id("print format");
read_settings();
signal_add("print text", (SIGNAL_FUNC) sig_print_text);
signal_add("gui dialog", (SIGNAL_FUNC) sig_gui_dialog);
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
}
void printtext_deinit(void)
{
signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
signal_remove("gui dialog", (SIGNAL_FUNC) sig_gui_dialog);
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
}