Forgot to remove some more .s strings and do a rename in order to prevent compiler...
[midnight-commander.git] / src / panelize.c
blobd79d2b9a5ed52af3cb60cba535a2a240a5ba1088
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>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
32 #include <mhl/memory.h>
33 #include <mhl/string.h>
35 #include "global.h"
36 #include "tty.h" /* attrset() */
37 #include "win.h"
38 #include "color.h"
39 #include "dialog.h"
40 #include "widget.h"
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" /* current_panel */
46 #include "main.h" /* repaint_screen */
47 #include "panelize.h"
48 #include "history.h"
50 #define UX 5
51 #define UY 2
53 #define BX 5
54 #define BY 18
56 #define BUTTONS 4
57 #define LABELS 3
58 #define B_ADD B_USER
59 #define B_REMOVE (B_USER + 1)
61 static WListbox *l_panelize;
62 static Dlg_head *panelize_dlg;
63 static int last_listitem;
64 static WInput *pname;
66 static struct {
67 int ret_cmd, flags, y, x;
68 const char *text;
69 } panelize_but [BUTTONS] = {
70 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
71 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
72 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
73 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") },
76 static const char *panelize_section = "Panelize";
77 static void do_external_panelize (char *command);
79 /* Directory panelize */
80 static struct panelize {
81 char *command;
82 char *label;
83 struct panelize *next;
84 } *panelize = NULL;
86 static void
87 update_command (void)
89 if (l_panelize->pos != last_listitem) {
90 last_listitem = l_panelize->pos;
91 assign_text (pname,
92 ((struct panelize *) l_panelize->current->data)->command);
93 pname->point = 0;
94 update_input (pname, 1);
98 static cb_ret_t
99 panelize_callback (Dlg_head *h, dlg_msg_t msg, int parm)
101 switch (msg) {
102 case DLG_DRAW:
103 common_dialog_repaint (h);
104 attrset (COLOR_NORMAL);
105 draw_box (h, UY, UX, h->lines - 10, h->cols - 10);
106 return MSG_HANDLED;
108 case DLG_POST_KEY:
109 /* fall */
110 case DLG_INIT:
111 attrset (MENU_ENTRY_COLOR);
112 update_command ();
113 return MSG_HANDLED;
115 default:
116 return default_dlg_callback (h, msg, parm);
120 static void
121 init_panelize (void)
123 int i, panelize_cols = COLS - 6;
124 struct panelize *current = panelize;
126 #ifdef ENABLE_NLS
127 static int i18n_flag = 0;
128 static int maxlen = 0;
130 if (!i18n_flag) {
131 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
132 while (i--) {
133 panelize_but[i].text = _(panelize_but[i].text);
134 maxlen += strlen (panelize_but[i].text) + 5;
136 maxlen += 10;
138 i18n_flag = 1;
140 panelize_cols = max (panelize_cols, maxlen);
142 panelize_but[2].x =
143 panelize_but[3].x + strlen (panelize_but[3].text) + 7;
144 panelize_but[1].x =
145 panelize_but[2].x + strlen (panelize_but[2].text) + 5;
146 panelize_but[0].x =
147 panelize_cols - strlen (panelize_but[0].text) - 8 - BX;
149 #endif /* ENABLE_NLS */
151 last_listitem = 0;
153 do_refresh ();
155 panelize_dlg =
156 create_dlg (0, 0, 22, panelize_cols, dialog_colors,
157 panelize_callback, "[External panelize]",
158 _("External panelize"), DLG_CENTER | DLG_REVERSE);
160 for (i = 0; i < BUTTONS; i++)
161 add_widget (panelize_dlg,
162 button_new (BY + panelize_but[i].y,
163 BX + panelize_but[i].x,
164 panelize_but[i].ret_cmd,
165 panelize_but[i].flags,
166 panelize_but[i].text, 0));
168 pname =
169 input_new (UY + 14, UX, INPUT_COLOR, panelize_dlg->cols - 10, "",
170 "in", INPUT_COMPLETE_DEFAULT);
171 add_widget (panelize_dlg, pname);
173 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
175 /* get new listbox */
176 l_panelize =
177 listbox_new (UY + 1, UX + 1, panelize_dlg->cols - 12, 10, NULL);
179 while (current) {
180 listbox_add_item (l_panelize, 0, 0, current->label, current);
181 current = current->next;
184 /* add listbox to the dialogs */
185 add_widget (panelize_dlg, l_panelize);
187 listbox_select_entry (l_panelize,
188 listbox_search_text (l_panelize,
189 _("Other command")));
192 static void panelize_done (void)
194 destroy_dlg (panelize_dlg);
195 repaint_screen ();
198 static void add2panelize (char *label, char *command)
200 struct panelize *current, *old;
202 old = NULL;
203 current = panelize;
204 while (current && strcmp (current->label, label) <= 0){
205 old = current;
206 current = current->next;
209 if (old == NULL){
210 panelize = g_new (struct panelize, 1);
211 panelize->label = label;
212 panelize->command = command;
213 panelize->next = current;
214 } else {
215 struct panelize *new;
216 new = g_new (struct panelize, 1);
217 new->label = label;
218 new->command = command;
219 old->next = new;
220 new->next = current;
224 static void
225 add2panelize_cmd (void)
227 char *label;
229 if (pname->buffer && (*pname->buffer)) {
230 label = input_dialog (_(" Add to external panelize "),
231 _(" Enter command label: "),
232 MC_HISTORY_FM_PANELIZE_ADD,
233 "");
234 if (!label)
235 return;
236 if (!*label) {
237 g_free (label);
238 return;
241 add2panelize (label, g_strdup (pname->buffer));
245 static void remove_from_panelize (struct panelize *entry)
247 if (strcmp (entry->label, _("Other command")) != 0) {
248 if (entry == panelize) {
249 panelize = panelize->next;
250 } else {
251 struct panelize *current = panelize;
252 while (current && current->next != entry)
253 current = current->next;
254 if (current) {
255 current->next = entry->next;
259 g_free (entry->label);
260 g_free (entry->command);
261 g_free (entry);
265 void
266 external_panelize (void)
268 char *target = NULL;
270 if (!vfs_current_is_local ()){
271 message (D_ERROR, MSG_ERROR,
272 _(" Cannot run external panelize in a non-local directory "));
273 return;
276 init_panelize ();
278 /* display file info */
279 attrset (SELECTED_COLOR);
281 run_dlg (panelize_dlg);
283 switch (panelize_dlg->ret_value) {
284 case B_CANCEL:
285 break;
287 case B_ADD:
288 add2panelize_cmd ();
289 break;
291 case B_REMOVE:
292 remove_from_panelize (l_panelize->current->data);
293 break;
295 case B_ENTER:
296 target = pname->buffer;
297 if (target != NULL && *target) {
298 char *cmd = g_strdup (target);
299 destroy_dlg (panelize_dlg);
300 do_external_panelize (cmd);
301 g_free (cmd);
302 repaint_screen ();
303 return;
305 break;
308 panelize_done ();
311 void load_panelize (void)
313 void *profile_keys;
314 char *key, *value;
316 profile_keys = profile_init_iterator (panelize_section, profile_name);
318 add2panelize (g_strdup (_("Other command")), g_strdup (""));
320 if (!profile_keys){
321 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
322 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
323 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
324 return;
327 while (profile_keys){
328 profile_keys = profile_iterator_next (profile_keys, &key, &value);
329 add2panelize (g_strdup (key), g_strdup (value));
333 void save_panelize (void)
335 struct panelize *current = panelize;
337 profile_clean_section (panelize_section, profile_name);
338 for (;current; current = current->next){
339 if (strcmp (current->label, _("Other command")))
340 WritePrivateProfileString (panelize_section,
341 current->label,
342 current->command,
343 profile_name);
345 sync_profiles ();
348 void done_panelize (void)
350 struct panelize *current = panelize;
351 struct panelize *next;
353 for (; current; current = next){
354 next = current->next;
355 g_free (current->label);
356 g_free (current->command);
357 g_free (current);
361 static void do_external_panelize (char *command)
363 int status, link_to_dir, stale_link;
364 int next_free = 0;
365 struct stat st;
366 dir_list *list = &current_panel->dir;
367 char line [MC_MAXPATHLEN];
368 char *name;
369 FILE *external;
371 open_error_pipe ();
372 external = popen (command, "r");
373 if (!external){
374 close_error_pipe (D_ERROR, _("Cannot invoke command."));
375 return;
377 /* Clear the counters and the directory list */
378 panel_clean_dir (current_panel);
380 while (1) {
381 clearerr(external);
382 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
383 if (ferror(external) && errno == EINTR)
384 continue;
385 else
386 break;
388 if (line[strlen(line)-1] == '\n')
389 line[strlen(line)-1] = 0;
390 if (strlen(line) < 1)
391 continue;
392 if (line [0] == '.' && line[1] == PATH_SEP)
393 name = line + 2;
394 else
395 name = line;
396 status = handle_path (list, name, &st, next_free, &link_to_dir,
397 &stale_link);
398 if (status == 0)
399 continue;
400 if (status == -1)
401 break;
402 list->list [next_free].fnamelen = strlen (name);
403 list->list [next_free].fname = g_strdup (name);
404 file_mark (current_panel, next_free, 0);
405 list->list [next_free].f.link_to_dir = link_to_dir;
406 list->list [next_free].f.stale_link = stale_link;
407 list->list [next_free].f.dir_size_computed = 0;
408 list->list [next_free].st = st;
409 next_free++;
410 if (!(next_free & 32))
411 rotate_dash ();
414 current_panel->is_panelized = 1;
415 if (next_free){
416 current_panel->count = next_free;
417 if (list->list [0].fname [0] == PATH_SEP){
418 strcpy (current_panel->cwd, PATH_SEP_STR);
419 chdir (PATH_SEP_STR);
421 } else {
422 current_panel->count = set_zero_dir (list);
424 if (pclose (external) < 0)
425 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
426 close_error_pipe (D_NORMAL, NULL);
427 try_to_select (current_panel, NULL);
428 panel_re_sort (current_panel);