+/*
+ * Copyright (C) 2008 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ * 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 Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include <gtk/gtk.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "xmpp.h"
+#include "message.h"
+
+static int fd;
+static GtkWidget *textview;
+static GtkWidget *entry_to;
+static GtkWidget *entry_body;
+
+static void
+hc_xmpp_chat (hc_xmpp_t *xmpp, iks *message)
+{
+ GtkTextBuffer *textbuffer;
+ GtkTextIter *textiter;
+ char *str;
+ char *from;
+ char *body;
+ str = iks_string (iks_stack (message), message);
+ write (fd, str, strlen (str));
+ from = iks_find_attrib (message, "from");
+ body = iks_cdata (iks_child (iks_find (message, "body")));
+ textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
+ gtk_text_buffer_get_end_iter (textbuffer, textiter);
+ gtk_text_buffer_insert (textbuffer, textiter, from, -1);
+ gtk_text_buffer_insert (textbuffer, textiter, ": ", -1);
+ gtk_text_buffer_insert (textbuffer, textiter, body, -1);
+ gtk_text_buffer_insert (textbuffer, textiter, "\n", -1);
+}
+
+static void
+hc_xmpp_chat_send (GtkWidget *button, gpointer data)
+{
+ hc_xmpp_t *xmpp;
+ char *to;
+ char *body;
+ xmpp = (hc_xmpp_t *) data;
+ to = (char *) gtk_entry_get_text (GTK_ENTRY (entry_to));
+ body = (char *) gtk_entry_get_text (GTK_ENTRY (entry_body));
+ hc_xmpp_send_message (xmpp, to, body);
+}
+
+void
+hc_xmpp_chat_open (hc_xmpp_t *xmpp)
+{
+ GtkWidget *window;
+ GtkWidget *vbox;
+ GtkWidget *hbox;
+ GtkWidget *button;
+ fd = open ("/home/cascardo/.hcxmpp/log", O_RDWR | O_APPEND | O_CREAT);
+ if (fd < 0)
+ {
+ fprintf (stderr, "Could not open log file: %s\n", strerror (errno));
+ abort ();
+ }
+ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ vbox = gtk_vbox_new (FALSE, 5);
+ hbox = gtk_hbox_new (FALSE, 5);
+ entry_to = gtk_entry_new ();
+ entry_body = gtk_entry_new ();
+ button = gtk_button_new_with_label ("Send");
+ g_signal_connect (G_OBJECT (button), "clicked",
+ G_CALLBACK (hc_xmpp_chat_send), xmpp);
+ textview = gtk_text_view_new ();
+ gtk_container_add (GTK_CONTAINER (window), vbox);
+ gtk_container_add (GTK_CONTAINER (vbox), textview);
+ gtk_container_add (GTK_CONTAINER (vbox), hbox);
+ gtk_container_add (GTK_CONTAINER (hbox), entry_to);
+ gtk_container_add (GTK_CONTAINER (hbox), entry_body);
+ gtk_container_add (GTK_CONTAINER (hbox), button);
+ gtk_widget_show_all (window);
+ hc_xmpp_set_msg_hook (xmpp, hc_xmpp_chat);
+ hc_xmpp_set_sent_msg_hook (xmpp, hc_xmpp_chat);
+}