Not only comment it out but removing it
[midnight-commander.git] / src / panelize.c
blob3deb0210a86ac8033c89f983620fc60361aa676c
1 /* External panelize
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007 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 #include <config.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
33 #include "global.h"
34 #include "tty.h" /* attrset() */
35 #include "win.h"
36 #include "color.h"
37 #include "dialog.h"
38 #include "widget.h"
39 #include "wtools.h" /* For common_dialog_repaint() */
40 #include "setup.h" /* For profile_bname */
41 #include "profile.h" /* Load/save directories panelize */
42 #include "dir.h"
43 #include "panel.h" /* current_panel */
44 #include "main.h" /* repaint_screen */
45 #include "panelize.h"
46 #include "history.h"
48 #define UX 5
49 #define UY 2
51 #define BX 5
52 #define BY 18
54 #define BUTTONS 4
55 #define LABELS 3
56 #define B_ADD B_USER
57 #define B_REMOVE (B_USER + 1)
59 static WListbox *l_panelize;
60 static Dlg_head *panelize_dlg;
61 static int last_listitem;
62 static WInput *pname;
64 static struct {
65 int ret_cmd, flags, y, x;
66 const char *text;
67 } panelize_but [BUTTONS] = {
68 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
69 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
70 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
71 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") },
74 static const char *panelize_section = "Panelize";
75 static void do_external_panelize (char *command);
77 /* Directory panelize */
78 static struct panelize {
79 char *command;
80 char *label;
81 struct panelize *next;
82 } *panelize = NULL;
84 static void
85 update_command (void)
87 if (l_panelize->pos != last_listitem) {
88 last_listitem = l_panelize->pos;
89 assign_text (pname,
90 ((struct panelize *) l_panelize->current->data)->command);
91 pname->point = 0;
92 update_input (pname, 1);
96 static cb_ret_t
97 panelize_callback (Dlg_head *h, dlg_msg_t msg, int parm)
99 switch (msg) {
100 case DLG_DRAW:
101 common_dialog_repaint (h);
102 attrset (COLOR_NORMAL);
103 draw_box (h, UY, UX, h->lines - 10, h->cols - 10);
104 return MSG_HANDLED;
106 case DLG_POST_KEY:
107 /* fall */
108 case DLG_INIT:
109 attrset (MENU_ENTRY_COLOR);
110 update_command ();
111 return MSG_HANDLED;
113 default:
114 return default_dlg_callback (h, msg, parm);
118 static void
119 init_panelize (void)
121 int i, panelize_cols = COLS - 6;
122 struct panelize *current = panelize;
124 #ifdef ENABLE_NLS
125 static int i18n_flag = 0;
126 static int maxlen = 0;
128 if (!i18n_flag) {
129 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
130 while (i--) {
131 panelize_but[i].text = _(panelize_but[i].text);
132 maxlen += strlen (panelize_but[i].text) + 5;
134 maxlen += 10;
136 i18n_flag = 1;
138 panelize_cols = max (panelize_cols, maxlen);
140 panelize_but[2].x =
141 panelize_but[3].x + strlen (panelize_but[3].text) + 7;
142 panelize_but[1].x =
143 panelize_but[2].x + strlen (panelize_but[2].text) + 5;
144 panelize_but[0].x =
145 panelize_cols - strlen (panelize_but[0].text) - 8 - BX;
147 #endif /* ENABLE_NLS */
149 last_listitem = 0;
151 do_refresh ();
153 panelize_dlg =
154 create_dlg (0, 0, 22, panelize_cols, dialog_colors,
155 panelize_callback, "[External panelize]",
156 _("External panelize"), DLG_CENTER | DLG_REVERSE);
158 for (i = 0; i < BUTTONS; i++)
159 add_widget (panelize_dlg,
160 button_new (BY + panelize_but[i].y,
161 BX + panelize_but[i].x,
162 panelize_but[i].ret_cmd,
163 panelize_but[i].flags,
164 panelize_but[i].text, 0));
166 pname =
167 input_new (UY + 14, UX, INPUT_COLOR, panelize_dlg->cols - 10, "",
168 "in");
169 add_widget (panelize_dlg, pname);
171 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
173 /* get new listbox */
174 l_panelize =
175 listbox_new (UY + 1, UX + 1, panelize_dlg->cols - 12, 10, NULL);
177 while (current) {
178 listbox_add_item (l_panelize, 0, 0, current->label, current);
179 current = current->next;
182 /* add listbox to the dialogs */
183 add_widget (panelize_dlg, l_panelize);
185 listbox_select_entry (l_panelize,
186 listbox_search_text (l_panelize,
187 _("Other command")));
190 static void panelize_done (void)
192 destroy_dlg (panelize_dlg);
193 repaint_screen ();
196 static void add2panelize (char *label, char *command)
198 struct panelize *current, *old;
200 old = NULL;
201 current = panelize;
202 while (current && strcmp (current->label, label) <= 0){
203 old = current;
204 current = current->next;
207 if (old == NULL){
208 panelize = g_new (struct panelize, 1);
209 panelize->label = label;
210 panelize->command = command;
211 panelize->next = current;
212 } else {
213 struct panelize *new;
214 new = g_new (struct panelize, 1);
215 new->label = label;
216 new->command = command;
217 old->next = new;
218 new->next = current;
222 static void
223 add2panelize_cmd (void)
225 char *label;
227 if (pname->buffer && (*pname->buffer)) {
228 label = input_dialog (_(" Add to external panelize "),
229 _(" Enter command label: "),
230 MC_HISTORY_FM_PANELIZE_ADD,
231 "");
232 if (!label)
233 return;
234 if (!*label) {
235 g_free (label);
236 return;
239 add2panelize (label, g_strdup (pname->buffer));
243 static void remove_from_panelize (struct panelize *entry)
245 if (strcmp (entry->label, _("Other command")) != 0) {
246 if (entry == panelize) {
247 panelize = panelize->next;
248 } else {
249 struct panelize *current = panelize;
250 while (current && current->next != entry)
251 current = current->next;
252 if (current) {
253 current->next = entry->next;
257 g_free (entry->label);
258 g_free (entry->command);
259 g_free (entry);
263 void
264 external_panelize (void)
266 char *target = NULL;
268 if (!vfs_current_is_local ()){
269 message (1, MSG_ERROR,
270 _(" Cannot run external panelize in a non-local directory "));
271 return;
274 init_panelize ();
276 /* display file info */
277 attrset (SELECTED_COLOR);
279 run_dlg (panelize_dlg);
281 switch (panelize_dlg->ret_value) {
282 case B_CANCEL:
283 break;
285 case B_ADD:
286 add2panelize_cmd ();
287 break;
289 case B_REMOVE:
290 remove_from_panelize (l_panelize->current->data);
291 break;
293 case B_ENTER:
294 target = pname->buffer;
295 if (target != NULL && *target) {
296 char *cmd = g_strdup (target);
297 destroy_dlg (panelize_dlg);
298 do_external_panelize (cmd);
299 g_free (cmd);
300 repaint_screen ();
301 return;
303 break;
306 panelize_done ();
309 void load_panelize (void)
311 void *profile_keys;
312 char *key, *value;
314 profile_keys = profile_init_iterator (panelize_section, profile_name);
316 add2panelize (g_strdup (_("Other command")), g_strdup (""));
318 if (!profile_keys){
319 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
320 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
321 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
322 return;
325 while (profile_keys){
326 profile_keys = profile_iterator_next (profile_keys, &key, &value);
327 add2panelize (g_strdup (key), g_strdup (value));
331 void save_panelize (void)
333 struct panelize *current = panelize;
335 profile_clean_section (panelize_section, profile_name);
336 for (;current; current = current->next){
337 if (strcmp (current->label, _("Other command")))
338 WritePrivateProfileString (panelize_section,
339 current->label,
340 current->command,
341 profile_name);
343 sync_profiles ();
346 void done_panelize (void)
348 struct panelize *current = panelize;
349 struct panelize *next;
351 for (; current; current = next){
352 next = current->next;
353 g_free (current->label);
354 g_free (current->command);
355 g_free (current);
359 static void do_external_panelize (char *command)
361 int status, link_to_dir, stale_link;
362 int next_free = 0;
363 struct stat st;
364 dir_list *list = &current_panel->dir;
365 char line [MC_MAXPATHLEN];
366 char *name;
367 FILE *external;
369 open_error_pipe ();
370 external = popen (command, "r");
371 if (!external){
372 close_error_pipe (1, _("Cannot invoke command."));
373 return;
375 /* Clear the counters and the directory list */
376 panel_clean_dir (current_panel);
378 while (1) {
379 clearerr(external);
380 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
381 if (ferror(external) && errno == EINTR)
382 continue;
383 else
384 break;
386 if (line[strlen(line)-1] == '\n')
387 line[strlen(line)-1] = 0;
388 if (strlen(line) < 1)
389 continue;
390 if (line [0] == '.' && line[1] == PATH_SEP)
391 name = line + 2;
392 else
393 name = line;
394 status = handle_path (list, name, &st, next_free, &link_to_dir,
395 &stale_link);
396 if (status == 0)
397 continue;
398 if (status == -1)
399 break;
400 list->list [next_free].fnamelen = strlen (name);
401 list->list [next_free].fname = g_strdup (name);
402 file_mark (current_panel, next_free, 0);
403 list->list [next_free].f.link_to_dir = link_to_dir;
404 list->list [next_free].f.stale_link = stale_link;
405 list->list [next_free].f.dir_size_computed = 0;
406 list->list [next_free].st = st;
407 next_free++;
408 if (!(next_free & 32))
409 rotate_dash ();
412 current_panel->is_panelized = 1;
413 if (next_free){
414 current_panel->count = next_free;
415 if (list->list [0].fname [0] == PATH_SEP){
416 strcpy (current_panel->cwd, PATH_SEP_STR);
417 chdir (PATH_SEP_STR);
419 } else {
420 current_panel->count = set_zero_dir (list);
422 if (pclose (external) < 0)
423 message (0, _("External panelize"), _("Pipe close failed"));
424 close_error_pipe (0, 0);
425 try_to_select (current_panel, NULL);
426 panel_re_sort (current_panel);