Remove min() and max() macros. Use MIN() and MAX() macros from GLib.
[midnight-commander.git] / src / filemanager / panelize.c
blob4529a0c2d66eaca3dab4dfee38751767c73cf34c
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 case MSG_NOTIFY: /* MSG_NOTIFY is fired by the listbox to tell us the item has changed. */
117 update_command ();
118 return MSG_HANDLED;
120 default:
121 return dlg_default_callback (w, sender, msg, parm, data);
125 /* --------------------------------------------------------------------------------------------- */
127 static void
128 init_panelize (void)
130 struct
132 int ret_cmd;
133 button_flags_t flags;
134 const char *text;
135 } panelize_but[] =
137 /* *INDENT-OFF* */
138 { B_ENTER, DEFPUSH_BUTTON, N_("Pane&lize") },
139 { B_REMOVE, NORMAL_BUTTON, N_("&Remove") },
140 { B_ADD, NORMAL_BUTTON, N_("&Add new") },
141 { B_CANCEL, NORMAL_BUTTON, N_("&Cancel") }
142 /* *INDENT-ON* */
145 size_t i;
146 int blen;
147 int panelize_cols;
148 struct panelize *current;
149 int x, y;
151 last_listitem = 0;
153 do_refresh ();
155 i = G_N_ELEMENTS (panelize_but);
156 blen = i - 1; /* gaps between buttons */
157 while (i-- != 0)
159 #ifdef ENABLE_NLS
160 panelize_but[i].text = _(panelize_but[i].text);
161 #endif
162 blen += str_term_width1 (panelize_but[i].text) + 3 + 1;
163 if (panelize_but[i].flags == DEFPUSH_BUTTON)
164 blen += 2;
167 panelize_cols = COLS - 6;
168 panelize_cols = MAX (panelize_cols, blen + 4);
170 panelize_dlg =
171 dlg_create (TRUE, 0, 0, 20, panelize_cols, dialog_colors, panelize_callback, NULL,
172 "[External panelize]", _("External panelize"), DLG_CENTER);
174 /* add listbox to the dialogs */
175 y = UY;
176 add_widget (panelize_dlg, groupbox_new (y++, UX, 12, panelize_cols - UX * 2, ""));
178 l_panelize = listbox_new (y, UX + 1, 10, panelize_cols - UX * 2 - 2, FALSE, NULL);
179 for (current = panelize; current != NULL; current = current->next)
180 listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current, FALSE);
181 listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
182 add_widget (panelize_dlg, l_panelize);
184 y += WIDGET (l_panelize)->lines + 1;
185 add_widget (panelize_dlg, label_new (y++, UX, _("Command")));
186 pname =
187 input_new (y++, UX, input_colors, panelize_cols - UX * 2, "", "in",
188 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_COMMANDS |
189 INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES | INPUT_COMPLETE_CD |
190 INPUT_COMPLETE_SHELL_ESC);
191 add_widget (panelize_dlg, pname);
195 add_widget (panelize_dlg, hline_new (y++, -1, -1));
197 x = (panelize_cols - blen) / 2;
198 for (i = 0; i < G_N_ELEMENTS (panelize_but); i++)
200 WButton *b;
202 b = button_new (y, x,
203 panelize_but[i].ret_cmd, panelize_but[i].flags, panelize_but[i].text, NULL);
204 add_widget (panelize_dlg, b);
206 x += button_get_len (b) + 1;
209 dlg_select_widget (l_panelize);
212 /* --------------------------------------------------------------------------------------------- */
214 static void
215 panelize_done (void)
217 dlg_destroy (panelize_dlg);
218 repaint_screen ();
221 /* --------------------------------------------------------------------------------------------- */
223 static void
224 add2panelize (char *label, char *command)
226 struct panelize *current, *old;
228 old = NULL;
229 current = panelize;
230 while (current && strcmp (current->label, label) <= 0)
232 old = current;
233 current = current->next;
236 if (old == NULL)
238 panelize = g_new (struct panelize, 1);
239 panelize->label = label;
240 panelize->command = command;
241 panelize->next = current;
243 else
245 struct panelize *new;
246 new = g_new (struct panelize, 1);
247 new->label = label;
248 new->command = command;
249 old->next = new;
250 new->next = current;
254 /* --------------------------------------------------------------------------------------------- */
256 static void
257 add2panelize_cmd (void)
259 if (!input_is_empty (pname))
261 char *label;
263 label = input_dialog (_("Add to external panelize"),
264 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "",
265 INPUT_COMPLETE_NONE);
266 if (label == NULL || *label == '\0')
267 g_free (label);
268 else
269 add2panelize (label, g_strdup (pname->buffer));
273 /* --------------------------------------------------------------------------------------------- */
275 static void
276 remove_from_panelize (struct panelize *entry)
278 if (strcmp (entry->label, _("Other command")) != 0)
280 if (entry == panelize)
282 panelize = panelize->next;
284 else
286 struct panelize *current = panelize;
287 while (current && current->next != entry)
288 current = current->next;
289 if (current)
291 current->next = entry->next;
295 g_free (entry->label);
296 g_free (entry->command);
297 g_free (entry);
301 /* --------------------------------------------------------------------------------------------- */
303 static void
304 do_external_panelize (char *command)
306 int link_to_dir, stale_link;
307 struct stat st;
308 dir_list *list = &current_panel->dir;
309 char line[MC_MAXPATHLEN];
310 char *name;
311 FILE *external;
313 open_error_pipe ();
314 external = popen (command, "r");
315 if (!external)
317 close_error_pipe (D_ERROR, _("Cannot invoke command."));
318 return;
320 /* Clear the counters and the directory list */
321 panel_clean_dir (current_panel);
323 panelize_change_root (current_panel->cwd_vpath);
325 dir_list_init (list);
327 while (TRUE)
329 clearerr (external);
330 if (fgets (line, sizeof (line), external) == NULL)
332 if (ferror (external) && errno == EINTR)
333 continue;
334 else
335 break;
337 if (line[strlen (line) - 1] == '\n')
338 line[strlen (line) - 1] = 0;
339 if (strlen (line) < 1)
340 continue;
341 if (line[0] == '.' && IS_PATH_SEP (line[1]))
342 name = line + 2;
343 else
344 name = line;
346 if (!handle_path (name, &st, &link_to_dir, &stale_link))
347 continue;
349 if (!dir_list_append (list, name, &st, link_to_dir != 0, stale_link != 0))
350 break;
352 file_mark (current_panel, list->len - 1, 0);
354 if ((list->len & 31) == 0)
355 rotate_dash (TRUE);
358 current_panel->is_panelized = TRUE;
360 if (list->len == 0)
361 dir_list_init (list);
362 else if (IS_PATH_SEP (list->list[0].fname[0]))
364 vfs_path_t *vpath_root;
365 int ret;
367 vpath_root = vfs_path_from_str (PATH_SEP_STR);
368 panel_set_cwd (current_panel, vpath_root);
369 ret = mc_chdir (vpath_root);
370 vfs_path_free (vpath_root);
372 (void) ret;
375 if (pclose (external) < 0)
376 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
377 close_error_pipe (D_NORMAL, NULL);
378 try_to_select (current_panel, NULL);
379 panel_re_sort (current_panel);
380 rotate_dash (FALSE);
383 /* --------------------------------------------------------------------------------------------- */
385 static void
386 do_panelize_cd (WPanel * panel)
388 int i;
389 dir_list *list;
390 gboolean panelized_same;
392 dir_list_clean (&panel->dir);
393 if (panelized_panel.root_vpath == NULL)
394 panelize_change_root (current_panel->cwd_vpath);
396 if (panelized_panel.list.len < 1)
397 dir_list_init (&panelized_panel.list);
398 else if (panelized_panel.list.len > panel->dir.size)
399 dir_list_grow (&panel->dir, panelized_panel.list.len - panel->dir.size);
401 list = &panel->dir;
402 list->len = panelized_panel.list.len;
403 panel->is_panelized = TRUE;
405 panelized_same = vfs_path_equal (panelized_panel.root_vpath, panel->cwd_vpath);
407 for (i = 0; i < panelized_panel.list.len; i++)
409 if (panelized_same || DIR_IS_DOTDOT (panelized_panel.list.list[i].fname))
411 list->list[i].fnamelen = panelized_panel.list.list[i].fnamelen;
412 list->list[i].fname = g_strndup (panelized_panel.list.list[i].fname,
413 panelized_panel.list.list[i].fnamelen);
415 else
417 vfs_path_t *tmp_vpath;
418 const char *fname;
420 tmp_vpath =
421 vfs_path_append_new (panelized_panel.root_vpath, panelized_panel.list.list[i].fname,
422 (char *) NULL);
423 fname = vfs_path_as_str (tmp_vpath);
424 list->list[i].fnamelen = strlen (fname);
425 list->list[i].fname = g_strndup (fname, list->list[i].fnamelen);
426 vfs_path_free (tmp_vpath);
428 list->list[i].f.link_to_dir = panelized_panel.list.list[i].f.link_to_dir;
429 list->list[i].f.stale_link = panelized_panel.list.list[i].f.stale_link;
430 list->list[i].f.dir_size_computed = panelized_panel.list.list[i].f.dir_size_computed;
431 list->list[i].f.marked = panelized_panel.list.list[i].f.marked;
432 list->list[i].st = panelized_panel.list.list[i].st;
433 list->list[i].sort_key = panelized_panel.list.list[i].sort_key;
434 list->list[i].second_sort_key = panelized_panel.list.list[i].second_sort_key;
436 try_to_select (panel, NULL);
439 /* --------------------------------------------------------------------------------------------- */
440 /*** public functions ****************************************************************************/
441 /* --------------------------------------------------------------------------------------------- */
444 * Change root directory of panelized content.
445 * @param new_root - object with new path.
447 void
448 panelize_change_root (const vfs_path_t * new_root)
450 vfs_path_free (panelized_panel.root_vpath);
451 panelized_panel.root_vpath = vfs_path_clone (new_root);
454 /* --------------------------------------------------------------------------------------------- */
456 void
457 panelize_save_panel (WPanel * panel)
459 int i;
460 dir_list *list = &panel->dir;
462 panelize_change_root (current_panel->cwd_vpath);
464 if (panelized_panel.list.len > 0)
465 dir_list_clean (&panelized_panel.list);
466 if (panel->dir.len == 0)
467 return;
469 if (panel->dir.len > panelized_panel.list.size)
470 dir_list_grow (&panelized_panel.list, panel->dir.len - panelized_panel.list.size);
471 panelized_panel.list.len = panel->dir.len;
473 for (i = 0; i < panel->dir.len; i++)
475 panelized_panel.list.list[i].fnamelen = list->list[i].fnamelen;
476 panelized_panel.list.list[i].fname =
477 g_strndup (list->list[i].fname, list->list[i].fnamelen);
478 panelized_panel.list.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
479 panelized_panel.list.list[i].f.stale_link = list->list[i].f.stale_link;
480 panelized_panel.list.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
481 panelized_panel.list.list[i].f.marked = list->list[i].f.marked;
482 panelized_panel.list.list[i].st = list->list[i].st;
483 panelized_panel.list.list[i].sort_key = list->list[i].sort_key;
484 panelized_panel.list.list[i].second_sort_key = list->list[i].second_sort_key;
488 /* --------------------------------------------------------------------------------------------- */
490 void
491 cd_panelize_cmd (void)
493 if (!SELECTED_IS_PANEL)
494 set_display_type (MENU_PANEL_IDX, view_listing);
496 do_panelize_cd (PANEL (get_panel_widget (MENU_PANEL_IDX)));
499 /* --------------------------------------------------------------------------------------------- */
501 void
502 external_panelize (void)
504 if (!vfs_current_is_local ())
506 message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
507 return;
510 init_panelize ();
512 /* display file info */
513 tty_setcolor (SELECTED_COLOR);
515 switch (dlg_run (panelize_dlg))
517 case B_CANCEL:
518 break;
520 case B_ADD:
521 add2panelize_cmd ();
522 break;
524 case B_REMOVE:
526 struct panelize *entry;
528 listbox_get_current (l_panelize, NULL, (void **) &entry);
529 remove_from_panelize (entry);
530 break;
533 case B_ENTER:
534 if (!input_is_empty (pname))
536 char *cmd;
538 cmd = g_strdup (pname->buffer);
539 dlg_destroy (panelize_dlg);
540 do_external_panelize (cmd);
541 g_free (cmd);
542 repaint_screen ();
543 return;
545 break;
547 default:
548 break;
551 panelize_done ();
554 /* --------------------------------------------------------------------------------------------- */
556 void
557 load_panelize (void)
559 char **keys;
561 keys = mc_config_get_keys (mc_main_config, panelize_section, NULL);
563 add2panelize (g_strdup (_("Other command")), g_strdup (""));
565 if (*keys == NULL)
567 add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
568 add2panelize (g_strdup (_("Find rejects after patching")),
569 g_strdup ("find . -name \\*.rej -print"));
570 add2panelize (g_strdup (_("Find *.orig after patching")),
571 g_strdup ("find . -name \\*.orig -print"));
572 add2panelize (g_strdup (_("Find SUID and SGID programs")),
573 g_strdup
574 ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 -a -perm /01 \\) \\) -print"));
576 else
578 GIConv conv;
579 char **profile_keys;
581 conv = str_crt_conv_from ("UTF-8");
583 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
585 GString *buffer;
587 if (mc_global.utf8_display || conv == INVALID_CONV)
588 buffer = g_string_new (*profile_keys);
589 else
591 buffer = g_string_new ("");
592 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
593 g_string_assign (buffer, *profile_keys);
596 add2panelize (g_string_free (buffer, FALSE),
597 mc_config_get_string (mc_main_config, panelize_section, *profile_keys,
598 ""));
601 str_close_conv (conv);
604 g_strfreev (keys);
607 /* --------------------------------------------------------------------------------------------- */
609 void
610 save_panelize (void)
612 struct panelize *current = panelize;
614 mc_config_del_group (mc_main_config, panelize_section);
615 for (; current; current = current->next)
617 if (strcmp (current->label, _("Other command")))
618 mc_config_set_string (mc_main_config,
619 panelize_section, current->label, current->command);
623 /* --------------------------------------------------------------------------------------------- */
625 void
626 done_panelize (void)
628 struct panelize *current = panelize;
629 struct panelize *next;
631 for (; current; current = next)
633 next = current->next;
634 g_free (current->label);
635 g_free (current->command);
636 g_free (current);
640 /* --------------------------------------------------------------------------------------------- */