1
1
mc/src/filemanager/panelize.c

541 строка
15 KiB
C
Исходник Обычный вид История

1998-02-27 04:54:42 +00:00
/* External panelize
Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2007, 2009 Free Software Foundation, Inc.
1998-02-27 04:54:42 +00:00
Written by: 1995 Janne Kukonlehto
1995 Jakub Jelinek
1998-02-27 04:54:42 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1998-02-27 04:54:42 +00:00
*/
/** \file panelize.c
* \brief Source: External panelization module
*/
1998-02-27 04:54:42 +00:00
#include <config.h>
2005-02-08 09:04:03 +00:00
1998-02-27 04:54:42 +00:00
#include <errno.h>
2005-02-08 09:04:03 +00:00
#include <stdio.h>
#include <string.h>
1998-02-27 04:54:42 +00:00
#include <sys/types.h>
#include <sys/stat.h>
2005-02-08 09:04:03 +00:00
#include <unistd.h>
#include "lib/global.h"
#include "lib/skin.h"
#include "lib/vfs/mc-vfs/vfs.h"
#include "lib/mcconfig.h" /* Load/save directories panelize */
#include "lib/strutil.h"
#include "lib/util.h"
#include "lib/widget.h"
#include "src/setup.h" /* For profile_bname */
#include "src/main.h"
#include "src/history.h"
1998-02-27 04:54:42 +00:00
#include "dir.h"
#include "midnight.h" /* current_panel */
#include "layout.h" /* repaint_screen() */
1998-02-27 04:54:42 +00:00
#include "panelize.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
#define UX 5
#define UY 2
1998-02-27 04:54:42 +00:00
#define BX 5
#define BY 18
1998-02-27 04:54:42 +00:00
#define BUTTONS 4
#define LABELS 3
#define B_ADD B_USER
#define B_REMOVE (B_USER + 1)
1998-02-27 04:54:42 +00:00
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
1998-02-27 04:54:42 +00:00
static WListbox *l_panelize;
static Dlg_head *panelize_dlg;
static int last_listitem;
static WInput *pname;
1998-02-27 04:54:42 +00:00
static struct
{
int ret_cmd;
button_flags_t flags;
int y, x;
const char *text;
} panelize_but[BUTTONS] =
{
/* *INDENT-OFF* */
{ B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
{ B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
{ B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
{ B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") }
/* *INDENT-ON* */
1998-02-27 04:54:42 +00:00
};
static const char *panelize_section = "Panelize";
1998-02-27 04:54:42 +00:00
/* Directory panelize */
static struct panelize
{
1998-02-27 04:54:42 +00:00
char *command;
char *label;
struct panelize *next;
} *panelize = NULL;
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static void do_external_panelize (char *command);
/* --------------------------------------------------------------------------------------------- */
Ok, most of the "Elliot Lee confidential bug report" has been dealt with with this commit. This also addresses a bunch of the comments from the status.shtml from DrMike. Miguel. 1998-04-15 Miguel de Icaza <miguel@nuclecu.unam.mx> * screen.c (GT): Assign two spaces for the minimum size of the "type" field for the GNOME edition. This gives some extra space for the icon that gets displayed. * dlg.c (remove_widget): New function: used to remove a widget from an existing Dlg_head; (destroy_widget): Destroy a specific Widget. (add_widgetl): Extended to deal with the fact that a running Dlg_head can become empty. * panelize.c (l_call): Update the input line every time the user selects the entry with the mouse (pretty common in the gnome edition). * hotlist.c (add_new_group_input): Removed an extra field that was causing problems. * find.c (find_parameters): Tree button is gone for gnome until we get the tree function working on gnome. * cmd.c (save_setup_cmd): Per Elliot's suggestion, do not pop up a dialog box to inform the user about the saved setup. 1998-04-15 Miguel de Icaza <miguel@nuclecu.unam.mx> * gcmd.c (gnome_close_panel): Implement the close-the-panel functionality. * gscreen.c (x_panel_destroy): Implement the gnome mode way of destroying the panel. * gview.c (view_status): Add cacheing of the information status, to avoid excessive flicker. It would be better if GtkLabel did not flicker though. (scrollbar_moved): Scroll correctly. We now use view_move_backward and view_move_forward to adjust the scrollbar contents. This displays the scrollbar correctly. * gwidget.c (x_listbox_select_nth): This may be invoked before the widget has been created, work around this problem. * gscreen.c (show_dir): Set the title bar for the window to the current directoy. Reported by both Mike and Elliot. * layout: Updated to the new hotlist dialog box.
1998-04-16 02:45:53 +00:00
static void
update_command (void)
1998-02-27 04:54:42 +00:00
{
if (l_panelize->pos != last_listitem)
{
struct panelize *data = NULL;
last_listitem = l_panelize->pos;
listbox_get_current (l_panelize, NULL, (void **) &data);
input_assign_text (pname, data->command);
pname->point = 0;
input_update (pname, TRUE);
1998-02-27 04:54:42 +00:00
}
}
/* --------------------------------------------------------------------------------------------- */
static cb_ret_t
panelize_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1998-02-27 04:54:42 +00:00
{
switch (msg)
{
case DLG_INIT:
case DLG_POST_KEY:
tty_setcolor (MENU_ENTRY_COLOR);
update_command ();
return MSG_HANDLED;
1998-02-27 04:54:42 +00:00
case DLG_DRAW:
common_dialog_repaint (h);
tty_setcolor (COLOR_NORMAL);
draw_box (h, UY, UX, h->lines - 10, h->cols - 10, TRUE);
return MSG_HANDLED;
1998-02-27 04:54:42 +00:00
default:
return default_dlg_callback (h, sender, msg, parm, data);
1998-02-27 04:54:42 +00:00
}
}
/* --------------------------------------------------------------------------------------------- */
static void
init_panelize (void)
1998-02-27 04:54:42 +00:00
{
int i, panelize_cols = COLS - 6;
struct panelize *current = panelize;
#ifdef ENABLE_NLS
static int i18n_flag = 0;
static int maxlen = 0;
if (!i18n_flag)
{
i = sizeof (panelize_but) / sizeof (panelize_but[0]);
while (i--)
{
panelize_but[i].text = _(panelize_but[i].text);
maxlen += str_term_width1 (panelize_but[i].text) + 5;
}
maxlen += 10;
i18n_flag = 1;
}
panelize_cols = max (panelize_cols, maxlen);
panelize_but[2].x = panelize_but[3].x + str_term_width1 (panelize_but[3].text) + 7;
panelize_but[1].x = panelize_but[2].x + str_term_width1 (panelize_but[2].text) + 5;
panelize_but[0].x = panelize_cols - str_term_width1 (panelize_but[0].text) - 8 - BX;
#endif /* ENABLE_NLS */
1998-02-27 04:54:42 +00:00
last_listitem = 0;
do_refresh ();
1998-02-27 04:54:42 +00:00
panelize_dlg =
create_dlg (TRUE, 0, 0, 22, panelize_cols, dialog_colors,
panelize_callback, "[External panelize]",
_("External panelize"), DLG_CENTER | DLG_REVERSE);
1998-02-27 04:54:42 +00:00
for (i = 0; i < BUTTONS; i++)
add_widget (panelize_dlg,
button_new (BY + panelize_but[i].y,
BX + panelize_but[i].x,
panelize_but[i].ret_cmd,
panelize_but[i].flags, panelize_but[i].text, 0));
pname =
input_new (UY + 14, UX, input_get_default_colors (),
panelize_dlg->cols - 10, "", "in", INPUT_COMPLETE_DEFAULT);
add_widget (panelize_dlg, pname);
1998-02-27 04:54:42 +00:00
add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
1998-02-27 04:54:42 +00:00
/* get new listbox */
l_panelize = listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, FALSE, NULL);
1998-02-27 04:54:42 +00:00
while (current)
{
listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current);
current = current->next;
1998-02-27 04:54:42 +00:00
}
/* add listbox to the dialogs */
add_widget (panelize_dlg, l_panelize);
1998-02-27 04:54:42 +00:00
listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
1998-02-27 04:54:42 +00:00
}
/* --------------------------------------------------------------------------------------------- */
static void
panelize_done (void)
1998-02-27 04:54:42 +00:00
{
destroy_dlg (panelize_dlg);
repaint_screen ();
}
/* --------------------------------------------------------------------------------------------- */
static void
add2panelize (char *label, char *command)
1998-02-27 04:54:42 +00:00
{
struct panelize *current, *old;
old = NULL;
current = panelize;
while (current && strcmp (current->label, label) <= 0)
{
old = current;
current = current->next;
1998-02-27 04:54:42 +00:00
}
if (old == NULL)
{
panelize = g_new (struct panelize, 1);
panelize->label = label;
panelize->command = command;
panelize->next = current;
}
else
{
struct panelize *new;
new = g_new (struct panelize, 1);
new->label = label;
new->command = command;
old->next = new;
new->next = current;
1998-02-27 04:54:42 +00:00
}
}
/* --------------------------------------------------------------------------------------------- */
static void
1998-12-02 21:27:27 +00:00
add2panelize_cmd (void)
1998-02-27 04:54:42 +00:00
{
char *label;
if (pname->buffer && (*pname->buffer))
{
label = input_dialog (_("Add to external panelize"),
_("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "");
if (!label)
return;
if (!*label)
{
g_free (label);
return;
}
add2panelize (label, g_strdup (pname->buffer));
1998-02-27 04:54:42 +00:00
}
}
/* --------------------------------------------------------------------------------------------- */
static void
remove_from_panelize (struct panelize *entry)
1998-02-27 04:54:42 +00:00
{
if (strcmp (entry->label, _("Other command")) != 0)
{
if (entry == panelize)
{
panelize = panelize->next;
}
else
{
struct panelize *current = panelize;
while (current && current->next != entry)
current = current->next;
if (current)
{
current->next = entry->next;
}
}
g_free (entry->label);
g_free (entry->command);
g_free (entry);
1998-02-27 04:54:42 +00:00
}
}
/* --------------------------------------------------------------------------------------------- */
static void
do_external_panelize (char *command)
{
int status, link_to_dir, stale_link;
int next_free = 0;
struct stat st;
dir_list *list = &current_panel->dir;
char line[MC_MAXPATHLEN];
char *name;
FILE *external;
open_error_pipe ();
external = popen (command, "r");
if (!external)
{
close_error_pipe (D_ERROR, _("Cannot invoke command."));
return;
}
/* Clear the counters and the directory list */
panel_clean_dir (current_panel);
while (1)
{
clearerr (external);
if (fgets (line, MC_MAXPATHLEN, external) == NULL)
{
if (ferror (external) && errno == EINTR)
continue;
else
break;
}
if (line[strlen (line) - 1] == '\n')
line[strlen (line) - 1] = 0;
if (strlen (line) < 1)
continue;
if (line[0] == '.' && line[1] == PATH_SEP)
name = line + 2;
else
name = line;
status = handle_path (list, name, &st, next_free, &link_to_dir, &stale_link);
if (status == 0)
continue;
if (status == -1)
break;
list->list[next_free].fnamelen = strlen (name);
list->list[next_free].fname = g_strdup (name);
file_mark (current_panel, next_free, 0);
list->list[next_free].f.link_to_dir = link_to_dir;
list->list[next_free].f.stale_link = stale_link;
list->list[next_free].f.dir_size_computed = 0;
list->list[next_free].st = st;
list->list[next_free].sort_key = NULL;
list->list[next_free].second_sort_key = NULL;
next_free++;
if (!(next_free & 32))
rotate_dash ();
}
current_panel->is_panelized = 1;
if (next_free)
{
current_panel->count = next_free;
if (list->list[0].fname[0] == PATH_SEP)
{
int ret;
strcpy (current_panel->cwd, PATH_SEP_STR);
ret = chdir (PATH_SEP_STR);
}
}
else
{
current_panel->count = set_zero_dir (list) ? 1 : 0;
}
if (pclose (external) < 0)
message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
close_error_pipe (D_NORMAL, NULL);
try_to_select (current_panel, NULL);
panel_re_sort (current_panel);
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
1998-12-02 21:27:27 +00:00
void
external_panelize (void)
1998-02-27 04:54:42 +00:00
{
char *target = NULL;
if (!vfs_current_is_local ())
{
message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
return;
1998-02-27 04:54:42 +00:00
}
init_panelize ();
1998-02-27 04:54:42 +00:00
/* display file info */
tty_setcolor (SELECTED_COLOR);
1998-02-27 04:54:42 +00:00
run_dlg (panelize_dlg);
switch (panelize_dlg->ret_value)
{
1998-02-27 04:54:42 +00:00
case B_CANCEL:
break;
1998-02-27 04:54:42 +00:00
case B_ADD:
add2panelize_cmd ();
break;
1998-02-27 04:54:42 +00:00
case B_REMOVE:
{
struct panelize *entry;
listbox_get_current (l_panelize, NULL, (void **) &entry);
remove_from_panelize (entry);
break;
}
1998-02-27 04:54:42 +00:00
case B_ENTER:
target = pname->buffer;
if (target != NULL && *target)
{
char *cmd = g_strdup (target);
destroy_dlg (panelize_dlg);
do_external_panelize (cmd);
g_free (cmd);
repaint_screen ();
return;
}
break;
1998-02-27 04:54:42 +00:00
}
panelize_done ();
}
/* --------------------------------------------------------------------------------------------- */
void
load_panelize (void)
1998-02-27 04:54:42 +00:00
{
gchar **profile_keys, **keys;
gsize len;
GIConv conv;
GString *buffer;
conv = str_crt_conv_from ("UTF-8");
profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section, &len);
add2panelize (g_strdup (_("Other command")), g_strdup (""));
1998-02-27 04:54:42 +00:00
if (!profile_keys || *profile_keys == NULL)
{
add2panelize (g_strdup (_("Find rejects after patching")),
g_strdup ("find . -name \\*.rej -print"));
add2panelize (g_strdup (_("Find *.orig after patching")),
g_strdup ("find . -name \\*.orig -print"));
add2panelize (g_strdup (_("Find SUID and SGID programs")),
g_strdup
("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
return;
1998-02-27 04:54:42 +00:00
}
while (*profile_keys)
{
if (utf8_display || conv == INVALID_CONV)
{
buffer = g_string_new (*profile_keys);
}
else
{
buffer = g_string_new ("");
if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
{
g_string_free (buffer, TRUE);
buffer = g_string_new (*profile_keys);
}
}
add2panelize (g_string_free (buffer, FALSE),
mc_config_get_string (mc_main_config, panelize_section, *profile_keys, ""));
profile_keys++;
1998-02-27 04:54:42 +00:00
}
g_strfreev (keys);
str_close_conv (conv);
1998-02-27 04:54:42 +00:00
}
/* --------------------------------------------------------------------------------------------- */
void
save_panelize (void)
1998-02-27 04:54:42 +00:00
{
struct panelize *current = panelize;
mc_config_del_group (mc_main_config, panelize_section);
for (; current; current = current->next)
{
if (strcmp (current->label, _("Other command")))
mc_config_set_string (mc_main_config,
panelize_section, current->label, current->command);
1998-02-27 04:54:42 +00:00
}
mc_config_save_file (mc_main_config, NULL);
1998-02-27 04:54:42 +00:00
}
/* --------------------------------------------------------------------------------------------- */
void
done_panelize (void)
1998-02-27 04:54:42 +00:00
{
struct panelize *current = panelize;
struct panelize *next;
for (; current; current = next)
{
next = current->next;
g_free (current->label);
g_free (current->command);
g_free (current);
1998-02-27 04:54:42 +00:00
}
}
/* --------------------------------------------------------------------------------------------- */