1 /* X-Chat
2  * Copyright (C) 1998 Peter Zelezny.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include <stdio.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 
24 #ifdef WIN32
25 #include <io.h>
26 #else
27 #include <unistd.h>
28 #endif
29 
30 #include "fe-gtk.h"
31 
32 #include <gdk/gdkkeysyms.h>
33 
34 #include "../common/hexchat.h"
35 #include "../common/hexchatc.h"
36 #include "../common/cfgfiles.h"
37 #include "../common/server.h"
38 #include "gtkutil.h"
39 #include "palette.h"
40 #include "maingui.h"
41 #include "rawlog.h"
42 #include "xtext.h"
43 #include "fkeys.h"
44 
45 static void
close_rawlog(GtkWidget * wid,server * serv)46 close_rawlog (GtkWidget *wid, server *serv)
47 {
48 	if (is_server (serv))
49 		serv->gui->rawlog_window = 0;
50 }
51 
52 static void
rawlog_save(server * serv,char * file)53 rawlog_save (server *serv, char *file)
54 {
55 	int fh = -1;
56 
57 	if (file)
58 	{
59 		if (serv->gui->rawlog_window)
60 			fh = hexchat_open_file (file, O_TRUNC | O_WRONLY | O_CREAT,
61 										 0600, XOF_DOMODE | XOF_FULLPATH);
62 		if (fh != -1)
63 		{
64 			gtk_xtext_save (GTK_XTEXT (serv->gui->rawlog_textlist), fh);
65 			close (fh);
66 		}
67 	}
68 }
69 
70 static int
rawlog_clearbutton(GtkWidget * wid,server * serv)71 rawlog_clearbutton (GtkWidget * wid, server *serv)
72 {
73 	gtk_xtext_clear (GTK_XTEXT (serv->gui->rawlog_textlist)->buffer, 0);
74 	return FALSE;
75 }
76 
77 static int
rawlog_savebutton(GtkWidget * wid,server * serv)78 rawlog_savebutton (GtkWidget * wid, server *serv)
79 {
80 	gtkutil_file_req (_("Save As..."), rawlog_save, serv, NULL, NULL, FRF_WRITE);
81 	return FALSE;
82 }
83 
84 static gboolean
rawlog_key_cb(GtkWidget * wid,GdkEventKey * key,gpointer userdata)85 rawlog_key_cb (GtkWidget * wid, GdkEventKey * key, gpointer userdata)
86 {
87 	/* Copy rawlog selection to clipboard when Ctrl+Shift+C is pressed,
88 	 * but make sure not to copy twice, i.e. when auto-copy is enabled.
89 	 */
90 	if (!prefs.hex_text_autocopy_text &&
91 		(key->keyval == GDK_KEY_c || key->keyval == GDK_KEY_C) &&
92 		key->state & STATE_SHIFT &&
93 		key->state & STATE_CTRL)
94 	{
95 		gtk_xtext_copy_selection (userdata);
96 	}
97 	return FALSE;
98 }
99 
100 void
open_rawlog(struct server * serv)101 open_rawlog (struct server *serv)
102 {
103 	GtkWidget *bbox, *scrolledwindow, *vbox;
104 	char tbuf[256];
105 
106 	if (serv->gui->rawlog_window)
107 	{
108 		mg_bring_tofront (serv->gui->rawlog_window);
109 		return;
110 	}
111 
112 	g_snprintf (tbuf, sizeof tbuf, _("Raw Log (%s) - %s"), serv->servername, _(DISPLAY_NAME));
113 	serv->gui->rawlog_window =
114 		mg_create_generic_tab ("RawLog", tbuf, FALSE, TRUE, close_rawlog, serv,
115 							 640, 320, &vbox, serv);
116 	gtkutil_destroy_on_esc (serv->gui->rawlog_window);
117 
118 	scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
119 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
120 	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow), GTK_SHADOW_IN);
121 	gtk_container_add (GTK_CONTAINER (vbox), scrolledwindow);
122 
123 	serv->gui->rawlog_textlist = gtk_xtext_new (colors, 0);
124 	gtk_container_add (GTK_CONTAINER (scrolledwindow), serv->gui->rawlog_textlist);
125 	gtk_xtext_set_font (GTK_XTEXT (serv->gui->rawlog_textlist), prefs.hex_text_font);
126 	GTK_XTEXT (serv->gui->rawlog_textlist)->ignore_hidden = 1;
127 
128 	bbox = gtk_hbutton_box_new ();
129 	gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_SPREAD);
130 	gtk_box_pack_end (GTK_BOX (vbox), bbox, 0, 0, 4);
131 
132 	gtkutil_button (bbox, GTK_STOCK_CLEAR, NULL, rawlog_clearbutton,
133 						 serv, _("Clear Raw Log"));
134 
135 	gtkutil_button (bbox, GTK_STOCK_SAVE_AS, NULL, rawlog_savebutton,
136 						 serv, _("Save As..."));
137 
138 	/* Copy selection to clipboard when Ctrl+Shift+C is pressed AND text auto-copy is disabled */
139 	g_signal_connect (G_OBJECT (serv->gui->rawlog_window), "key_press_event", G_CALLBACK (rawlog_key_cb), serv->gui->rawlog_textlist);
140 
141 	gtk_widget_show_all (serv->gui->rawlog_window);
142 }
143 
144 void
fe_add_rawlog(server * serv,char * text,int len,int outbound)145 fe_add_rawlog (server *serv, char *text, int len, int outbound)
146 {
147 	char **split_text;
148 	char *new_text;
149 	size_t i;
150 
151 	if (!serv->gui->rawlog_window)
152 		return;
153 
154 	split_text = g_strsplit (text, "\r\n", 0);
155 
156 	for (i = 0; i < g_strv_length (split_text); i++)
157 	{
158 		if (split_text[i][0] == 0)
159 			break;
160 
161 		if (outbound)
162 			new_text = g_strconcat ("\0034<<\017 ", split_text[i], NULL);
163 		else
164 			new_text = g_strconcat ("\0033>>\017 ", split_text[i], NULL);
165 
166 		gtk_xtext_append (GTK_XTEXT (serv->gui->rawlog_textlist)->buffer, new_text, strlen (new_text), 0);
167 
168 		g_free (new_text);
169 	}
170 
171 	g_strfreev (split_text);
172 }
173