changeset 111:d927bb34e2c6

[gaim-migrate @ 121] I'm spending way to much time on this plugin. I added an xchat-like config menu. I think it was more to prove to myself that configuring plugins is cool. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Thu, 13 Apr 2000 08:05:57 +0000
parents f7c6366ca703
children a57fd3390ee4
files plugins/spellchk.c
diffstat 1 files changed, 295 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/spellchk.c	Wed Apr 12 22:18:15 2000 +0000
+++ b/plugins/spellchk.c	Thu Apr 13 08:05:57 2000 +0000
@@ -1,9 +1,25 @@
+/*
+ * A lot of this code (especially the config code) was taken directly
+ * or nearly directly from xchat, version 1.4.2 by Peter Zelezny and others.
+ *
+ * TODO:
+ * 	? I think i did everything i want to with it.
+ *
+ * BUGS:
+ * 	If you have a numeric replacement, and is preceded by numeric text,
+ * 	it doesn't catch it. I don't know why. Probably because it's not
+ * 	designed to work with numbers.
+ */
 #define GAIM_PLUGINS
 #include "gaim.h"
 
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
 
 struct replace_words {
 	char *bad;
@@ -43,31 +59,91 @@
 	}
 }
 
-void gaim_plugin_init(void *handle) {
-	struct replace_words *p;
-	FILE *file;
-	char buffer[256];
-	char *good;
-	char *bad;
+int buf_get_line(char *ibuf, char **buf, int *position, int len) {
+	int pos = *position, spos = pos;
+
+	if (pos == len)
+		return 0;
+
+	while (ibuf[pos++] != '\n') {
+		if (pos == len)
+			return 0;
+	}
+	pos--;
+	ibuf[pos] = 0;
+	*buf = &ibuf[spos];
+	pos++;
+	*position = pos;
+	return 1;
+}
+
+void load_conf() {
+	char *defaultconf = "BAD r\nGOOD are\n\n"
+			"BAD u\nGOOD you\n\n"
+			"BAD teh\nGOOD the\n\n";
+	char *buf, *ibuf;
+	char name[82];
+	char cmd[256];
+	int fh, pnt = 0;
+	struct stat st;
+
+	if (words != NULL)
+		g_list_free(words);
+	words = NULL;
 
-	sprintf(buffer, "%s/.gaim/dict", getenv("HOME"));
-	file = fopen(buffer, "r");
-	while (fgets(buffer, sizeof buffer, file)) {
-		buffer[strlen(buffer) - 1] = 0;
-		p = malloc(sizeof *p);
-		good = strdup(strpbrk(strpbrk(buffer, " \t\r\f\n"),
-		       "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"));
-		bad = strpbrk(buffer, " \t\r\f\n");
-		bad[0] = 0;
-		bad = strdup(buffer);
-		p->bad = bad;
-		p->good = good;
-		words = g_list_append(words, p);
+	buf = malloc(1000);
+	snprintf(buf, 1000, "%s/.gaim/dict", getenv("HOME"));
+	fh = open(buf, O_RDONLY);
+	if (fh == -1) {
+		fh = open(buf, O_TRUNC | O_WRONLY | O_CREAT);
+		if (fh != -1) {
+			write(fh, defaultconf, strlen(defaultconf));
+			close(fh);
+			free(buf);
+			load_conf();
+		}
+		return;
 	}
+	free(buf);
+	if (fstat(fh, &st) != 0)
+		return;
+	ibuf = malloc(st.st_size);
+	read(fh, ibuf, st.st_size);
+	close(fh);
+
+	cmd[0] = 0;
+	name[0] = 0;
+
+	while(buf_get_line(ibuf, &buf, &pnt, st.st_size)) {
+		if (*buf != '#') {
+			if (!strncasecmp(buf, "BAD ", 4))
+				strncpy(name, buf + 4, 81);
+			if (!strncasecmp(buf, "GOOD ", 5)) {
+				strncpy(cmd, buf + 5, 255);
+				if (*name) {
+					struct replace_words *r;
+					r = malloc(sizeof *r);
+					r->bad = strdup(name);
+					r->good = strdup(cmd);
+					words = g_list_append(words, r);
+					cmd[0] = 0;
+					name[0] = 0;
+				}
+			}
+		}
+	}
+	free(ibuf);
+}
+
+void gaim_plugin_init(void *handle) {
+	load_conf();
 
 	gaim_signal_connect(handle, event_im_send, spell_check, NULL);
 }
 
+void gaim_plugin_remove() {
+}
+
 char *name() {
 	return "IM Spell Check";
 }
@@ -161,3 +237,203 @@
 	*mes = new;
 	free(tmp);
 }
+
+GtkWidget *configwin = NULL;
+GtkWidget *list;
+GtkWidget *bad_entry;
+GtkWidget *good_entry;
+
+void row_unselect() {
+	gtk_entry_set_text(GTK_ENTRY(bad_entry), "");
+	gtk_entry_set_text(GTK_ENTRY(good_entry), "");
+}
+
+void row_select() {
+	char *badwrd, *goodwrd;
+	int row;
+
+	if (GTK_CLIST(list)->selection)
+		row = (int) GTK_CLIST (list)->selection->data;
+	else
+		row = -1;
+	if (row != -1) {
+		gtk_clist_get_text(GTK_CLIST(list), row, 0, &badwrd);
+		gtk_clist_get_text(GTK_CLIST(list), row, 1, &goodwrd);
+		gtk_entry_set_text(GTK_ENTRY(bad_entry), badwrd);
+		gtk_entry_set_text(GTK_ENTRY(good_entry), goodwrd);
+	} else {
+		row_unselect();
+	}
+}
+
+void list_add_new() {
+	int i;
+	gchar *item[2] = {"*NEW*", "EDIT ME"};
+
+	i = gtk_clist_append(GTK_CLIST(list), item);
+	gtk_clist_select_row(GTK_CLIST(list), i, 0);
+	gtk_clist_moveto(GTK_CLIST(list), i, 0, 0.5, 0);
+}
+
+void list_delete() {
+	int row;
+
+	if (GTK_CLIST(list)->selection)
+		row = (int) GTK_CLIST (list)->selection->data;
+	else
+		row = -1;
+	if (row != -1) {
+		gtk_clist_unselect_all(GTK_CLIST(list));
+		gtk_clist_remove(GTK_CLIST(list), row);
+	}
+}
+
+void close_config() {
+	if (configwin)
+		gtk_widget_destroy(configwin);
+	configwin = NULL;
+}
+
+void save_list() {
+	int fh, i = 0;
+	char buf[512];
+	char *a, *b;
+
+	snprintf(buf, sizeof buf, "%s/.gaim/dict", getenv("HOME"));
+	fh = open(buf, O_TRUNC | O_WRONLY | O_CREAT);
+	if (fh != 1) {
+		while (gtk_clist_get_text(GTK_CLIST(list), i, 0, &a)) {
+			gtk_clist_get_text(GTK_CLIST(list), i, 1, &b);
+			snprintf(buf, sizeof buf, "BAD %s\nGOOD %s\n\n", a, b);
+			write(fh, buf, strlen(buf));
+			i++;
+		}
+		close (fh);
+	}
+	close_config();
+	load_conf();
+}
+
+void bad_changed() {
+	int row;
+	char *m;
+
+	if (GTK_CLIST(list)->selection)
+		row = (int) GTK_CLIST (list)->selection->data;
+	else
+		row = -1;
+	if (row != -1) {
+		m = gtk_entry_get_text(GTK_ENTRY(bad_entry));
+		gtk_clist_set_text(GTK_CLIST(list), row, 0, m);
+	}
+}
+
+void good_changed() {
+	int row;
+	char *m;
+
+	if (GTK_CLIST(list)->selection)
+		row = (int) GTK_CLIST (list)->selection->data;
+	else
+		row = -1;
+	if (row != -1) {
+		m = gtk_entry_get_text(GTK_ENTRY(good_entry));
+		gtk_clist_set_text(GTK_CLIST(list), row, 1, m);
+	}
+}
+
+void gaim_plugin_config() {
+	GtkWidget *win;
+	GtkWidget *vbox;
+	GtkWidget *hbox;
+	GtkWidget *button;
+	GList *w = words;
+	struct replace_words *r;
+	char *pair[2] = {"Replace", "With"};
+	
+	if (configwin) return;
+	configwin = gtk_window_new(GTK_WINDOW_DIALOG);
+	gtk_widget_set_usize(configwin, 450, 250);
+	gtk_window_set_title(GTK_WINDOW(configwin), "Spell Check Config");
+
+	vbox = gtk_vbox_new(0, 2);
+	gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
+	gtk_container_add(GTK_CONTAINER(configwin), vbox);
+	gtk_widget_show (vbox);
+
+	win = gtk_scrolled_window_new(0, 0);
+	gtk_container_add(GTK_CONTAINER(vbox), win);
+	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (win),
+			GTK_POLICY_AUTOMATIC,
+			GTK_POLICY_ALWAYS);
+	gtk_widget_show(win);
+	list = gtk_clist_new_with_titles(2, pair);
+	gtk_clist_set_column_width(GTK_CLIST(list), 0, 90);
+	gtk_clist_set_selection_mode(GTK_CLIST(list), GTK_SELECTION_BROWSE);
+	gtk_clist_column_titles_passive(GTK_CLIST(list));
+	gtk_container_add(GTK_CONTAINER(win), list);
+	gtk_signal_connect(GTK_OBJECT(list), "select_row",
+			   GTK_SIGNAL_FUNC(row_select), NULL);
+	gtk_signal_connect(GTK_OBJECT(list), "unselect_row",
+			   GTK_SIGNAL_FUNC(row_unselect), NULL);
+	gtk_widget_show(list);
+
+	hbox = gtk_hbox_new(FALSE, 2);
+	gtk_box_pack_end(GTK_BOX(vbox), hbox, 0, 0, 0);
+	gtk_widget_show(hbox);
+
+	button = gtk_button_new_with_label("Add New");
+	gtk_signal_connect(GTK_OBJECT(button), "clicked",
+			   GTK_SIGNAL_FUNC(list_add_new), NULL);
+	gtk_box_pack_start(GTK_BOX(hbox), button, 0, 0, 0);
+	gtk_widget_set_usize(button, 100, 0);
+	gtk_widget_show(button);
+
+	button = gtk_button_new_with_label("Delete");
+	gtk_signal_connect(GTK_OBJECT(button), "clicked",
+			   GTK_SIGNAL_FUNC(list_delete), NULL);
+	gtk_box_pack_start(GTK_BOX(hbox), button, 0, 0, 0);
+	gtk_widget_set_usize(button, 100, 0);
+	gtk_widget_show(button);
+
+	button = gtk_button_new_with_label("Cancel");
+	gtk_signal_connect(GTK_OBJECT(button), "clicked",
+			   GTK_SIGNAL_FUNC(close_config), NULL);
+	gtk_box_pack_start(GTK_BOX(hbox), button, 0, 0, 0);
+	gtk_widget_set_usize(button, 100, 0);
+	gtk_widget_show(button);
+
+	button = gtk_button_new_with_label("Save");
+	gtk_signal_connect(GTK_OBJECT(button), "clicked",
+			   GTK_SIGNAL_FUNC(save_list), NULL);
+	gtk_box_pack_start(GTK_BOX(hbox), button, 0, 0, 0);
+	gtk_widget_set_usize(button, 100, 0);
+	gtk_widget_show(button);
+
+	hbox = gtk_hbox_new(FALSE, 2);
+	gtk_box_pack_end(GTK_BOX(vbox), hbox, 0, 0, 0);
+	gtk_widget_show(hbox);
+
+	bad_entry = gtk_entry_new_with_max_length(40);
+	gtk_widget_set_usize(bad_entry, 96, 0);
+	gtk_signal_connect(GTK_OBJECT(bad_entry), "changed",
+			   GTK_SIGNAL_FUNC(bad_changed), NULL);
+	gtk_box_pack_start(GTK_BOX(hbox), bad_entry, 0, 0, 0);
+	gtk_widget_show(bad_entry);
+
+	good_entry = gtk_entry_new_with_max_length(255);
+	gtk_signal_connect(GTK_OBJECT(good_entry), "changed",
+			   GTK_SIGNAL_FUNC(good_changed), NULL);
+	gtk_container_add(GTK_CONTAINER(hbox), good_entry);
+	gtk_widget_show(good_entry);
+
+	while (w) {
+		r = (struct replace_words *)(w->data);
+		pair[0] = r->bad;
+		pair[1] = r->good;
+		gtk_clist_append(GTK_CLIST(list), pair);
+		w = w->next;
+	}
+
+	gtk_widget_show(configwin);
+}