Fix some bugs feeding the textview
[cascardo/chat.git] / chat.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
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 along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <gtk/gtk.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include "xmpp.h"
28 #include "message.h"
29
30 static int fd;
31 static GtkWidget *textview;
32 static GtkWidget *entry_to;
33 static GtkWidget *entry_body;
34
35 static void
36 hc_xmpp_chat (hc_xmpp_t *xmpp, iks *message)
37 {
38   GtkTextBuffer *textbuffer;
39   GtkTextIter textiter;
40   char *str;
41   char *from;
42   char *body;
43   str = iks_string (iks_stack (message), message);
44   write (fd, str, strlen (str));
45   if ((from = iks_find_attrib (message, "from")) == NULL)
46     from = "";
47   if ((body = iks_cdata (iks_child (iks_find (message, "body")))) == NULL)
48     body = "";
49   textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
50   gtk_text_buffer_get_end_iter (textbuffer, &textiter);
51   gtk_text_buffer_insert (textbuffer, &textiter, from, -1);
52   gtk_text_buffer_insert (textbuffer, &textiter, ": ", -1);
53   gtk_text_buffer_insert (textbuffer, &textiter, body, -1);
54   gtk_text_buffer_insert (textbuffer, &textiter, "\n", -1);
55 }
56
57 static void
58 hc_xmpp_chat_send (GtkWidget *button, gpointer data)
59 {
60   hc_xmpp_t *xmpp;
61   char *to;
62   char *body;
63   xmpp = (hc_xmpp_t *) data;
64   to = (char *) gtk_entry_get_text (GTK_ENTRY (entry_to));
65   body = (char *) gtk_entry_get_text (GTK_ENTRY (entry_body));
66   hc_xmpp_send_message (xmpp, to, body);
67 }
68
69 void
70 hc_xmpp_chat_open (hc_xmpp_t *xmpp)
71 {
72   GtkWidget *window;
73   GtkWidget *vbox;
74   GtkWidget *hbox;
75   GtkWidget *button;
76   fd = open ("/home/cascardo/.hcxmpp/log", O_RDWR | O_APPEND | O_CREAT);
77   if (fd < 0)
78     {
79       fprintf (stderr, "Could not open log file: %s\n", strerror (errno));
80       abort ();
81     }
82   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
83   vbox = gtk_vbox_new (FALSE, 5);
84   hbox = gtk_hbox_new (FALSE, 5);
85   entry_to = gtk_entry_new ();
86   entry_body = gtk_entry_new ();
87   button = gtk_button_new_with_label ("Send");
88   g_signal_connect (G_OBJECT (button), "clicked",
89                     G_CALLBACK (hc_xmpp_chat_send), xmpp);
90   textview = gtk_text_view_new ();
91   gtk_container_add (GTK_CONTAINER (window), vbox);
92   gtk_container_add (GTK_CONTAINER (vbox), textview);
93   gtk_container_add (GTK_CONTAINER (vbox), hbox);
94   gtk_container_add (GTK_CONTAINER (hbox), entry_to);
95   gtk_container_add (GTK_CONTAINER (hbox), entry_body);
96   gtk_container_add (GTK_CONTAINER (hbox), button);
97   gtk_widget_show_all (window);
98   hc_xmpp_set_msg_hook (xmpp, hc_xmpp_chat);
99   hc_xmpp_set_sent_msg_hook (xmpp, hc_xmpp_chat);
100 }