Moved dir $(srcdir)/syntax into $(srcdir)/misc/syntax
[midnight-commander.git] / src / panelize.c
blobf2779c4df4257359a96bb1f972dc1c86b639351d
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 "global.h"
38 #include "../src/skin/skin.h"
40 #include "../../lib/vfs/mc-vfs/vfs.h"
42 #include "dialog.h"
43 #include "widget.h"
44 #include "wtools.h" /* For common_dialog_repaint() */
45 #include "setup.h" /* For profile_bname */
46 #include "../src/mcconfig/mcconfig.h" /* Load/save directories panelize */
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"
53 #include "strutil.h"
55 #define UX 5
56 #define UY 2
58 #define BX 5
59 #define BY 18
61 #define BUTTONS 4
62 #define LABELS 3
63 #define B_ADD B_USER
64 #define B_REMOVE (B_USER + 1)
66 static WListbox *l_panelize;
67 static Dlg_head *panelize_dlg;
68 static int last_listitem;
69 static WInput *pname;
71 static struct {
72 int ret_cmd, flags, y, x;
73 const char *text;
74 } panelize_but [BUTTONS] = {
75 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
76 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
77 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
78 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") },
81 static const char *panelize_section = "Panelize";
82 static void do_external_panelize (char *command);
84 /* Directory panelize */
85 static struct panelize {
86 char *command;
87 char *label;
88 struct panelize *next;
89 } *panelize = NULL;
91 static void
92 update_command (void)
94 if (l_panelize->pos != last_listitem) {
95 last_listitem = l_panelize->pos;
96 assign_text (pname,
97 ((struct panelize *) l_panelize->current->data)->command);
98 pname->point = 0;
99 update_input (pname, 1);
103 static cb_ret_t
104 panelize_callback (Dlg_head *h, Widget *sender,
105 dlg_msg_t msg, int parm, void *data)
107 switch (msg) {
108 case DLG_INIT:
109 case DLG_POST_KEY:
110 tty_setcolor (MENU_ENTRY_COLOR);
111 update_command ();
112 return MSG_HANDLED;
114 case DLG_DRAW:
115 common_dialog_repaint (h);
116 tty_setcolor (COLOR_NORMAL);
117 draw_box (h, UY, UX, h->lines - 10, h->cols - 10);
118 return MSG_HANDLED;
120 default:
121 return default_dlg_callback (h, sender, msg, parm, data);
125 static void
126 init_panelize (void)
128 int i, panelize_cols = COLS - 6;
129 struct panelize *current = panelize;
131 #ifdef ENABLE_NLS
132 static int i18n_flag = 0;
133 static int maxlen = 0;
135 if (!i18n_flag) {
136 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
137 while (i--) {
138 panelize_but[i].text = _(panelize_but[i].text);
139 maxlen += str_term_width1 (panelize_but[i].text) + 5;
141 maxlen += 10;
143 i18n_flag = 1;
145 panelize_cols = max (panelize_cols, maxlen);
147 panelize_but[2].x =
148 panelize_but[3].x + str_term_width1 (panelize_but[3].text) + 7;
149 panelize_but[1].x =
150 panelize_but[2].x + str_term_width1 (panelize_but[2].text) + 5;
151 panelize_but[0].x =
152 panelize_cols - str_term_width1 (panelize_but[0].text) - 8 - BX;
154 #endif /* ENABLE_NLS */
156 last_listitem = 0;
158 do_refresh ();
160 panelize_dlg =
161 create_dlg (0, 0, 22, panelize_cols, dialog_colors,
162 panelize_callback, "[External panelize]",
163 _("External panelize"), DLG_CENTER | DLG_REVERSE);
165 for (i = 0; i < BUTTONS; i++)
166 add_widget (panelize_dlg,
167 button_new (BY + panelize_but[i].y,
168 BX + panelize_but[i].x,
169 panelize_but[i].ret_cmd,
170 panelize_but[i].flags,
171 panelize_but[i].text, 0));
173 pname =
174 input_new (UY + 14, UX, INPUT_COLOR, panelize_dlg->cols - 10, "",
175 "in", INPUT_COMPLETE_DEFAULT);
176 add_widget (panelize_dlg, pname);
178 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
180 /* get new listbox */
181 l_panelize =
182 listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, NULL);
184 while (current) {
185 listbox_add_item (l_panelize, 0, 0, current->label, current);
186 current = current->next;
189 /* add listbox to the dialogs */
190 add_widget (panelize_dlg, l_panelize);
192 listbox_select_entry (l_panelize,
193 listbox_search_text (l_panelize,
194 _("Other command")));
197 static void panelize_done (void)
199 destroy_dlg (panelize_dlg);
200 repaint_screen ();
203 static void add2panelize (char *label, char *command)
205 struct panelize *current, *old;
207 old = NULL;
208 current = panelize;
209 while (current && strcmp (current->label, label) <= 0){
210 old = current;
211 current = current->next;
214 if (old == NULL){
215 panelize = g_new (struct panelize, 1);
216 panelize->label = label;
217 panelize->command = command;
218 panelize->next = current;
219 } else {
220 struct panelize *new;
221 new = g_new (struct panelize, 1);
222 new->label = label;
223 new->command = command;
224 old->next = new;
225 new->next = current;
229 static void
230 add2panelize_cmd (void)
232 char *label;
234 if (pname->buffer && (*pname->buffer)) {
235 label = input_dialog (_(" Add to external panelize "),
236 _(" Enter command label: "),
237 MC_HISTORY_FM_PANELIZE_ADD,
238 "");
239 if (!label)
240 return;
241 if (!*label) {
242 g_free (label);
243 return;
246 add2panelize (label, g_strdup (pname->buffer));
250 static void remove_from_panelize (struct panelize *entry)
252 if (strcmp (entry->label, _("Other command")) != 0) {
253 if (entry == panelize) {
254 panelize = panelize->next;
255 } else {
256 struct panelize *current = panelize;
257 while (current && current->next != entry)
258 current = current->next;
259 if (current) {
260 current->next = entry->next;
264 g_free (entry->label);
265 g_free (entry->command);
266 g_free (entry);
270 void
271 external_panelize (void)
273 char *target = NULL;
275 if (!vfs_current_is_local ()){
276 message (D_ERROR, MSG_ERROR,
277 _(" Cannot run external panelize in a non-local directory "));
278 return;
281 init_panelize ();
283 /* display file info */
284 tty_setcolor (SELECTED_COLOR);
286 run_dlg (panelize_dlg);
288 switch (panelize_dlg->ret_value) {
289 case B_CANCEL:
290 break;
292 case B_ADD:
293 add2panelize_cmd ();
294 break;
296 case B_REMOVE:
297 remove_from_panelize (l_panelize->current->data);
298 break;
300 case B_ENTER:
301 target = pname->buffer;
302 if (target != NULL && *target) {
303 char *cmd = g_strdup (target);
304 destroy_dlg (panelize_dlg);
305 do_external_panelize (cmd);
306 g_free (cmd);
307 repaint_screen ();
308 return;
310 break;
313 panelize_done ();
316 void load_panelize (void)
318 gchar **profile_keys, **keys;
319 gsize len;
320 GIConv conv;
321 GString *buffer;
323 conv = str_crt_conv_from ("UTF-8");
325 profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section,&len);
327 add2panelize (g_strdup (_("Other command")), g_strdup (""));
329 if (!profile_keys || *profile_keys == NULL){
330 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
331 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
332 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
333 return;
336 while (*profile_keys){
338 if (utf8_display || conv == INVALID_CONV){
339 buffer = g_string_new (*profile_keys);
340 } else {
341 buffer = g_string_new ("");
342 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
344 g_string_free(buffer, TRUE);
345 buffer = g_string_new (*profile_keys);
349 add2panelize (
350 g_string_free(buffer, FALSE),
351 mc_config_get_string(mc_main_config,panelize_section,*profile_keys,"")
353 profile_keys++;
355 g_strfreev(keys);
356 str_close_conv (conv);
359 void save_panelize (void)
361 struct panelize *current = panelize;
363 mc_config_del_group (mc_main_config, panelize_section);
364 for (;current; current = current->next){
365 if (strcmp (current->label, _("Other command")))
366 mc_config_set_string(mc_main_config,
367 panelize_section,
368 current->label,
369 current->command);
371 mc_config_save_file (mc_main_config, NULL);
374 void done_panelize (void)
376 struct panelize *current = panelize;
377 struct panelize *next;
379 for (; current; current = next){
380 next = current->next;
381 g_free (current->label);
382 g_free (current->command);
383 g_free (current);
387 static void do_external_panelize (char *command)
389 int status, link_to_dir, stale_link;
390 int next_free = 0;
391 struct stat st;
392 dir_list *list = &current_panel->dir;
393 char line [MC_MAXPATHLEN];
394 char *name;
395 FILE *external;
397 open_error_pipe ();
398 external = popen (command, "r");
399 if (!external){
400 close_error_pipe (D_ERROR, _("Cannot invoke command."));
401 return;
403 /* Clear the counters and the directory list */
404 panel_clean_dir (current_panel);
406 while (1) {
407 clearerr(external);
408 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
409 if (ferror(external) && errno == EINTR)
410 continue;
411 else
412 break;
414 if (line[strlen(line)-1] == '\n')
415 line[strlen(line)-1] = 0;
416 if (strlen(line) < 1)
417 continue;
418 if (line [0] == '.' && line[1] == PATH_SEP)
419 name = line + 2;
420 else
421 name = line;
422 status = handle_path (list, name, &st, next_free, &link_to_dir,
423 &stale_link);
424 if (status == 0)
425 continue;
426 if (status == -1)
427 break;
428 list->list [next_free].fnamelen = strlen (name);
429 list->list [next_free].fname = g_strdup (name);
430 file_mark (current_panel, next_free, 0);
431 list->list [next_free].f.link_to_dir = link_to_dir;
432 list->list [next_free].f.stale_link = stale_link;
433 list->list [next_free].f.dir_size_computed = 0;
434 list->list [next_free].st = st;
435 list->list[next_free].sort_key = NULL;
436 list->list[next_free].second_sort_key = NULL;
437 next_free++;
438 if (!(next_free & 32))
439 rotate_dash ();
442 current_panel->is_panelized = 1;
443 if (next_free){
444 current_panel->count = next_free;
445 if (list->list [0].fname [0] == PATH_SEP){
446 strcpy (current_panel->cwd, PATH_SEP_STR);
447 chdir (PATH_SEP_STR);
449 } else {
450 current_panel->count = set_zero_dir (list) ? 1 : 0;
452 if (pclose (external) < 0)
453 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
454 close_error_pipe (D_NORMAL, NULL);
455 try_to_select (current_panel, NULL);
456 panel_re_sort (current_panel);