4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
6 The Free Software Foundation, Inc.
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: External panelization module
37 #include <sys/types.h>
41 #include "lib/global.h"
44 #include "lib/vfs/vfs.h"
45 #include "lib/mcconfig.h" /* Load/save directories panelize */
46 #include "lib/strutil.h"
48 #include "lib/widget.h"
50 #include "src/setup.h" /* For profile_bname */
51 #include "src/history.h"
54 #include "midnight.h" /* current_panel */
55 #include "panel.h" /* WPanel */
60 /*** global variables ****************************************************************************/
62 /*** file scope macro definitions ****************************************************************/
69 #define B_REMOVE (B_USER + 1)
71 /*** file scope type declarations ****************************************************************/
73 /*** file scope variables ************************************************************************/
75 static WListbox
*l_panelize
;
76 static WDialog
*panelize_dlg
;
77 static int last_listitem
;
80 static const char *panelize_section
= "Panelize";
82 /* Directory panelize */
83 static struct panelize
87 struct panelize
*next
;
90 /* --------------------------------------------------------------------------------------------- */
91 /*** file scope functions ************************************************************************/
92 /* --------------------------------------------------------------------------------------------- */
97 if (l_panelize
->pos
!= last_listitem
)
99 struct panelize
*data
= NULL
;
101 last_listitem
= l_panelize
->pos
;
102 listbox_get_current (l_panelize
, NULL
, (void **) &data
);
103 input_assign_text (pname
, data
->command
);
105 input_update (pname
, TRUE
);
109 /* --------------------------------------------------------------------------------------------- */
112 panelize_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
119 tty_setcolor (MENU_ENTRY_COLOR
);
124 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
128 /* --------------------------------------------------------------------------------------------- */
136 button_flags_t flags
;
141 { B_ENTER
, DEFPUSH_BUTTON
, N_("Pane&lize") },
142 { B_REMOVE
, NORMAL_BUTTON
, N_("&Remove") },
143 { B_ADD
, NORMAL_BUTTON
, N_("&Add new") },
144 { B_CANCEL
, NORMAL_BUTTON
, N_("&Cancel") }
151 struct panelize
*current
;
158 i
= G_N_ELEMENTS (panelize_but
);
159 blen
= i
- 1; /* gaps between buttons */
162 panelize_but
[i
].text
= _(panelize_but
[i
].text
);
163 blen
+= str_term_width1 (panelize_but
[i
].text
) + 3 + 1;
164 if (panelize_but
[i
].flags
== DEFPUSH_BUTTON
)
168 panelize_cols
= COLS
- 6;
169 panelize_cols
= max (panelize_cols
, blen
+ 4);
172 create_dlg (TRUE
, 0, 0, 20, panelize_cols
, dialog_colors
, panelize_callback
, NULL
,
173 "[External panelize]", _("External panelize"), DLG_CENTER
);
175 /* add listbox to the dialogs */
177 add_widget (panelize_dlg
, groupbox_new (y
++, UX
, 12, panelize_cols
- UX
* 2, ""));
179 l_panelize
= listbox_new (y
, UX
+ 1, 10, panelize_cols
- UX
* 2 - 2, FALSE
, NULL
);
180 for (current
= panelize
; current
!= NULL
; current
= current
->next
)
181 listbox_add_item (l_panelize
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
);
182 listbox_select_entry (l_panelize
, listbox_search_text (l_panelize
, _("Other command")));
183 add_widget (panelize_dlg
, l_panelize
);
185 y
+= WIDGET (l_panelize
)->lines
+ 1;
186 add_widget (panelize_dlg
, label_new (y
++, UX
, _("Command")));
188 input_new (y
++, UX
, input_get_default_colors (), panelize_cols
- UX
* 2, "", "in",
189 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_COMMANDS
|
190 INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
| INPUT_COMPLETE_CD
|
191 INPUT_COMPLETE_SHELL_ESC
);
192 add_widget (panelize_dlg
, pname
);
196 add_widget (panelize_dlg
, hline_new (y
++, -1, -1));
198 x
= (panelize_cols
- blen
) / 2;
199 for (i
= 0; i
< G_N_ELEMENTS (panelize_but
); i
++)
203 b
= button_new (y
, x
,
204 panelize_but
[i
].ret_cmd
, panelize_but
[i
].flags
, panelize_but
[i
].text
, NULL
);
205 add_widget (panelize_dlg
, b
);
207 x
+= button_get_len (b
) + 1;
210 dlg_select_widget (l_panelize
);
213 /* --------------------------------------------------------------------------------------------- */
218 destroy_dlg (panelize_dlg
);
222 /* --------------------------------------------------------------------------------------------- */
225 add2panelize (char *label
, char *command
)
227 struct panelize
*current
, *old
;
231 while (current
&& strcmp (current
->label
, label
) <= 0)
234 current
= current
->next
;
239 panelize
= g_new (struct panelize
, 1);
240 panelize
->label
= label
;
241 panelize
->command
= command
;
242 panelize
->next
= current
;
246 struct panelize
*new;
247 new = g_new (struct panelize
, 1);
249 new->command
= command
;
255 /* --------------------------------------------------------------------------------------------- */
258 add2panelize_cmd (void)
262 if (pname
->buffer
&& (*pname
->buffer
))
264 label
= input_dialog (_("Add to external panelize"),
265 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD
, "",
266 INPUT_COMPLETE_NONE
);
275 add2panelize (label
, g_strdup (pname
->buffer
));
279 /* --------------------------------------------------------------------------------------------- */
282 remove_from_panelize (struct panelize
*entry
)
284 if (strcmp (entry
->label
, _("Other command")) != 0)
286 if (entry
== panelize
)
288 panelize
= panelize
->next
;
292 struct panelize
*current
= panelize
;
293 while (current
&& current
->next
!= entry
)
294 current
= current
->next
;
297 current
->next
= entry
->next
;
301 g_free (entry
->label
);
302 g_free (entry
->command
);
307 /* --------------------------------------------------------------------------------------------- */
310 do_external_panelize (char *command
)
312 int status
, link_to_dir
, stale_link
;
315 dir_list
*list
= ¤t_panel
->dir
;
316 char line
[MC_MAXPATHLEN
];
321 external
= popen (command
, "r");
324 close_error_pipe (D_ERROR
, _("Cannot invoke command."));
327 /* Clear the counters and the directory list */
328 panel_clean_dir (current_panel
);
330 panelize_change_root (current_panel
->cwd_vpath
);
332 if (set_zero_dir (list
))
338 if (fgets (line
, MC_MAXPATHLEN
, external
) == NULL
)
340 if (ferror (external
) && errno
== EINTR
)
345 if (line
[strlen (line
) - 1] == '\n')
346 line
[strlen (line
) - 1] = 0;
347 if (strlen (line
) < 1)
349 if (line
[0] == '.' && line
[1] == PATH_SEP
)
353 status
= handle_path (list
, name
, &st
, next_free
, &link_to_dir
, &stale_link
);
358 list
->list
[next_free
].fnamelen
= strlen (name
);
359 list
->list
[next_free
].fname
= g_strndup (name
, list
->list
[next_free
].fnamelen
);
360 file_mark (current_panel
, next_free
, 0);
361 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
362 list
->list
[next_free
].f
.stale_link
= stale_link
;
363 list
->list
[next_free
].f
.dir_size_computed
= 0;
364 list
->list
[next_free
].st
= st
;
365 list
->list
[next_free
].sort_key
= NULL
;
366 list
->list
[next_free
].second_sort_key
= NULL
;
368 if (!(next_free
& 32))
372 current_panel
->is_panelized
= TRUE
;
375 current_panel
->count
= next_free
;
376 if (list
->list
[0].fname
[0] == PATH_SEP
)
379 panel_set_cwd (current_panel
, PATH_SEP_STR
);
380 ret
= chdir (PATH_SEP_STR
);
386 current_panel
->count
= set_zero_dir (list
) ? 1 : 0;
388 if (pclose (external
) < 0)
389 message (D_NORMAL
, _("External panelize"), _("Pipe close failed"));
390 close_error_pipe (D_NORMAL
, NULL
);
391 try_to_select (current_panel
, NULL
);
392 panel_re_sort (current_panel
);
395 /* --------------------------------------------------------------------------------------------- */
398 do_panelize_cd (struct WPanel
*panel
)
401 dir_list
*list
= &panel
->dir
;
402 gboolean panelized_same
;
404 clean_dir (list
, panel
->count
);
405 if (panelized_panel
.root_vpath
== NULL
)
406 panelize_change_root (current_panel
->cwd_vpath
);
408 if (panelized_panel
.count
< 1)
410 if (set_zero_dir (&panelized_panel
.list
))
411 panelized_panel
.count
= 1;
413 else if (panelized_panel
.count
>= list
->size
)
415 list
->list
= g_try_realloc (list
->list
, sizeof (file_entry
) * panelized_panel
.count
);
416 list
->size
= panelized_panel
.count
;
418 panel
->count
= panelized_panel
.count
;
419 panel
->is_panelized
= TRUE
;
421 panelized_same
= (vfs_path_cmp (panelized_panel
.root_vpath
, panel
->cwd_vpath
) == 0);
423 for (i
= 0; i
< panelized_panel
.count
; i
++)
426 || (panelized_panel
.list
.list
[i
].fname
[0] == '.'
427 && panelized_panel
.list
.list
[i
].fname
[1] == '.'
428 && panelized_panel
.list
.list
[i
].fname
[2] == '\0'))
430 list
->list
[i
].fnamelen
= panelized_panel
.list
.list
[i
].fnamelen
;
431 list
->list
[i
].fname
= g_strndup (panelized_panel
.list
.list
[i
].fname
,
432 panelized_panel
.list
.list
[i
].fnamelen
);
436 vfs_path_t
*tmp_vpath
;
439 vfs_path_append_new (panelized_panel
.root_vpath
, panelized_panel
.list
.list
[i
].fname
,
441 list
->list
[i
].fname
= vfs_path_to_str (tmp_vpath
);
442 vfs_path_free (tmp_vpath
);
443 list
->list
[i
].fnamelen
= strlen (list
->list
[i
].fname
);
445 list
->list
[i
].f
.link_to_dir
= panelized_panel
.list
.list
[i
].f
.link_to_dir
;
446 list
->list
[i
].f
.stale_link
= panelized_panel
.list
.list
[i
].f
.stale_link
;
447 list
->list
[i
].f
.dir_size_computed
= panelized_panel
.list
.list
[i
].f
.dir_size_computed
;
448 list
->list
[i
].f
.marked
= panelized_panel
.list
.list
[i
].f
.marked
;
449 list
->list
[i
].st
= panelized_panel
.list
.list
[i
].st
;
450 list
->list
[i
].sort_key
= panelized_panel
.list
.list
[i
].sort_key
;
451 list
->list
[i
].second_sort_key
= panelized_panel
.list
.list
[i
].second_sort_key
;
453 try_to_select (panel
, NULL
);
456 /* --------------------------------------------------------------------------------------------- */
457 /*** public functions ****************************************************************************/
458 /* --------------------------------------------------------------------------------------------- */
461 * Change root directory of panelized content.
462 * @param new_root - object with new path.
465 panelize_change_root (const vfs_path_t
* new_root
)
467 vfs_path_free (panelized_panel
.root_vpath
);
468 panelized_panel
.root_vpath
= vfs_path_clone (new_root
);
471 /* --------------------------------------------------------------------------------------------- */
474 panelize_save_panel (struct WPanel
*panel
)
477 dir_list
*list
= &panel
->dir
;
479 panelize_change_root (current_panel
->cwd_vpath
);
481 if (panelized_panel
.count
> 0)
482 clean_dir (&panelized_panel
.list
, panelized_panel
.count
);
483 if (panel
->count
< 1)
486 panelized_panel
.count
= panel
->count
;
487 if (panel
->count
>= panelized_panel
.list
.size
)
489 panelized_panel
.list
.list
= g_try_realloc (panelized_panel
.list
.list
,
490 sizeof (file_entry
) * panel
->count
);
491 panelized_panel
.list
.size
= panel
->count
;
493 for (i
= 0; i
< panel
->count
; i
++)
495 panelized_panel
.list
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
496 panelized_panel
.list
.list
[i
].fname
=
497 g_strndup (list
->list
[i
].fname
, list
->list
[i
].fnamelen
);
498 panelized_panel
.list
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
499 panelized_panel
.list
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
500 panelized_panel
.list
.list
[i
].f
.dir_size_computed
= list
->list
[i
].f
.dir_size_computed
;
501 panelized_panel
.list
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
502 panelized_panel
.list
.list
[i
].st
= list
->list
[i
].st
;
503 panelized_panel
.list
.list
[i
].sort_key
= list
->list
[i
].sort_key
;
504 panelized_panel
.list
.list
[i
].second_sort_key
= list
->list
[i
].second_sort_key
;
508 /* --------------------------------------------------------------------------------------------- */
511 cd_panelize_cmd (void)
513 if (get_display_type (MENU_PANEL_IDX
) != view_listing
)
514 set_display_type (MENU_PANEL_IDX
, view_listing
);
516 do_panelize_cd ((struct WPanel
*) get_panel_widget (MENU_PANEL_IDX
));
519 /* --------------------------------------------------------------------------------------------- */
522 external_panelize (void)
526 if (!vfs_current_is_local ())
528 message (D_ERROR
, MSG_ERROR
, _("Cannot run external panelize in a non-local directory"));
534 /* display file info */
535 tty_setcolor (SELECTED_COLOR
);
537 run_dlg (panelize_dlg
);
539 switch (panelize_dlg
->ret_value
)
550 struct panelize
*entry
;
552 listbox_get_current (l_panelize
, NULL
, (void **) &entry
);
553 remove_from_panelize (entry
);
558 target
= pname
->buffer
;
559 if (target
!= NULL
&& *target
)
561 char *cmd
= g_strdup (target
);
562 destroy_dlg (panelize_dlg
);
563 do_external_panelize (cmd
);
574 /* --------------------------------------------------------------------------------------------- */
579 gchar
**profile_keys
, **keys
;
583 conv
= str_crt_conv_from ("UTF-8");
585 profile_keys
= keys
= mc_config_get_keys (mc_main_config
, panelize_section
, &len
);
587 add2panelize (g_strdup (_("Other command")), g_strdup (""));
589 if (!profile_keys
|| *profile_keys
== NULL
)
591 add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
592 add2panelize (g_strdup (_("Find rejects after patching")),
593 g_strdup ("find . -name \\*.rej -print"));
594 add2panelize (g_strdup (_("Find *.orig after patching")),
595 g_strdup ("find . -name \\*.orig -print"));
596 add2panelize (g_strdup (_("Find SUID and SGID programs")),
598 ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
602 while (*profile_keys
)
606 if (mc_global
.utf8_display
|| conv
== INVALID_CONV
)
607 buffer
= g_string_new (*profile_keys
);
610 buffer
= g_string_new ("");
611 if (str_convert (conv
, *profile_keys
, buffer
) == ESTR_FAILURE
)
612 g_string_assign (buffer
, *profile_keys
);
615 add2panelize (g_string_free (buffer
, FALSE
),
616 mc_config_get_string (mc_main_config
, panelize_section
, *profile_keys
, ""));
621 str_close_conv (conv
);
624 /* --------------------------------------------------------------------------------------------- */
629 struct panelize
*current
= panelize
;
631 mc_config_del_group (mc_main_config
, panelize_section
);
632 for (; current
; current
= current
->next
)
634 if (strcmp (current
->label
, _("Other command")))
635 mc_config_set_string (mc_main_config
,
636 panelize_section
, current
->label
, current
->command
);
640 /* --------------------------------------------------------------------------------------------- */
645 struct panelize
*current
= panelize
;
646 struct panelize
*next
;
648 for (; current
; current
= next
)
650 next
= current
->next
;
651 g_free (current
->label
);
652 g_free (current
->command
);
657 /* --------------------------------------------------------------------------------------------- */