(get_fs_usage): avoid compile warning about mixed declarations and code.
[midnight-commander.git] / src / filemanager / panelize.c
blobc49d77688172dba6cf921a9e6b07fe2b7a470e75
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 case DLG_FOCUS:
142 tty_setcolor (MENU_ENTRY_COLOR);
143 update_command ();
144 return MSG_HANDLED;
146 case DLG_DRAW:
147 common_dialog_repaint (h);
148 tty_setcolor (COLOR_NORMAL);
149 draw_box (h, UY, UX, h->lines - 10, h->cols - 10, TRUE);
150 return MSG_HANDLED;
152 default:
153 return default_dlg_callback (h, sender, msg, parm, data);
157 /* --------------------------------------------------------------------------------------------- */
159 static void
160 init_panelize (void)
162 int i, panelize_cols = COLS - 6;
163 struct panelize *current = panelize;
165 #ifdef ENABLE_NLS
166 static int i18n_flag = 0;
167 static int maxlen = 0;
169 if (!i18n_flag)
171 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
172 while (i--)
174 panelize_but[i].text = _(panelize_but[i].text);
175 maxlen += str_term_width1 (panelize_but[i].text) + 5;
177 maxlen += 10;
179 i18n_flag = 1;
181 panelize_cols = max (panelize_cols, maxlen);
183 panelize_but[2].x = panelize_but[3].x + str_term_width1 (panelize_but[3].text) + 7;
184 panelize_but[1].x = panelize_but[2].x + str_term_width1 (panelize_but[2].text) + 5;
185 panelize_but[0].x = panelize_cols - str_term_width1 (panelize_but[0].text) - 8 - BX;
187 #endif /* ENABLE_NLS */
189 last_listitem = 0;
191 do_refresh ();
193 panelize_dlg =
194 create_dlg (TRUE, 0, 0, 22, panelize_cols, dialog_colors,
195 panelize_callback, NULL, "[External panelize]",
196 _("External panelize"), DLG_CENTER | DLG_REVERSE);
198 for (i = 0; i < BUTTONS; i++)
199 add_widget (panelize_dlg,
200 button_new (BY + panelize_but[i].y,
201 BX + panelize_but[i].x,
202 panelize_but[i].ret_cmd,
203 panelize_but[i].flags, panelize_but[i].text, 0));
205 pname =
206 input_new (UY + 14, UX, input_get_default_colors (),
207 panelize_dlg->cols - 10, "", "in", INPUT_COMPLETE_DEFAULT);
208 add_widget (panelize_dlg, pname);
210 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
212 /* get new listbox */
213 l_panelize = listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, FALSE, NULL);
215 while (current)
217 listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current);
218 current = current->next;
221 /* add listbox to the dialogs */
222 add_widget (panelize_dlg, l_panelize);
224 listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
227 /* --------------------------------------------------------------------------------------------- */
229 static void
230 panelize_done (void)
232 destroy_dlg (panelize_dlg);
233 repaint_screen ();
236 /* --------------------------------------------------------------------------------------------- */
238 static void
239 add2panelize (char *label, char *command)
241 struct panelize *current, *old;
243 old = NULL;
244 current = panelize;
245 while (current && strcmp (current->label, label) <= 0)
247 old = current;
248 current = current->next;
251 if (old == NULL)
253 panelize = g_new (struct panelize, 1);
254 panelize->label = label;
255 panelize->command = command;
256 panelize->next = current;
258 else
260 struct panelize *new;
261 new = g_new (struct panelize, 1);
262 new->label = label;
263 new->command = command;
264 old->next = new;
265 new->next = current;
269 /* --------------------------------------------------------------------------------------------- */
271 static void
272 add2panelize_cmd (void)
274 char *label;
276 if (pname->buffer && (*pname->buffer))
278 label = input_dialog (_("Add to external panelize"),
279 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "");
280 if (!label)
281 return;
282 if (!*label)
284 g_free (label);
285 return;
288 add2panelize (label, g_strdup (pname->buffer));
292 /* --------------------------------------------------------------------------------------------- */
294 static void
295 remove_from_panelize (struct panelize *entry)
297 if (strcmp (entry->label, _("Other command")) != 0)
299 if (entry == panelize)
301 panelize = panelize->next;
303 else
305 struct panelize *current = panelize;
306 while (current && current->next != entry)
307 current = current->next;
308 if (current)
310 current->next = entry->next;
314 g_free (entry->label);
315 g_free (entry->command);
316 g_free (entry);
320 /* --------------------------------------------------------------------------------------------- */
322 static void
323 do_external_panelize (char *command)
325 int status, link_to_dir, stale_link;
326 int next_free = 0;
327 struct stat st;
328 dir_list *list = &current_panel->dir;
329 char line[MC_MAXPATHLEN];
330 char *name;
331 FILE *external;
333 open_error_pipe ();
334 external = popen (command, "r");
335 if (!external)
337 close_error_pipe (D_ERROR, _("Cannot invoke command."));
338 return;
340 /* Clear the counters and the directory list */
341 panel_clean_dir (current_panel);
343 panelize_change_root (current_panel->cwd_vpath);
345 if (set_zero_dir (list))
346 next_free++;
348 while (1)
350 clearerr (external);
351 if (fgets (line, MC_MAXPATHLEN, external) == NULL)
353 if (ferror (external) && errno == EINTR)
354 continue;
355 else
356 break;
358 if (line[strlen (line) - 1] == '\n')
359 line[strlen (line) - 1] = 0;
360 if (strlen (line) < 1)
361 continue;
362 if (line[0] == '.' && line[1] == PATH_SEP)
363 name = line + 2;
364 else
365 name = line;
366 status = handle_path (list, name, &st, next_free, &link_to_dir, &stale_link);
367 if (status == 0)
368 continue;
369 if (status == -1)
370 break;
371 list->list[next_free].fnamelen = strlen (name);
372 list->list[next_free].fname = g_strndup (name, list->list[next_free].fnamelen);
373 file_mark (current_panel, next_free, 0);
374 list->list[next_free].f.link_to_dir = link_to_dir;
375 list->list[next_free].f.stale_link = stale_link;
376 list->list[next_free].f.dir_size_computed = 0;
377 list->list[next_free].st = st;
378 list->list[next_free].sort_key = NULL;
379 list->list[next_free].second_sort_key = NULL;
380 next_free++;
381 if (!(next_free & 32))
382 rotate_dash ();
385 current_panel->is_panelized = TRUE;
386 if (next_free)
388 current_panel->count = next_free;
389 if (list->list[0].fname[0] == PATH_SEP)
391 int ret;
392 panel_set_cwd (current_panel, PATH_SEP_STR);
393 ret = chdir (PATH_SEP_STR);
396 else
398 current_panel->count = set_zero_dir (list) ? 1 : 0;
400 if (pclose (external) < 0)
401 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
402 close_error_pipe (D_NORMAL, NULL);
403 try_to_select (current_panel, NULL);
404 panel_re_sort (current_panel);
407 /* --------------------------------------------------------------------------------------------- */
409 static void
410 do_panelize_cd (struct WPanel *panel)
412 int i;
413 dir_list *list = &panel->dir;
414 gboolean panelized_same;
416 clean_dir (list, panel->count);
417 if (panelized_panel.root_vpath == NULL)
418 panelize_change_root (current_panel->cwd_vpath);
420 if (panelized_panel.count < 1)
422 if (set_zero_dir (&panelized_panel.list))
423 panelized_panel.count = 1;
425 else if (panelized_panel.count >= list->size)
427 list->list = g_try_realloc (list->list, sizeof (file_entry) * panelized_panel.count);
428 list->size = panelized_panel.count;
430 panel->count = panelized_panel.count;
431 panel->is_panelized = TRUE;
433 panelized_same = (vfs_path_cmp (panelized_panel.root_vpath, panel->cwd_vpath) == 0);
435 for (i = 0; i < panelized_panel.count; i++)
437 if (panelized_same
438 || (panelized_panel.list.list[i].fname[0] == '.'
439 && panelized_panel.list.list[i].fname[1] == '.'
440 && panelized_panel.list.list[i].fname[2] == '\0'))
442 list->list[i].fnamelen = panelized_panel.list.list[i].fnamelen;
443 list->list[i].fname = g_strndup (panelized_panel.list.list[i].fname,
444 panelized_panel.list.list[i].fnamelen);
446 else
448 vfs_path_t *tmp_vpath;
450 tmp_vpath =
451 vfs_path_append_new (panelized_panel.root_vpath, panelized_panel.list.list[i].fname,
452 NULL);
453 list->list[i].fname = vfs_path_to_str (tmp_vpath);
454 vfs_path_free (tmp_vpath);
455 list->list[i].fnamelen = strlen (list->list[i].fname);
457 list->list[i].f.link_to_dir = panelized_panel.list.list[i].f.link_to_dir;
458 list->list[i].f.stale_link = panelized_panel.list.list[i].f.stale_link;
459 list->list[i].f.dir_size_computed = panelized_panel.list.list[i].f.dir_size_computed;
460 list->list[i].f.marked = panelized_panel.list.list[i].f.marked;
461 list->list[i].st = panelized_panel.list.list[i].st;
462 list->list[i].sort_key = panelized_panel.list.list[i].sort_key;
463 list->list[i].second_sort_key = panelized_panel.list.list[i].second_sort_key;
465 try_to_select (panel, NULL);
468 /* --------------------------------------------------------------------------------------------- */
469 /*** public functions ****************************************************************************/
470 /* --------------------------------------------------------------------------------------------- */
473 * Change root directory of panelized content.
474 * @param new_root - object with new path.
476 void
477 panelize_change_root (const vfs_path_t * new_root)
479 vfs_path_free (panelized_panel.root_vpath);
480 panelized_panel.root_vpath = vfs_path_clone (new_root);
483 /* --------------------------------------------------------------------------------------------- */
485 void
486 panelize_save_panel (struct WPanel *panel)
488 int i;
489 dir_list *list = &panel->dir;
491 panelize_change_root (current_panel->cwd_vpath);
493 if (panelized_panel.count > 0)
494 clean_dir (&panelized_panel.list, panelized_panel.count);
495 if (panel->count < 1)
496 return;
498 panelized_panel.count = panel->count;
499 if (panel->count >= panelized_panel.list.size)
501 panelized_panel.list.list = g_try_realloc (panelized_panel.list.list,
502 sizeof (file_entry) * panel->count);
503 panelized_panel.list.size = panel->count;
505 for (i = 0; i < panel->count; i++)
507 panelized_panel.list.list[i].fnamelen = list->list[i].fnamelen;
508 panelized_panel.list.list[i].fname =
509 g_strndup (list->list[i].fname, list->list[i].fnamelen);
510 panelized_panel.list.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
511 panelized_panel.list.list[i].f.stale_link = list->list[i].f.stale_link;
512 panelized_panel.list.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
513 panelized_panel.list.list[i].f.marked = list->list[i].f.marked;
514 panelized_panel.list.list[i].st = list->list[i].st;
515 panelized_panel.list.list[i].sort_key = list->list[i].sort_key;
516 panelized_panel.list.list[i].second_sort_key = list->list[i].second_sort_key;
520 /* --------------------------------------------------------------------------------------------- */
522 void
523 cd_panelize_cmd (void)
525 if (get_display_type (MENU_PANEL_IDX) != view_listing)
526 set_display_type (MENU_PANEL_IDX, view_listing);
528 do_panelize_cd ((struct WPanel *) get_panel_widget (MENU_PANEL_IDX));
531 /* --------------------------------------------------------------------------------------------- */
533 void
534 external_panelize (void)
536 char *target = NULL;
538 if (!vfs_current_is_local ())
540 message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
541 return;
544 init_panelize ();
546 /* display file info */
547 tty_setcolor (SELECTED_COLOR);
549 run_dlg (panelize_dlg);
551 switch (panelize_dlg->ret_value)
553 case B_CANCEL:
554 break;
556 case B_ADD:
557 add2panelize_cmd ();
558 break;
560 case B_REMOVE:
562 struct panelize *entry;
564 listbox_get_current (l_panelize, NULL, (void **) &entry);
565 remove_from_panelize (entry);
566 break;
569 case B_ENTER:
570 target = pname->buffer;
571 if (target != NULL && *target)
573 char *cmd = g_strdup (target);
574 destroy_dlg (panelize_dlg);
575 do_external_panelize (cmd);
576 g_free (cmd);
577 repaint_screen ();
578 return;
580 break;
583 panelize_done ();
586 /* --------------------------------------------------------------------------------------------- */
588 void
589 load_panelize (void)
591 gchar **profile_keys, **keys;
592 gsize len;
593 GIConv conv;
595 conv = str_crt_conv_from ("UTF-8");
597 profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section, &len);
599 add2panelize (g_strdup (_("Other command")), g_strdup (""));
601 if (!profile_keys || *profile_keys == NULL)
603 add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
604 add2panelize (g_strdup (_("Find rejects after patching")),
605 g_strdup ("find . -name \\*.rej -print"));
606 add2panelize (g_strdup (_("Find *.orig after patching")),
607 g_strdup ("find . -name \\*.orig -print"));
608 add2panelize (g_strdup (_("Find SUID and SGID programs")),
609 g_strdup
610 ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
611 return;
614 while (*profile_keys)
616 GString *buffer;
618 if (mc_global.utf8_display || conv == INVALID_CONV)
619 buffer = g_string_new (*profile_keys);
620 else
622 buffer = g_string_new ("");
623 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
624 g_string_assign (buffer, *profile_keys);
627 add2panelize (g_string_free (buffer, FALSE),
628 mc_config_get_string (mc_main_config, panelize_section, *profile_keys, ""));
629 profile_keys++;
632 g_strfreev (keys);
633 str_close_conv (conv);
636 /* --------------------------------------------------------------------------------------------- */
638 void
639 save_panelize (void)
641 struct panelize *current = panelize;
643 mc_config_del_group (mc_main_config, panelize_section);
644 for (; current; current = current->next)
646 if (strcmp (current->label, _("Other command")))
647 mc_config_set_string (mc_main_config,
648 panelize_section, current->label, current->command);
652 /* --------------------------------------------------------------------------------------------- */
654 void
655 done_panelize (void)
657 struct panelize *current = panelize;
658 struct panelize *next;
660 for (; current; current = next)
662 next = current->next;
663 g_free (current->label);
664 g_free (current->command);
665 g_free (current);
669 /* --------------------------------------------------------------------------------------------- */