Just a little correction at the it.po file.
[midnight-commander.git] / src / panelize.c
blob3adf13182c664712deaa13c0af6a14e49af8ea78
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" /* attrset() */
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 "wtools.h" /* For common_dialog_repaint() */
42 #include "setup.h" /* For profile_bname */
43 #include "profile.h" /* Load/save directories panelize */
44 #include "dir.h"
45 #include "panel.h" /* cpanel */
46 #include "main.h" /* repaint_screen */
47 #include "panelize.h"
49 #define UX 5
50 #define UY 2
52 #define BX 5
53 #define BY 18
55 #define BUTTONS 4
56 #define LABELS 3
57 #define B_ADD B_USER
58 #define B_REMOVE B_USER + 1
60 static WListbox *l_panelize;
61 static Dlg_head *panelize_dlg;
62 static int last_listitem;
63 static WInput *pname;
65 static struct {
66 int ret_cmd, flags, y, x;
67 char *text;
68 char *tkname;
69 } panelize_but [BUTTONS] = {
70 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"), "c"},
71 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new"), "a"},
72 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove"), "r"},
73 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize"),"l"},
76 static char *panelize_section = "Panelize";
78 /* Directory panelize */
79 static struct panelize {
80 char *command;
81 char *label;
82 struct panelize *next;
83 } *panelize = NULL;
85 static void
86 update_command (void)
88 if (l_panelize->pos != last_listitem) {
89 last_listitem = l_panelize->pos;
90 assign_text (pname,
91 ((struct panelize *) l_panelize->current->data)->command);
92 pname->point = 0;
93 update_input (pname, 1);
97 static int
98 panelize_callback (Dlg_head * h, int Par, int Msg)
100 switch (Msg) {
101 case DLG_DRAW:
102 common_dialog_repaint (h);
103 attrset (COLOR_NORMAL);
104 draw_box (h, UY, UX, h->lines - 10, h->cols - 10);
105 break;
107 case DLG_POST_KEY:
108 /* fall */
109 case DLG_INIT:
110 attrset (MENU_ENTRY_COLOR);
111 update_command ();
112 break;
114 return 0;
117 static int l_call (void *data)
119 return listbox_nothing;
122 static void init_panelize (void)
124 int i, panelize_cols = COLS - 6;
125 struct panelize *current = panelize;
127 #ifdef ENABLE_NLS
128 static int i18n_flag = 0;
129 static int maxlen = 0;
131 if (!i18n_flag)
133 i = sizeof(panelize_but) / sizeof(panelize_but[0]);
134 while (i--)
136 panelize_but [i].text = _(panelize_but [i].text);
137 maxlen += strlen (panelize_but [i].text) + 5;
139 maxlen += 10;
141 i18n_flag = 1;
143 panelize_cols = max(panelize_cols, maxlen);
145 panelize_but [2].x = panelize_but [3].x
146 + strlen (panelize_but [3].text) + 7;
147 panelize_but [1].x = panelize_but [2].x
148 + strlen (panelize_but [2].text) + 5;
149 panelize_but [0].x = panelize_cols
150 - strlen (panelize_but[0].text) - 8 - BX;
152 #endif /* ENABLE_NLS */
154 last_listitem = 0;
156 do_refresh ();
158 panelize_dlg = create_dlg (0, 0, 22, panelize_cols, dialog_colors,
159 panelize_callback, "[External panelize]",
160 _("External panelize"), DLG_CENTER);
162 #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
164 for (i = 0; i < BUTTONS; i++)
165 add_widget(panelize_dlg, button_new (XTRACT (i)));
167 pname = input_new (UY+14, UX, INPUT_COLOR, panelize_dlg->cols-10, "", "in");
168 add_widget (panelize_dlg, pname);
170 add_widget (panelize_dlg, label_new (UY+13, UX, _("Command"), "label-command"));
172 /* get new listbox */
173 l_panelize = listbox_new (UY + 1, UX + 1, panelize_dlg->cols-12, 10, 0, l_call, "li");
175 while (current){
176 listbox_add_item (l_panelize, 0, 0, current->label, current);
177 current = current->next;
180 /* add listbox to the dialogs */
181 add_widget (panelize_dlg, l_panelize);
183 listbox_select_entry (l_panelize,
184 listbox_search_text (l_panelize, _("Other command")));
187 static void panelize_done (void)
189 destroy_dlg (panelize_dlg);
190 repaint_screen ();
193 static void add2panelize (char *label, char *command)
195 struct panelize *current, *old;
197 old = NULL;
198 current = panelize;
199 while (current && strcmp (current->label, label) <= 0){
200 old = current;
201 current = current->next;
204 if (old == NULL){
205 panelize = g_new (struct panelize, 1);
206 panelize->label = label;
207 panelize->command = command;
208 panelize->next = current;
209 } else {
210 struct panelize *new;
211 new = g_new (struct panelize, 1);
212 new->label = label;
213 new->command = command;
214 old->next = new;
215 new->next = current;
219 void
220 add2panelize_cmd (void)
222 char *label;
224 if (pname->buffer && (*pname->buffer)) {
225 label = input_dialog (_(" Add to external panelize "),
226 _(" Enter command label: "),
227 "");
228 if (!label)
229 return;
230 if (!*label) {
231 g_free (label);
232 return;
235 add2panelize (label, g_strdup (pname->buffer));
239 static void remove_from_panelize (struct panelize *entry)
241 if (strcmp (entry->label, _("Other command")) != 0) {
242 if (entry == panelize) {
243 panelize = panelize->next;
244 } else {
245 struct panelize *current = panelize;
246 while (current && current->next != entry)
247 current = current->next;
248 if (current) {
249 current->next = entry->next;
253 g_free (entry->label);
254 g_free (entry->command);
255 g_free (entry);
259 void
260 external_panelize (void)
262 char *target = NULL;
264 if (!vfs_current_is_local ()){
265 message (1, MSG_ERROR,
266 _(" Cannot run external panelize in a non-local directory "));
267 return;
270 init_panelize ();
272 /* display file info */
273 attrset (SELECTED_COLOR);
275 run_dlg (panelize_dlg);
277 switch (panelize_dlg->ret_value) {
278 case B_CANCEL:
279 break;
281 case B_ADD:
282 add2panelize_cmd ();
283 break;
285 case B_REMOVE:
286 remove_from_panelize (l_panelize->current->data);
287 break;
289 case B_ENTER:
290 target = pname->buffer;
291 if (target != NULL && *target) {
292 char *cmd = g_strdup (target);
293 destroy_dlg (panelize_dlg);
294 do_external_panelize (cmd);
295 g_free (cmd);
296 repaint_screen ();
297 return;
299 break;
302 panelize_done ();
305 void load_panelize (void)
307 void *profile_keys;
308 char *key, *value;
310 profile_keys = profile_init_iterator (panelize_section, profile_name);
312 add2panelize (g_strdup (_("Other command")), g_strdup (""));
314 if (!profile_keys){
315 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
316 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
317 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
318 return;
321 while (profile_keys){
322 profile_keys = profile_iterator_next (profile_keys, &key, &value);
323 add2panelize (g_strdup (key), g_strdup (value));
327 void save_panelize (void)
329 struct panelize *current = panelize;
331 profile_clean_section (panelize_section, profile_name);
332 for (;current; current = current->next){
333 if (strcmp (current->label, _("Other command")))
334 WritePrivateProfileString (panelize_section,
335 current->label,
336 current->command,
337 profile_name);
339 sync_profiles ();
342 void done_panelize (void)
344 struct panelize *current = panelize;
345 struct panelize *next;
347 for (; current; current = next){
348 next = current->next;
349 g_free (current->label);
350 g_free (current->command);
351 g_free (current);
355 void do_external_panelize (char *command)
357 int status, link_to_dir, stale_link;
358 int next_free = 0;
359 struct stat buf;
360 dir_list *list = &cpanel->dir;
361 char line [MC_MAXPATHLEN];
362 char *name;
363 FILE *external;
365 open_error_pipe ();
366 external = popen (command, "r");
367 if (!external){
368 close_error_pipe (1, _("Cannot invoke command."));
369 return;
371 /* Clear the counters and the directory list */
372 panel_clean_dir (cpanel);
374 while (1) {
375 clearerr(external);
376 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
377 if (ferror(external) && errno == EINTR)
378 continue;
379 else
380 break;
382 if (line[strlen(line)-1] == '\n')
383 line[strlen(line)-1] = 0;
384 if (strlen(line) < 1)
385 continue;
386 if (line [0] == '.' && line[1] == PATH_SEP)
387 name = line + 2;
388 else
389 name = line;
390 status = handle_path (list, name, &buf, next_free, &link_to_dir,
391 &stale_link);
392 if (status == 0)
393 continue;
394 if (status == -1)
395 break;
396 list->list [next_free].fnamelen = strlen (name);
397 list->list [next_free].fname = g_strdup (name);
398 file_mark (cpanel, next_free, 0);
399 list->list [next_free].f.link_to_dir = link_to_dir;
400 list->list [next_free].f.stale_link = stale_link;
401 list->list [next_free].f.dir_size_computed = 0;
402 list->list [next_free].buf = buf;
403 next_free++;
404 if (!(next_free & 32))
405 rotate_dash ();
408 cpanel->is_panelized = 1;
409 if (next_free){
410 cpanel->count = next_free;
411 if (list->list [0].fname [0] == PATH_SEP){
412 strcpy (cpanel->cwd, PATH_SEP_STR);
413 chdir (PATH_SEP_STR);
415 } else {
416 cpanel->count = set_zero_dir (list);
418 #ifndef SCO_FLAVOR
419 if (pclose (external) < 0)
420 #else /* SCO_FLAVOR */
421 if (WEXITSTATUS(pclose (external)) < 0)
422 #endif /* SCO_FLAVOR */
423 message (0, _("External panelize"), _("Pipe close failed"));
424 close_error_pipe (0, 0);
425 try_to_select (cpanel, NULL);
426 panel_re_sort (cpanel);
427 paint_panel (cpanel);