Removed file contrib/dist/mc.qpg.in
[pantumic.git] / src / panelize.c
blob5a35f4f187f57825b8578cb914c713cf6dc46781
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 "setup.h" /* For profile_bname */
46 #include "dir.h"
47 #include "panel.h" /* current_panel */
48 #include "layout.h" /* repaint_screen() */
49 #include "main.h"
50 #include "panelize.h"
51 #include "history.h"
53 /*** global variables ****************************************************************************/
55 /*** file scope macro definitions ****************************************************************/
57 #define UX 5
58 #define UY 2
60 #define BX 5
61 #define BY 18
63 #define BUTTONS 4
64 #define LABELS 3
65 #define B_ADD B_USER
66 #define B_REMOVE (B_USER + 1)
68 /*** file scope type declarations ****************************************************************/
70 /*** file scope variables ************************************************************************/
72 static WListbox *l_panelize;
73 static Dlg_head *panelize_dlg;
74 static int last_listitem;
75 static WInput *pname;
77 static struct
79 int ret_cmd;
80 button_flags_t flags;
81 int y, x;
82 const char *text;
83 } panelize_but[BUTTONS] =
85 /* *INDENT-OFF* */
86 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
87 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
88 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
89 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") }
90 /* *INDENT-ON* */
93 static const char *panelize_section = "Panelize";
95 /* Directory panelize */
96 static struct panelize
98 char *command;
99 char *label;
100 struct panelize *next;
101 } *panelize = NULL;
103 /*** file scope functions ************************************************************************/
104 /* --------------------------------------------------------------------------------------------- */
106 static void do_external_panelize (char *command);
108 /* --------------------------------------------------------------------------------------------- */
110 static void
111 update_command (void)
113 if (l_panelize->pos != last_listitem)
115 struct panelize *data = NULL;
117 last_listitem = l_panelize->pos;
118 listbox_get_current (l_panelize, NULL, (void **) &data);
119 input_assign_text (pname, data->command);
120 pname->point = 0;
121 input_update (pname, TRUE);
125 /* --------------------------------------------------------------------------------------------- */
127 static cb_ret_t
128 panelize_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
130 switch (msg)
132 case DLG_INIT:
133 case DLG_POST_KEY:
134 tty_setcolor (MENU_ENTRY_COLOR);
135 update_command ();
136 return MSG_HANDLED;
138 case DLG_DRAW:
139 common_dialog_repaint (h);
140 tty_setcolor (COLOR_NORMAL);
141 draw_box (h, UY, UX, h->lines - 10, h->cols - 10, TRUE);
142 return MSG_HANDLED;
144 default:
145 return default_dlg_callback (h, sender, msg, parm, data);
149 /* --------------------------------------------------------------------------------------------- */
151 static void
152 init_panelize (void)
154 int i, panelize_cols = COLS - 6;
155 struct panelize *current = panelize;
157 #ifdef ENABLE_NLS
158 static int i18n_flag = 0;
159 static int maxlen = 0;
161 if (!i18n_flag)
163 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
164 while (i--)
166 panelize_but[i].text = _(panelize_but[i].text);
167 maxlen += str_term_width1 (panelize_but[i].text) + 5;
169 maxlen += 10;
171 i18n_flag = 1;
173 panelize_cols = max (panelize_cols, maxlen);
175 panelize_but[2].x = panelize_but[3].x + str_term_width1 (panelize_but[3].text) + 7;
176 panelize_but[1].x = panelize_but[2].x + str_term_width1 (panelize_but[2].text) + 5;
177 panelize_but[0].x = panelize_cols - str_term_width1 (panelize_but[0].text) - 8 - BX;
179 #endif /* ENABLE_NLS */
181 last_listitem = 0;
183 do_refresh ();
185 panelize_dlg =
186 create_dlg (TRUE, 0, 0, 22, panelize_cols, dialog_colors,
187 panelize_callback, "[External panelize]",
188 _("External panelize"), DLG_CENTER | DLG_REVERSE);
190 for (i = 0; i < BUTTONS; i++)
191 add_widget (panelize_dlg,
192 button_new (BY + panelize_but[i].y,
193 BX + panelize_but[i].x,
194 panelize_but[i].ret_cmd,
195 panelize_but[i].flags, panelize_but[i].text, 0));
197 pname =
198 input_new (UY + 14, UX, input_get_default_colors (),
199 panelize_dlg->cols - 10, "", "in", INPUT_COMPLETE_DEFAULT);
200 add_widget (panelize_dlg, pname);
202 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
204 /* get new listbox */
205 l_panelize = listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, FALSE, NULL);
207 while (current)
209 listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current);
210 current = current->next;
213 /* add listbox to the dialogs */
214 add_widget (panelize_dlg, l_panelize);
216 listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
219 /* --------------------------------------------------------------------------------------------- */
221 static void
222 panelize_done (void)
224 destroy_dlg (panelize_dlg);
225 repaint_screen ();
228 /* --------------------------------------------------------------------------------------------- */
230 static void
231 add2panelize (char *label, char *command)
233 struct panelize *current, *old;
235 old = NULL;
236 current = panelize;
237 while (current && strcmp (current->label, label) <= 0)
239 old = current;
240 current = current->next;
243 if (old == NULL)
245 panelize = g_new (struct panelize, 1);
246 panelize->label = label;
247 panelize->command = command;
248 panelize->next = current;
250 else
252 struct panelize *new;
253 new = g_new (struct panelize, 1);
254 new->label = label;
255 new->command = command;
256 old->next = new;
257 new->next = current;
261 /* --------------------------------------------------------------------------------------------- */
263 static void
264 add2panelize_cmd (void)
266 char *label;
268 if (pname->buffer && (*pname->buffer))
270 label = input_dialog (_("Add to external panelize"),
271 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "");
272 if (!label)
273 return;
274 if (!*label)
276 g_free (label);
277 return;
280 add2panelize (label, g_strdup (pname->buffer));
284 /* --------------------------------------------------------------------------------------------- */
286 static void
287 remove_from_panelize (struct panelize *entry)
289 if (strcmp (entry->label, _("Other command")) != 0)
291 if (entry == panelize)
293 panelize = panelize->next;
295 else
297 struct panelize *current = panelize;
298 while (current && current->next != entry)
299 current = current->next;
300 if (current)
302 current->next = entry->next;
306 g_free (entry->label);
307 g_free (entry->command);
308 g_free (entry);
312 /* --------------------------------------------------------------------------------------------- */
314 static void
315 do_external_panelize (char *command)
317 int status, link_to_dir, stale_link;
318 int next_free = 0;
319 struct stat st;
320 dir_list *list = &current_panel->dir;
321 char line[MC_MAXPATHLEN];
322 char *name;
323 FILE *external;
325 open_error_pipe ();
326 external = popen (command, "r");
327 if (!external)
329 close_error_pipe (D_ERROR, _("Cannot invoke command."));
330 return;
332 /* Clear the counters and the directory list */
333 panel_clean_dir (current_panel);
335 while (1)
337 clearerr (external);
338 if (fgets (line, MC_MAXPATHLEN, external) == NULL)
340 if (ferror (external) && errno == EINTR)
341 continue;
342 else
343 break;
345 if (line[strlen (line) - 1] == '\n')
346 line[strlen (line) - 1] = 0;
347 if (strlen (line) < 1)
348 continue;
349 if (line[0] == '.' && line[1] == PATH_SEP)
350 name = line + 2;
351 else
352 name = line;
353 status = handle_path (list, name, &st, next_free, &link_to_dir, &stale_link);
354 if (status == 0)
355 continue;
356 if (status == -1)
357 break;
358 list->list[next_free].fnamelen = strlen (name);
359 list->list[next_free].fname = g_strdup (name);
360 file_mark (current_panel, next_free, 0);
361 list->list[next_free].f.link_to_dir = link_to_dir;
362 list->list[next_free].f.stale_link = stale_link;
363 list->list[next_free].f.dir_size_computed = 0;
364 list->list[next_free].st = st;
365 list->list[next_free].sort_key = NULL;
366 list->list[next_free].second_sort_key = NULL;
367 next_free++;
368 if (!(next_free & 32))
369 rotate_dash ();
372 current_panel->is_panelized = 1;
373 if (next_free)
375 current_panel->count = next_free;
376 if (list->list[0].fname[0] == PATH_SEP)
378 int ret;
379 strcpy (current_panel->cwd, PATH_SEP_STR);
380 ret = chdir (PATH_SEP_STR);
383 else
385 current_panel->count = set_zero_dir (list) ? 1 : 0;
387 if (pclose (external) < 0)
388 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
389 close_error_pipe (D_NORMAL, NULL);
390 try_to_select (current_panel, NULL);
391 panel_re_sort (current_panel);
394 /* --------------------------------------------------------------------------------------------- */
395 /*** public functions ****************************************************************************/
396 /* --------------------------------------------------------------------------------------------- */
398 void
399 external_panelize (void)
401 char *target = NULL;
403 if (!vfs_current_is_local ())
405 message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
406 return;
409 init_panelize ();
411 /* display file info */
412 tty_setcolor (SELECTED_COLOR);
414 run_dlg (panelize_dlg);
416 switch (panelize_dlg->ret_value)
418 case B_CANCEL:
419 break;
421 case B_ADD:
422 add2panelize_cmd ();
423 break;
425 case B_REMOVE:
427 struct panelize *entry;
429 listbox_get_current (l_panelize, NULL, (void **) &entry);
430 remove_from_panelize (entry);
431 break;
434 case B_ENTER:
435 target = pname->buffer;
436 if (target != NULL && *target)
438 char *cmd = g_strdup (target);
439 destroy_dlg (panelize_dlg);
440 do_external_panelize (cmd);
441 g_free (cmd);
442 repaint_screen ();
443 return;
445 break;
448 panelize_done ();
451 /* --------------------------------------------------------------------------------------------- */
453 void
454 load_panelize (void)
456 gchar **profile_keys, **keys;
457 gsize len;
458 GIConv conv;
459 GString *buffer;
461 conv = str_crt_conv_from ("UTF-8");
463 profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section, &len);
465 add2panelize (g_strdup (_("Other command")), g_strdup (""));
467 if (!profile_keys || *profile_keys == NULL)
469 add2panelize (g_strdup (_("Find rejects after patching")),
470 g_strdup ("find . -name \\*.rej -print"));
471 add2panelize (g_strdup (_("Find *.orig after patching")),
472 g_strdup ("find . -name \\*.orig -print"));
473 add2panelize (g_strdup (_("Find SUID and SGID programs")),
474 g_strdup
475 ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
476 return;
479 while (*profile_keys)
482 if (utf8_display || conv == INVALID_CONV)
484 buffer = g_string_new (*profile_keys);
486 else
488 buffer = g_string_new ("");
489 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
491 g_string_free (buffer, TRUE);
492 buffer = g_string_new (*profile_keys);
496 add2panelize (g_string_free (buffer, FALSE),
497 mc_config_get_string (mc_main_config, panelize_section, *profile_keys, ""));
498 profile_keys++;
500 g_strfreev (keys);
501 str_close_conv (conv);
504 /* --------------------------------------------------------------------------------------------- */
506 void
507 save_panelize (void)
509 struct panelize *current = panelize;
511 mc_config_del_group (mc_main_config, panelize_section);
512 for (; current; current = current->next)
514 if (strcmp (current->label, _("Other command")))
515 mc_config_set_string (mc_main_config,
516 panelize_section, current->label, current->command);
518 mc_config_save_file (mc_main_config, NULL);
521 /* --------------------------------------------------------------------------------------------- */
523 void
524 done_panelize (void)
526 struct panelize *current = panelize;
527 struct panelize *next;
529 for (; current; current = next)
531 next = current->next;
532 g_free (current->label);
533 g_free (current->command);
534 g_free (current);
538 /* --------------------------------------------------------------------------------------------- */