Fix CID 1491093: attrib leaked if attvalue is null
[claws.git] / src / grouplistdialog.c
blobab54da12bed1697ad39f5fc8259a09bf82e97abe
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 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) {
121 gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
122 gtk_main();
125 manage_window_focus_out(dialog, NULL, NULL);
126 gtk_widget_hide(dialog);
128 if (!ack) {
129 slist_free_strings_full(subscribed);
130 subscribed = NULL;
132 for (node = folder->node->children; node != NULL;
133 node = node->next) {
134 item = FOLDER_ITEM(node->data);
135 subscribed = g_slist_append(subscribed,
136 g_strdup(item->path));
140 grouplist_clear();
142 return subscribed;
145 static void grouplist_dialog_create(void)
147 GtkWidget *grid;
148 GtkWidget *hgrid;
149 GtkWidget *msg_label;
150 GtkWidget *search_button;
151 GtkWidget *cancel_button;
152 GtkWidget *refresh_button;
153 GtkWidget *scrolledwin;
154 static GdkGeometry geometry;
155 gchar *titles[3];
156 gint i;
158 dialog = gtk_dialog_new();
159 gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
160 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
161 gtk_window_set_title(GTK_WINDOW(dialog), _("Newsgroup subscription"));
162 g_signal_connect(G_OBJECT(dialog), "delete_event",
163 G_CALLBACK(window_deleted), NULL);
164 g_signal_connect(G_OBJECT(dialog), "key_press_event",
165 G_CALLBACK(key_pressed), NULL);
166 g_signal_connect(G_OBJECT(dialog), "size_allocate",
167 G_CALLBACK(grouplist_size_allocate), NULL);
168 MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
170 grid = gtk_grid_new();
171 gtk_grid_set_row_spacing(GTK_GRID(grid), 8);
172 gtk_container_set_border_width(GTK_CONTAINER(grid), 8);
173 gtk_orientable_set_orientation(GTK_ORIENTABLE(grid),
174 GTK_ORIENTATION_VERTICAL);
175 gtk_container_add(GTK_CONTAINER(
176 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), grid);
178 msg_label = gtk_label_new(_("Select newsgroups for subscription:"));
179 gtk_widget_set_hexpand(msg_label, TRUE);
180 gtk_widget_set_vexpand(msg_label, FALSE);
181 gtk_widget_set_halign(msg_label, GTK_ALIGN_START);
182 gtk_container_add(GTK_CONTAINER(grid), msg_label);
184 hgrid = gtk_grid_new();
185 gtk_grid_set_column_spacing(GTK_GRID(hgrid), 8);
186 gtk_orientable_set_orientation(GTK_ORIENTABLE(hgrid),
187 GTK_ORIENTATION_HORIZONTAL);
188 gtk_widget_set_hexpand(hgrid, TRUE);
189 gtk_widget_set_vexpand(hgrid, FALSE);
190 gtk_container_add(GTK_CONTAINER(grid), hgrid);
192 msg_label = gtk_label_new(_("Find groups:"));
193 gtk_container_add(GTK_CONTAINER(hgrid), msg_label);
195 entry = gtk_entry_new();
196 gtk_widget_set_hexpand(entry, TRUE);
197 gtk_container_add(GTK_CONTAINER(hgrid), entry);
198 g_signal_connect(G_OBJECT(entry), "activate",
199 G_CALLBACK(entry_activated), NULL);
201 search_button = gtk_button_new_with_label(_(" Search "));
202 gtk_container_add(GTK_CONTAINER(hgrid), search_button);
204 g_signal_connect(G_OBJECT(search_button), "clicked",
205 G_CALLBACK(search_clicked), NULL);
207 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
208 gtk_widget_set_hexpand(scrolledwin, TRUE);
209 gtk_widget_set_vexpand(scrolledwin, TRUE);
210 gtk_container_add(GTK_CONTAINER(grid), scrolledwin);
211 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
212 GTK_POLICY_AUTOMATIC,
213 GTK_POLICY_AUTOMATIC);
215 titles[0] = _("Newsgroup name");
216 titles[1] = _("Messages");
217 titles[2] = _("Type");
218 ctree = gtk_sctree_new_with_titles(3, 0, titles);
219 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
220 gtk_cmclist_set_column_width
221 (GTK_CMCLIST(ctree), 0, GROUPLIST_COL_NAME_WIDTH);
222 gtk_cmclist_set_column_auto_resize(GTK_CMCLIST(ctree), 0, TRUE);
223 gtk_cmclist_set_selection_mode(GTK_CMCLIST(ctree), GTK_SELECTION_MULTIPLE);
225 gtk_cmctree_set_expander_style(GTK_CMCTREE(ctree),
226 GTK_CMCTREE_EXPANDER_TRIANGLE);
228 for (i = 0; i < 3; i++)
229 gtk_widget_set_can_focus(GTK_CMCLIST(ctree)->column[i].button, FALSE);
230 g_signal_connect(G_OBJECT(ctree), "button-press-event",
231 G_CALLBACK(button_press_cb), NULL);
233 status_label = gtk_label_new("");
234 gtk_widget_set_hexpand(status_label, TRUE);
235 gtk_widget_set_vexpand(status_label, FALSE);
236 gtk_widget_set_halign(status_label, GTK_ALIGN_START);
237 gtk_container_add(GTK_CONTAINER(grid), status_label);
239 refresh_button = gtk_dialog_add_button(GTK_DIALOG(dialog), _("_Refresh"),
240 GTK_RESPONSE_NONE);
241 cancel_button = gtk_dialog_add_button(GTK_DIALOG(dialog),_("_Cancel"),
242 GTK_RESPONSE_NONE);
243 ok_button = gtk_dialog_add_button(GTK_DIALOG(dialog),_("_OK"),
244 GTK_RESPONSE_NONE);
245 gtk_widget_grab_default(ok_button);
247 g_signal_connect(G_OBJECT(ok_button), "clicked",
248 G_CALLBACK(ok_clicked), NULL);
249 g_signal_connect(G_OBJECT(cancel_button), "clicked",
250 G_CALLBACK(cancel_clicked), NULL);
251 g_signal_connect(G_OBJECT(refresh_button), "clicked",
252 G_CALLBACK(refresh_clicked), NULL);
254 if (!geometry.min_width) {
255 geometry.min_width = GROUPLIST_DIALOG_WIDTH;
256 geometry.min_height = GROUPLIST_DIALOG_HEIGHT;
259 gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry,
260 GDK_HINT_MIN_SIZE);
261 gtk_window_set_default_size(GTK_WINDOW(dialog),
262 prefs_common.news_subscribe_width,
263 prefs_common.news_subscribe_height);
265 gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
268 static GHashTable *branch_node_table;
270 static void grouplist_hash_init(void)
272 branch_node_table = g_hash_table_new(g_str_hash, g_str_equal);
275 static void grouplist_hash_done(void)
277 hash_free_strings(branch_node_table);
278 g_hash_table_destroy(branch_node_table);
281 static GtkCMCTreeNode *grouplist_hash_get_branch_node(const gchar *name)
283 return g_hash_table_lookup(branch_node_table, name);
286 static void grouplist_hash_set_branch_node(const gchar *name,
287 GtkCMCTreeNode *node)
289 g_hash_table_insert(branch_node_table, g_strdup(name), node);
292 static gchar *grouplist_get_parent_name(const gchar *name)
294 gchar *p;
296 p = strrchr(name, '.');
297 if (!p)
298 return g_strdup("");
299 return g_strndup(name, p - name);
302 static GtkCMCTreeNode *grouplist_create_parent(const gchar *name,
303 const gchar *pattern)
305 GtkCMCTreeNode *parent;
306 GtkCMCTreeNode *node;
307 gchar *cols[3];
308 gchar *parent_name;
310 if (*name == '\0') return NULL;
311 node = grouplist_hash_get_branch_node(name);
312 if (node != NULL) return node;
314 cols[0] = (gchar *)name;
315 cols[1] = cols[2] = "";
317 parent_name = grouplist_get_parent_name(name);
318 parent = grouplist_create_parent(parent_name, pattern);
320 node = parent ? GTK_CMCTREE_ROW(parent)->children
321 : GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
322 node = gtk_sctree_insert_node(GTK_CMCTREE(ctree), parent, node,
323 cols, 0, NULL, NULL,
324 FALSE, FALSE);
325 if (parent && g_pattern_match_simple(pattern, parent_name) == FALSE)
326 gtk_cmctree_expand(GTK_CMCTREE(ctree), parent);
327 gtk_cmctree_node_set_selectable(GTK_CMCTREE(ctree), node, FALSE);
329 grouplist_hash_set_branch_node(name, node);
331 g_free(parent_name);
333 return node;
336 static GtkCMCTreeNode *grouplist_create_branch(NewsGroupInfo *ginfo,
337 const gchar *pattern)
339 GtkCMCTreeNode *node;
340 GtkCMCTreeNode *parent;
341 gchar *name = (gchar *)ginfo->name;
342 gchar *parent_name;
343 gchar *count_str;
344 gchar *cols[3];
345 gint count;
347 count = ginfo->last - ginfo->first;
348 if (count < 0)
349 count = 0;
350 count_str = itos(count);
352 cols[0] = ginfo->name;
353 cols[1] = count_str;
354 if (ginfo->type == 'y')
355 cols[2] = "";
356 else if (ginfo->type == 'm')
357 cols[2] = _("moderated");
358 else if (ginfo->type == 'n')
359 cols[2] = _("read-only");
360 else
361 cols[2] = _("unknown");
363 parent_name = grouplist_get_parent_name(name);
364 parent = grouplist_create_parent(parent_name, pattern);
365 node = grouplist_hash_get_branch_node(name);
366 if (node) {
367 gtk_cmctree_set_node_info(GTK_CMCTREE(ctree), node, cols[0], 0,
368 NULL, NULL, FALSE, FALSE);
369 gtk_cmctree_node_set_text(GTK_CMCTREE(ctree), node, 1, cols[1]);
370 gtk_cmctree_node_set_text(GTK_CMCTREE(ctree), node, 2, cols[2]);
371 } else {
372 node = parent ? GTK_CMCTREE_ROW(parent)->children
373 : GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
374 node = gtk_sctree_insert_node(GTK_CMCTREE(ctree), parent, node,
375 cols, 0, NULL, NULL,
376 TRUE, FALSE);
377 if (parent &&
378 g_pattern_match_simple(pattern, parent_name) == FALSE)
379 gtk_cmctree_expand(GTK_CMCTREE(ctree), parent);
381 gtk_cmctree_node_set_selectable(GTK_CMCTREE(ctree), node, TRUE);
382 if (node)
383 gtk_cmctree_node_set_row_data(GTK_CMCTREE(ctree), node, ginfo);
385 g_free(parent_name);
387 return node;
390 static void grouplist_expand_upwards(GtkCMCTree *ctree, const gchar *name) {
391 const gchar *ptr;
392 gchar *newname=g_malloc0(strlen(name) + 1);
393 GtkCMCTreeNode *node;
395 for (ptr=name; *ptr; ptr++) {
396 if (*ptr == '.') {
397 node = grouplist_hash_get_branch_node(newname);
398 if (node != NULL)
399 gtk_cmctree_expand(ctree, node);
401 newname[ptr-name] = *ptr;
403 g_free(newname);
406 static void grouplist_dialog_set_list(const gchar *pattern, gboolean refresh)
408 static GdkCursor *watch_cursor = NULL;
409 GSList *cur;
410 GtkCMCTreeNode *node;
411 GPatternSpec *pspec;
412 GdkWindow *window;
414 if (locked) return;
415 locked = TRUE;
417 if (!pattern || *pattern == '\0')
418 pattern = "*";
420 window = gtk_widget_get_window(dialog);
421 if (!watch_cursor)
422 watch_cursor = gdk_cursor_new_for_display(
423 gdk_window_get_display(window), GDK_WATCH);
424 gdk_window_set_cursor(window, watch_cursor);
425 main_window_cursor_wait(mainwindow_get_mainwindow());
426 GTK_EVENTS_FLUSH();
428 if (refresh) {
429 ack = TRUE;
430 grouplist_clear();
431 recv_set_ui_func(grouplist_recv_func, NULL);
432 group_list = news_get_group_list(news_folder);
433 group_list = g_slist_reverse(group_list);
434 recv_set_ui_func(NULL, NULL);
435 if (group_list == NULL && ack == TRUE) {
436 alertpanel_error(_("Can't retrieve newsgroup list."));
437 locked = FALSE;
438 gdk_window_set_cursor(window, NULL);
439 main_window_cursor_normal(mainwindow_get_mainwindow());
440 return;
442 } else
443 gtk_cmclist_clear(GTK_CMCLIST(ctree));
445 gtk_entry_set_text(GTK_ENTRY(entry), pattern);
447 grouplist_hash_init();
449 gtk_cmclist_freeze(GTK_CMCLIST(ctree));
451 pspec = g_pattern_spec_new(pattern);
453 for (cur = group_list; cur != NULL ; cur = cur->next) {
454 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
455 #if GLIB_CHECK_VERSION(2,70,0)
456 if (g_pattern_spec_match_string(pspec, ginfo->name)) {
457 #else
458 if (g_pattern_match_string(pspec, ginfo->name)) {
459 #endif
460 node = grouplist_create_branch(ginfo, pattern);
461 if (g_slist_find_custom(subscribed, ginfo->name,
462 (GCompareFunc)g_ascii_strcasecmp)
463 != NULL)
464 gtk_cmctree_select(GTK_CMCTREE(ctree), node);
467 for (cur = subscribed; cur; cur = g_slist_next(cur))
468 grouplist_expand_upwards(GTK_CMCTREE(ctree), (gchar *)cur->data);
470 g_pattern_spec_free(pspec);
472 gtk_cmclist_thaw(GTK_CMCLIST(ctree));
474 grouplist_hash_done();
476 gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
478 gdk_window_set_cursor(window, NULL);
479 main_window_cursor_normal(mainwindow_get_mainwindow());
481 locked = FALSE;
484 static void grouplist_search(void)
486 gchar *str;
488 if (locked) return;
490 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
491 grouplist_dialog_set_list(str, FALSE);
492 g_free(str);
495 static void grouplist_clear(void)
497 gtk_cmclist_clear(GTK_CMCLIST(ctree));
498 gtk_entry_set_text(GTK_ENTRY(entry), "");
499 news_group_list_free(group_list);
500 group_list = NULL;
503 static gboolean grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
504 gpointer data)
506 gchar buf[BUFFSIZE];
508 g_snprintf(buf, sizeof(buf),
509 _("%d newsgroups received (%s read)"),
510 count, to_human_readable((goffset)read_bytes));
511 gtk_label_set_text(GTK_LABEL(status_label), buf);
512 GTK_EVENTS_FLUSH();
513 if (ack == FALSE)
514 return FALSE;
515 else
516 return TRUE;
519 static void grouplist_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
521 cm_return_if_fail( allocation != NULL );
523 gtk_window_get_size(GTK_WINDOW(widget),
524 &prefs_common.news_subscribe_width, &prefs_common.news_subscribe_height);
527 static gint window_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
529 ack = FALSE;
530 if (gtk_main_level() > 1)
531 gtk_main_quit();
533 return TRUE;
536 static void ok_clicked(GtkWidget *widget, gpointer data)
538 ack = TRUE;
539 if (gtk_main_level() > 1)
540 gtk_main_quit();
543 static void cancel_clicked(GtkWidget *widget, gpointer data)
545 ack = FALSE;
546 if (gtk_main_level() > 1)
547 gtk_main_quit();
550 static void refresh_clicked(GtkWidget *widget, gpointer data)
552 gchar *str;
554 if (locked) return;
556 news_remove_group_list_cache(news_folder);
558 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
559 grouplist_dialog_set_list(str, TRUE);
560 g_free(str);
563 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
565 if (event && event->keyval == GDK_KEY_Escape)
566 cancel_clicked(NULL, NULL);
567 return FALSE;
570 /* clist/ctree clear old selection on click (gtk2 only)
571 * - intercept all button clicks (always return TRUE)
572 * - only allow left button single click
573 * - handle click on expander
574 * - update "subscribed" list and un-/select row
576 static gboolean button_press_cb(GtkCMCTree *ctree, GdkEventButton *button,
577 gpointer data)
579 gint row, col;
580 GtkCMCTreeNode *node;
581 NewsGroupInfo *ginfo;
582 GSList *list;
584 if (button->type != GDK_BUTTON_PRESS) return TRUE;
585 if (button->button != 1) return TRUE;
586 if (button->window != GTK_CMCLIST(ctree)->clist_window) return TRUE;
588 if (!gtk_cmclist_get_selection_info(GTK_CMCLIST(ctree),
589 button->x, button->y, &row, &col))
590 return TRUE;
591 node = gtk_cmctree_node_nth(ctree, row);
592 if (!node) return TRUE;
594 if (gtk_cmctree_is_hot_spot(ctree, button->x, button->y)) {
595 gtk_cmctree_toggle_expansion(ctree, node);
596 return TRUE;
599 ginfo = gtk_cmctree_node_get_row_data(ctree, node);
600 if (!ginfo) return TRUE;
602 list = g_slist_find_custom(subscribed, ginfo->name,
603 (GCompareFunc)g_ascii_strcasecmp);
604 if (list) {
606 * Make a copy so we can g_free() it after we remove
607 * list from the subscribed list. Calling g_free()
608 * first triggers a use-after-free Coverity issue.
610 gpointer tmpdata = list->data;
611 subscribed = g_slist_remove(subscribed, list->data);
612 g_free(tmpdata);
613 gtk_cmclist_unselect_row(GTK_CMCLIST(ctree), row, 0);
614 } else {
615 subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
616 gtk_cmclist_select_row(GTK_CMCLIST(ctree), row, 0);
619 return TRUE;
622 static void entry_activated(GtkEditable *editable)
624 grouplist_search();
627 static void search_clicked(GtkWidget *widget, gpointer data)
629 grouplist_search();