Changed do_cd() and do_panel() functions
[midnight-commander.git] / src / filemanager / panelize.c
blobfc05bdebe2e9500e6a99bf9c94425a88cfa53c03
1 /*
2 External panelize
4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Janne Kukonlehto, 1995
10 Jakub Jelinek, 1995
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/>.
28 /** \file panelize.c
29 * \brief Source: External panelization module
32 #include <config.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
41 #include "lib/global.h"
43 #include "lib/skin.h"
44 #include "lib/vfs/vfs.h"
45 #include "lib/mcconfig.h" /* Load/save directories panelize */
46 #include "lib/strutil.h"
47 #include "lib/util.h"
48 #include "lib/widget.h"
50 #include "src/setup.h" /* For profile_bname */
51 #include "src/history.h"
53 #include "dir.h"
54 #include "midnight.h" /* current_panel */
55 #include "panel.h" /* WPanel */
57 #include "panelize.h"
58 #include "panel.h"
60 /*** global variables ****************************************************************************/
62 /*** file scope macro definitions ****************************************************************/
64 #define UX 5
65 #define UY 2
67 #define BX 5
68 #define BY 18
70 #define BUTTONS 4
71 #define LABELS 3
72 #define B_ADD B_USER
73 #define B_REMOVE (B_USER + 1)
75 /*** file scope type declarations ****************************************************************/
77 /*** file scope variables ************************************************************************/
79 static WListbox *l_panelize;
80 static Dlg_head *panelize_dlg;
81 static int last_listitem;
82 static WInput *pname;
84 static struct
86 int ret_cmd;
87 button_flags_t flags;
88 int y, x;
89 const char *text;
90 } panelize_but[BUTTONS] =
92 /* *INDENT-OFF* */
93 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
94 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
95 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
96 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") }
97 /* *INDENT-ON* */
100 static const char *panelize_section = "Panelize";
102 /* Directory panelize */
103 static struct panelize
105 char *command;
106 char *label;
107 struct panelize *next;
108 } *panelize = NULL;
110 /*** file scope functions ************************************************************************/
111 /* --------------------------------------------------------------------------------------------- */
113 static void do_external_panelize (char *command);
115 /* --------------------------------------------------------------------------------------------- */
117 static void
118 update_command (void)
120 if (l_panelize->pos != last_listitem)
122 struct panelize *data = NULL;
124 last_listitem = l_panelize->pos;
125 listbox_get_current (l_panelize, NULL, (void **) &data);
126 input_assign_text (pname, data->command);
127 pname->point = 0;
128 input_update (pname, TRUE);
132 /* --------------------------------------------------------------------------------------------- */
134 static cb_ret_t
135 panelize_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
137 switch (msg)
139 case DLG_INIT:
140 case DLG_POST_KEY:
141 tty_setcolor (MENU_ENTRY_COLOR);
142 update_command ();
143 return MSG_HANDLED;
145 case DLG_DRAW:
146 common_dialog_repaint (h);
147 tty_setcolor (COLOR_NORMAL);
148 draw_box (h, UY, UX, h->lines - 10, h->cols - 10, TRUE);
149 return MSG_HANDLED;
151 default:
152 return default_dlg_callback (h, sender, msg, parm, data);
156 /* --------------------------------------------------------------------------------------------- */
158 static void
159 init_panelize (void)
161 int i, panelize_cols = COLS - 6;
162 struct panelize *current = panelize;
164 #ifdef ENABLE_NLS
165 static int i18n_flag = 0;
166 static int maxlen = 0;
168 if (!i18n_flag)
170 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
171 while (i--)
173 panelize_but[i].text = _(panelize_but[i].text);
174 maxlen += str_term_width1 (panelize_but[i].text) + 5;
176 maxlen += 10;
178 i18n_flag = 1;
180 panelize_cols = max (panelize_cols, maxlen);
182 panelize_but[2].x = panelize_but[3].x + str_term_width1 (panelize_but[3].text) + 7;
183 panelize_but[1].x = panelize_but[2].x + str_term_width1 (panelize_but[2].text) + 5;
184 panelize_but[0].x = panelize_cols - str_term_width1 (panelize_but[0].text) - 8 - BX;
186 #endif /* ENABLE_NLS */
188 last_listitem = 0;
190 do_refresh ();
192 panelize_dlg =
193 create_dlg (TRUE, 0, 0, 22, panelize_cols, dialog_colors,
194 panelize_callback, "[External panelize]",
195 _("External panelize"), DLG_CENTER | DLG_REVERSE);
197 for (i = 0; i < BUTTONS; i++)
198 add_widget (panelize_dlg,
199 button_new (BY + panelize_but[i].y,
200 BX + panelize_but[i].x,
201 panelize_but[i].ret_cmd,
202 panelize_but[i].flags, panelize_but[i].text, 0));
204 pname =
205 input_new (UY + 14, UX, input_get_default_colors (),
206 panelize_dlg->cols - 10, "", "in", INPUT_COMPLETE_DEFAULT);
207 add_widget (panelize_dlg, pname);
209 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
211 /* get new listbox */
212 l_panelize = listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, FALSE, NULL);
214 while (current)
216 listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current);
217 current = current->next;
220 /* add listbox to the dialogs */
221 add_widget (panelize_dlg, l_panelize);
223 listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
226 /* --------------------------------------------------------------------------------------------- */
228 static void
229 panelize_done (void)
231 destroy_dlg (panelize_dlg);
232 repaint_screen ();
235 /* --------------------------------------------------------------------------------------------- */
237 static void
238 add2panelize (char *label, char *command)
240 struct panelize *current, *old;
242 old = NULL;
243 current = panelize;
244 while (current && strcmp (current->label, label) <= 0)
246 old = current;
247 current = current->next;
250 if (old == NULL)
252 panelize = g_new (struct panelize, 1);
253 panelize->label = label;
254 panelize->command = command;
255 panelize->next = current;
257 else
259 struct panelize *new;
260 new = g_new (struct panelize, 1);
261 new->label = label;
262 new->command = command;
263 old->next = new;
264 new->next = current;
268 /* --------------------------------------------------------------------------------------------- */
270 static void
271 add2panelize_cmd (void)
273 char *label;
275 if (pname->buffer && (*pname->buffer))
277 label = input_dialog (_("Add to external panelize"),
278 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "");
279 if (!label)
280 return;
281 if (!*label)
283 g_free (label);
284 return;
287 add2panelize (label, g_strdup (pname->buffer));
291 /* --------------------------------------------------------------------------------------------- */
293 static void
294 remove_from_panelize (struct panelize *entry)
296 if (strcmp (entry->label, _("Other command")) != 0)
298 if (entry == panelize)
300 panelize = panelize->next;
302 else
304 struct panelize *current = panelize;
305 while (current && current->next != entry)
306 current = current->next;
307 if (current)
309 current->next = entry->next;
313 g_free (entry->label);
314 g_free (entry->command);
315 g_free (entry);
319 /* --------------------------------------------------------------------------------------------- */
321 static void
322 do_external_panelize (char *command)
324 int status, link_to_dir, stale_link;
325 int next_free = 0;
326 struct stat st;
327 dir_list *list = &current_panel->dir;
328 char line[MC_MAXPATHLEN];
329 char *name;
330 FILE *external;
332 open_error_pipe ();
333 external = popen (command, "r");
334 if (!external)
336 close_error_pipe (D_ERROR, _("Cannot invoke command."));
337 return;
339 /* Clear the counters and the directory list */
340 panel_clean_dir (current_panel);
342 panelize_change_root (current_panel->cwd_vpath);
344 if (set_zero_dir (list))
345 next_free++;
347 while (1)
349 clearerr (external);
350 if (fgets (line, MC_MAXPATHLEN, external) == NULL)
352 if (ferror (external) && errno == EINTR)
353 continue;
354 else
355 break;
357 if (line[strlen (line) - 1] == '\n')
358 line[strlen (line) - 1] = 0;
359 if (strlen (line) < 1)
360 continue;
361 if (line[0] == '.' && line[1] == PATH_SEP)
362 name = line + 2;
363 else
364 name = line;
365 status = handle_path (list, name, &st, next_free, &link_to_dir, &stale_link);
366 if (status == 0)
367 continue;
368 if (status == -1)
369 break;
370 list->list[next_free].fnamelen = strlen (name);
371 list->list[next_free].fname = g_strndup (name, list->list[next_free].fnamelen);
372 file_mark (current_panel, next_free, 0);
373 list->list[next_free].f.link_to_dir = link_to_dir;
374 list->list[next_free].f.stale_link = stale_link;
375 list->list[next_free].f.dir_size_computed = 0;
376 list->list[next_free].st = st;
377 list->list[next_free].sort_key = NULL;
378 list->list[next_free].second_sort_key = NULL;
379 next_free++;
380 if (!(next_free & 32))
381 rotate_dash ();
384 current_panel->is_panelized = TRUE;
385 if (next_free)
387 current_panel->count = next_free;
388 if (list->list[0].fname[0] == PATH_SEP)
390 int ret;
391 panel_set_cwd (current_panel, PATH_SEP_STR);
392 ret = chdir (PATH_SEP_STR);
395 else
397 current_panel->count = set_zero_dir (list) ? 1 : 0;
399 if (pclose (external) < 0)
400 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
401 close_error_pipe (D_NORMAL, NULL);
402 try_to_select (current_panel, NULL);
403 panel_re_sort (current_panel);
406 /* --------------------------------------------------------------------------------------------- */
408 static void
409 do_panelize_cd (struct WPanel *panel)
411 int i;
412 dir_list *list = &panel->dir;
413 gboolean panelized_same;
415 clean_dir (list, panel->count);
416 if (panelized_panel.root_vpath == NULL)
417 panelize_change_root (current_panel->cwd_vpath);
419 if (panelized_panel.count < 1)
421 if (set_zero_dir (&panelized_panel.list))
422 panelized_panel.count = 1;
424 else if (panelized_panel.count >= list->size)
426 list->list = g_try_realloc (list->list, sizeof (file_entry) * panelized_panel.count);
427 list->size = panelized_panel.count;
429 panel->count = panelized_panel.count;
430 panel->is_panelized = TRUE;
432 panelized_same = (vfs_path_cmp (panelized_panel.root_vpath, panel->cwd_vpath) == 0);
434 for (i = 0; i < panelized_panel.count; i++)
436 if (panelized_same
437 || (panelized_panel.list.list[i].fname[0] == '.'
438 && panelized_panel.list.list[i].fname[1] == '.'
439 && panelized_panel.list.list[i].fname[2] == '\0'))
441 list->list[i].fnamelen = panelized_panel.list.list[i].fnamelen;
442 list->list[i].fname = g_strndup (panelized_panel.list.list[i].fname,
443 panelized_panel.list.list[i].fnamelen);
445 else
447 vfs_path_t *tmp_vpath;
449 tmp_vpath =
450 vfs_path_append_new (panelized_panel.root_vpath, panelized_panel.list.list[i].fname,
451 NULL);
452 list->list[i].fname = vfs_path_to_str (tmp_vpath);
453 vfs_path_free (tmp_vpath);
454 list->list[i].fnamelen = strlen (list->list[i].fname);
456 list->list[i].f.link_to_dir = panelized_panel.list.list[i].f.link_to_dir;
457 list->list[i].f.stale_link = panelized_panel.list.list[i].f.stale_link;
458 list->list[i].f.dir_size_computed = panelized_panel.list.list[i].f.dir_size_computed;
459 list->list[i].f.marked = panelized_panel.list.list[i].f.marked;
460 list->list[i].st = panelized_panel.list.list[i].st;
461 list->list[i].sort_key = panelized_panel.list.list[i].sort_key;
462 list->list[i].second_sort_key = panelized_panel.list.list[i].second_sort_key;
464 try_to_select (panel, NULL);
467 /* --------------------------------------------------------------------------------------------- */
468 /*** public functions ****************************************************************************/
469 /* --------------------------------------------------------------------------------------------- */
472 * Change root directory of panelized content.
473 * @param new_root - object with new path.
475 void
476 panelize_change_root (const vfs_path_t * new_root)
478 vfs_path_free (panelized_panel.root_vpath);
479 panelized_panel.root_vpath = vfs_path_clone (new_root);
482 /* --------------------------------------------------------------------------------------------- */
484 void
485 panelize_save_panel (struct WPanel *panel)
487 int i;
488 dir_list *list = &panel->dir;
490 panelize_change_root (current_panel->cwd_vpath);
492 if (panelized_panel.count > 0)
493 clean_dir (&panelized_panel.list, panelized_panel.count);
494 if (panel->count < 1)
495 return;
497 panelized_panel.count = panel->count;
498 if (panel->count >= panelized_panel.list.size)
500 panelized_panel.list.list = g_try_realloc (panelized_panel.list.list,
501 sizeof (file_entry) * panel->count);
502 panelized_panel.list.size = panel->count;
504 for (i = 0; i < panel->count; i++)
506 panelized_panel.list.list[i].fnamelen = list->list[i].fnamelen;
507 panelized_panel.list.list[i].fname =
508 g_strndup (list->list[i].fname, list->list[i].fnamelen);
509 panelized_panel.list.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
510 panelized_panel.list.list[i].f.stale_link = list->list[i].f.stale_link;
511 panelized_panel.list.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
512 panelized_panel.list.list[i].f.marked = list->list[i].f.marked;
513 panelized_panel.list.list[i].st = list->list[i].st;
514 panelized_panel.list.list[i].sort_key = list->list[i].sort_key;
515 panelized_panel.list.list[i].second_sort_key = list->list[i].second_sort_key;
519 /* --------------------------------------------------------------------------------------------- */
521 void
522 cd_panelize_cmd (void)
524 if (get_display_type (MENU_PANEL_IDX) != view_listing)
525 set_display_type (MENU_PANEL_IDX, view_listing);
527 do_panelize_cd ((struct WPanel *) get_panel_widget (MENU_PANEL_IDX));
530 /* --------------------------------------------------------------------------------------------- */
532 void
533 external_panelize (void)
535 char *target = NULL;
537 if (!vfs_current_is_local ())
539 message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
540 return;
543 init_panelize ();
545 /* display file info */
546 tty_setcolor (SELECTED_COLOR);
548 run_dlg (panelize_dlg);
550 switch (panelize_dlg->ret_value)
552 case B_CANCEL:
553 break;
555 case B_ADD:
556 add2panelize_cmd ();
557 break;
559 case B_REMOVE:
561 struct panelize *entry;
563 listbox_get_current (l_panelize, NULL, (void **) &entry);
564 remove_from_panelize (entry);
565 break;
568 case B_ENTER:
569 target = pname->buffer;
570 if (target != NULL && *target)
572 char *cmd = g_strdup (target);
573 destroy_dlg (panelize_dlg);
574 do_external_panelize (cmd);
575 g_free (cmd);
576 repaint_screen ();
577 return;
579 break;
582 panelize_done ();
585 /* --------------------------------------------------------------------------------------------- */
587 void
588 load_panelize (void)
590 gchar **profile_keys, **keys;
591 gsize len;
592 GIConv conv;
594 conv = str_crt_conv_from ("UTF-8");
596 profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section, &len);
598 add2panelize (g_strdup (_("Other command")), g_strdup (""));
600 if (!profile_keys || *profile_keys == NULL)
602 add2panelize (g_strdup (_("Find rejects after patching")),
603 g_strdup ("find . -name \\*.rej -print"));
604 add2panelize (g_strdup (_("Find *.orig after patching")),
605 g_strdup ("find . -name \\*.orig -print"));
606 add2panelize (g_strdup (_("Find SUID and SGID programs")),
607 g_strdup
608 ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
609 return;
612 while (*profile_keys)
614 GString *buffer;
616 if (mc_global.utf8_display || conv == INVALID_CONV)
617 buffer = g_string_new (*profile_keys);
618 else
620 buffer = g_string_new ("");
621 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
622 g_string_assign (buffer, *profile_keys);
625 add2panelize (g_string_free (buffer, FALSE),
626 mc_config_get_string (mc_main_config, panelize_section, *profile_keys, ""));
627 profile_keys++;
630 g_strfreev (keys);
631 str_close_conv (conv);
634 /* --------------------------------------------------------------------------------------------- */
636 void
637 save_panelize (void)
639 struct panelize *current = panelize;
641 mc_config_del_group (mc_main_config, panelize_section);
642 for (; current; current = current->next)
644 if (strcmp (current->label, _("Other command")))
645 mc_config_set_string (mc_main_config,
646 panelize_section, current->label, current->command);
650 /* --------------------------------------------------------------------------------------------- */
652 void
653 done_panelize (void)
655 struct panelize *current = panelize;
656 struct panelize *next;
658 for (; current; current = next)
660 next = current->next;
661 g_free (current->label);
662 g_free (current->command);
663 g_free (current);
667 /* --------------------------------------------------------------------------------------------- */