Initial import of programs to send emails to speakers for an event
[cascardo/avaliacao2008.git] / ui.c
1 /*
2  *  Copyright (C) 2007  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
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 #include <villa.h>
20 #include <cabin.h>
21 #include <gtk/gtk.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "avaliacao.h"
25
26 VILLA *db;
27
28 void
29 gtk_container_remove_r (GtkWidget *widget, gpointer data)
30 {
31   gtk_container_remove (data, widget);
32 }
33
34 void
35 gtk_container_remove_all (GtkContainer *container)
36 {
37   gtk_container_foreach (container, gtk_container_remove_r, container);
38 }
39
40
41 void
42 addcur (const gchar *key, const gchar *val)
43 {
44   char *smap;
45   int slen;
46   CBMAP *map;
47   smap = vlcurval (db, &slen);
48   map = cbmapload (smap, slen);
49   cbmapput (map, key, -1, val, -1, 1);
50   free (smap);
51   smap = cbmapdump (map, &slen);
52   cbmapclose (map);
53   vlcurput (db, smap, slen, VL_CPCURRENT);
54 }
55
56 void
57 showcur (gpointer data)
58 {
59   char *val;
60   int vlen;
61   CBMAP *map;
62   const char *mapkey;
63   GtkWidget *hbox;
64   GtkWidget *label;
65   GtkWidget *entry;
66   GtkSizeGroup *sgroup;
67   sgroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
68   gtk_container_remove_all (GTK_CONTAINER (data));
69   val = vlcurval (db, &vlen);
70   map = cbmapload (val, vlen);
71   cbmapiterinit (map);
72   for (mapkey = cbmapiternext (map, NULL); mapkey != NULL;
73        mapkey = cbmapiternext (map, NULL))
74     {
75       const char *mapval;
76       mapval = cbmapget (map, mapkey, -1, NULL);
77       hbox = gtk_hbox_new (FALSE, 5);
78       gtk_box_pack_start (GTK_BOX (data), hbox, FALSE, FALSE, 0);
79       label = gtk_label_new (mapkey);
80       g_object_set (G_OBJECT (label), "xalign", 0.0, NULL);
81       gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
82       entry = gtk_entry_new ();
83       gtk_entry_set_text (GTK_ENTRY (entry), mapval);
84       gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
85       gtk_size_group_add_widget (sgroup, label);
86     }
87   cbmapclose (map);
88   g_object_unref (sgroup);
89   gtk_widget_show_all (GTK_WIDGET (data));
90   free (val);
91 }
92
93 void
94 first (GtkButton *button, gpointer data)
95 {
96   if (vlcurfirst (db))
97     showcur (data);
98 }
99
100 void
101 next (GtkButton *button, gpointer data)
102 {
103   if (vlcurnext (db))
104     showcur (data);
105 }
106
107 void
108 prev (GtkButton *button, gpointer data)
109 {
110   if (vlcurprev (db))
111     showcur (data);
112 }
113
114 void
115 last (GtkButton *button, gpointer data)
116 {
117   if (vlcurlast (db))
118     showcur (data);
119 }
120
121 void
122 removecur (GtkButton *button, gpointer data)
123 {
124   if (vlcurout (db))
125     showcur (data);
126 }
127
128 void
129 addfield (GtkButton *button, gpointer data)
130 {
131   GList *children;
132   GtkEntry *key;
133   GtkEntry *value;
134   children = gtk_container_get_children (GTK_CONTAINER (data));
135   key = children->data;
136   value = children->next->data;
137   g_list_free (children);
138   addcur (gtk_entry_get_text (key), gtk_entry_get_text (value));
139 }
140
141 void
142 viewcur (GtkButton *button, gpointer data)
143 {
144   GtkTextBuffer *buffer;
145   gchar *message;
146   message = regex_create_message ();
147   buffer = gtk_text_buffer_new (NULL);
148   gtk_text_buffer_set_text (buffer, message, -1);
149   g_free (message);
150   gtk_text_view_set_buffer (GTK_TEXT_VIEW (data), buffer);
151 }
152
153 void
154 ui (gpointer data)
155 {
156
157
158   GtkWidget *window;
159   GtkWidget *vbox;
160   GtkWidget *hbox;
161   GtkWidget *button;
162   GtkWidget *form;
163
164   GtkWidget *hbox2;
165   GtkWidget *entry;
166   GtkWidget *button2;
167
168   GtkWidget *text;
169
170   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
171   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
172   g_signal_connect (G_OBJECT (window), "destroy", gtk_main_quit, NULL);
173
174
175
176   vbox = gtk_vbox_new (FALSE, 5);
177   gtk_container_add (GTK_CONTAINER (window), vbox);
178
179   hbox = gtk_hbox_new (FALSE, 5);
180   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
181
182   form = gtk_vbox_new (FALSE, 5);
183   gtk_box_pack_start (GTK_BOX (vbox), form, FALSE, FALSE, 0);
184
185
186   button = gtk_button_new_with_label ("First");
187   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
188   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK(first), form);
189   button = gtk_button_new_with_label ("Prev");
190   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
191   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK(prev), form);
192   button = gtk_button_new_with_label ("Next");
193   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
194   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK(next), form);
195   button = gtk_button_new_with_label ("Last");
196   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
197   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK(last), form);
198
199
200   hbox2 = gtk_hbox_new (FALSE, 5);
201   gtk_box_pack_start (GTK_BOX (vbox), hbox2, FALSE, FALSE, 0);
202
203   entry = gtk_entry_new ();
204   gtk_box_pack_start (GTK_BOX (hbox2), entry, FALSE, FALSE, 0);
205   entry = gtk_entry_new ();
206   gtk_box_pack_start (GTK_BOX (hbox2), entry, FALSE, FALSE, 0);
207   button2 = gtk_button_new_with_label ("Add");
208   gtk_box_pack_start (GTK_BOX (hbox2), button2, FALSE, FALSE, 0);
209   g_signal_connect (G_OBJECT (button2), "clicked", G_CALLBACK (addfield), hbox2);
210
211   button2 = gtk_button_new_with_label ("Remove");
212   gtk_box_pack_start (GTK_BOX (hbox), button2, FALSE, FALSE, 0);
213   g_signal_connect (G_OBJECT (button2), "clicked", G_CALLBACK (removecur), form);
214
215   text = gtk_text_view_new ();
216   gtk_box_pack_start (GTK_BOX (vbox), text, TRUE, TRUE, 0);
217
218   button2 = gtk_button_new_with_label ("View");
219   gtk_box_pack_start (GTK_BOX (hbox), button2, FALSE, FALSE, 0);
220   g_signal_connect (G_OBJECT (button2), "clicked", G_CALLBACK (viewcur), text);
221
222   gtk_widget_show_all (window);
223
224
225 }
226
227 int main (int argc, char **argv)
228 {
229   gtk_init (&argc, &argv);
230   regex_init ();
231   db = vlopen ("palestras.vl", VL_OWRITER, VL_CMPINT);
232   ui (db);
233   gtk_main ();
234   vlclose (db);
235   return 0;
236 }