Ticket #1791: a nice dark skin created by Lee Bigelow.
[kaloumi3.git] / src / panelize.c
blobff78118e9852399f27e7c3352df1ec5a8fd4155b
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 "lib/global.h"
38 #include "lib/skin.h"
39 #include "lib/vfs/mc-vfs/vfs.h"
40 #include "lib/mcconfig.h" /* Load/save directories panelize */
41 #include "lib/strutil.h"
43 #include "dialog.h"
44 #include "widget.h"
45 #include "wtools.h" /* For common_dialog_repaint() */
46 #include "setup.h" /* For profile_bname */
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"
54 #define UX 5
55 #define UY 2
57 #define BX 5
58 #define BY 18
60 #define BUTTONS 4
61 #define LABELS 3
62 #define B_ADD B_USER
63 #define B_REMOVE (B_USER + 1)
65 static WListbox *l_panelize;
66 static Dlg_head *panelize_dlg;
67 static int last_listitem;
68 static WInput *pname;
70 static struct {
71 int ret_cmd, flags, y, x;
72 const char *text;
73 } panelize_but [BUTTONS] = {
74 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
75 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
76 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
77 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") },
80 static const char *panelize_section = "Panelize";
81 static void do_external_panelize (char *command);
83 /* Directory panelize */
84 static struct panelize {
85 char *command;
86 char *label;
87 struct panelize *next;
88 } *panelize = NULL;
90 static void
91 update_command (void)
93 if (l_panelize->pos != last_listitem) {
94 struct panelize *data = NULL;
96 last_listitem = l_panelize->pos;
97 listbox_get_current (l_panelize, NULL, (void **) &data);
98 assign_text (pname, data->command);
99 pname->point = 0;
100 update_input (pname, 1);
104 static cb_ret_t
105 panelize_callback (Dlg_head *h, Widget *sender,
106 dlg_msg_t msg, int parm, void *data)
108 switch (msg) {
109 case DLG_INIT:
110 case DLG_POST_KEY:
111 tty_setcolor (MENU_ENTRY_COLOR);
112 update_command ();
113 return MSG_HANDLED;
115 case DLG_DRAW:
116 common_dialog_repaint (h);
117 tty_setcolor (COLOR_NORMAL);
118 draw_box (h, UY, UX, h->lines - 10, h->cols - 10);
119 return MSG_HANDLED;
121 default:
122 return default_dlg_callback (h, sender, msg, parm, data);
126 static void
127 init_panelize (void)
129 int i, panelize_cols = COLS - 6;
130 struct panelize *current = panelize;
132 #ifdef ENABLE_NLS
133 static int i18n_flag = 0;
134 static int maxlen = 0;
136 if (!i18n_flag) {
137 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
138 while (i--) {
139 panelize_but[i].text = _(panelize_but[i].text);
140 maxlen += str_term_width1 (panelize_but[i].text) + 5;
142 maxlen += 10;
144 i18n_flag = 1;
146 panelize_cols = max (panelize_cols, maxlen);
148 panelize_but[2].x =
149 panelize_but[3].x + str_term_width1 (panelize_but[3].text) + 7;
150 panelize_but[1].x =
151 panelize_but[2].x + str_term_width1 (panelize_but[2].text) + 5;
152 panelize_but[0].x =
153 panelize_cols - str_term_width1 (panelize_but[0].text) - 8 - BX;
155 #endif /* ENABLE_NLS */
157 last_listitem = 0;
159 do_refresh ();
161 panelize_dlg =
162 create_dlg (0, 0, 22, panelize_cols, dialog_colors,
163 panelize_callback, "[External panelize]",
164 _("External panelize"), DLG_CENTER | DLG_REVERSE);
166 for (i = 0; i < BUTTONS; i++)
167 add_widget (panelize_dlg,
168 button_new (BY + panelize_but[i].y,
169 BX + panelize_but[i].x,
170 panelize_but[i].ret_cmd,
171 panelize_but[i].flags,
172 panelize_but[i].text, 0));
174 pname =
175 input_new (UY + 14, UX, INPUT_COLOR, panelize_dlg->cols - 10, "",
176 "in", INPUT_COMPLETE_DEFAULT);
177 add_widget (panelize_dlg, pname);
179 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
181 /* get new listbox */
182 l_panelize =
183 listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, FALSE, NULL);
185 while (current) {
186 listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current);
187 current = current->next;
190 /* add listbox to the dialogs */
191 add_widget (panelize_dlg, l_panelize);
193 listbox_select_entry (l_panelize,
194 listbox_search_text (l_panelize,
195 _("Other command")));
198 static void panelize_done (void)
200 destroy_dlg (panelize_dlg);
201 repaint_screen ();
204 static void add2panelize (char *label, char *command)
206 struct panelize *current, *old;
208 old = NULL;
209 current = panelize;
210 while (current && strcmp (current->label, label) <= 0){
211 old = current;
212 current = current->next;
215 if (old == NULL){
216 panelize = g_new (struct panelize, 1);
217 panelize->label = label;
218 panelize->command = command;
219 panelize->next = current;
220 } else {
221 struct panelize *new;
222 new = g_new (struct panelize, 1);
223 new->label = label;
224 new->command = command;
225 old->next = new;
226 new->next = current;
230 static void
231 add2panelize_cmd (void)
233 char *label;
235 if (pname->buffer && (*pname->buffer)) {
236 label = input_dialog (_(" Add to external panelize "),
237 _(" Enter command label: "),
238 MC_HISTORY_FM_PANELIZE_ADD,
239 "");
240 if (!label)
241 return;
242 if (!*label) {
243 g_free (label);
244 return;
247 add2panelize (label, g_strdup (pname->buffer));
251 static void remove_from_panelize (struct panelize *entry)
253 if (strcmp (entry->label, _("Other command")) != 0) {
254 if (entry == panelize) {
255 panelize = panelize->next;
256 } else {
257 struct panelize *current = panelize;
258 while (current && current->next != entry)
259 current = current->next;
260 if (current) {
261 current->next = entry->next;
265 g_free (entry->label);
266 g_free (entry->command);
267 g_free (entry);
271 void
272 external_panelize (void)
274 char *target = NULL;
276 if (!vfs_current_is_local ()){
277 message (D_ERROR, MSG_ERROR,
278 _(" Cannot run external panelize in a non-local directory "));
279 return;
282 init_panelize ();
284 /* display file info */
285 tty_setcolor (SELECTED_COLOR);
287 run_dlg (panelize_dlg);
289 switch (panelize_dlg->ret_value) {
290 case B_CANCEL:
291 break;
293 case B_ADD:
294 add2panelize_cmd ();
295 break;
297 case B_REMOVE:
299 struct panelize *entry;
301 listbox_get_current (l_panelize, NULL, (void **) &entry);
302 remove_from_panelize (entry);
303 break;
306 case B_ENTER:
307 target = pname->buffer;
308 if (target != NULL && *target) {
309 char *cmd = g_strdup (target);
310 destroy_dlg (panelize_dlg);
311 do_external_panelize (cmd);
312 g_free (cmd);
313 repaint_screen ();
314 return;
316 break;
319 panelize_done ();
322 void load_panelize (void)
324 gchar **profile_keys, **keys;
325 gsize len;
326 GIConv conv;
327 GString *buffer;
329 conv = str_crt_conv_from ("UTF-8");
331 profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section,&len);
333 add2panelize (g_strdup (_("Other command")), g_strdup (""));
335 if (!profile_keys || *profile_keys == NULL){
336 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
337 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
338 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
339 return;
342 while (*profile_keys){
344 if (utf8_display || conv == INVALID_CONV){
345 buffer = g_string_new (*profile_keys);
346 } else {
347 buffer = g_string_new ("");
348 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
350 g_string_free(buffer, TRUE);
351 buffer = g_string_new (*profile_keys);
355 add2panelize (
356 g_string_free(buffer, FALSE),
357 mc_config_get_string(mc_main_config,panelize_section,*profile_keys,"")
359 profile_keys++;
361 g_strfreev(keys);
362 str_close_conv (conv);
365 void save_panelize (void)
367 struct panelize *current = panelize;
369 mc_config_del_group (mc_main_config, panelize_section);
370 for (;current; current = current->next){
371 if (strcmp (current->label, _("Other command")))
372 mc_config_set_string(mc_main_config,
373 panelize_section,
374 current->label,
375 current->command);
377 mc_config_save_file (mc_main_config, NULL);
380 void done_panelize (void)
382 struct panelize *current = panelize;
383 struct panelize *next;
385 for (; current; current = next){
386 next = current->next;
387 g_free (current->label);
388 g_free (current->command);
389 g_free (current);
393 static void do_external_panelize (char *command)
395 int status, link_to_dir, stale_link;
396 int next_free = 0;
397 struct stat st;
398 dir_list *list = &current_panel->dir;
399 char line [MC_MAXPATHLEN];
400 char *name;
401 FILE *external;
403 open_error_pipe ();
404 external = popen (command, "r");
405 if (!external){
406 close_error_pipe (D_ERROR, _("Cannot invoke command."));
407 return;
409 /* Clear the counters and the directory list */
410 panel_clean_dir (current_panel);
412 while (1) {
413 clearerr(external);
414 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
415 if (ferror(external) && errno == EINTR)
416 continue;
417 else
418 break;
420 if (line[strlen(line)-1] == '\n')
421 line[strlen(line)-1] = 0;
422 if (strlen(line) < 1)
423 continue;
424 if (line [0] == '.' && line[1] == PATH_SEP)
425 name = line + 2;
426 else
427 name = line;
428 status = handle_path (list, name, &st, next_free, &link_to_dir,
429 &stale_link);
430 if (status == 0)
431 continue;
432 if (status == -1)
433 break;
434 list->list [next_free].fnamelen = strlen (name);
435 list->list [next_free].fname = g_strdup (name);
436 file_mark (current_panel, next_free, 0);
437 list->list [next_free].f.link_to_dir = link_to_dir;
438 list->list [next_free].f.stale_link = stale_link;
439 list->list [next_free].f.dir_size_computed = 0;
440 list->list [next_free].st = st;
441 list->list[next_free].sort_key = NULL;
442 list->list[next_free].second_sort_key = NULL;
443 next_free++;
444 if (!(next_free & 32))
445 rotate_dash ();
448 current_panel->is_panelized = 1;
449 if (next_free){
450 current_panel->count = next_free;
451 if (list->list [0].fname [0] == PATH_SEP){
452 strcpy (current_panel->cwd, PATH_SEP_STR);
453 chdir (PATH_SEP_STR);
455 } else {
456 current_panel->count = set_zero_dir (list) ? 1 : 0;
458 if (pclose (external) < 0)
459 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
460 close_error_pipe (D_NORMAL, NULL);
461 try_to_select (current_panel, NULL);
462 panel_re_sort (current_panel);