Ticket #3565: "External Panelize": use WListbox's notification. notification.
[midnight-commander.git] / src / filemanager / panelize.c
blob805024407e859c279053d249b33abe47992e9dec
1 /*
2 External panelize
4 Copyright (C) 1995-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Janne Kukonlehto, 1995
9 Jakub Jelinek, 1995
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file panelize.c
28 * \brief Source: External panelization module
31 #include <config.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
40 #include "lib/global.h"
42 #include "lib/skin.h"
43 #include "lib/vfs/vfs.h"
44 #include "lib/mcconfig.h" /* Load/save directories panelize */
45 #include "lib/strutil.h"
46 #include "lib/util.h"
47 #include "lib/widget.h"
49 #include "src/setup.h" /* For profile_bname */
50 #include "src/history.h"
52 #include "dir.h"
53 #include "midnight.h" /* current_panel */
54 #include "layout.h" /* rotate_dash() */
55 #include "panel.h" /* WPanel */
57 #include "panelize.h"
59 /*** global variables ****************************************************************************/
61 /*** file scope macro definitions ****************************************************************/
63 #define UX 3
64 #define UY 2
66 #define LABELS 3
67 #define B_ADD B_USER
68 #define B_REMOVE (B_USER + 1)
70 /*** file scope type declarations ****************************************************************/
72 /*** file scope variables ************************************************************************/
74 static WListbox *l_panelize;
75 static WDialog *panelize_dlg;
76 static int last_listitem;
77 static WInput *pname;
79 static const char *panelize_section = "Panelize";
81 /* Directory panelize */
82 static struct panelize
84 char *command;
85 char *label;
86 struct panelize *next;
87 } *panelize = NULL;
89 /* --------------------------------------------------------------------------------------------- */
90 /*** file scope functions ************************************************************************/
91 /* --------------------------------------------------------------------------------------------- */
93 static void
94 update_command (void)
96 if (l_panelize->pos != last_listitem)
98 struct panelize *data = NULL;
100 last_listitem = l_panelize->pos;
101 listbox_get_current (l_panelize, NULL, (void **) &data);
102 input_assign_text (pname, data->command);
103 pname->point = 0;
104 input_update (pname, TRUE);
108 /* --------------------------------------------------------------------------------------------- */
110 static cb_ret_t
111 panelize_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
113 switch (msg)
115 case MSG_INIT:
116 update_command ();
117 return MSG_HANDLED;
119 case MSG_ACTION:
120 if (sender == NULL)
121 return MSG_NOT_HANDLED;
123 /* The listbox tells us the item has changed. */
124 update_command ();
125 return MSG_HANDLED;
127 default:
128 return dlg_default_callback (w, sender, msg, parm, data);
132 /* --------------------------------------------------------------------------------------------- */
134 static void
135 init_panelize (void)
137 struct
139 int ret_cmd;
140 button_flags_t flags;
141 const char *text;
142 } panelize_but[] =
144 /* *INDENT-OFF* */
145 { B_ENTER, DEFPUSH_BUTTON, N_("Pane&lize") },
146 { B_REMOVE, NORMAL_BUTTON, N_("&Remove") },
147 { B_ADD, NORMAL_BUTTON, N_("&Add new") },
148 { B_CANCEL, NORMAL_BUTTON, N_("&Cancel") }
149 /* *INDENT-ON* */
152 size_t i;
153 int blen;
154 int panelize_cols;
155 struct panelize *current;
156 int x, y;
158 last_listitem = 0;
160 do_refresh ();
162 i = G_N_ELEMENTS (panelize_but);
163 blen = i - 1; /* gaps between buttons */
164 while (i-- != 0)
166 #ifdef ENABLE_NLS
167 panelize_but[i].text = _(panelize_but[i].text);
168 #endif
169 blen += str_term_width1 (panelize_but[i].text) + 3 + 1;
170 if (panelize_but[i].flags == DEFPUSH_BUTTON)
171 blen += 2;
174 panelize_cols = COLS - 6;
175 panelize_cols = max (panelize_cols, blen + 4);
177 panelize_dlg =
178 dlg_create (TRUE, 0, 0, 20, panelize_cols, dialog_colors, panelize_callback, NULL,
179 "[External panelize]", _("External panelize"), DLG_CENTER);
181 /* add listbox to the dialogs */
182 y = UY;
183 add_widget (panelize_dlg, groupbox_new (y++, UX, 12, panelize_cols - UX * 2, ""));
185 l_panelize = listbox_new (y, UX + 1, 10, panelize_cols - UX * 2 - 2, FALSE, NULL);
186 for (current = panelize; current != NULL; current = current->next)
187 listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current, FALSE);
188 listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
189 add_widget (panelize_dlg, l_panelize);
191 y += WIDGET (l_panelize)->lines + 1;
192 add_widget (panelize_dlg, label_new (y++, UX, _("Command")));
193 pname =
194 input_new (y++, UX, input_colors, panelize_cols - UX * 2, "", "in",
195 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_COMMANDS |
196 INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES | INPUT_COMPLETE_CD |
197 INPUT_COMPLETE_SHELL_ESC);
198 add_widget (panelize_dlg, pname);
202 add_widget (panelize_dlg, hline_new (y++, -1, -1));
204 x = (panelize_cols - blen) / 2;
205 for (i = 0; i < G_N_ELEMENTS (panelize_but); i++)
207 WButton *b;
209 b = button_new (y, x,
210 panelize_but[i].ret_cmd, panelize_but[i].flags, panelize_but[i].text, NULL);
211 add_widget (panelize_dlg, b);
213 x += button_get_len (b) + 1;
216 dlg_select_widget (l_panelize);
219 /* --------------------------------------------------------------------------------------------- */
221 static void
222 panelize_done (void)
224 dlg_destroy (panelize_dlg);
225 repaint_screen ();
228 /* --------------------------------------------------------------------------------------------- */
230 static void
231 add2panelize (char *label, char *command)
233 struct panelize *current, *old;
235 old = NULL;
236 current = panelize;
237 while (current && strcmp (current->label, label) <= 0)
239 old = current;
240 current = current->next;
243 if (old == NULL)
245 panelize = g_new (struct panelize, 1);
246 panelize->label = label;
247 panelize->command = command;
248 panelize->next = current;
250 else
252 struct panelize *new;
253 new = g_new (struct panelize, 1);
254 new->label = label;
255 new->command = command;
256 old->next = new;
257 new->next = current;
261 /* --------------------------------------------------------------------------------------------- */
263 static void
264 add2panelize_cmd (void)
266 if (pname->buffer != NULL && *pname->buffer != '\0')
268 char *label;
270 label = input_dialog (_("Add to external panelize"),
271 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "",
272 INPUT_COMPLETE_NONE);
273 if (label == NULL || *label == '\0')
274 g_free (label);
275 else
276 add2panelize (label, g_strdup (pname->buffer));
280 /* --------------------------------------------------------------------------------------------- */
282 static void
283 remove_from_panelize (struct panelize *entry)
285 if (strcmp (entry->label, _("Other command")) != 0)
287 if (entry == panelize)
289 panelize = panelize->next;
291 else
293 struct panelize *current = panelize;
294 while (current && current->next != entry)
295 current = current->next;
296 if (current)
298 current->next = entry->next;
302 g_free (entry->label);
303 g_free (entry->command);
304 g_free (entry);
308 /* --------------------------------------------------------------------------------------------- */
310 static void
311 do_external_panelize (char *command)
313 int link_to_dir, stale_link;
314 struct stat st;
315 dir_list *list = &current_panel->dir;
316 char line[MC_MAXPATHLEN];
317 char *name;
318 FILE *external;
320 open_error_pipe ();
321 external = popen (command, "r");
322 if (!external)
324 close_error_pipe (D_ERROR, _("Cannot invoke command."));
325 return;
327 /* Clear the counters and the directory list */
328 panel_clean_dir (current_panel);
330 panelize_change_root (current_panel->cwd_vpath);
332 dir_list_init (list);
334 while (TRUE)
336 clearerr (external);
337 if (fgets (line, sizeof (line), external) == NULL)
339 if (ferror (external) && errno == EINTR)
340 continue;
341 else
342 break;
344 if (line[strlen (line) - 1] == '\n')
345 line[strlen (line) - 1] = 0;
346 if (strlen (line) < 1)
347 continue;
348 if (line[0] == '.' && IS_PATH_SEP (line[1]))
349 name = line + 2;
350 else
351 name = line;
353 if (!handle_path (name, &st, &link_to_dir, &stale_link))
354 continue;
356 if (!dir_list_append (list, name, &st, link_to_dir != 0, stale_link != 0))
357 break;
359 file_mark (current_panel, list->len - 1, 0);
361 if ((list->len & 31) == 0)
362 rotate_dash (TRUE);
365 current_panel->is_panelized = TRUE;
367 if (list->len == 0)
368 dir_list_init (list);
369 else if (IS_PATH_SEP (list->list[0].fname[0]))
371 vfs_path_t *vpath_root;
372 int ret;
374 vpath_root = vfs_path_from_str (PATH_SEP_STR);
375 panel_set_cwd (current_panel, vpath_root);
376 ret = mc_chdir (vpath_root);
377 vfs_path_free (vpath_root);
379 (void) ret;
382 if (pclose (external) < 0)
383 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
384 close_error_pipe (D_NORMAL, NULL);
385 try_to_select (current_panel, NULL);
386 panel_re_sort (current_panel);
387 rotate_dash (FALSE);
390 /* --------------------------------------------------------------------------------------------- */
392 static void
393 do_panelize_cd (WPanel * panel)
395 int i;
396 dir_list *list;
397 gboolean panelized_same;
399 dir_list_clean (&panel->dir);
400 if (panelized_panel.root_vpath == NULL)
401 panelize_change_root (current_panel->cwd_vpath);
403 if (panelized_panel.list.len < 1)
404 dir_list_init (&panelized_panel.list);
405 else if (panelized_panel.list.len > panel->dir.size)
406 dir_list_grow (&panel->dir, panelized_panel.list.len - panel->dir.size);
408 list = &panel->dir;
409 list->len = panelized_panel.list.len;
410 panel->is_panelized = TRUE;
412 panelized_same = vfs_path_equal (panelized_panel.root_vpath, panel->cwd_vpath);
414 for (i = 0; i < panelized_panel.list.len; i++)
416 if (panelized_same || DIR_IS_DOTDOT (panelized_panel.list.list[i].fname))
418 list->list[i].fnamelen = panelized_panel.list.list[i].fnamelen;
419 list->list[i].fname = g_strndup (panelized_panel.list.list[i].fname,
420 panelized_panel.list.list[i].fnamelen);
422 else
424 vfs_path_t *tmp_vpath;
425 const char *fname;
427 tmp_vpath =
428 vfs_path_append_new (panelized_panel.root_vpath, panelized_panel.list.list[i].fname,
429 NULL);
430 fname = vfs_path_as_str (tmp_vpath);
431 list->list[i].fnamelen = strlen (fname);
432 list->list[i].fname = g_strndup (fname, list->list[i].fnamelen);
433 vfs_path_free (tmp_vpath);
435 list->list[i].f.link_to_dir = panelized_panel.list.list[i].f.link_to_dir;
436 list->list[i].f.stale_link = panelized_panel.list.list[i].f.stale_link;
437 list->list[i].f.dir_size_computed = panelized_panel.list.list[i].f.dir_size_computed;
438 list->list[i].f.marked = panelized_panel.list.list[i].f.marked;
439 list->list[i].st = panelized_panel.list.list[i].st;
440 list->list[i].sort_key = panelized_panel.list.list[i].sort_key;
441 list->list[i].second_sort_key = panelized_panel.list.list[i].second_sort_key;
443 try_to_select (panel, NULL);
446 /* --------------------------------------------------------------------------------------------- */
447 /*** public functions ****************************************************************************/
448 /* --------------------------------------------------------------------------------------------- */
451 * Change root directory of panelized content.
452 * @param new_root - object with new path.
454 void
455 panelize_change_root (const vfs_path_t * new_root)
457 vfs_path_free (panelized_panel.root_vpath);
458 panelized_panel.root_vpath = vfs_path_clone (new_root);
461 /* --------------------------------------------------------------------------------------------- */
463 void
464 panelize_save_panel (WPanel * panel)
466 int i;
467 dir_list *list = &panel->dir;
469 panelize_change_root (current_panel->cwd_vpath);
471 if (panelized_panel.list.len > 0)
472 dir_list_clean (&panelized_panel.list);
473 if (panel->dir.len == 0)
474 return;
476 if (panel->dir.len > panelized_panel.list.size)
477 dir_list_grow (&panelized_panel.list, panel->dir.len - panelized_panel.list.size);
478 panelized_panel.list.len = panel->dir.len;
480 for (i = 0; i < panel->dir.len; i++)
482 panelized_panel.list.list[i].fnamelen = list->list[i].fnamelen;
483 panelized_panel.list.list[i].fname =
484 g_strndup (list->list[i].fname, list->list[i].fnamelen);
485 panelized_panel.list.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
486 panelized_panel.list.list[i].f.stale_link = list->list[i].f.stale_link;
487 panelized_panel.list.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
488 panelized_panel.list.list[i].f.marked = list->list[i].f.marked;
489 panelized_panel.list.list[i].st = list->list[i].st;
490 panelized_panel.list.list[i].sort_key = list->list[i].sort_key;
491 panelized_panel.list.list[i].second_sort_key = list->list[i].second_sort_key;
495 /* --------------------------------------------------------------------------------------------- */
497 void
498 cd_panelize_cmd (void)
500 if (!SELECTED_IS_PANEL)
501 set_display_type (MENU_PANEL_IDX, view_listing);
503 do_panelize_cd (PANEL (get_panel_widget (MENU_PANEL_IDX)));
506 /* --------------------------------------------------------------------------------------------- */
508 void
509 external_panelize (void)
511 char *target = NULL;
513 if (!vfs_current_is_local ())
515 message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
516 return;
519 init_panelize ();
521 /* display file info */
522 tty_setcolor (SELECTED_COLOR);
524 switch (dlg_run (panelize_dlg))
526 case B_CANCEL:
527 break;
529 case B_ADD:
530 add2panelize_cmd ();
531 break;
533 case B_REMOVE:
535 struct panelize *entry;
537 listbox_get_current (l_panelize, NULL, (void **) &entry);
538 remove_from_panelize (entry);
539 break;
542 case B_ENTER:
543 target = pname->buffer;
544 if (target != NULL && *target)
546 char *cmd = g_strdup (target);
548 dlg_destroy (panelize_dlg);
549 do_external_panelize (cmd);
550 g_free (cmd);
551 repaint_screen ();
552 return;
554 break;
556 default:
557 break;
560 panelize_done ();
563 /* --------------------------------------------------------------------------------------------- */
565 void
566 load_panelize (void)
568 char **keys;
570 keys = mc_config_get_keys (mc_main_config, panelize_section, NULL);
572 add2panelize (g_strdup (_("Other command")), g_strdup (""));
574 if (*keys == NULL)
576 add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
577 add2panelize (g_strdup (_("Find rejects after patching")),
578 g_strdup ("find . -name \\*.rej -print"));
579 add2panelize (g_strdup (_("Find *.orig after patching")),
580 g_strdup ("find . -name \\*.orig -print"));
581 add2panelize (g_strdup (_("Find SUID and SGID programs")),
582 g_strdup
583 ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 -a -perm /01 \\) \\) -print"));
585 else
587 GIConv conv;
588 char **profile_keys;
590 conv = str_crt_conv_from ("UTF-8");
592 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
594 GString *buffer;
596 if (mc_global.utf8_display || conv == INVALID_CONV)
597 buffer = g_string_new (*profile_keys);
598 else
600 buffer = g_string_new ("");
601 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
602 g_string_assign (buffer, *profile_keys);
605 add2panelize (g_string_free (buffer, FALSE),
606 mc_config_get_string (mc_main_config, panelize_section, *profile_keys,
607 ""));
610 str_close_conv (conv);
613 g_strfreev (keys);
616 /* --------------------------------------------------------------------------------------------- */
618 void
619 save_panelize (void)
621 struct panelize *current = panelize;
623 mc_config_del_group (mc_main_config, panelize_section);
624 for (; current; current = current->next)
626 if (strcmp (current->label, _("Other command")))
627 mc_config_set_string (mc_main_config,
628 panelize_section, current->label, current->command);
632 /* --------------------------------------------------------------------------------------------- */
634 void
635 done_panelize (void)
637 struct panelize *current = panelize;
638 struct panelize *next;
640 for (; current; current = next)
642 next = current->next;
643 g_free (current->label);
644 g_free (current->command);
645 g_free (current);
649 /* --------------------------------------------------------------------------------------------- */