1
0
mirror of https://github.com/moparisthebest/hexchat synced 2024-12-02 14:12:17 -05:00
hexchat/src/fe-gtk/rawlog.c

180 lines
4.7 KiB
C
Raw Normal View History

2011-02-23 22:14:30 -05:00
/* X-Chat
* Copyright (C) 1998 Peter Zelezny.
*
* 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
2012-12-23 14:36:54 -05:00
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
2011-02-23 22:14:30 -05:00
*/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
2012-07-21 08:26:19 -04:00
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
2011-02-23 22:14:30 -05:00
#include "fe-gtk.h"
#include <gdk/gdkkeysyms.h>
2011-02-23 22:14:30 -05:00
2012-10-24 15:33:02 -04:00
#include "../common/hexchat.h"
#include "../common/hexchatc.h"
2011-02-23 22:14:30 -05:00
#include "../common/cfgfiles.h"
#include "../common/server.h"
#include "gtkutil.h"
#include "palette.h"
#include "maingui.h"
#include "rawlog.h"
#include "xtext.h"
#include "fkeys.h"
2011-02-23 22:14:30 -05:00
static void
close_rawlog (GtkWidget *wid, server *serv)
{
if (is_server (serv))
serv->gui->rawlog_window = 0;
}
static void
rawlog_save (server *serv, char *file)
{
int fh = -1;
if (file)
{
if (serv->gui->rawlog_window)
2012-10-30 06:35:39 -04:00
fh = hexchat_open_file (file, O_TRUNC | O_WRONLY | O_CREAT,
2011-02-23 22:14:30 -05:00
0600, XOF_DOMODE | XOF_FULLPATH);
if (fh != -1)
{
gtk_xtext_save (GTK_XTEXT (serv->gui->rawlog_textlist), fh);
close (fh);
}
}
}
static int
rawlog_clearbutton (GtkWidget * wid, server *serv)
{
gtk_xtext_clear (GTK_XTEXT (serv->gui->rawlog_textlist)->buffer, 0);
return FALSE;
}
static int
rawlog_savebutton (GtkWidget * wid, server *serv)
{
gtkutil_file_req (_("Save As..."), rawlog_save, serv, NULL, NULL, FRF_WRITE);
2011-02-23 22:14:30 -05:00
return FALSE;
}
static gboolean
rawlog_key_cb (GtkWidget * wid, GdkEventKey * key, gpointer userdata)
{
/* Copy rawlog selection to clipboard when Ctrl+Shift+C is pressed,
* but make sure not to copy twice, i.e. when auto-copy is enabled.
*/
if (!prefs.hex_text_autocopy_text &&
2013-09-15 03:21:56 -04:00
(key->keyval == GDK_KEY_c || key->keyval == GDK_KEY_C) &&
key->state & STATE_SHIFT &&
key->state & STATE_CTRL)
{
gtk_xtext_copy_selection (userdata);
}
return FALSE;
}
2011-02-23 22:14:30 -05:00
void
open_rawlog (struct server *serv)
{
GtkWidget *hbox, *vscrollbar, *vbox;
char tbuf[256];
if (serv->gui->rawlog_window)
{
mg_bring_tofront (serv->gui->rawlog_window);
return;
}
2012-10-31 00:31:35 -04:00
snprintf (tbuf, sizeof tbuf, _(DISPLAY_NAME": Raw Log (%s)"), serv->servername);
2011-02-23 22:14:30 -05:00
serv->gui->rawlog_window =
mg_create_generic_tab ("RawLog", tbuf, FALSE, TRUE, close_rawlog, serv,
640, 320, &vbox, serv);
gtkutil_destroy_on_esc (serv->gui->rawlog_window);
2011-02-23 22:14:30 -05:00
hbox = gtk_hbox_new (FALSE, 2);
gtk_container_add (GTK_CONTAINER (vbox), hbox);
gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
gtk_widget_show (hbox);
serv->gui->rawlog_textlist = gtk_xtext_new (colors, 0);
gtk_container_add (GTK_CONTAINER (hbox), serv->gui->rawlog_textlist);
2012-10-22 09:55:43 -04:00
gtk_xtext_set_font (GTK_XTEXT (serv->gui->rawlog_textlist), prefs.hex_text_font);
2011-02-23 22:14:30 -05:00
GTK_XTEXT (serv->gui->rawlog_textlist)->ignore_hidden = 1;
gtk_widget_show (serv->gui->rawlog_textlist);
vscrollbar = gtk_vscrollbar_new (GTK_XTEXT (serv->gui->rawlog_textlist)->adj);
gtk_box_pack_start (GTK_BOX (hbox), vscrollbar, FALSE, FALSE, 0);
show_and_unfocus (vscrollbar);
hbox = gtk_hbutton_box_new ();
gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_SPREAD);
gtk_box_pack_end (GTK_BOX (vbox), hbox, 0, 0, 0);
gtk_widget_show (hbox);
gtkutil_button (hbox, GTK_STOCK_CLEAR, NULL, rawlog_clearbutton,
2012-10-31 00:31:35 -04:00
serv, _("Clear Raw Log"));
2011-02-23 22:14:30 -05:00
gtkutil_button (hbox, GTK_STOCK_SAVE_AS, NULL, rawlog_savebutton,
serv, _("Save As..."));
/* Copy selection to clipboard when Ctrl+Shift+C is pressed AND text auto-copy is disabled */
g_signal_connect (G_OBJECT (serv->gui->rawlog_window), "key_press_event", G_CALLBACK (rawlog_key_cb), serv->gui->rawlog_textlist);
2011-02-23 22:14:30 -05:00
gtk_widget_show (serv->gui->rawlog_window);
}
void
fe_add_rawlog (server *serv, char *text, int len, int outbound)
{
char **split_text;
2011-02-23 22:14:30 -05:00
char *new_text;
int i;
2011-02-23 22:14:30 -05:00
if (!serv->gui->rawlog_window)
return;
split_text = g_strsplit (text, "\r\n", 0);
2011-02-23 22:14:30 -05:00
for (i = 0; i < g_strv_length (split_text); i++)
2011-02-23 22:14:30 -05:00
{
if (split_text[i][0] == 0)
break;
if (outbound)
new_text = g_strconcat ("\0034<<\017 ", split_text[i], NULL);
else
new_text = g_strconcat ("\0033>>\017 ", split_text[i], NULL);
gtk_xtext_append (GTK_XTEXT (serv->gui->rawlog_textlist)->buffer, new_text, strlen (new_text));
g_free (new_text);
2011-02-23 22:14:30 -05:00
}
g_strfreev (split_text);
2011-02-23 22:14:30 -05:00
}