Added GTK+ interface to connect to server
[cascardo/chat.git] / ui.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 <glib.h>
23 #include <udns.h>
24 #include "tcp_connect.h"
25 #include "xmpp.h"
26
27 static gboolean
28 rploop (GIOChannel *src, GIOCondition cond, gpointer xmpp)
29 {
30   hc_xmpp_read_and_parse (xmpp);
31   return TRUE;
32 }
33
34 GtkWidget *entry1;
35 GtkWidget *entry2;
36 GtkWidget *entry3;
37
38 static void
39 connect_clicked (GtkWidget *button, gpointer data)
40 {
41   GIOChannel *channel;
42   hc_xmpp_t *xmpp;
43   char *server;
44   char *user;
45   char *password;
46   server = (char *) gtk_entry_get_text (GTK_ENTRY (entry1));
47   user = (char *) gtk_entry_get_text (GTK_ENTRY (entry2));
48   password = (char *) gtk_entry_get_text (GTK_ENTRY (entry3));
49   xmpp = hc_xmpp_new (hc_xmpp_hook, server, user, password);
50   hc_xmpp_send_stream (xmpp);
51   channel = g_io_channel_unix_new (hc_xmpp_fd (xmpp));
52   g_io_add_watch (channel, G_IO_IN, rploop, xmpp);
53 }
54
55 static void
56 ui (void)
57 {
58   GtkWidget *window;
59   GtkWidget *vbox;
60   GtkWidget *button;
61   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
62   vbox = gtk_vbox_new (TRUE, 5);
63   entry1 = gtk_entry_new ();
64   entry2 = gtk_entry_new ();
65   entry3 = gtk_entry_new ();
66   gtk_entry_set_visibility (GTK_ENTRY (entry3), FALSE);
67   button = gtk_button_new_with_label ("Connect");
68   g_signal_connect (G_OBJECT (button), "clicked",
69                     G_CALLBACK (connect_clicked), NULL);
70   gtk_container_add (GTK_CONTAINER (window), vbox);
71   gtk_container_add (GTK_CONTAINER (vbox), entry1);
72   gtk_container_add (GTK_CONTAINER (vbox), entry2);
73   gtk_container_add (GTK_CONTAINER (vbox), entry3);
74   gtk_container_add (GTK_CONTAINER (vbox), button);
75   gtk_widget_show_all (window);
76 }
77
78 int
79 main (int argc, char **argv)
80 {
81   GMainLoop *loop;
82   gtk_init (&argc, &argv);
83   dns_init (NULL, 1);
84   loop = g_main_loop_new (g_main_context_default (), TRUE);
85   ui ();
86   g_main_loop_run (loop);
87   return 0;
88 }