4 Copyright (C) 1995-2016
5 Free Software Foundation, Inc.
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/>.
28 * \brief Source: External panelization module
36 #include <sys/types.h>
40 #include "lib/global.h"
43 #include "lib/vfs/vfs.h"
44 #include "lib/mcconfig.h" /* Load/save directories panelize */
45 #include "lib/strutil.h"
47 #include "lib/widget.h"
49 #include "src/setup.h" /* For profile_bname */
50 #include "src/history.h"
53 #include "midnight.h" /* current_panel */
54 #include "layout.h" /* rotate_dash() */
55 #include "panel.h" /* WPanel */
59 /*** global variables ****************************************************************************/
61 /*** file scope macro definitions ****************************************************************/
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
;
79 static const char *panelize_section
= "Panelize";
81 /* Directory panelize */
82 static struct panelize
86 struct panelize
*next
;
89 /* --------------------------------------------------------------------------------------------- */
90 /*** file scope functions ************************************************************************/
91 /* --------------------------------------------------------------------------------------------- */
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
);
104 input_update (pname
, TRUE
);
108 /* --------------------------------------------------------------------------------------------- */
111 panelize_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
116 case MSG_NOTIFY
: /* MSG_NOTIFY is fired by the listbox to tell us the item has changed. */
121 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
125 /* --------------------------------------------------------------------------------------------- */
133 button_flags_t flags
;
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") }
148 struct panelize
*current
;
155 i
= G_N_ELEMENTS (panelize_but
);
156 blen
= i
- 1; /* gaps between buttons */
160 panelize_but
[i
].text
= _(panelize_but
[i
].text
);
162 blen
+= str_term_width1 (panelize_but
[i
].text
) + 3 + 1;
163 if (panelize_but
[i
].flags
== DEFPUSH_BUTTON
)
167 panelize_cols
= COLS
- 6;
168 panelize_cols
= MAX (panelize_cols
, blen
+ 4);
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 */
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")));
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
++)
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 /* --------------------------------------------------------------------------------------------- */
217 dlg_destroy (panelize_dlg
);
221 /* --------------------------------------------------------------------------------------------- */
224 add2panelize (char *label
, char *command
)
226 struct panelize
*current
, *old
;
230 while (current
&& strcmp (current
->label
, label
) <= 0)
233 current
= current
->next
;
238 panelize
= g_new (struct panelize
, 1);
239 panelize
->label
= label
;
240 panelize
->command
= command
;
241 panelize
->next
= current
;
245 struct panelize
*new;
246 new = g_new (struct panelize
, 1);
248 new->command
= command
;
254 /* --------------------------------------------------------------------------------------------- */
257 add2panelize_cmd (void)
259 if (!input_is_empty (pname
))
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')
269 add2panelize (label
, g_strdup (pname
->buffer
));
273 /* --------------------------------------------------------------------------------------------- */
276 remove_from_panelize (struct panelize
*entry
)
278 if (strcmp (entry
->label
, _("Other command")) != 0)
280 if (entry
== panelize
)
282 panelize
= panelize
->next
;
286 struct panelize
*current
= panelize
;
287 while (current
&& current
->next
!= entry
)
288 current
= current
->next
;
291 current
->next
= entry
->next
;
295 g_free (entry
->label
);
296 g_free (entry
->command
);
301 /* --------------------------------------------------------------------------------------------- */
304 do_external_panelize (char *command
)
306 int link_to_dir
, stale_link
;
308 dir_list
*list
= ¤t_panel
->dir
;
309 char line
[MC_MAXPATHLEN
];
314 external
= popen (command
, "r");
317 close_error_pipe (D_ERROR
, _("Cannot invoke command."));
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
);
330 if (fgets (line
, sizeof (line
), external
) == NULL
)
332 if (ferror (external
) && errno
== EINTR
)
337 if (line
[strlen (line
) - 1] == '\n')
338 line
[strlen (line
) - 1] = 0;
339 if (strlen (line
) < 1)
341 if (line
[0] == '.' && IS_PATH_SEP (line
[1]))
346 if (!handle_path (name
, &st
, &link_to_dir
, &stale_link
))
349 if (!dir_list_append (list
, name
, &st
, link_to_dir
!= 0, stale_link
!= 0))
352 file_mark (current_panel
, list
->len
- 1, 0);
354 if ((list
->len
& 31) == 0)
358 current_panel
->is_panelized
= TRUE
;
361 dir_list_init (list
);
362 else if (IS_PATH_SEP (list
->list
[0].fname
[0]))
364 vfs_path_t
*vpath_root
;
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
);
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
);
383 /* --------------------------------------------------------------------------------------------- */
386 do_panelize_cd (WPanel
* panel
)
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
);
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
);
417 vfs_path_t
*tmp_vpath
;
421 vfs_path_append_new (panelized_panel
.root_vpath
, panelized_panel
.list
.list
[i
].fname
,
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.
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 /* --------------------------------------------------------------------------------------------- */
457 panelize_save_panel (WPanel
* panel
)
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)
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 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
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"));
512 /* display file info */
513 tty_setcolor (SELECTED_COLOR
);
515 switch (dlg_run (panelize_dlg
))
526 struct panelize
*entry
;
528 listbox_get_current (l_panelize
, NULL
, (void **) &entry
);
529 remove_from_panelize (entry
);
534 if (!input_is_empty (pname
))
538 cmd
= g_strdup (pname
->buffer
);
539 dlg_destroy (panelize_dlg
);
540 do_external_panelize (cmd
);
554 /* --------------------------------------------------------------------------------------------- */
561 keys
= mc_config_get_keys (mc_main_config
, panelize_section
, NULL
);
563 add2panelize (g_strdup (_("Other command")), g_strdup (""));
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")),
574 ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 -a -perm /01 \\) \\) -print"));
581 conv
= str_crt_conv_from ("UTF-8");
583 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
587 if (mc_global
.utf8_display
|| conv
== INVALID_CONV
)
588 buffer
= g_string_new (*profile_keys
);
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
,
601 str_close_conv (conv
);
607 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
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
);
640 /* --------------------------------------------------------------------------------------------- */