comparison plugins/spellchk.c @ 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 e05e6373ea5a
children a57fd3390ee4
comparison
equal deleted inserted replaced
110:f7c6366ca703 111:d927bb34e2c6
1 /*
2 * A lot of this code (especially the config code) was taken directly
3 * or nearly directly from xchat, version 1.4.2 by Peter Zelezny and others.
4 *
5 * TODO:
6 * ? I think i did everything i want to with it.
7 *
8 * BUGS:
9 * If you have a numeric replacement, and is preceded by numeric text,
10 * it doesn't catch it. I don't know why. Probably because it's not
11 * designed to work with numbers.
12 */
1 #define GAIM_PLUGINS 13 #define GAIM_PLUGINS
2 #include "gaim.h" 14 #include "gaim.h"
3 15
4 #include <string.h> 16 #include <string.h>
5 #include <ctype.h> 17 #include <ctype.h>
6 #include <stdlib.h> 18 #include <stdlib.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <fcntl.h>
7 23
8 struct replace_words { 24 struct replace_words {
9 char *bad; 25 char *bad;
10 char *good; 26 char *good;
11 }; 27 };
41 w = w->next; 57 w = w->next;
42 } 58 }
43 } 59 }
44 } 60 }
45 61
62 int buf_get_line(char *ibuf, char **buf, int *position, int len) {
63 int pos = *position, spos = pos;
64
65 if (pos == len)
66 return 0;
67
68 while (ibuf[pos++] != '\n') {
69 if (pos == len)
70 return 0;
71 }
72 pos--;
73 ibuf[pos] = 0;
74 *buf = &ibuf[spos];
75 pos++;
76 *position = pos;
77 return 1;
78 }
79
80 void load_conf() {
81 char *defaultconf = "BAD r\nGOOD are\n\n"
82 "BAD u\nGOOD you\n\n"
83 "BAD teh\nGOOD the\n\n";
84 char *buf, *ibuf;
85 char name[82];
86 char cmd[256];
87 int fh, pnt = 0;
88 struct stat st;
89
90 if (words != NULL)
91 g_list_free(words);
92 words = NULL;
93
94 buf = malloc(1000);
95 snprintf(buf, 1000, "%s/.gaim/dict", getenv("HOME"));
96 fh = open(buf, O_RDONLY);
97 if (fh == -1) {
98 fh = open(buf, O_TRUNC | O_WRONLY | O_CREAT);
99 if (fh != -1) {
100 write(fh, defaultconf, strlen(defaultconf));
101 close(fh);
102 free(buf);
103 load_conf();
104 }
105 return;
106 }
107 free(buf);
108 if (fstat(fh, &st) != 0)
109 return;
110 ibuf = malloc(st.st_size);
111 read(fh, ibuf, st.st_size);
112 close(fh);
113
114 cmd[0] = 0;
115 name[0] = 0;
116
117 while(buf_get_line(ibuf, &buf, &pnt, st.st_size)) {
118 if (*buf != '#') {
119 if (!strncasecmp(buf, "BAD ", 4))
120 strncpy(name, buf + 4, 81);
121 if (!strncasecmp(buf, "GOOD ", 5)) {
122 strncpy(cmd, buf + 5, 255);
123 if (*name) {
124 struct replace_words *r;
125 r = malloc(sizeof *r);
126 r->bad = strdup(name);
127 r->good = strdup(cmd);
128 words = g_list_append(words, r);
129 cmd[0] = 0;
130 name[0] = 0;
131 }
132 }
133 }
134 }
135 free(ibuf);
136 }
137
46 void gaim_plugin_init(void *handle) { 138 void gaim_plugin_init(void *handle) {
47 struct replace_words *p; 139 load_conf();
48 FILE *file;
49 char buffer[256];
50 char *good;
51 char *bad;
52
53 sprintf(buffer, "%s/.gaim/dict", getenv("HOME"));
54 file = fopen(buffer, "r");
55 while (fgets(buffer, sizeof buffer, file)) {
56 buffer[strlen(buffer) - 1] = 0;
57 p = malloc(sizeof *p);
58 good = strdup(strpbrk(strpbrk(buffer, " \t\r\f\n"),
59 "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"));
60 bad = strpbrk(buffer, " \t\r\f\n");
61 bad[0] = 0;
62 bad = strdup(buffer);
63 p->bad = bad;
64 p->good = good;
65 words = g_list_append(words, p);
66 }
67 140
68 gaim_signal_connect(handle, event_im_send, spell_check, NULL); 141 gaim_signal_connect(handle, event_im_send, spell_check, NULL);
142 }
143
144 void gaim_plugin_remove() {
69 } 145 }
70 146
71 char *name() { 147 char *name() {
72 return "IM Spell Check"; 148 return "IM Spell Check";
73 } 149 }
159 strcat(new, &(*mes)[pos + m]); 235 strcat(new, &(*mes)[pos + m]);
160 tmp = *mes; 236 tmp = *mes;
161 *mes = new; 237 *mes = new;
162 free(tmp); 238 free(tmp);
163 } 239 }
240
241 GtkWidget *configwin = NULL;
242 GtkWidget *list;
243 GtkWidget *bad_entry;
244 GtkWidget *good_entry;
245
246 void row_unselect() {
247 gtk_entry_set_text(GTK_ENTRY(bad_entry), "");
248 gtk_entry_set_text(GTK_ENTRY(good_entry), "");
249 }
250
251 void row_select() {
252 char *badwrd, *goodwrd;
253 int row;
254
255 if (GTK_CLIST(list)->selection)
256 row = (int) GTK_CLIST (list)->selection->data;
257 else
258 row = -1;
259 if (row != -1) {
260 gtk_clist_get_text(GTK_CLIST(list), row, 0, &badwrd);
261 gtk_clist_get_text(GTK_CLIST(list), row, 1, &goodwrd);
262 gtk_entry_set_text(GTK_ENTRY(bad_entry), badwrd);
263 gtk_entry_set_text(GTK_ENTRY(good_entry), goodwrd);
264 } else {
265 row_unselect();
266 }
267 }
268
269 void list_add_new() {
270 int i;
271 gchar *item[2] = {"*NEW*", "EDIT ME"};
272
273 i = gtk_clist_append(GTK_CLIST(list), item);
274 gtk_clist_select_row(GTK_CLIST(list), i, 0);
275 gtk_clist_moveto(GTK_CLIST(list), i, 0, 0.5, 0);
276 }
277
278 void list_delete() {
279 int row;
280
281 if (GTK_CLIST(list)->selection)
282 row = (int) GTK_CLIST (list)->selection->data;
283 else
284 row = -1;
285 if (row != -1) {
286 gtk_clist_unselect_all(GTK_CLIST(list));
287 gtk_clist_remove(GTK_CLIST(list), row);
288 }
289 }
290
291 void close_config() {
292 if (configwin)
293 gtk_widget_destroy(configwin);
294 configwin = NULL;
295 }
296
297 void save_list() {
298 int fh, i = 0;
299 char buf[512];
300 char *a, *b;
301
302 snprintf(buf, sizeof buf, "%s/.gaim/dict", getenv("HOME"));
303 fh = open(buf, O_TRUNC | O_WRONLY | O_CREAT);
304 if (fh != 1) {
305 while (gtk_clist_get_text(GTK_CLIST(list), i, 0, &a)) {
306 gtk_clist_get_text(GTK_CLIST(list), i, 1, &b);
307 snprintf(buf, sizeof buf, "BAD %s\nGOOD %s\n\n", a, b);
308 write(fh, buf, strlen(buf));
309 i++;
310 }
311 close (fh);
312 }
313 close_config();
314 load_conf();
315 }
316
317 void bad_changed() {
318 int row;
319 char *m;
320
321 if (GTK_CLIST(list)->selection)
322 row = (int) GTK_CLIST (list)->selection->data;
323 else
324 row = -1;
325 if (row != -1) {
326 m = gtk_entry_get_text(GTK_ENTRY(bad_entry));
327 gtk_clist_set_text(GTK_CLIST(list), row, 0, m);
328 }
329 }
330
331 void good_changed() {
332 int row;
333 char *m;
334
335 if (GTK_CLIST(list)->selection)
336 row = (int) GTK_CLIST (list)->selection->data;
337 else
338 row = -1;
339 if (row != -1) {
340 m = gtk_entry_get_text(GTK_ENTRY(good_entry));
341 gtk_clist_set_text(GTK_CLIST(list), row, 1, m);
342 }
343 }
344
345 void gaim_plugin_config() {
346 GtkWidget *win;
347 GtkWidget *vbox;
348 GtkWidget *hbox;
349 GtkWidget *button;
350 GList *w = words;
351 struct replace_words *r;
352 char *pair[2] = {"Replace", "With"};
353
354 if (configwin) return;
355 configwin = gtk_window_new(GTK_WINDOW_DIALOG);
356 gtk_widget_set_usize(configwin, 450, 250);
357 gtk_window_set_title(GTK_WINDOW(configwin), "Spell Check Config");
358
359 vbox = gtk_vbox_new(0, 2);
360 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
361 gtk_container_add(GTK_CONTAINER(configwin), vbox);
362 gtk_widget_show (vbox);
363
364 win = gtk_scrolled_window_new(0, 0);
365 gtk_container_add(GTK_CONTAINER(vbox), win);
366 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (win),
367 GTK_POLICY_AUTOMATIC,
368 GTK_POLICY_ALWAYS);
369 gtk_widget_show(win);
370 list = gtk_clist_new_with_titles(2, pair);
371 gtk_clist_set_column_width(GTK_CLIST(list), 0, 90);
372 gtk_clist_set_selection_mode(GTK_CLIST(list), GTK_SELECTION_BROWSE);
373 gtk_clist_column_titles_passive(GTK_CLIST(list));
374 gtk_container_add(GTK_CONTAINER(win), list);
375 gtk_signal_connect(GTK_OBJECT(list), "select_row",
376 GTK_SIGNAL_FUNC(row_select), NULL);
377 gtk_signal_connect(GTK_OBJECT(list), "unselect_row",
378 GTK_SIGNAL_FUNC(row_unselect), NULL);
379 gtk_widget_show(list);
380
381 hbox = gtk_hbox_new(FALSE, 2);
382 gtk_box_pack_end(GTK_BOX(vbox), hbox, 0, 0, 0);
383 gtk_widget_show(hbox);
384
385 button = gtk_button_new_with_label("Add New");
386 gtk_signal_connect(GTK_OBJECT(button), "clicked",
387 GTK_SIGNAL_FUNC(list_add_new), NULL);
388 gtk_box_pack_start(GTK_BOX(hbox), button, 0, 0, 0);
389 gtk_widget_set_usize(button, 100, 0);
390 gtk_widget_show(button);
391
392 button = gtk_button_new_with_label("Delete");
393 gtk_signal_connect(GTK_OBJECT(button), "clicked",
394 GTK_SIGNAL_FUNC(list_delete), NULL);
395 gtk_box_pack_start(GTK_BOX(hbox), button, 0, 0, 0);
396 gtk_widget_set_usize(button, 100, 0);
397 gtk_widget_show(button);
398
399 button = gtk_button_new_with_label("Cancel");
400 gtk_signal_connect(GTK_OBJECT(button), "clicked",
401 GTK_SIGNAL_FUNC(close_config), NULL);
402 gtk_box_pack_start(GTK_BOX(hbox), button, 0, 0, 0);
403 gtk_widget_set_usize(button, 100, 0);
404 gtk_widget_show(button);
405
406 button = gtk_button_new_with_label("Save");
407 gtk_signal_connect(GTK_OBJECT(button), "clicked",
408 GTK_SIGNAL_FUNC(save_list), NULL);
409 gtk_box_pack_start(GTK_BOX(hbox), button, 0, 0, 0);
410 gtk_widget_set_usize(button, 100, 0);
411 gtk_widget_show(button);
412
413 hbox = gtk_hbox_new(FALSE, 2);
414 gtk_box_pack_end(GTK_BOX(vbox), hbox, 0, 0, 0);
415 gtk_widget_show(hbox);
416
417 bad_entry = gtk_entry_new_with_max_length(40);
418 gtk_widget_set_usize(bad_entry, 96, 0);
419 gtk_signal_connect(GTK_OBJECT(bad_entry), "changed",
420 GTK_SIGNAL_FUNC(bad_changed), NULL);
421 gtk_box_pack_start(GTK_BOX(hbox), bad_entry, 0, 0, 0);
422 gtk_widget_show(bad_entry);
423
424 good_entry = gtk_entry_new_with_max_length(255);
425 gtk_signal_connect(GTK_OBJECT(good_entry), "changed",
426 GTK_SIGNAL_FUNC(good_changed), NULL);
427 gtk_container_add(GTK_CONTAINER(hbox), good_entry);
428 gtk_widget_show(good_entry);
429
430 while (w) {
431 r = (struct replace_words *)(w->data);
432 pair[0] = r->bad;
433 pair[1] = r->good;
434 gtk_clist_append(GTK_CLIST(list), pair);
435 w = w->next;
436 }
437
438 gtk_widget_show(configwin);
439 }