bugfix for newsgroup selection dialog
[claws.git] / src / grouplistdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkwidget.h>
30 #include <gtk/gtkdialog.h>
31 #include <gtk/gtkwindow.h>
32 #include <gtk/gtksignal.h>
33 #include <gtk/gtkvbox.h>
34 #include <gtk/gtkhbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtkclist.h>
38 #include <gtk/gtkscrolledwindow.h>
39 #include <gtk/gtkbutton.h>
40 #include <gtk/gtkhbbox.h>
41 #include <string.h>
42 #include <fnmatch.h>
43
44 #include "intl.h"
45 #include "grouplistdialog.h"
46 #include "manage_window.h"
47 #include "gtkutils.h"
48 #include "utils.h"
49 #include "news.h"
50 #include "folder.h"
51 #include "alertpanel.h"
52 #include "recv.h"
53 #include "socket.h"
54
55 #define GROUPLIST_DIALOG_WIDTH  500
56 #define GROUPLIST_NAMES         250
57 #define GROUPLIST_DIALOG_HEIGHT 400
58
59 static GList * subscribed = NULL;
60 gboolean dont_unsubscribed = FALSE;
61
62 static gboolean ack;
63 static gboolean locked;
64
65 static GtkWidget *dialog;
66 static GtkWidget *entry;
67 static GtkWidget *clist;
68 static GtkWidget *status_label;
69 static GtkWidget *ok_button;
70 static GSList *group_list = NULL;
71 static Folder *news_folder;
72
73 static void grouplist_dialog_create     (void);
74 static void grouplist_dialog_set_list   (gchar * pattern);
75 static void grouplist_clear             (void);
76 static void grouplist_recv_func         (SockInfo       *sock,
77                                          gint            count,
78                                          gint            read_bytes,
79                                          gpointer        data);
80
81 static void ok_clicked          (GtkWidget      *widget,
82                                  gpointer        data);
83 static void cancel_clicked      (GtkWidget      *widget,
84                                  gpointer        data);
85 static void refresh_clicked     (GtkWidget      *widget,
86                                  gpointer        data);
87 static void key_pressed         (GtkWidget      *widget,
88                                  GdkEventKey    *event,
89                                  gpointer        data);
90 static void clist_selected      (GtkCList       *clist,
91                                  gint            row,
92                                  gint            column,
93                                  GdkEventButton *event,
94                                  gpointer        user_data);
95 static void clist_unselected    (GtkCList       *clist,
96                                  gint            row,
97                                  gint            column,
98                                  GdkEventButton *event,
99                                  gpointer        user_data);
100 static void entry_activated     (GtkEditable    *editable);
101
102 GList *grouplist_dialog(Folder *folder, GList * cur_subscriptions)
103 {
104         gchar *str;
105         GList * l;
106
107         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
108
109         if (!dialog)
110                 grouplist_dialog_create();
111
112         news_folder = folder;
113
114         gtk_widget_show(dialog);
115         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
116         manage_window_set_transient(GTK_WINDOW(dialog));
117         GTK_EVENTS_FLUSH();
118
119         subscribed = NULL;
120
121         for(l = cur_subscriptions ; l != NULL ; l = l->next)
122           subscribed = g_list_append(subscribed, g_strdup((gchar *) l->data));
123
124         grouplist_dialog_set_list(NULL);
125
126         gtk_main();
127
128         manage_window_focus_out(dialog, NULL, NULL);
129         gtk_widget_hide(dialog);
130
131         /*
132         if (ack) {
133                 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
134                 if (str && *str == '\0') {
135                         g_free(str);
136                         str = NULL;
137                 }
138         } else
139                 str = NULL;
140         */
141         if (!ack) {
142           list_free_strings(subscribed);
143           g_list_free(subscribed);
144
145           subscribed = NULL;
146           
147           for(l = cur_subscriptions ; l != NULL ; l = l->next)
148             subscribed = g_list_append(subscribed, g_strdup((gchar *) l->data));
149         }
150
151         GTK_EVENTS_FLUSH();
152
153         debug_print("return string = %s\n", str ? str : "(none)");
154         return subscribed;
155 }
156
157 static void grouplist_clear(void)
158 {
159         dont_unsubscribed = TRUE;
160         gtk_clist_clear(GTK_CLIST(clist));
161         gtk_entry_set_text(GTK_ENTRY(entry), "");
162         dont_unsubscribed = FALSE;
163 }
164
165 static void grouplist_dialog_create(void)
166 {
167         GtkWidget *vbox;
168         GtkWidget *hbox;
169         GtkWidget *msg_label;
170         GtkWidget *confirm_area;
171         GtkWidget *cancel_button;       
172         GtkWidget *refresh_button;      
173         GtkWidget *scrolledwin;
174         gchar * col_names[3] = {
175                 _("name"), _("count of messages"), _("type")
176         };
177
178         dialog = gtk_dialog_new();
179         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, TRUE, FALSE);
180         gtk_widget_set_usize(dialog,
181                              GROUPLIST_DIALOG_WIDTH, GROUPLIST_DIALOG_HEIGHT);
182         gtk_container_set_border_width
183                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
184         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
185         gtk_window_set_title(GTK_WINDOW(dialog), _("Subscribe to newsgroup"));
186         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
187                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
188         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
189                            GTK_SIGNAL_FUNC(key_pressed), NULL);
190         gtk_signal_connect(GTK_OBJECT(dialog), "focus_in_event",
191                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
192         gtk_signal_connect(GTK_OBJECT(dialog), "focus_out_event",
193                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
194
195         gtk_widget_realize(dialog);
196
197         vbox = gtk_vbox_new(FALSE, 8);
198         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
199         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
200
201         hbox = gtk_hbox_new(FALSE, 0);
202         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
203
204         msg_label = gtk_label_new(_("Input subscribing newsgroup:"));
205         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
206
207         entry = gtk_entry_new();
208         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
209         gtk_signal_connect(GTK_OBJECT(entry), "activate",
210                            GTK_SIGNAL_FUNC(entry_activated), NULL);
211
212         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
213         gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
214         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
215                                        GTK_POLICY_AUTOMATIC,
216                                        GTK_POLICY_AUTOMATIC);
217
218         clist = gtk_clist_new_with_titles(3, col_names);
219         gtk_container_add(GTK_CONTAINER(scrolledwin), clist);
220         gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_MULTIPLE);
221         GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[0].button,
222                                GTK_CAN_FOCUS);
223         gtk_signal_connect(GTK_OBJECT(clist), "select_row",
224                            GTK_SIGNAL_FUNC(clist_selected), NULL);
225         gtk_signal_connect(GTK_OBJECT(clist), "unselect_row",
226                            GTK_SIGNAL_FUNC(clist_unselected), NULL);
227
228         hbox = gtk_hbox_new(FALSE, 0);
229         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
230
231         status_label = gtk_label_new("");
232         gtk_box_pack_start(GTK_BOX(hbox), status_label, FALSE, FALSE, 0);
233
234         gtkut_button_set_create(&confirm_area,
235                                 &ok_button,      _("OK"),
236                                 &cancel_button,  _("Cancel"),
237                                 &refresh_button, _("Refresh"));
238         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
239                           confirm_area);
240         gtk_widget_grab_default(ok_button);
241
242         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
243                            GTK_SIGNAL_FUNC(ok_clicked), NULL);
244         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
245                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
246         gtk_signal_connect(GTK_OBJECT(refresh_button), "clicked",
247                            GTK_SIGNAL_FUNC(refresh_clicked), NULL);
248
249         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
250 }
251
252 static void grouplist_dialog_set_list(gchar * pattern)
253 {
254         GSList *cur;
255         gint row;
256
257         if (pattern == NULL)
258                 pattern = "*";
259
260         if (locked) return;
261         locked = TRUE;
262
263         grouplist_clear();
264
265         if (group_list == NULL) {
266           recv_set_ui_func(grouplist_recv_func, NULL);
267           group_list = news_get_group_list(news_folder);
268           recv_set_ui_func(NULL, NULL);
269           if (group_list == NULL) {
270             alertpanel_error(_("Can't retrieve newsgroup list."));
271             locked = FALSE;
272             return;
273           }
274         }
275
276         dont_unsubscribed = TRUE;
277
278         gtk_clist_freeze(GTK_CLIST(clist));
279         for (cur = group_list; cur != NULL ; cur = cur->next) {
280                 struct NNTPGroupInfo * info;
281
282                 info = (struct NNTPGroupInfo *) cur->data;
283
284                 if (fnmatch(pattern, info->name, 0) == 0) {
285                         gchar count_str[10];
286                         gchar * cols[3];
287                         gint count;
288                         GList * l;
289
290                         count = info->last - info->first;
291                         if (count < 0)
292                                 count = 0;
293                         snprintf(count_str, 10, "%i", count);
294
295                         cols[0] = info->name;
296                         cols[1] = count_str;
297                         if (info->type == 'y')
298                                 cols[2] = "";
299                         else if (info->type == 'm')
300                                 cols[2] = "moderated";
301                         else if (info->type == 'n')
302                                 cols[2] = "readonly";
303                         else
304                                 cols[2] = "unkown";
305
306                         row = gtk_clist_append(GTK_CLIST(clist), cols);
307                         gtk_clist_set_row_data(GTK_CLIST(clist), row, info);
308
309                         l = g_list_find_custom(subscribed, info->name,
310                                                (GCompareFunc) g_strcasecmp);
311                         
312                         if (l != NULL)
313                           gtk_clist_select_row(GTK_CLIST(clist), row, 0);
314                 }
315         }
316
317         gtk_clist_moveto(GTK_CLIST(clist), 0, 0, 0, 0);
318
319         dont_unsubscribed = FALSE;
320
321         gtk_clist_set_column_width(GTK_CLIST(clist), 0, GROUPLIST_NAMES);
322         gtk_clist_set_column_justification (GTK_CLIST(clist), 1,
323                                             GTK_JUSTIFY_RIGHT);
324         gtk_clist_set_column_auto_resize(GTK_CLIST(clist), 1, TRUE);
325         gtk_clist_set_column_auto_resize(GTK_CLIST(clist), 2, TRUE);
326
327         gtk_clist_thaw(GTK_CLIST(clist));
328
329         gtk_widget_grab_focus(ok_button);
330         gtk_widget_grab_focus(clist);
331
332         gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
333
334         locked = FALSE;
335 }
336
337 static void grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
338                                 gpointer data)
339 {
340         gchar buf[BUFFSIZE];
341
342         g_snprintf(buf, sizeof(buf),
343                    _("%d newsgroups received (%s read)"),
344                    count, to_human_readable(read_bytes));
345         gtk_label_set_text(GTK_LABEL(status_label), buf);
346         GTK_EVENTS_FLUSH();
347 }
348
349 static void ok_clicked(GtkWidget *widget, gpointer data)
350 {
351         gchar * str;
352         gboolean update_list;
353
354         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
355
356         update_list = FALSE;
357
358         if (strchr(str, '*') != NULL)
359           update_list = TRUE;
360
361         if (update_list) {
362                 grouplist_dialog_set_list(str);
363                 g_free(str);
364         }
365         else {
366                 g_free(str);
367                 ack = TRUE;
368                 if (gtk_main_level() > 1)
369                         gtk_main_quit();
370         }
371 }
372
373 static void cancel_clicked(GtkWidget *widget, gpointer data)
374 {
375         ack = FALSE;
376         if (gtk_main_level() > 1)
377                 gtk_main_quit();
378 }
379
380 static void refresh_clicked(GtkWidget *widget, gpointer data)
381 {
382         gchar * str;
383  
384         if (locked) return;
385
386         news_group_list_free(group_list);
387         group_list = NULL;
388         news_remove_group_list(news_folder);
389
390         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
391         grouplist_dialog_set_list(str);
392         g_free(str);
393 }
394
395 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
396 {
397         if (event && event->keyval == GDK_Escape)
398                 cancel_clicked(NULL, NULL);
399 }
400
401 static void clist_selected(GtkCList *clist, gint row, gint column,
402                            GdkEventButton *event, gpointer user_data)
403 {
404         struct NNTPGroupInfo * group;
405         GList * l;
406
407         group = (struct NNTPGroupInfo *)
408           gtk_clist_get_row_data(GTK_CLIST(clist), row);
409
410         if (!dont_unsubscribed) {
411                 subscribed = g_list_append(subscribed, g_strdup(group->name));
412         }
413 }
414
415 static void clist_unselected(GtkCList *clist, gint row, gint column,
416                              GdkEventButton *event, gpointer user_data)
417 {
418         struct NNTPGroupInfo * group;
419         GList * l;
420
421         group = (struct NNTPGroupInfo *)
422           gtk_clist_get_row_data(GTK_CLIST(clist), row);
423
424         if (!dont_unsubscribed) {
425                 l = g_list_find_custom(subscribed, group->name,
426                                        (GCompareFunc) g_strcasecmp);
427                 if (l != NULL) {
428                   g_free(l->data);
429                         subscribed = g_list_remove(subscribed, l->data);
430                 }
431         }
432 }
433
434 static gboolean match_string(gchar * str, gchar * expr)
435 {
436 }
437
438 static void entry_activated(GtkEditable *editable)
439 {
440         gchar * str;
441         gboolean update_list;
442
443         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
444
445         update_list = FALSE;
446
447         if (strchr(str, '*') != NULL)
448           update_list = TRUE;
449
450         if (update_list) {
451                 grouplist_dialog_set_list(str);
452                 g_free(str);
453         }
454         else {
455                 g_free(str);
456                 ok_clicked(NULL, NULL);
457         }
458 }