sync with Hiroyuki's 0.5.0claws2-3
[claws.git] / src / grouplistdialog.c
similarity index 53%
rename from src/grouplist_dialog.c
rename to src/grouplistdialog.c
index 32931c0..4e8ac5a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2001 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -21,6 +21,8 @@
 #  include "config.h"
 #endif
 
+#include "defs.h"
+
 #include <glib.h>
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtkmain.h>
 #include <gtk/gtkvbox.h>
 #include <gtk/gtkhbox.h>
 #include <gtk/gtklabel.h>
+#include <gtk/gtkentry.h>
 #include <gtk/gtkclist.h>
 #include <gtk/gtkscrolledwindow.h>
 #include <gtk/gtkbutton.h>
 #include <gtk/gtkhbbox.h>
 
 #include "intl.h"
-#include "grouplist_dialog.h"
+#include "grouplistdialog.h"
 #include "manage_window.h"
 #include "gtkutils.h"
 #include "utils.h"
 #include "news.h"
 #include "folder.h"
 #include "alertpanel.h"
+#include "recv.h"
+#include "socket.h"
 
 #define GROUPLIST_DIALOG_WIDTH 420
 #define GROUPLIST_DIALOG_HEIGHT        400
 
 static gboolean ack;
+static gboolean locked;
 
 static GtkWidget *dialog;
-static GtkWidget *msg_label;
-static GtkWidget *group_clist;
+static GtkWidget *entry;
+static GtkWidget *clist;
+static GtkWidget *status_label;
 static GtkWidget *ok_button;
-static gchar * group_selected;
-static GSList * group_list;
-static FolderItem * item;
+static GSList *group_list;
+static Folder *news_folder;
 
 static void grouplist_dialog_create    (void);
-static void grouplist_dialog_set       (void);
-static void grouplist_free             (void);
+static void grouplist_dialog_set_list  (void);
+static void grouplist_clear            (void);
+static void grouplist_recv_func                (SockInfo       *sock,
+                                        gint            count,
+                                        gint            read_bytes,
+                                        gpointer        data);
 
 static void ok_clicked         (GtkWidget      *widget,
                                 gpointer        data);
 static void cancel_clicked     (GtkWidget      *widget,
                                 gpointer        data);
-static void refresh_clicked(GtkWidget *widget, gpointer data);
+static void refresh_clicked    (GtkWidget      *widget,
+                                gpointer        data);
 static void key_pressed                (GtkWidget      *widget,
                                 GdkEventKey    *event,
                                 gpointer        data);
-static void group_clist_select(GtkCList *clist, gint row, gint column,
-                             GdkEventButton *event, gpointer user_data);
-
-gchar *grouplist_dialog(FolderItem * i)
+static void clist_selected     (GtkCList       *clist,
+                                gint            row,
+                                gint            column,
+                                GdkEventButton *event,
+                                gpointer        user_data);
+static void entry_activated    (GtkEditable    *editable);
+
+gchar *grouplist_dialog(Folder *folder)
 {
        gchar *str;
 
@@ -82,12 +97,14 @@ gchar *grouplist_dialog(FolderItem * i)
        if (!dialog)
                grouplist_dialog_create();
 
-       item = i;
+       news_folder = folder;
 
-       grouplist_dialog_set();
        gtk_widget_show(dialog);
        gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
        manage_window_set_transient(GTK_WINDOW(dialog));
+       GTK_EVENTS_FLUSH();
+
+       grouplist_dialog_set_list();
 
        gtk_main();
 
@@ -95,30 +112,26 @@ gchar *grouplist_dialog(FolderItem * i)
        gtk_widget_hide(dialog);
 
        if (ack) {
-               if (group_selected != NULL)
-                       str = g_strdup(group_selected);
-               else
-                       str = NULL;
-               if (str && *str == '\0')
+               str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
+               if (str && *str == '\0') {
+                       g_free(str);
                        str = NULL;
+               }
        } else
                str = NULL;
 
-       grouplist_free();
-
+       grouplist_clear();
        GTK_EVENTS_FLUSH();
 
        debug_print("return string = %s\n", str ? str : "(none)");
        return str;
 }
 
-static void grouplist_free(void)
+static void grouplist_clear(void)
 {
-       GSList * group_elt;
-
-       for(group_elt = group_list ; group_elt != NULL ;
-           group_elt = group_elt->next)
-           g_free(group_elt->data);
+       gtk_clist_clear(GTK_CLIST(clist));
+       gtk_entry_set_text(GTK_ENTRY(entry), "");
+       slist_free_strings(group_list);
        g_slist_free(group_list);
 }
 
@@ -126,17 +139,20 @@ static void grouplist_dialog_create(void)
 {
        GtkWidget *vbox;
        GtkWidget *hbox;
+       GtkWidget *msg_label;
        GtkWidget *confirm_area;
        GtkWidget *cancel_button;       
        GtkWidget *refresh_button;      
        GtkWidget *scrolledwin;
 
        dialog = gtk_dialog_new();
-       gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
-       gtk_widget_set_usize(dialog, GROUPLIST_DIALOG_WIDTH, GROUPLIST_DIALOG_HEIGHT);
+       gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, TRUE, FALSE);
+       gtk_widget_set_usize(dialog,
+                            GROUPLIST_DIALOG_WIDTH, GROUPLIST_DIALOG_HEIGHT);
        gtk_container_set_border_width
                (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
        gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
+       gtk_window_set_title(GTK_WINDOW(dialog), _("Subscribe to newsgroup"));
        gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
                           GTK_SIGNAL_FUNC(cancel_clicked), NULL);
        gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
@@ -155,31 +171,38 @@ static void grouplist_dialog_create(void)
        hbox = gtk_hbox_new(FALSE, 0);
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
 
-       msg_label = gtk_label_new("");
+       msg_label = gtk_label_new(_("Input subscribing newsgroup:"));
        gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
-       gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
-
-
-       scrolledwin = gtk_scrolled_window_new (NULL, NULL);
-       gtk_box_pack_start (GTK_BOX (vbox), scrolledwin,
-                           TRUE, TRUE, 0);
-       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
-                                       GTK_POLICY_AUTOMATIC,
-                                       GTK_POLICY_AUTOMATIC);
-
-       group_clist = gtk_clist_new(1);
-       gtk_container_add (GTK_CONTAINER (scrolledwin), group_clist);
-       gtk_clist_set_column_width (GTK_CLIST (group_clist), 0, 80);
-       gtk_clist_set_selection_mode (GTK_CLIST (group_clist),
-                                     GTK_SELECTION_BROWSE);
-       GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (group_clist)->column[0].button,
-                               GTK_CAN_FOCUS);
-       gtk_signal_connect (GTK_OBJECT (group_clist), "select_row",
-                           GTK_SIGNAL_FUNC (group_clist_select), NULL);
+
+       entry = gtk_entry_new();
+       gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
+       gtk_signal_connect(GTK_OBJECT(entry), "activate",
+                          GTK_SIGNAL_FUNC(entry_activated), NULL);
+
+       scrolledwin = gtk_scrolled_window_new(NULL, NULL);
+       gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
+       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
+                                      GTK_POLICY_AUTOMATIC,
+                                      GTK_POLICY_AUTOMATIC);
+
+       clist = gtk_clist_new(1);
+       gtk_container_add(GTK_CONTAINER(scrolledwin), clist);
+       gtk_clist_set_column_width(GTK_CLIST(clist), 0, 80);
+       gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_BROWSE);
+       GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[0].button,
+                              GTK_CAN_FOCUS);
+       gtk_signal_connect(GTK_OBJECT(clist), "select_row",
+                          GTK_SIGNAL_FUNC(clist_selected), NULL);
+
+       hbox = gtk_hbox_new(FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+
+       status_label = gtk_label_new("");
+       gtk_box_pack_start(GTK_BOX(hbox), status_label, FALSE, FALSE, 0);
 
        gtkut_button_set_create(&confirm_area,
-                               &ok_button,     _("OK"),
-                               &cancel_button, _("Cancel"),
+                               &ok_button,      _("OK"),
+                               &cancel_button,  _("Cancel"),
                                &refresh_button, _("Refresh"));
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
                          confirm_area);
@@ -192,76 +215,93 @@ static void grouplist_dialog_create(void)
        gtk_signal_connect(GTK_OBJECT(refresh_button), "clicked",
                           GTK_SIGNAL_FUNC(refresh_clicked), NULL);
 
-
        gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
 }
 
 static void grouplist_dialog_set_list(void)
 {
-       GSList * elt;
+       GSList *cur;
        gint row;
 
-       group_selected = NULL;
-       group_list = news_get_group_list(item);
+       if (locked) return;
+       locked = TRUE;
+
+       recv_set_ui_func(grouplist_recv_func, NULL);
+       group_list = news_get_group_list(news_folder);
+       recv_set_ui_func(NULL, NULL);
        if (group_list == NULL) {
-               alertpanel_error(_("Can't retrieve group list."));
+               alertpanel_error(_("Can't retrieve newsgroup list."));
+               locked = FALSE;
                return;
        }
 
-       gtk_clist_clear(GTK_CLIST(group_clist));
-       for(elt = group_list; elt != NULL ; elt = elt->next)
-         {
-           row = gtk_clist_append(GTK_CLIST(group_clist),
-                                  (void *) &(elt->data));
-           gtk_clist_set_row_data(GTK_CLIST(group_clist), row, elt->data);
-         }
+       gtk_clist_freeze(GTK_CLIST(clist));
+       for (cur = group_list; cur != NULL ; cur = cur->next) {
+               row = gtk_clist_append(GTK_CLIST(clist),
+                                      (gchar **)&(cur->data));
+               gtk_clist_set_row_data(GTK_CLIST(clist), row, cur->data);
+       }
+       gtk_clist_thaw(GTK_CLIST(clist));
 
        gtk_widget_grab_focus(ok_button);
-       gtk_widget_grab_focus(group_clist);
+       gtk_widget_grab_focus(clist);
 
-       if (group_list != NULL)
-         group_selected = group_list->data;
-       else
-         group_selected = NULL;
+       gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
+
+       locked = FALSE;
 }
 
-static void grouplist_dialog_set(void)
+static void grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
+                               gpointer data)
 {
-       gtk_window_set_title(GTK_WINDOW(dialog), _("Subscribe newsgroup"));
-       gtk_label_set_text(GTK_LABEL(msg_label), _("Input subscribing newsgroup:"));
-       grouplist_dialog_set_list();
+       gchar buf[BUFFSIZE];
+
+       g_snprintf(buf, sizeof(buf),
+                  _("%d newsgroups received (%d bytes read)"),
+                  count, read_bytes);
+       gtk_label_set_text(GTK_LABEL(status_label), buf);
+       GTK_EVENTS_FLUSH();
 }
 
 static void ok_clicked(GtkWidget *widget, gpointer data)
 {
        ack = TRUE;
-       gtk_main_quit();
+       if (gtk_main_level() > 1)
+               gtk_main_quit();
 }
 
 static void cancel_clicked(GtkWidget *widget, gpointer data)
 {
        ack = FALSE;
-       gtk_main_quit();
+       if (gtk_main_level() > 1)
+               gtk_main_quit();
 }
 
 static void refresh_clicked(GtkWidget *widget, gpointer data)
 { 
-       grouplist_free();
-       news_reset_group_list(item);
+       if (locked) return;
+
+       grouplist_clear();
+       news_remove_group_list(news_folder);
        grouplist_dialog_set_list();
 }
 
 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
 {
-       if (event && event->keyval == GDK_Escape) {
-               ack = FALSE;
-               gtk_main_quit();
-       }
+       if (event && event->keyval == GDK_Escape)
+               cancel_clicked(NULL, NULL);
+}
+
+static void clist_selected(GtkCList *clist, gint row, gint column,
+                          GdkEventButton *event, gpointer user_data)
+{
+       gchar *group;
+
+       group = (gchar *)gtk_clist_get_row_data(GTK_CLIST(clist), row);
+       gtk_entry_set_text(GTK_ENTRY(entry), group ? group : "");
 }
 
-static void group_clist_select(GtkCList *clist, gint row, gint column,
-                             GdkEventButton *event, gpointer user_data)
+static void entry_activated(GtkEditable *editable)
 {
-  group_selected = (gchar *)
-    gtk_clist_get_row_data(GTK_CLIST(group_clist), row);
+       ok_clicked(NULL, NULL);
 }