Added two macro examples:
[zuwinko.git] / src / filemanager / panelize.c
blob93bb955ca3f232868ee3af7e1747a50d051f585a
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"
42 #include "lib/util.h"
43 #include "lib/widget.h"
45 #include "src/setup.h" /* For profile_bname */
46 #include "src/main.h"
47 #include "src/history.h"
49 #include "dir.h"
50 #include "midnight.h" /* current_panel */
51 #include "layout.h" /* repaint_screen() */
53 #include "panelize.h"
55 /*** global variables ****************************************************************************/
57 /*** file scope macro definitions ****************************************************************/
59 #define UX 5
60 #define UY 2
62 #define BX 5
63 #define BY 18
65 #define BUTTONS 4
66 #define LABELS 3
67 #define B_ADD B_USER
68 #define B_REMOVE (B_USER + 1)
70 /*** file scope type declarations ****************************************************************/
72 /*** file scope variables ************************************************************************/
74 static WListbox *l_panelize;
75 static Dlg_head *panelize_dlg;
76 static int last_listitem;
77 static WInput *pname;
79 static struct
81 int ret_cmd;
82 button_flags_t flags;
83 int y, x;
84 const char *text;
85 } panelize_but[BUTTONS] =
87 /* *INDENT-OFF* */
88 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
89 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
90 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
91 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") }
92 /* *INDENT-ON* */
95 static const char *panelize_section = "Panelize";
97 /* Directory panelize */
98 static struct panelize
100 char *command;
101 char *label;
102 struct panelize *next;
103 } *panelize = NULL;
105 /*** file scope functions ************************************************************************/
106 /* --------------------------------------------------------------------------------------------- */
108 static void do_external_panelize (char *command);
110 /* --------------------------------------------------------------------------------------------- */
112 static void
113 update_command (void)
115 if (l_panelize->pos != last_listitem)
117 struct panelize *data = NULL;
119 last_listitem = l_panelize->pos;
120 listbox_get_current (l_panelize, NULL, (void **) &data);
121 input_assign_text (pname, data->command);
122 pname->point = 0;
123 input_update (pname, TRUE);
127 /* --------------------------------------------------------------------------------------------- */
129 static cb_ret_t
130 panelize_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
132 switch (msg)
134 case DLG_INIT:
135 case DLG_POST_KEY:
136 tty_setcolor (MENU_ENTRY_COLOR);
137 update_command ();
138 return MSG_HANDLED;
140 case DLG_DRAW:
141 common_dialog_repaint (h);
142 tty_setcolor (COLOR_NORMAL);
143 draw_box (h, UY, UX, h->lines - 10, h->cols - 10, TRUE);
144 return MSG_HANDLED;
146 default:
147 return default_dlg_callback (h, sender, msg, parm, data);
151 /* --------------------------------------------------------------------------------------------- */
153 static void
154 init_panelize (void)
156 int i, panelize_cols = COLS - 6;
157 struct panelize *current = panelize;
159 #ifdef ENABLE_NLS
160 static int i18n_flag = 0;
161 static int maxlen = 0;
163 if (!i18n_flag)
165 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
166 while (i--)
168 panelize_but[i].text = _(panelize_but[i].text);
169 maxlen += str_term_width1 (panelize_but[i].text) + 5;
171 maxlen += 10;
173 i18n_flag = 1;
175 panelize_cols = max (panelize_cols, maxlen);
177 panelize_but[2].x = panelize_but[3].x + str_term_width1 (panelize_but[3].text) + 7;
178 panelize_but[1].x = panelize_but[2].x + str_term_width1 (panelize_but[2].text) + 5;
179 panelize_but[0].x = panelize_cols - str_term_width1 (panelize_but[0].text) - 8 - BX;
181 #endif /* ENABLE_NLS */
183 last_listitem = 0;
185 do_refresh ();
187 panelize_dlg =
188 create_dlg (TRUE, 0, 0, 22, panelize_cols, dialog_colors,
189 panelize_callback, "[External panelize]",
190 _("External panelize"), DLG_CENTER | DLG_REVERSE);
192 for (i = 0; i < BUTTONS; i++)
193 add_widget (panelize_dlg,
194 button_new (BY + panelize_but[i].y,
195 BX + panelize_but[i].x,
196 panelize_but[i].ret_cmd,
197 panelize_but[i].flags, panelize_but[i].text, 0));
199 pname =
200 input_new (UY + 14, UX, input_get_default_colors (),
201 panelize_dlg->cols - 10, "", "in", INPUT_COMPLETE_DEFAULT);
202 add_widget (panelize_dlg, pname);
204 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
206 /* get new listbox */
207 l_panelize = listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, FALSE, NULL);
209 while (current)
211 listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current);
212 current = current->next;
215 /* add listbox to the dialogs */
216 add_widget (panelize_dlg, l_panelize);
218 listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
221 /* --------------------------------------------------------------------------------------------- */
223 static void
224 panelize_done (void)
226 destroy_dlg (panelize_dlg);
227 repaint_screen ();
230 /* --------------------------------------------------------------------------------------------- */
232 static void
233 add2panelize (char *label, char *command)
235 struct panelize *current, *old;
237 old = NULL;
238 current = panelize;
239 while (current && strcmp (current->label, label) <= 0)
241 old = current;
242 current = current->next;
245 if (old == NULL)
247 panelize = g_new (struct panelize, 1);
248 panelize->label = label;
249 panelize->command = command;
250 panelize->next = current;
252 else
254 struct panelize *new;
255 new = g_new (struct panelize, 1);
256 new->label = label;
257 new->command = command;
258 old->next = new;
259 new->next = current;
263 /* --------------------------------------------------------------------------------------------- */
265 static void
266 add2panelize_cmd (void)
268 char *label;
270 if (pname->buffer && (*pname->buffer))
272 label = input_dialog (_("Add to external panelize"),
273 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "");
274 if (!label)
275 return;
276 if (!*label)
278 g_free (label);
279 return;
282 add2panelize (label, g_strdup (pname->buffer));
286 /* --------------------------------------------------------------------------------------------- */
288 static void
289 remove_from_panelize (struct panelize *entry)
291 if (strcmp (entry->label, _("Other command")) != 0)
293 if (entry == panelize)
295 panelize = panelize->next;
297 else
299 struct panelize *current = panelize;
300 while (current && current->next != entry)
301 current = current->next;
302 if (current)
304 current->next = entry->next;
308 g_free (entry->label);
309 g_free (entry->command);
310 g_free (entry);
314 /* --------------------------------------------------------------------------------------------- */
316 static void
317 do_external_panelize (char *command)
319 int status, link_to_dir, stale_link;
320 int next_free = 0;
321 struct stat st;
322 dir_list *list = &current_panel->dir;
323 char line[MC_MAXPATHLEN];
324 char *name;
325 FILE *external;
327 open_error_pipe ();
328 external = popen (command, "r");
329 if (!external)
331 close_error_pipe (D_ERROR, _("Cannot invoke command."));
332 return;
334 /* Clear the counters and the directory list */
335 panel_clean_dir (current_panel);
337 while (1)
339 clearerr (external);
340 if (fgets (line, MC_MAXPATHLEN, external) == NULL)
342 if (ferror (external) && errno == EINTR)
343 continue;
344 else
345 break;
347 if (line[strlen (line) - 1] == '\n')
348 line[strlen (line) - 1] = 0;
349 if (strlen (line) < 1)
350 continue;
351 if (line[0] == '.' && line[1] == PATH_SEP)
352 name = line + 2;
353 else
354 name = line;
355 status = handle_path (list, name, &st, next_free, &link_to_dir, &stale_link);
356 if (status == 0)
357 continue;
358 if (status == -1)
359 break;
360 list->list[next_free].fnamelen = strlen (name);
361 list->list[next_free].fname = g_strdup (name);
362 file_mark (current_panel, next_free, 0);
363 list->list[next_free].f.link_to_dir = link_to_dir;
364 list->list[next_free].f.stale_link = stale_link;
365 list->list[next_free].f.dir_size_computed = 0;
366 list->list[next_free].st = st;
367 list->list[next_free].sort_key = NULL;
368 list->list[next_free].second_sort_key = NULL;
369 next_free++;
370 if (!(next_free & 32))
371 rotate_dash ();
374 current_panel->is_panelized = 1;
375 if (next_free)
377 current_panel->count = next_free;
378 if (list->list[0].fname[0] == PATH_SEP)
380 int ret;
381 strcpy (current_panel->cwd, PATH_SEP_STR);
382 ret = chdir (PATH_SEP_STR);
385 else
387 current_panel->count = set_zero_dir (list) ? 1 : 0;
389 if (pclose (external) < 0)
390 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
391 close_error_pipe (D_NORMAL, NULL);
392 try_to_select (current_panel, NULL);
393 panel_re_sort (current_panel);
396 /* --------------------------------------------------------------------------------------------- */
397 /*** public functions ****************************************************************************/
398 /* --------------------------------------------------------------------------------------------- */
400 void
401 external_panelize (void)
403 char *target = NULL;
405 if (!vfs_current_is_local ())
407 message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
408 return;
411 init_panelize ();
413 /* display file info */
414 tty_setcolor (SELECTED_COLOR);
416 run_dlg (panelize_dlg);
418 switch (panelize_dlg->ret_value)
420 case B_CANCEL:
421 break;
423 case B_ADD:
424 add2panelize_cmd ();
425 break;
427 case B_REMOVE:
429 struct panelize *entry;
431 listbox_get_current (l_panelize, NULL, (void **) &entry);
432 remove_from_panelize (entry);
433 break;
436 case B_ENTER:
437 target = pname->buffer;
438 if (target != NULL && *target)
440 char *cmd = g_strdup (target);
441 destroy_dlg (panelize_dlg);
442 do_external_panelize (cmd);
443 g_free (cmd);
444 repaint_screen ();
445 return;
447 break;
450 panelize_done ();
453 /* --------------------------------------------------------------------------------------------- */
455 void
456 load_panelize (void)
458 gchar **profile_keys, **keys;
459 gsize len;
460 GIConv conv;
461 GString *buffer;
463 conv = str_crt_conv_from ("UTF-8");
465 profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section, &len);
467 add2panelize (g_strdup (_("Other command")), g_strdup (""));
469 if (!profile_keys || *profile_keys == NULL)
471 add2panelize (g_strdup (_("Find rejects after patching")),
472 g_strdup ("find . -name \\*.rej -print"));
473 add2panelize (g_strdup (_("Find *.orig after patching")),
474 g_strdup ("find . -name \\*.orig -print"));
475 add2panelize (g_strdup (_("Find SUID and SGID programs")),
476 g_strdup
477 ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
478 return;
481 while (*profile_keys)
484 if (utf8_display || conv == INVALID_CONV)
486 buffer = g_string_new (*profile_keys);
488 else
490 buffer = g_string_new ("");
491 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
493 g_string_free (buffer, TRUE);
494 buffer = g_string_new (*profile_keys);
498 add2panelize (g_string_free (buffer, FALSE),
499 mc_config_get_string (mc_main_config, panelize_section, *profile_keys, ""));
500 profile_keys++;
502 g_strfreev (keys);
503 str_close_conv (conv);
506 /* --------------------------------------------------------------------------------------------- */
508 void
509 save_panelize (void)
511 struct panelize *current = panelize;
513 mc_config_del_group (mc_main_config, panelize_section);
514 for (; current; current = current->next)
516 if (strcmp (current->label, _("Other command")))
517 mc_config_set_string (mc_main_config,
518 panelize_section, current->label, current->command);
520 mc_config_save_file (mc_main_config, NULL);
523 /* --------------------------------------------------------------------------------------------- */
525 void
526 done_panelize (void)
528 struct panelize *current = panelize;
529 struct panelize *next;
531 for (; current; current = next)
533 next = current->next;
534 g_free (current->label);
535 g_free (current->command);
536 g_free (current);
540 /* --------------------------------------------------------------------------------------------- */