Last fuzzy trimmed...
[midnight-commander.git] / src / panelize.c
blob0300e60348f0ba8e85c42e0b8d287af025d6c14e
1 /* External panelize
2 Copyright (C) 1995 The Free Software Foundation
4 Written by: 1995 Janne Kukonlehto
5 1995 Jakub Jelinek
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <config.h>
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26 #include <string.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
32 /* Needed for the extern declarations of integer parameters */
33 #define DIR_H_INCLUDE_HANDLE_DIRENT
34 #include "global.h"
35 #include "tty.h"
36 #include "win.h"
37 #include "color.h"
38 #include "dlg.h"
39 #include "widget.h"
40 #include "dialog.h" /* For do_refresh() */
41 #include "setup.h" /* For profile_bname */
42 #include "profile.h" /* Load/save directories panelize */
43 #include "dir.h"
44 #include "panel.h" /* Needed for the externs */
45 #include "file.h"
46 #include "main.h"
47 #include "../vfs/vfs.h"
48 #include "panelize.h"
50 void do_external_panelize (char *command);
52 #define UX 5
53 #define UY 2
55 #define BX 5
56 #define BY 18
58 #define BUTTONS 4
59 #define LABELS 3
60 #define B_ADD B_USER
61 #define B_REMOVE B_USER + 1
63 static WListbox *l_panelize;
64 static Dlg_head *panelize_dlg;
65 static int last_listitem;
66 static WInput *pname;
68 static struct {
69 int ret_cmd, flags, y, x;
70 char *text;
71 char *tkname;
72 } panelize_but [BUTTONS] = {
73 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"), "c"},
74 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new"), "a"},
75 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove"), "r"},
76 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize"),"l"},
79 static char *panelize_section = "Panelize";
81 /* Directory panelize */
82 static struct panelize {
83 char *command;
84 char *label;
85 struct panelize *next;
86 } *panelize = NULL;
88 static char* panelize_title = N_(" External panelize ");
90 static void
91 panelize_refresh (void)
93 attrset (COLOR_NORMAL);
94 dlg_erase (panelize_dlg);
96 draw_box (panelize_dlg, 1, 2, panelize_dlg->lines-2, panelize_dlg->cols-4);
97 draw_box (panelize_dlg, UY, UX, panelize_dlg->lines-10, panelize_dlg->cols-10);
99 attrset (COLOR_HOT_NORMAL);
100 dlg_move (panelize_dlg, 1, (panelize_dlg->cols - strlen(panelize_title)) / 2);
101 addstr (panelize_title);
104 static void
105 update_command (void)
107 if (l_panelize->pos != last_listitem) {
108 last_listitem = l_panelize->pos;
109 assign_text (pname,
110 ((struct panelize *) l_panelize->current->data)->command);
111 pname->point = 0;
112 update_input (pname, 1);
116 static int
117 panelize_callback (Dlg_head * h, int Par, int Msg)
119 switch (Msg) {
120 case DLG_DRAW:
121 panelize_refresh ();
122 break;
124 case DLG_POST_KEY:
125 /* fall */
126 case DLG_INIT:
127 attrset (MENU_ENTRY_COLOR);
128 update_command ();
129 break;
131 return 0;
134 static int l_call (void *data)
136 return listbox_nothing;
139 static void init_panelize (void)
141 int i, panelize_cols = COLS - 6;
142 struct panelize *current = panelize;
144 #ifdef ENABLE_NLS
145 static int i18n_flag = 0;
146 static int maxlen = 0;
148 if (!i18n_flag)
150 i = sizeof(panelize_but) / sizeof(panelize_but[0]);
151 while (i--)
153 panelize_but [i].text = _(panelize_but [i].text);
154 maxlen += strlen (panelize_but [i].text) + 5;
156 maxlen += 10;
157 panelize_title = _(panelize_title);
159 i18n_flag = 1;
161 panelize_cols = max(panelize_cols, maxlen);
163 panelize_but [2].x = panelize_but [3].x
164 + strlen (panelize_but [3].text) + 7;
165 panelize_but [1].x = panelize_but [2].x
166 + strlen (panelize_but [2].text) + 5;
167 panelize_but [0].x = panelize_cols
168 - strlen (panelize_but[0].text) - 8 - BX;
170 #endif /* ENABLE_NLS */
172 last_listitem = 0;
174 do_refresh ();
176 panelize_dlg = create_dlg (0, 0, 22, panelize_cols, dialog_colors,
177 panelize_callback, "[External panelize]", "panelize",
178 DLG_CENTER);
179 x_set_dialog_title (panelize_dlg, _("External panelize"));
181 #define XTRACT(i) BY+panelize_but[i].y, BX+panelize_but[i].x, panelize_but[i].ret_cmd, panelize_but[i].flags, panelize_but[i].text, 0, 0, panelize_but[i].tkname
183 for (i = 0; i < BUTTONS; i++)
184 add_widget(panelize_dlg, button_new (XTRACT (i)));
186 pname = input_new (UY+14, UX, INPUT_COLOR, panelize_dlg->cols-10, "", "in");
187 add_widget (panelize_dlg, pname);
189 add_widget (panelize_dlg, label_new (UY+13, UX, _("Command"), "label-command"));
191 /* get new listbox */
192 l_panelize = listbox_new (UY + 1, UX + 1, panelize_dlg->cols-12, 10, 0, l_call, "li");
194 while (current){
195 listbox_add_item (l_panelize, 0, 0, current->label, current);
196 current = current->next;
199 /* add listbox to the dialogs */
200 add_widget (panelize_dlg, l_panelize);
202 listbox_select_entry (l_panelize,
203 listbox_search_text (l_panelize, _("Other command")));
206 static void panelize_done (void)
208 destroy_dlg (panelize_dlg);
209 repaint_screen ();
212 static void add2panelize (char *label, char *command)
214 struct panelize *current, *old;
216 old = NULL;
217 current = panelize;
218 while (current && strcmp (current->label, label) <= 0){
219 old = current;
220 current = current->next;
223 if (old == NULL){
224 panelize = g_new (struct panelize, 1);
225 panelize->label = label;
226 panelize->command = command;
227 panelize->next = current;
228 } else {
229 struct panelize *new;
230 new = g_new (struct panelize, 1);
231 new->label = label;
232 new->command = command;
233 old->next = new;
234 new->next = current;
238 void
239 add2panelize_cmd (void)
241 char *label;
243 if (pname->buffer && (*pname->buffer)) {
244 label = input_dialog (_(" Add to external panelize "),
245 _(" Enter command label: "),
246 "");
247 if (!label)
248 return;
249 if (!*label) {
250 g_free (label);
251 return;
254 add2panelize (label, g_strdup (pname->buffer));
258 static void remove_from_panelize (struct panelize *entry)
260 if (strcmp (entry->label, _("Other command")) != 0) {
261 if (entry == panelize) {
262 panelize = panelize->next;
263 } else {
264 struct panelize *current = panelize;
265 while (current && current->next != entry)
266 current = current->next;
267 if (current) {
268 current->next = entry->next;
272 g_free (entry->label);
273 g_free (entry->command);
274 g_free (entry);
278 void
279 external_panelize (void)
281 char *target = NULL;
283 if (!vfs_current_is_local ()){
284 message (1, _(" Oops... "),
285 _(" I can't run external panelize while logged on a non local directory "));
286 return;
289 init_panelize ();
291 /* display file info */
292 attrset (SELECTED_COLOR);
294 run_dlg (panelize_dlg);
296 switch (panelize_dlg->ret_value) {
297 case B_CANCEL:
298 break;
300 case B_ADD:
301 add2panelize_cmd ();
302 break;
304 case B_REMOVE:
305 remove_from_panelize (l_panelize->current->data);
306 break;
308 case B_ENTER:
309 target = pname->buffer;
310 if (target != NULL && *target) {
311 char *cmd = g_strdup (target);
312 destroy_dlg (panelize_dlg);
313 do_external_panelize (cmd);
314 g_free (cmd);
315 repaint_screen ();
316 return;
318 break;
321 panelize_done ();
324 void load_panelize (void)
326 void *profile_keys;
327 char *key, *value;
329 profile_keys = profile_init_iterator (panelize_section, profile_name);
331 add2panelize (g_strdup (_("Other command")), g_strdup (""));
333 if (!profile_keys){
334 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
335 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
336 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
337 return;
340 while (profile_keys){
341 profile_keys = profile_iterator_next (profile_keys, &key, &value);
342 add2panelize (g_strdup (key), g_strdup (value));
346 void save_panelize (void)
348 struct panelize *current = panelize;
350 profile_clean_section (panelize_section, profile_name);
351 for (;current; current = current->next){
352 if (strcmp (current->label, _("Other command")))
353 WritePrivateProfileString (panelize_section,
354 current->label,
355 current->command,
356 profile_name);
358 sync_profiles ();
361 void done_panelize (void)
363 struct panelize *current = panelize;
364 struct panelize *next;
366 for (; current; current = next){
367 next = current->next;
368 g_free (current->label);
369 g_free (current->command);
370 g_free (current);
374 void do_external_panelize (char *command)
376 int status, link_to_dir, stalled_link;
377 int next_free = 0;
378 struct stat buf;
379 dir_list *list = &cpanel->dir;
380 char line [MC_MAXPATHLEN];
381 char *name;
382 FILE *external;
384 open_error_pipe ();
385 external = popen (command, "r");
386 if (!external){
387 close_error_pipe (1, _("Cannot invoke command."));
388 return;
390 /* Clear the counters and the directory list */
391 panel_clean_dir (cpanel);
393 while (1) {
394 clearerr(external);
395 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
396 if (ferror(external) && errno == EINTR)
397 continue;
398 else
399 break;
401 if (line[strlen(line)-1] == '\n')
402 line[strlen(line)-1] = 0;
403 if (strlen(line) < 1)
404 continue;
405 if (line [0] == '.' && line[1] == PATH_SEP)
406 name = line + 2;
407 else
408 name = line;
409 status = handle_path (list, name, &buf, next_free, &link_to_dir,
410 &stalled_link);
411 if (status == 0)
412 continue;
413 if (status == -1)
414 break;
415 list->list [next_free].fnamelen = strlen (name);
416 list->list [next_free].fname = g_strdup (name);
417 file_mark (cpanel, next_free, 0);
418 list->list [next_free].f.link_to_dir = link_to_dir;
419 list->list [next_free].f.stalled_link = stalled_link;
420 list->list [next_free].f.dir_size_computed = 0;
421 list->list [next_free].buf = buf;
422 next_free++;
423 if (!(next_free & 32))
424 rotate_dash ();
427 cpanel->is_panelized = 1;
428 if (next_free){
429 cpanel->count = next_free;
430 if (list->list [0].fname [0] == PATH_SEP){
431 strcpy (cpanel->cwd, PATH_SEP_STR);
432 chdir (PATH_SEP_STR);
434 } else {
435 cpanel->count = set_zero_dir (list);
437 #ifndef SCO_FLAVOR
438 if (pclose (external) < 0)
439 #else /* SCO_FLAVOR */
440 if (WEXITSTATUS(pclose (external)) < 0)
441 #endif /* SCO_FLAVOR */
442 message (0, _("External panelize"), _("Pipe close failed"));
443 close_error_pipe (0, 0);
444 try_to_select (cpanel, NULL);
445 panel_re_sort (cpanel);
446 paint_panel (cpanel);