Added a very simple interface to chat with known issues
authorThadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
Tue, 25 Nov 2008 02:19:12 +0000 (00:19 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
Tue, 25 Nov 2008 12:14:34 +0000 (10:14 -0200)
This interface does not show a different textview for every different
chat or contact. It also requires the user to type the contact ID to
send him a message.

Makefile
chat.c [new file with mode: 0644]
ui.c

index 89a6998..a816b97 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -9,11 +9,11 @@ all: tictactoe ui
 tictactoe: $(OBJECTS) tictactoe.o
        $(CC) $(CFLAGS) $(LIBS) -o tictactoe $(OBJECTS) tictactoe.o
 
-ui: $(OBJECTS) ui.o
-       $(CC) $(CFLAGS) $(LIBS) -o ui $(OBJECTS) ui.o
+ui: $(OBJECTS) ui.o chat.o
+       $(CC) $(CFLAGS) $(LIBS) -o ui $(OBJECTS) ui.o chat.o
 
 .c.o:
        $(CC) $(CFLAGS) -c $< -o $@
 
 clean:
-       rm -f $(OBJECTS) tictactoe.o tictactoe ui.o ui
+       rm -f $(OBJECTS) tictactoe.o tictactoe ui.o chat.o ui
diff --git a/chat.c b/chat.c
new file mode 100644 (file)
index 0000000..75fdf85
--- /dev/null
+++ b/chat.c
@@ -0,0 +1,98 @@
+/*
+ *  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);
+}
diff --git a/ui.c b/ui.c
index 4527047..c2d5b2f 100644 (file)
--- a/ui.c
+++ b/ui.c
@@ -35,6 +35,8 @@ GtkWidget *entry1;
 GtkWidget *entry2;
 GtkWidget *entry3;
 
+extern void hc_xmpp_chat_open (hc_xmpp_t *);
+
 static void
 connect_clicked (GtkWidget *button, gpointer data)
 {
@@ -50,6 +52,7 @@ connect_clicked (GtkWidget *button, gpointer data)
   hc_xmpp_send_stream (xmpp);
   channel = g_io_channel_unix_new (hc_xmpp_fd (xmpp));
   g_io_add_watch (channel, G_IO_IN, rploop, xmpp);
+  hc_xmpp_chat_open (xmpp);
 }
 
 static void