2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007 Free Software Foundation, Inc.
5 Written by: 1995 Janne Kukonlehto
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <sys/types.h>
32 #include <mhl/memory.h>
33 #include <mhl/string.h>
36 #include "tty.h" /* attrset() */
41 #include "wtools.h" /* For common_dialog_repaint() */
42 #include "setup.h" /* For profile_bname */
43 #include "profile.h" /* Load/save directories panelize */
45 #include "panel.h" /* current_panel */
46 #include "main.h" /* repaint_screen */
59 #define B_REMOVE (B_USER + 1)
61 static WListbox
*l_panelize
;
62 static Dlg_head
*panelize_dlg
;
63 static int last_listitem
;
67 int ret_cmd
, flags
, y
, x
;
69 } panelize_but
[BUTTONS
] = {
70 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel") },
71 { B_ADD
, NORMAL_BUTTON
, 0, 28, N_("&Add new") },
72 { B_REMOVE
, NORMAL_BUTTON
, 0, 16, N_("&Remove") },
73 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Pane&lize") },
76 static const char *panelize_section
= "Panelize";
77 static void do_external_panelize (char *command
);
79 /* Directory panelize */
80 static struct panelize
{
83 struct panelize
*next
;
89 if (l_panelize
->pos
!= last_listitem
) {
90 last_listitem
= l_panelize
->pos
;
92 ((struct panelize
*) l_panelize
->current
->data
)->command
);
94 update_input (pname
, 1);
99 panelize_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
103 common_dialog_repaint (h
);
104 attrset (COLOR_NORMAL
);
105 draw_box (h
, UY
, UX
, h
->lines
- 10, h
->cols
- 10);
111 attrset (MENU_ENTRY_COLOR
);
116 return default_dlg_callback (h
, msg
, parm
);
123 int i
, panelize_cols
= COLS
- 6;
124 struct panelize
*current
= panelize
;
127 static int i18n_flag
= 0;
128 static int maxlen
= 0;
131 i
= sizeof (panelize_but
) / sizeof (panelize_but
[0]);
133 panelize_but
[i
].text
= _(panelize_but
[i
].text
);
134 maxlen
+= strlen (panelize_but
[i
].text
) + 5;
140 panelize_cols
= max (panelize_cols
, maxlen
);
143 panelize_but
[3].x
+ strlen (panelize_but
[3].text
) + 7;
145 panelize_but
[2].x
+ strlen (panelize_but
[2].text
) + 5;
147 panelize_cols
- strlen (panelize_but
[0].text
) - 8 - BX
;
149 #endif /* ENABLE_NLS */
156 create_dlg (0, 0, 22, panelize_cols
, dialog_colors
,
157 panelize_callback
, "[External panelize]",
158 _("External panelize"), DLG_CENTER
| DLG_REVERSE
);
160 for (i
= 0; i
< BUTTONS
; i
++)
161 add_widget (panelize_dlg
,
162 button_new (BY
+ panelize_but
[i
].y
,
163 BX
+ panelize_but
[i
].x
,
164 panelize_but
[i
].ret_cmd
,
165 panelize_but
[i
].flags
,
166 panelize_but
[i
].text
, 0));
169 input_new (UY
+ 14, UX
, INPUT_COLOR
, panelize_dlg
->cols
- 10, "",
170 "in", INPUT_COMPLETE_DEFAULT
);
171 add_widget (panelize_dlg
, pname
);
173 add_widget (panelize_dlg
, label_new (UY
+ 13, UX
, _("Command")));
175 /* get new listbox */
177 listbox_new (UY
+ 1, UX
+ 1, panelize_dlg
->cols
- 12, 10, NULL
);
180 listbox_add_item (l_panelize
, 0, 0, current
->label
, current
);
181 current
= current
->next
;
184 /* add listbox to the dialogs */
185 add_widget (panelize_dlg
, l_panelize
);
187 listbox_select_entry (l_panelize
,
188 listbox_search_text (l_panelize
,
189 _("Other command")));
192 static void panelize_done (void)
194 destroy_dlg (panelize_dlg
);
198 static void add2panelize (char *label
, char *command
)
200 struct panelize
*current
, *old
;
204 while (current
&& strcmp (current
->label
, label
) <= 0){
206 current
= current
->next
;
210 panelize
= g_new (struct panelize
, 1);
211 panelize
->label
= label
;
212 panelize
->command
= command
;
213 panelize
->next
= current
;
215 struct panelize
*new;
216 new = g_new (struct panelize
, 1);
218 new->command
= command
;
225 add2panelize_cmd (void)
229 if (pname
->buffer
&& (*pname
->buffer
)) {
230 label
= input_dialog (_(" Add to external panelize "),
231 _(" Enter command label: "),
232 MC_HISTORY_FM_PANELIZE_ADD
,
241 add2panelize (label
, g_strdup (pname
->buffer
));
245 static void remove_from_panelize (struct panelize
*entry
)
247 if (strcmp (entry
->label
, _("Other command")) != 0) {
248 if (entry
== panelize
) {
249 panelize
= panelize
->next
;
251 struct panelize
*current
= panelize
;
252 while (current
&& current
->next
!= entry
)
253 current
= current
->next
;
255 current
->next
= entry
->next
;
259 g_free (entry
->label
);
260 g_free (entry
->command
);
266 external_panelize (void)
270 if (!vfs_current_is_local ()){
271 message (D_ERROR
, MSG_ERROR
,
272 _(" Cannot run external panelize in a non-local directory "));
278 /* display file info */
279 attrset (SELECTED_COLOR
);
281 run_dlg (panelize_dlg
);
283 switch (panelize_dlg
->ret_value
) {
292 remove_from_panelize (l_panelize
->current
->data
);
296 target
= pname
->buffer
;
297 if (target
!= NULL
&& *target
) {
298 char *cmd
= g_strdup (target
);
299 destroy_dlg (panelize_dlg
);
300 do_external_panelize (cmd
);
311 void load_panelize (void)
316 profile_keys
= profile_init_iterator (panelize_section
, profile_name
);
318 add2panelize (g_strdup (_("Other command")), g_strdup (""));
321 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
322 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
323 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
327 while (profile_keys
){
328 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
329 add2panelize (g_strdup (key
), g_strdup (value
));
333 void save_panelize (void)
335 struct panelize
*current
= panelize
;
337 profile_clean_section (panelize_section
, profile_name
);
338 for (;current
; current
= current
->next
){
339 if (strcmp (current
->label
, _("Other command")))
340 WritePrivateProfileString (panelize_section
,
348 void done_panelize (void)
350 struct panelize
*current
= panelize
;
351 struct panelize
*next
;
353 for (; current
; current
= next
){
354 next
= current
->next
;
355 g_free (current
->label
);
356 g_free (current
->command
);
361 static void do_external_panelize (char *command
)
363 int status
, link_to_dir
, stale_link
;
366 dir_list
*list
= ¤t_panel
->dir
;
367 char line
[MC_MAXPATHLEN
];
372 external
= popen (command
, "r");
374 close_error_pipe (D_ERROR
, _("Cannot invoke command."));
377 /* Clear the counters and the directory list */
378 panel_clean_dir (current_panel
);
382 if (fgets (line
, MC_MAXPATHLEN
, external
) == NULL
) {
383 if (ferror(external
) && errno
== EINTR
)
388 if (line
[strlen(line
)-1] == '\n')
389 line
[strlen(line
)-1] = 0;
390 if (strlen(line
) < 1)
392 if (line
[0] == '.' && line
[1] == PATH_SEP
)
396 status
= handle_path (list
, name
, &st
, next_free
, &link_to_dir
,
402 list
->list
[next_free
].fnamelen
= strlen (name
);
403 list
->list
[next_free
].fname
= g_strdup (name
);
404 file_mark (current_panel
, next_free
, 0);
405 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
406 list
->list
[next_free
].f
.stale_link
= stale_link
;
407 list
->list
[next_free
].f
.dir_size_computed
= 0;
408 list
->list
[next_free
].st
= st
;
410 if (!(next_free
& 32))
414 current_panel
->is_panelized
= 1;
416 current_panel
->count
= next_free
;
417 if (list
->list
[0].fname
[0] == PATH_SEP
){
418 strcpy (current_panel
->cwd
, PATH_SEP_STR
);
419 chdir (PATH_SEP_STR
);
422 current_panel
->count
= set_zero_dir (list
);
424 if (pclose (external
) < 0)
425 message (D_NORMAL
, _("External panelize"), _("Pipe close failed"));
426 close_error_pipe (D_NORMAL
, NULL
);
427 try_to_select (current_panel
, NULL
);
428 panel_re_sort (current_panel
);