Merge branch '2123_crash_while_copy'
[midnight-commander.git] / src / panelize.c
blob02cda625b64c3bc9f530c9bc46ccb71fd4891cac
1 /* External panelize
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1995 Janne Kukonlehto
6 1995 Jakub Jelinek
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.
23 /** \file panelize.c
24 * \brief Source: External panelization module
27 #include <config.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
36 #include "lib/global.h"
38 #include "lib/skin.h"
39 #include "lib/vfs/mc-vfs/vfs.h"
40 #include "lib/mcconfig.h" /* Load/save directories panelize */
41 #include "lib/strutil.h"
43 #include "dialog.h"
44 #include "widget.h"
45 #include "wtools.h" /* For common_dialog_repaint() */
46 #include "setup.h" /* For profile_bname */
47 #include "dir.h"
48 #include "panel.h" /* current_panel */
49 #include "layout.h" /* repaint_screen() */
50 #include "main.h"
51 #include "panelize.h"
52 #include "history.h"
54 #define UX 5
55 #define UY 2
57 #define BX 5
58 #define BY 18
60 #define BUTTONS 4
61 #define LABELS 3
62 #define B_ADD B_USER
63 #define B_REMOVE (B_USER + 1)
65 static WListbox *l_panelize;
66 static Dlg_head *panelize_dlg;
67 static int last_listitem;
68 static WInput *pname;
70 static struct
72 int ret_cmd, flags, y, x;
73 const char *text;
74 } panelize_but[BUTTONS] =
76 /* *INDENT-OFF* */
77 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
78 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
79 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
80 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") }
81 /* *INDENT-ON* */
84 static const char *panelize_section = "Panelize";
85 static void do_external_panelize (char *command);
87 /* Directory panelize */
88 static struct panelize
90 char *command;
91 char *label;
92 struct panelize *next;
93 } *panelize = NULL;
95 static void
96 update_command (void)
98 if (l_panelize->pos != last_listitem)
100 struct panelize *data = NULL;
102 last_listitem = l_panelize->pos;
103 listbox_get_current (l_panelize, NULL, (void **) &data);
104 assign_text (pname, data->command);
105 pname->point = 0;
106 update_input (pname, 1);
110 static cb_ret_t
111 panelize_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
113 switch (msg)
115 case DLG_INIT:
116 case DLG_POST_KEY:
117 tty_setcolor (MENU_ENTRY_COLOR);
118 update_command ();
119 return MSG_HANDLED;
121 case DLG_DRAW:
122 common_dialog_repaint (h);
123 tty_setcolor (COLOR_NORMAL);
124 draw_box (h, UY, UX, h->lines - 10, h->cols - 10, TRUE);
125 return MSG_HANDLED;
127 default:
128 return default_dlg_callback (h, sender, msg, parm, data);
132 static void
133 init_panelize (void)
135 const int input_colors[3] =
137 INPUT_COLOR,
138 INPUT_UNCHANGED_COLOR,
139 INPUT_MARK_COLOR
142 int i, panelize_cols = COLS - 6;
143 struct panelize *current = panelize;
145 #ifdef ENABLE_NLS
146 static int i18n_flag = 0;
147 static int maxlen = 0;
149 if (!i18n_flag)
151 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
152 while (i--)
154 panelize_but[i].text = _(panelize_but[i].text);
155 maxlen += str_term_width1 (panelize_but[i].text) + 5;
157 maxlen += 10;
159 i18n_flag = 1;
161 panelize_cols = max (panelize_cols, maxlen);
163 panelize_but[2].x = panelize_but[3].x + str_term_width1 (panelize_but[3].text) + 7;
164 panelize_but[1].x = panelize_but[2].x + str_term_width1 (panelize_but[2].text) + 5;
165 panelize_but[0].x = panelize_cols - str_term_width1 (panelize_but[0].text) - 8 - BX;
167 #endif /* ENABLE_NLS */
169 last_listitem = 0;
171 do_refresh ();
173 panelize_dlg =
174 create_dlg (TRUE, 0, 0, 22, panelize_cols, dialog_colors,
175 panelize_callback, "[External panelize]",
176 _("External panelize"), DLG_CENTER | DLG_REVERSE);
178 for (i = 0; i < BUTTONS; i++)
179 add_widget (panelize_dlg,
180 button_new (BY + panelize_but[i].y,
181 BX + panelize_but[i].x,
182 panelize_but[i].ret_cmd,
183 panelize_but[i].flags, panelize_but[i].text, 0));
185 pname =
186 input_new (UY + 14, UX, (int *) input_colors,
187 panelize_dlg->cols - 10, "", "in", INPUT_COMPLETE_DEFAULT);
188 add_widget (panelize_dlg, pname);
190 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
192 /* get new listbox */
193 l_panelize = listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, FALSE, NULL);
195 while (current)
197 listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current);
198 current = current->next;
201 /* add listbox to the dialogs */
202 add_widget (panelize_dlg, l_panelize);
204 listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
207 static void
208 panelize_done (void)
210 destroy_dlg (panelize_dlg);
211 repaint_screen ();
214 static void
215 add2panelize (char *label, char *command)
217 struct panelize *current, *old;
219 old = NULL;
220 current = panelize;
221 while (current && strcmp (current->label, label) <= 0)
223 old = current;
224 current = current->next;
227 if (old == NULL)
229 panelize = g_new (struct panelize, 1);
230 panelize->label = label;
231 panelize->command = command;
232 panelize->next = current;
234 else
236 struct panelize *new;
237 new = g_new (struct panelize, 1);
238 new->label = label;
239 new->command = command;
240 old->next = new;
241 new->next = current;
245 static void
246 add2panelize_cmd (void)
248 char *label;
250 if (pname->buffer && (*pname->buffer))
252 label = input_dialog (_("Add to external panelize"),
253 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "");
254 if (!label)
255 return;
256 if (!*label)
258 g_free (label);
259 return;
262 add2panelize (label, g_strdup (pname->buffer));
266 static void
267 remove_from_panelize (struct panelize *entry)
269 if (strcmp (entry->label, _("Other command")) != 0)
271 if (entry == panelize)
273 panelize = panelize->next;
275 else
277 struct panelize *current = panelize;
278 while (current && current->next != entry)
279 current = current->next;
280 if (current)
282 current->next = entry->next;
286 g_free (entry->label);
287 g_free (entry->command);
288 g_free (entry);
292 void
293 external_panelize (void)
295 char *target = NULL;
297 if (!vfs_current_is_local ())
299 message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
300 return;
303 init_panelize ();
305 /* display file info */
306 tty_setcolor (SELECTED_COLOR);
308 run_dlg (panelize_dlg);
310 switch (panelize_dlg->ret_value)
312 case B_CANCEL:
313 break;
315 case B_ADD:
316 add2panelize_cmd ();
317 break;
319 case B_REMOVE:
321 struct panelize *entry;
323 listbox_get_current (l_panelize, NULL, (void **) &entry);
324 remove_from_panelize (entry);
325 break;
328 case B_ENTER:
329 target = pname->buffer;
330 if (target != NULL && *target)
332 char *cmd = g_strdup (target);
333 destroy_dlg (panelize_dlg);
334 do_external_panelize (cmd);
335 g_free (cmd);
336 repaint_screen ();
337 return;
339 break;
342 panelize_done ();
345 void
346 load_panelize (void)
348 gchar **profile_keys, **keys;
349 gsize len;
350 GIConv conv;
351 GString *buffer;
353 conv = str_crt_conv_from ("UTF-8");
355 profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section, &len);
357 add2panelize (g_strdup (_("Other command")), g_strdup (""));
359 if (!profile_keys || *profile_keys == NULL)
361 add2panelize (g_strdup (_("Find rejects after patching")),
362 g_strdup ("find . -name \\*.rej -print"));
363 add2panelize (g_strdup (_("Find *.orig after patching")),
364 g_strdup ("find . -name \\*.orig -print"));
365 add2panelize (g_strdup (_("Find SUID and SGID programs")),
366 g_strdup
367 ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
368 return;
371 while (*profile_keys)
374 if (utf8_display || conv == INVALID_CONV)
376 buffer = g_string_new (*profile_keys);
378 else
380 buffer = g_string_new ("");
381 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
383 g_string_free (buffer, TRUE);
384 buffer = g_string_new (*profile_keys);
388 add2panelize (g_string_free (buffer, FALSE),
389 mc_config_get_string (mc_main_config, panelize_section, *profile_keys, ""));
390 profile_keys++;
392 g_strfreev (keys);
393 str_close_conv (conv);
396 void
397 save_panelize (void)
399 struct panelize *current = panelize;
401 mc_config_del_group (mc_main_config, panelize_section);
402 for (; current; current = current->next)
404 if (strcmp (current->label, _("Other command")))
405 mc_config_set_string (mc_main_config,
406 panelize_section, current->label, current->command);
408 mc_config_save_file (mc_main_config, NULL);
411 void
412 done_panelize (void)
414 struct panelize *current = panelize;
415 struct panelize *next;
417 for (; current; current = next)
419 next = current->next;
420 g_free (current->label);
421 g_free (current->command);
422 g_free (current);
426 static void
427 do_external_panelize (char *command)
429 int status, link_to_dir, stale_link;
430 int next_free = 0;
431 struct stat st;
432 dir_list *list = &current_panel->dir;
433 char line[MC_MAXPATHLEN];
434 char *name;
435 FILE *external;
437 open_error_pipe ();
438 external = popen (command, "r");
439 if (!external)
441 close_error_pipe (D_ERROR, _("Cannot invoke command."));
442 return;
444 /* Clear the counters and the directory list */
445 panel_clean_dir (current_panel);
447 while (1)
449 clearerr (external);
450 if (fgets (line, MC_MAXPATHLEN, external) == NULL)
452 if (ferror (external) && errno == EINTR)
453 continue;
454 else
455 break;
457 if (line[strlen (line) - 1] == '\n')
458 line[strlen (line) - 1] = 0;
459 if (strlen (line) < 1)
460 continue;
461 if (line[0] == '.' && line[1] == PATH_SEP)
462 name = line + 2;
463 else
464 name = line;
465 status = handle_path (list, name, &st, next_free, &link_to_dir, &stale_link);
466 if (status == 0)
467 continue;
468 if (status == -1)
469 break;
470 list->list[next_free].fnamelen = strlen (name);
471 list->list[next_free].fname = g_strdup (name);
472 file_mark (current_panel, next_free, 0);
473 list->list[next_free].f.link_to_dir = link_to_dir;
474 list->list[next_free].f.stale_link = stale_link;
475 list->list[next_free].f.dir_size_computed = 0;
476 list->list[next_free].st = st;
477 list->list[next_free].sort_key = NULL;
478 list->list[next_free].second_sort_key = NULL;
479 next_free++;
480 if (!(next_free & 32))
481 rotate_dash ();
484 current_panel->is_panelized = 1;
485 if (next_free)
487 current_panel->count = next_free;
488 if (list->list[0].fname[0] == PATH_SEP)
490 int ret;
491 strcpy (current_panel->cwd, PATH_SEP_STR);
492 ret = chdir (PATH_SEP_STR);
495 else
497 current_panel->count = set_zero_dir (list) ? 1 : 0;
499 if (pclose (external) < 0)
500 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
501 close_error_pipe (D_NORMAL, NULL);
502 try_to_select (current_panel, NULL);
503 panel_re_sort (current_panel);