added forgot files grouplist_dialog.h grouplist_dialog.c
[claws.git] / src / grouplist_dialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <gtk/gtkmain.h>
27 #include <gtk/gtkwidget.h>
28 #include <gtk/gtkdialog.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkhbox.h>
33 #include <gtk/gtklabel.h>
34 #include <gtk/gtkclist.h>
35 #include <gtk/gtkscrolledwindow.h>
36 #include <gtk/gtkbutton.h>
37 #include <gtk/gtkhbbox.h>
38
39 #include "intl.h"
40 #include "grouplist_dialog.h"
41 #include "manage_window.h"
42 #include "gtkutils.h"
43 #include "utils.h"
44 #include "news.h"
45 #include "folder.h"
46
47 #define GROUPLIST_DIALOG_WIDTH  420
48 #define GROUPLIST_DIALOG_HEIGHT 400
49
50 static gboolean ack;
51
52 static GtkWidget *dialog;
53 static GtkWidget *msg_label;
54 static GtkWidget *group_clist;
55 static GtkWidget *ok_button;
56 static gchar * group_selected;
57 static GSList * group_list;
58 static FolderItem * item;
59
60 static void grouplist_dialog_create     (void);
61 static void grouplist_dialog_set        (void);
62 static void grouplist_free              (void);
63
64 static void ok_clicked          (GtkWidget      *widget,
65                                  gpointer        data);
66 static void cancel_clicked      (GtkWidget      *widget,
67                                  gpointer        data);
68 static void refresh_clicked(GtkWidget *widget, gpointer data);
69 static void key_pressed         (GtkWidget      *widget,
70                                  GdkEventKey    *event,
71                                  gpointer        data);
72 static void group_clist_select(GtkCList *clist, gint row, gint column,
73                               GdkEventButton *event, gpointer user_data);
74
75 gchar *grouplist_dialog(FolderItem * i)
76 {
77         gchar *str;
78
79         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
80
81         if (!dialog)
82                 grouplist_dialog_create();
83
84         item = i;
85
86         grouplist_dialog_set();
87         gtk_widget_show(dialog);
88         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
89         manage_window_set_transient(GTK_WINDOW(dialog));
90
91         gtk_main();
92
93         manage_window_focus_out(dialog, NULL, NULL);
94         gtk_widget_hide(dialog);
95
96         if (ack) {
97                 if (group_selected != NULL)
98                         str = g_strdup(group_selected);
99                 else
100                         str = NULL;
101                 if (str && *str == '\0')
102                         str = NULL;
103         } else
104                 str = NULL;
105
106         grouplist_free();
107
108         GTK_EVENTS_FLUSH();
109
110         debug_print("return string = %s\n", str ? str : "(none)");
111         return str;
112 }
113
114 static void grouplist_free(void)
115 {
116         GSList * group_elt;
117
118         for(group_elt = group_list ; group_elt != NULL ;
119             group_elt = group_elt->next)
120             g_free(group_elt->data);
121         g_slist_free(group_list);
122 }
123
124 static void grouplist_dialog_create(void)
125 {
126         GtkWidget *vbox;
127         GtkWidget *hbox;
128         GtkWidget *confirm_area;
129         GtkWidget *cancel_button;       
130         GtkWidget *refresh_button;      
131         GtkWidget *scrolledwin;
132
133         dialog = gtk_dialog_new();
134         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
135         gtk_widget_set_usize(dialog, GROUPLIST_DIALOG_WIDTH, GROUPLIST_DIALOG_HEIGHT);
136         gtk_container_set_border_width
137                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
138         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
139         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
140                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
141         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
142                            GTK_SIGNAL_FUNC(key_pressed), NULL);
143         gtk_signal_connect(GTK_OBJECT(dialog), "focus_in_event",
144                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
145         gtk_signal_connect(GTK_OBJECT(dialog), "focus_out_event",
146                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
147
148         gtk_widget_realize(dialog);
149
150         vbox = gtk_vbox_new(FALSE, 8);
151         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
152         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
153
154         hbox = gtk_hbox_new(FALSE, 0);
155         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
156
157         msg_label = gtk_label_new("");
158         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
159         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
160
161
162         scrolledwin = gtk_scrolled_window_new (NULL, NULL);
163         gtk_box_pack_start (GTK_BOX (vbox), scrolledwin,
164                             TRUE, TRUE, 0);
165         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
166                                         GTK_POLICY_AUTOMATIC,
167                                         GTK_POLICY_AUTOMATIC);
168
169         group_clist = gtk_clist_new(1);
170         gtk_container_add (GTK_CONTAINER (scrolledwin), group_clist);
171         gtk_clist_set_column_width (GTK_CLIST (group_clist), 0, 80);
172         gtk_clist_set_selection_mode (GTK_CLIST (group_clist),
173                                       GTK_SELECTION_BROWSE);
174         GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (group_clist)->column[0].button,
175                                 GTK_CAN_FOCUS);
176         gtk_signal_connect (GTK_OBJECT (group_clist), "select_row",
177                             GTK_SIGNAL_FUNC (group_clist_select), NULL);
178
179         gtkut_button_set_create(&confirm_area,
180                                 &ok_button,     _("OK"),
181                                 &cancel_button, _("Cancel"),
182                                 &refresh_button, _("Refresh"));
183         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
184                           confirm_area);
185         gtk_widget_grab_default(ok_button);
186
187         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
188                            GTK_SIGNAL_FUNC(ok_clicked), NULL);
189         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
190                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
191         gtk_signal_connect(GTK_OBJECT(refresh_button), "clicked",
192                            GTK_SIGNAL_FUNC(refresh_clicked), NULL);
193
194
195         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
196 }
197
198 static void grouplist_dialog_set_list(void)
199 {
200         GSList * elt;
201         gint row;
202
203         group_selected = NULL;
204         group_list = news_get_group_list(item);
205
206         gtk_clist_clear(GTK_CLIST(group_clist));
207         for(elt = group_list; elt != NULL ; elt = elt->next)
208           {
209             row = gtk_clist_append(GTK_CLIST(group_clist), &(elt->data));
210             gtk_clist_set_row_data(GTK_CLIST(group_clist), row, elt->data);
211           }
212
213         gtk_widget_grab_focus(ok_button);
214         gtk_widget_grab_focus(group_clist);
215
216         if (group_list != NULL)
217           group_selected = group_list->data;
218         else
219           group_selected = NULL;
220 }
221
222 static void grouplist_dialog_set(void)
223 {
224         gtk_window_set_title(GTK_WINDOW(dialog), _("Subscribe newsgroup"));
225         gtk_label_set_text(GTK_LABEL(msg_label), _("Input subscribing newsgroup:"));
226         grouplist_dialog_set_list();
227 }
228
229 static void ok_clicked(GtkWidget *widget, gpointer data)
230 {
231         ack = TRUE;
232         gtk_main_quit();
233 }
234
235 static void cancel_clicked(GtkWidget *widget, gpointer data)
236 {
237         ack = FALSE;
238         gtk_main_quit();
239 }
240
241 static void refresh_clicked(GtkWidget *widget, gpointer data)
242
243         grouplist_free();
244         news_reset_group_list(item);
245         grouplist_dialog_set_list();
246 }
247
248 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
249 {
250         if (event && event->keyval == GDK_Escape) {
251                 ack = FALSE;
252                 gtk_main_quit();
253         }
254 }
255
256 static void group_clist_select(GtkCList *clist, gint row, gint column,
257                               GdkEventButton *event, gpointer user_data)
258 {
259   group_selected = (gchar *)
260     gtk_clist_get_row_data(GTK_CLIST(group_clist), row);
261 }