fixed folder stats update after incorporation
[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 #include "alertpanel.h"
47
48 #define GROUPLIST_DIALOG_WIDTH  420
49 #define GROUPLIST_DIALOG_HEIGHT 400
50
51 static gboolean ack;
52
53 static GtkWidget *dialog;
54 static GtkWidget *msg_label;
55 static GtkWidget *group_clist;
56 static GtkWidget *ok_button;
57 static gchar * group_selected;
58 static GSList * group_list;
59 static FolderItem * item;
60
61 static void grouplist_dialog_create     (void);
62 static void grouplist_dialog_set        (void);
63 static void grouplist_free              (void);
64
65 static void ok_clicked          (GtkWidget      *widget,
66                                  gpointer        data);
67 static void cancel_clicked      (GtkWidget      *widget,
68                                  gpointer        data);
69 static void refresh_clicked(GtkWidget *widget, gpointer data);
70 static void key_pressed         (GtkWidget      *widget,
71                                  GdkEventKey    *event,
72                                  gpointer        data);
73 static void group_clist_select(GtkCList *clist, gint row, gint column,
74                               GdkEventButton *event, gpointer user_data);
75
76 gchar *grouplist_dialog(FolderItem * i)
77 {
78         gchar *str;
79
80         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
81
82         if (!dialog)
83                 grouplist_dialog_create();
84
85         item = i;
86
87         grouplist_dialog_set();
88         gtk_widget_show(dialog);
89         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
90         manage_window_set_transient(GTK_WINDOW(dialog));
91
92         gtk_main();
93
94         manage_window_focus_out(dialog, NULL, NULL);
95         gtk_widget_hide(dialog);
96
97         if (ack) {
98                 if (group_selected != NULL)
99                         str = g_strdup(group_selected);
100                 else
101                         str = NULL;
102                 if (str && *str == '\0')
103                         str = NULL;
104         } else
105                 str = NULL;
106
107         grouplist_free();
108
109         GTK_EVENTS_FLUSH();
110
111         debug_print("return string = %s\n", str ? str : "(none)");
112         return str;
113 }
114
115 static void grouplist_free(void)
116 {
117         GSList * group_elt;
118
119         for(group_elt = group_list ; group_elt != NULL ;
120             group_elt = group_elt->next)
121             g_free(group_elt->data);
122         g_slist_free(group_list);
123 }
124
125 static void grouplist_dialog_create(void)
126 {
127         GtkWidget *vbox;
128         GtkWidget *hbox;
129         GtkWidget *confirm_area;
130         GtkWidget *cancel_button;       
131         GtkWidget *refresh_button;      
132         GtkWidget *scrolledwin;
133
134         dialog = gtk_dialog_new();
135         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
136         gtk_widget_set_usize(dialog, GROUPLIST_DIALOG_WIDTH, GROUPLIST_DIALOG_HEIGHT);
137         gtk_container_set_border_width
138                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
139         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
140         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
141                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
142         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
143                            GTK_SIGNAL_FUNC(key_pressed), NULL);
144         gtk_signal_connect(GTK_OBJECT(dialog), "focus_in_event",
145                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
146         gtk_signal_connect(GTK_OBJECT(dialog), "focus_out_event",
147                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
148
149         gtk_widget_realize(dialog);
150
151         vbox = gtk_vbox_new(FALSE, 8);
152         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
153         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
154
155         hbox = gtk_hbox_new(FALSE, 0);
156         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
157
158         msg_label = gtk_label_new("");
159         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
160         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
161
162
163         scrolledwin = gtk_scrolled_window_new (NULL, NULL);
164         gtk_box_pack_start (GTK_BOX (vbox), scrolledwin,
165                             TRUE, TRUE, 0);
166         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
167                                         GTK_POLICY_AUTOMATIC,
168                                         GTK_POLICY_AUTOMATIC);
169
170         group_clist = gtk_clist_new(1);
171         gtk_container_add (GTK_CONTAINER (scrolledwin), group_clist);
172         gtk_clist_set_column_width (GTK_CLIST (group_clist), 0, 80);
173         gtk_clist_set_selection_mode (GTK_CLIST (group_clist),
174                                       GTK_SELECTION_BROWSE);
175         GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (group_clist)->column[0].button,
176                                 GTK_CAN_FOCUS);
177         gtk_signal_connect (GTK_OBJECT (group_clist), "select_row",
178                             GTK_SIGNAL_FUNC (group_clist_select), NULL);
179
180         gtkut_button_set_create(&confirm_area,
181                                 &ok_button,     _("OK"),
182                                 &cancel_button, _("Cancel"),
183                                 &refresh_button, _("Refresh"));
184         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
185                           confirm_area);
186         gtk_widget_grab_default(ok_button);
187
188         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
189                            GTK_SIGNAL_FUNC(ok_clicked), NULL);
190         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
191                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
192         gtk_signal_connect(GTK_OBJECT(refresh_button), "clicked",
193                            GTK_SIGNAL_FUNC(refresh_clicked), NULL);
194
195
196         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
197 }
198
199 static void grouplist_dialog_set_list(void)
200 {
201         GSList * elt;
202         gint row;
203
204         group_selected = NULL;
205         group_list = news_get_group_list(item);
206         if (group_list == NULL) {
207                 alertpanel_error(_("Can't retrieve group list."));
208                 return;
209         }
210
211         gtk_clist_clear(GTK_CLIST(group_clist));
212         for(elt = group_list; elt != NULL ; elt = elt->next)
213           {
214             row = gtk_clist_append(GTK_CLIST(group_clist),
215                                    (void *) &(elt->data));
216             gtk_clist_set_row_data(GTK_CLIST(group_clist), row, elt->data);
217           }
218
219         gtk_widget_grab_focus(ok_button);
220         gtk_widget_grab_focus(group_clist);
221
222         if (group_list != NULL)
223           group_selected = group_list->data;
224         else
225           group_selected = NULL;
226 }
227
228 static void grouplist_dialog_set(void)
229 {
230         gtk_window_set_title(GTK_WINDOW(dialog), _("Subscribe newsgroup"));
231         gtk_label_set_text(GTK_LABEL(msg_label), _("Input subscribing newsgroup:"));
232         grouplist_dialog_set_list();
233 }
234
235 static void ok_clicked(GtkWidget *widget, gpointer data)
236 {
237         ack = TRUE;
238         gtk_main_quit();
239 }
240
241 static void cancel_clicked(GtkWidget *widget, gpointer data)
242 {
243         ack = FALSE;
244         gtk_main_quit();
245 }
246
247 static void refresh_clicked(GtkWidget *widget, gpointer data)
248
249         grouplist_free();
250         news_reset_group_list(item);
251         grouplist_dialog_set_list();
252 }
253
254 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
255 {
256         if (event && event->keyval == GDK_Escape) {
257                 ack = FALSE;
258                 gtk_main_quit();
259         }
260 }
261
262 static void group_clist_select(GtkCList *clist, gint row, gint column,
263                               GdkEventButton *event, gpointer user_data)
264 {
265   group_selected = (gchar *)
266     gtk_clist_get_row_data(GTK_CLIST(group_clist), row);
267 }