Remove inclusion of sys/socket.h from nntp-thread.c
[claws.git] / src / grouplistdialog.c
blob66d7bc03a43936fbbf2b25cc1e258abefccddd35
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and Hiroyuki Yamamoto
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include "defs.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gtk/gtk.h>
31 #include <string.h>
33 #include "grouplistdialog.h"
34 #include "mainwindow.h"
35 #include "manage_window.h"
36 #include "gtkutils.h"
37 #include "utils.h"
38 #include "news.h"
39 #include "folder.h"
40 #include "alertpanel.h"
41 #include "recv.h"
42 #include "socket.h"
43 #include "prefs_common.h"
45 #define GROUPLIST_DIALOG_WIDTH 450
46 #define GROUPLIST_DIALOG_HEIGHT 400
47 #define GROUPLIST_COL_NAME_WIDTH 250
49 static gboolean ack;
50 static gboolean locked;
52 static GtkWidget *dialog;
53 static GtkWidget *entry;
54 static GtkWidget *ctree;
55 static GtkWidget *status_label;
56 static GtkWidget *ok_button;
57 static GSList *group_list;
58 static Folder *news_folder;
60 static GSList *subscribed;
62 static void grouplist_dialog_create (void);
63 static void grouplist_dialog_set_list (const gchar *pattern,
64 gboolean refresh);
65 static void grouplist_search (void);
66 static void grouplist_clear (void);
67 static gboolean grouplist_recv_func (SockInfo *sock,
68 gint count,
69 gint read_bytes,
70 gpointer data);
71 static void grouplist_size_allocate (GtkWidget *widget,
72 GtkAllocation *allocation);
74 static gint window_deleted (GtkWidget *widget,
75 GdkEventAny *event,
76 gpointer data);
77 static void ok_clicked (GtkWidget *widget,
78 gpointer data);
79 static void cancel_clicked (GtkWidget *widget,
80 gpointer data);
81 static void refresh_clicked (GtkWidget *widget,
82 gpointer data);
83 static gboolean key_pressed (GtkWidget *widget,
84 GdkEventKey *event,
85 gpointer data);
86 static gboolean button_press_cb (GtkCMCTree *ctree,
87 GdkEventButton *button,
88 gpointer data);
89 static void entry_activated (GtkEditable *editable);
90 static void search_clicked (GtkWidget *widget,
91 gpointer data);
93 GSList *grouplist_dialog(Folder *folder)
95 GNode *node;
96 FolderItem *item;
98 if (dialog && gtk_widget_get_visible(dialog)) return NULL;
100 if (!dialog)
101 grouplist_dialog_create();
103 news_folder = folder;
105 gtk_widget_show(dialog);
106 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
107 manage_window_set_transient(GTK_WINDOW(dialog));
108 gtk_widget_grab_focus(ok_button);
109 gtk_widget_grab_focus(ctree);
110 GTK_EVENTS_FLUSH();
112 subscribed = NULL;
113 for (node = folder->node->children; node != NULL; node = node->next) {
114 item = FOLDER_ITEM(node->data);
115 subscribed = g_slist_append(subscribed, g_strdup(item->path));
118 grouplist_dialog_set_list(NULL, TRUE);
120 if (ack) gtk_main();
122 manage_window_focus_out(dialog, NULL, NULL);
123 gtk_widget_hide(dialog);
125 if (!ack) {
126 slist_free_strings_full(subscribed);
127 subscribed = NULL;
129 for (node = folder->node->children; node != NULL;
130 node = node->next) {
131 item = FOLDER_ITEM(node->data);
132 subscribed = g_slist_append(subscribed,
133 g_strdup(item->path));
137 grouplist_clear();
139 return subscribed;
142 static void grouplist_dialog_create(void)
144 GtkWidget *grid;
145 GtkWidget *hgrid;
146 GtkWidget *msg_label;
147 GtkWidget *search_button;
148 GtkWidget *cancel_button;
149 GtkWidget *refresh_button;
150 GtkWidget *scrolledwin;
151 static GdkGeometry geometry;
152 gchar *titles[3];
153 gint i;
155 dialog = gtk_dialog_new();
156 gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
157 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
158 gtk_window_set_title(GTK_WINDOW(dialog), _("Newsgroup subscription"));
159 g_signal_connect(G_OBJECT(dialog), "delete_event",
160 G_CALLBACK(window_deleted), NULL);
161 g_signal_connect(G_OBJECT(dialog), "key_press_event",
162 G_CALLBACK(key_pressed), NULL);
163 g_signal_connect(G_OBJECT(dialog), "size_allocate",
164 G_CALLBACK(grouplist_size_allocate), NULL);
165 MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
167 grid = gtk_grid_new();
168 gtk_grid_set_row_spacing(GTK_GRID(grid), 8);
169 gtk_container_set_border_width(GTK_CONTAINER(grid), 8);
170 gtk_orientable_set_orientation(GTK_ORIENTABLE(grid),
171 GTK_ORIENTATION_VERTICAL);
172 gtk_container_add(GTK_CONTAINER(
173 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), grid);
175 msg_label = gtk_label_new(_("Select newsgroups for subscription:"));
176 gtk_widget_set_hexpand(msg_label, TRUE);
177 gtk_widget_set_vexpand(msg_label, FALSE);
178 gtk_widget_set_halign(msg_label, GTK_ALIGN_START);
179 gtk_container_add(GTK_CONTAINER(grid), msg_label);
181 hgrid = gtk_grid_new();
182 gtk_grid_set_column_spacing(GTK_GRID(hgrid), 8);
183 gtk_orientable_set_orientation(GTK_ORIENTABLE(hgrid),
184 GTK_ORIENTATION_HORIZONTAL);
185 gtk_widget_set_hexpand(hgrid, TRUE);
186 gtk_widget_set_vexpand(hgrid, FALSE);
187 gtk_container_add(GTK_CONTAINER(grid), hgrid);
189 msg_label = gtk_label_new(_("Find groups:"));
190 gtk_container_add(GTK_CONTAINER(hgrid), msg_label);
192 entry = gtk_entry_new();
193 gtk_widget_set_hexpand(entry, TRUE);
194 gtk_container_add(GTK_CONTAINER(hgrid), entry);
195 g_signal_connect(G_OBJECT(entry), "activate",
196 G_CALLBACK(entry_activated), NULL);
198 search_button = gtk_button_new_with_label(_(" Search "));
199 gtk_container_add(GTK_CONTAINER(hgrid), search_button);
201 g_signal_connect(G_OBJECT(search_button), "clicked",
202 G_CALLBACK(search_clicked), NULL);
204 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
205 gtk_widget_set_hexpand(scrolledwin, TRUE);
206 gtk_widget_set_vexpand(scrolledwin, TRUE);
207 gtk_container_add(GTK_CONTAINER(grid), scrolledwin);
208 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
209 GTK_POLICY_AUTOMATIC,
210 GTK_POLICY_AUTOMATIC);
212 titles[0] = _("Newsgroup name");
213 titles[1] = _("Messages");
214 titles[2] = _("Type");
215 ctree = gtk_sctree_new_with_titles(3, 0, titles);
216 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
217 gtk_cmclist_set_column_width
218 (GTK_CMCLIST(ctree), 0, GROUPLIST_COL_NAME_WIDTH);
219 gtk_cmclist_set_column_auto_resize(GTK_CMCLIST(ctree), 0, TRUE);
220 gtk_cmclist_set_selection_mode(GTK_CMCLIST(ctree), GTK_SELECTION_MULTIPLE);
222 gtk_cmctree_set_expander_style(GTK_CMCTREE(ctree),
223 GTK_CMCTREE_EXPANDER_TRIANGLE);
225 for (i = 0; i < 3; i++)
226 gtk_widget_set_can_focus(GTK_CMCLIST(ctree)->column[i].button, FALSE);
227 g_signal_connect(G_OBJECT(ctree), "button-press-event",
228 G_CALLBACK(button_press_cb), NULL);
230 status_label = gtk_label_new("");
231 gtk_widget_set_hexpand(status_label, TRUE);
232 gtk_widget_set_vexpand(status_label, FALSE);
233 gtk_widget_set_halign(status_label, GTK_ALIGN_START);
234 gtk_container_add(GTK_CONTAINER(grid), status_label);
236 refresh_button = gtk_dialog_add_button(GTK_DIALOG(dialog), _("_Refresh"),
237 GTK_RESPONSE_NONE);
238 cancel_button = gtk_dialog_add_button(GTK_DIALOG(dialog),_("_Cancel"),
239 GTK_RESPONSE_NONE);
240 ok_button = gtk_dialog_add_button(GTK_DIALOG(dialog),_("_OK"),
241 GTK_RESPONSE_NONE);
242 gtk_widget_grab_default(ok_button);
244 g_signal_connect(G_OBJECT(ok_button), "clicked",
245 G_CALLBACK(ok_clicked), NULL);
246 g_signal_connect(G_OBJECT(cancel_button), "clicked",
247 G_CALLBACK(cancel_clicked), NULL);
248 g_signal_connect(G_OBJECT(refresh_button), "clicked",
249 G_CALLBACK(refresh_clicked), NULL);
251 if (!geometry.min_width) {
252 geometry.min_width = GROUPLIST_DIALOG_WIDTH;
253 geometry.min_height = GROUPLIST_DIALOG_HEIGHT;
256 gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry,
257 GDK_HINT_MIN_SIZE);
258 gtk_window_set_default_size(GTK_WINDOW(dialog),
259 prefs_common.news_subscribe_width,
260 prefs_common.news_subscribe_height);
262 gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
265 static GHashTable *branch_node_table;
267 static void grouplist_hash_init(void)
269 branch_node_table = g_hash_table_new(g_str_hash, g_str_equal);
272 static void grouplist_hash_done(void)
274 hash_free_strings(branch_node_table);
275 g_hash_table_destroy(branch_node_table);
278 static GtkCMCTreeNode *grouplist_hash_get_branch_node(const gchar *name)
280 return g_hash_table_lookup(branch_node_table, name);
283 static void grouplist_hash_set_branch_node(const gchar *name,
284 GtkCMCTreeNode *node)
286 g_hash_table_insert(branch_node_table, g_strdup(name), node);
289 static gchar *grouplist_get_parent_name(const gchar *name)
291 gchar *p;
293 p = strrchr(name, '.');
294 if (!p)
295 return g_strdup("");
296 return g_strndup(name, p - name);
299 static GtkCMCTreeNode *grouplist_create_parent(const gchar *name,
300 const gchar *pattern)
302 GtkCMCTreeNode *parent;
303 GtkCMCTreeNode *node;
304 gchar *cols[3];
305 gchar *parent_name;
307 if (*name == '\0') return NULL;
308 node = grouplist_hash_get_branch_node(name);
309 if (node != NULL) return node;
311 cols[0] = (gchar *)name;
312 cols[1] = cols[2] = "";
314 parent_name = grouplist_get_parent_name(name);
315 parent = grouplist_create_parent(parent_name, pattern);
317 node = parent ? GTK_CMCTREE_ROW(parent)->children
318 : GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
319 node = gtk_sctree_insert_node(GTK_CMCTREE(ctree), parent, node,
320 cols, 0, NULL, NULL,
321 FALSE, FALSE);
322 if (parent && g_pattern_match_simple(pattern, parent_name) == FALSE)
323 gtk_cmctree_expand(GTK_CMCTREE(ctree), parent);
324 gtk_cmctree_node_set_selectable(GTK_CMCTREE(ctree), node, FALSE);
326 grouplist_hash_set_branch_node(name, node);
328 g_free(parent_name);
330 return node;
333 static GtkCMCTreeNode *grouplist_create_branch(NewsGroupInfo *ginfo,
334 const gchar *pattern)
336 GtkCMCTreeNode *node;
337 GtkCMCTreeNode *parent;
338 gchar *name = (gchar *)ginfo->name;
339 gchar *parent_name;
340 gchar *count_str;
341 gchar *cols[3];
342 gint count;
344 count = ginfo->last - ginfo->first;
345 if (count < 0)
346 count = 0;
347 count_str = itos(count);
349 cols[0] = ginfo->name;
350 cols[1] = count_str;
351 if (ginfo->type == 'y')
352 cols[2] = "";
353 else if (ginfo->type == 'm')
354 cols[2] = _("moderated");
355 else if (ginfo->type == 'n')
356 cols[2] = _("read-only");
357 else
358 cols[2] = _("unknown");
360 parent_name = grouplist_get_parent_name(name);
361 parent = grouplist_create_parent(parent_name, pattern);
362 node = grouplist_hash_get_branch_node(name);
363 if (node) {
364 gtk_cmctree_set_node_info(GTK_CMCTREE(ctree), node, cols[0], 0,
365 NULL, NULL, FALSE, FALSE);
366 gtk_cmctree_node_set_text(GTK_CMCTREE(ctree), node, 1, cols[1]);
367 gtk_cmctree_node_set_text(GTK_CMCTREE(ctree), node, 2, cols[2]);
368 } else {
369 node = parent ? GTK_CMCTREE_ROW(parent)->children
370 : GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
371 node = gtk_sctree_insert_node(GTK_CMCTREE(ctree), parent, node,
372 cols, 0, NULL, NULL,
373 TRUE, FALSE);
374 if (parent &&
375 g_pattern_match_simple(pattern, parent_name) == FALSE)
376 gtk_cmctree_expand(GTK_CMCTREE(ctree), parent);
378 gtk_cmctree_node_set_selectable(GTK_CMCTREE(ctree), node, TRUE);
379 if (node)
380 gtk_cmctree_node_set_row_data(GTK_CMCTREE(ctree), node, ginfo);
382 g_free(parent_name);
384 return node;
387 static void grouplist_expand_upwards(GtkCMCTree *ctree, const gchar *name) {
388 const gchar *ptr;
389 gchar *newname=g_malloc0(strlen(name) + 1);
390 GtkCMCTreeNode *node;
392 for (ptr=name; *ptr; ptr++) {
393 if (*ptr == '.') {
394 node = grouplist_hash_get_branch_node(newname);
395 if (node != NULL)
396 gtk_cmctree_expand(ctree, node);
398 newname[ptr-name] = *ptr;
400 g_free(newname);
403 static void grouplist_dialog_set_list(const gchar *pattern, gboolean refresh)
405 static GdkCursor *watch_cursor = NULL;
406 GSList *cur;
407 GtkCMCTreeNode *node;
408 GPatternSpec *pspec;
409 GdkWindow *window;
411 if (locked) return;
412 locked = TRUE;
414 if (!pattern || *pattern == '\0')
415 pattern = "*";
417 window = gtk_widget_get_window(dialog);
418 if (!watch_cursor)
419 watch_cursor = gdk_cursor_new_for_display(
420 gdk_window_get_display(window), GDK_WATCH);
421 gdk_window_set_cursor(window, watch_cursor);
422 main_window_cursor_wait(mainwindow_get_mainwindow());
423 GTK_EVENTS_FLUSH();
425 if (refresh) {
426 ack = TRUE;
427 grouplist_clear();
428 recv_set_ui_func(grouplist_recv_func, NULL);
429 group_list = news_get_group_list(news_folder);
430 group_list = g_slist_reverse(group_list);
431 recv_set_ui_func(NULL, NULL);
432 if (group_list == NULL && ack == TRUE) {
433 alertpanel_error(_("Can't retrieve newsgroup list."));
434 locked = FALSE;
435 gdk_window_set_cursor(window, NULL);
436 main_window_cursor_normal(mainwindow_get_mainwindow());
437 return;
439 } else
440 gtk_cmclist_clear(GTK_CMCLIST(ctree));
442 gtk_entry_set_text(GTK_ENTRY(entry), pattern);
444 grouplist_hash_init();
446 gtk_cmclist_freeze(GTK_CMCLIST(ctree));
448 pspec = g_pattern_spec_new(pattern);
450 for (cur = group_list; cur != NULL ; cur = cur->next) {
451 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
452 #if GLIB_CHECK_VERSION(2,70,0)
453 if (g_pattern_spec_match_string(pspec, ginfo->name)) {
454 #else
455 if (g_pattern_match_string(pspec, ginfo->name)) {
456 #endif
457 node = grouplist_create_branch(ginfo, pattern);
458 if (g_slist_find_custom(subscribed, ginfo->name,
459 (GCompareFunc)g_ascii_strcasecmp)
460 != NULL)
461 gtk_cmctree_select(GTK_CMCTREE(ctree), node);
464 for (cur = subscribed; cur; cur = g_slist_next(cur))
465 grouplist_expand_upwards(GTK_CMCTREE(ctree), (gchar *)cur->data);
467 g_pattern_spec_free(pspec);
469 gtk_cmclist_thaw(GTK_CMCLIST(ctree));
471 grouplist_hash_done();
473 gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
475 gdk_window_set_cursor(window, NULL);
476 main_window_cursor_normal(mainwindow_get_mainwindow());
478 locked = FALSE;
481 static void grouplist_search(void)
483 gchar *str;
485 if (locked) return;
487 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
488 grouplist_dialog_set_list(str, FALSE);
489 g_free(str);
492 static void grouplist_clear(void)
494 gtk_cmclist_clear(GTK_CMCLIST(ctree));
495 gtk_entry_set_text(GTK_ENTRY(entry), "");
496 news_group_list_free(group_list);
497 group_list = NULL;
500 static gboolean grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
501 gpointer data)
503 gchar buf[BUFFSIZE];
505 g_snprintf(buf, sizeof(buf),
506 _("%d newsgroups received (%s read)"),
507 count, to_human_readable((goffset)read_bytes));
508 gtk_label_set_text(GTK_LABEL(status_label), buf);
509 GTK_EVENTS_FLUSH();
510 if (ack == FALSE)
511 return FALSE;
512 else
513 return TRUE;
516 static void grouplist_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
518 cm_return_if_fail( allocation != NULL );
520 gtk_window_get_size(GTK_WINDOW(widget),
521 &prefs_common.news_subscribe_width, &prefs_common.news_subscribe_height);
524 static gint window_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
526 ack = FALSE;
527 if (gtk_main_level() > 1)
528 gtk_main_quit();
530 return TRUE;
533 static void ok_clicked(GtkWidget *widget, gpointer data)
535 ack = TRUE;
536 if (gtk_main_level() > 1)
537 gtk_main_quit();
540 static void cancel_clicked(GtkWidget *widget, gpointer data)
542 ack = FALSE;
543 if (gtk_main_level() > 1)
544 gtk_main_quit();
547 static void refresh_clicked(GtkWidget *widget, gpointer data)
549 gchar *str;
551 if (locked) return;
553 news_remove_group_list_cache(news_folder);
555 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
556 grouplist_dialog_set_list(str, TRUE);
557 g_free(str);
560 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
562 if (event && event->keyval == GDK_KEY_Escape)
563 cancel_clicked(NULL, NULL);
564 return FALSE;
567 /* clist/ctree clear old selection on click (gtk2 only)
568 * - intercept all button clicks (always return TRUE)
569 * - only allow left button single click
570 * - handle click on expander
571 * - update "subscribed" list and un-/select row
573 static gboolean button_press_cb(GtkCMCTree *ctree, GdkEventButton *button,
574 gpointer data)
576 gint row, col;
577 GtkCMCTreeNode *node;
578 NewsGroupInfo *ginfo;
579 GSList *list;
581 if (button->type != GDK_BUTTON_PRESS) return TRUE;
582 if (button->button != 1) return TRUE;
583 if (button->window != GTK_CMCLIST(ctree)->clist_window) return TRUE;
585 if (!gtk_cmclist_get_selection_info(GTK_CMCLIST(ctree),
586 button->x, button->y, &row, &col))
587 return TRUE;
588 node = gtk_cmctree_node_nth(ctree, row);
589 if (!node) return TRUE;
591 if (gtk_cmctree_is_hot_spot(ctree, button->x, button->y)) {
592 gtk_cmctree_toggle_expansion(ctree, node);
593 return TRUE;
596 ginfo = gtk_cmctree_node_get_row_data(ctree, node);
597 if (!ginfo) return TRUE;
599 list = g_slist_find_custom(subscribed, ginfo->name,
600 (GCompareFunc)g_ascii_strcasecmp);
601 if (list) {
602 g_free(list->data);
603 subscribed = g_slist_remove(subscribed, list->data);
604 gtk_cmclist_unselect_row(GTK_CMCLIST(ctree), row, 0);
605 } else {
606 subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
607 gtk_cmclist_select_row(GTK_CMCLIST(ctree), row, 0);
610 return TRUE;
613 static void entry_activated(GtkEditable *editable)
615 grouplist_search();
618 static void search_clicked(GtkWidget *widget, gpointer data)
620 grouplist_search();