summaryrefslogtreecommitdiffstats
path: root/src/filemanager/panelize.c
blob: ab0dbb6402012875b19ef2b1a9c034f3aabbf929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*
   External panelize

   Copyright (C) 1995-2022
   Free Software Foundation, Inc.

   Written by:
   Janne Kukonlehto, 1995
   Jakub Jelinek, 1995
   Andrew Borodin <aborodin@vmail.ru> 2011-2022

   This file is part of the Midnight Commander.

   The Midnight Commander 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 3 of the License,
   or (at your option) any later version.

   The Midnight Commander 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, see <http://www.gnu.org/licenses/>.
 */

/** \file panelize.c
 *  \brief Source: External panelization module
 */

#include <config.h>

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "lib/global.h"

#include "lib/skin.h"
#include "lib/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/history.h"

#include "dir.h"
#include "filemanager.h"        /* current_panel */
#include "layout.h"             /* rotate_dash() */
#include "panel.h"              /* WPanel */

#include "panelize.h"

/*** global variables ****************************************************************************/

/*** file scope macro definitions ****************************************************************/

#define UX 3
#define UY 2

#define B_ADD    B_USER
#define B_REMOVE (B_USER + 1)

/*** file scope type declarations ****************************************************************/

/*** file scope variables ************************************************************************/

static WListbox *l_panelize;
static WDialog *panelize_dlg;
static int last_listitem;
static WInput *pname;

static const char *panelize_section = "Panelize";

/* Directory panelize */
static struct panelize
{
    char *command;
    char *label;
    struct panelize *next;
} *panelize = NULL;

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

static void
update_command (void)
{
    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);
    }
}

/* --------------------------------------------------------------------------------------------- */

static cb_ret_t
panelize_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
{
    switch (msg)
    {
    case MSG_INIT:
        group_default_callback (w, NULL, MSG_INIT, 0, NULL);
        MC_FALLTHROUGH;

    case MSG_NOTIFY:           /* MSG_NOTIFY is fired by the listbox to tell us the item has changed. */
        update_command ();
        return MSG_HANDLED;

    default:
        return dlg_default_callback (w, sender, msg, parm, data);
    }
}

/* --------------------------------------------------------------------------------------------- */

static void
init_panelize (void)
{
    struct
    {
        int ret_cmd;
        button_flags_t flags;
        const char *text;
    } panelize_but[] =
    {
        /* *INDENT-OFF* */
        { B_ENTER, DEFPUSH_BUTTON, N_("Pane&lize") },
        { B_REMOVE, NORMAL_BUTTON, N_("&Remove") },
        { B_ADD, NORMAL_BUTTON, N_("&Add new") },
        { B_CANCEL, NORMAL_BUTTON, N_("&Cancel") }
        /* *INDENT-ON* */
    };

    WGroup *g;

    size_t i;
    int blen;
    int panelize_cols;
    struct panelize *current;
    int x, y;

    last_listitem = 0;

    do_refresh ();

    i = G_N_ELEMENTS (panelize_but);
    blen = i - 1;               /* gaps between buttons */
    while (i-- != 0)
    {
#ifdef ENABLE_NLS
        panelize_but[i].text = _(panelize_but[i].text);
#endif
        blen += str_term_width1 (panelize_but[i].text) + 3 + 1;
        if (panelize_but[i].flags == DEFPUSH_BUTTON)
            blen += 2;
    }

    panelize_cols = COLS - 6;
    panelize_cols = MAX (panelize_cols, blen + 4);

    panelize_dlg =
        dlg_create (TRUE, 0, 0, 20, panelize_cols, WPOS_CENTER, FALSE, dialog_colors,
                    panelize_callback, NULL, "[External panelize]", _("External panelize"));
    g = GROUP (panelize_dlg);

    /* add listbox to the dialogs */
    y = UY;
    group_add_widget (g, groupbox_new (y++, UX, 12, panelize_cols - UX * 2, ""));

    l_panelize = listbox_new (y, UX + 1, 10, panelize_cols - UX * 2 - 2, FALSE, NULL);
    for (current = panelize; current != NULL; current = current->next)
        listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current, FALSE);
    listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
    group_add_widget (g, l_panelize);

    y += WIDGET (l_panelize)->rect.lines + 1;
    group_add_widget (g, label_new (y++, UX, _("Command")));
    pname =
        input_new (y++, UX, input_colors, panelize_cols - UX * 2, "", "in",
                   INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_COMMANDS |
                   INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES | INPUT_COMPLETE_CD |
                   INPUT_COMPLETE_SHELL_ESC);
    group_add_widget (g, pname);

    group_add_widget (g, hline_new (y++, -1, -1));

    x = (panelize_cols - blen) / 2;
    for (i = 0; i < G_N_ELEMENTS (panelize_but); i++)
    {
        WButton *b;

        b = button_new (y, x,
                        panelize_but[i].ret_cmd, panelize_but[i].flags, panelize_but[i].text, NULL);
        group_add_widget (g, b);

        x += button_get_len (b) + 1;
    }

    widget_select (WIDGET (l_panelize));
}

/* --------------------------------------------------------------------------------------------- */

static void
panelize_done (void)
{
    widget_destroy (WIDGET (panelize_dlg));
    repaint_screen ();
}

/* --------------------------------------------------------------------------------------------- */

static void
add2panelize (char *label, char *command)
{
    struct panelize *current;
    struct panelize *old = NULL;

    current = panelize;
    while (current != NULL && strcmp (current->label, label) <= 0)
    {
        old = current;
        current = current->next;
    }

    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;
    }
}

/* --------------------------------------------------------------------------------------------- */

static void
add2panelize_cmd (void)
{
    if (!input_is_empty (pname))
    {
        char *label;

        label = input_dialog (_("Add to external panelize"),
                              _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "",
                              INPUT_COMPLETE_NONE);
        if (label == NULL || *label == '\0')
            g_free (label);
        else
            add2panelize (label, input_get_text (pname));
    }
}

/* --------------------------------------------------------------------------------------------- */

static void
remove_from_panelize (struct panelize *entry)
{
    if (strcmp (entry->label, _("Other command")) != 0)
    {
        if (entry == panelize)
            panelize = panelize->next;
        else
        {
            struct panelize *current = panelize;

            while (current != NULL && current->next != entry)
                current = current->next;

            if (current != NULL)
                current->next = entry->next;
        }

        g_free (entry->label);
        g_free (entry->command);
        g_free (entry);
    }
}

/* --------------------------------------------------------------------------------------------- */

static void
do_external_panelize (char *command)
{
    dir_list *list = &current_panel->dir;
    mc_pipe_t *external;
    GError *error = NULL;
    GString *remain_file_name = NULL;

    external = mc_popen (command, TRUE, TRUE, &error);
    if (external == NULL)
    {
        message (D_ERROR, _("External panelize"), "%s", error->message);
        g_error_free (error);
        return;
    }

    /* Clear the counters and the directory list */
    panel_clean_dir (current_panel);

    panelize_change_root (current_panel->cwd_vpath);

    dir_list_init (list);

    while (TRUE)
    {
        GString *line;
        gboolean ok;

        /* init buffers before call of mc_pread() */
        external->out.len = MC_PIPE_BUFSIZE;
        external->err.len = MC_PIPE_BUFSIZE;
        external->err.null_term = TRUE;

        mc_pread (external, &error);

        if (error != NULL)
        {
            message (D_ERROR, MSG_ERROR, _("External panelize:\n%s"), error->message);
            g_error_free (error);
            break;
        }

        if (external->err.len > 0)
            message (D_ERROR, MSG_ERROR, _("External panelize:\n%s"), external->err.buf);

        if (external->out.len == MC_PIPE_STREAM_EOF)
            break;

        if (external->out.len == 0)
            continue;

        if (external->out.len == MC_PIPE_ERROR_READ)
        {
            message (D_ERROR, MSG_ERROR,
                     _("External panelize:\nfailed to read data from child stdout:\n%s"),
                     unix_error_string (external->out.error));
            break;
        }

        ok = TRUE;

        while (ok && (line = mc_pstream_get_string (&external->out)) != NULL)
        {
            char *name;
            gboolean link_to_dir, stale_link;
            struct stat st;

            /* handle a \n-separated file list */

            if (line->str[line->len - 1] == '\n')
            {
                /* entire file name or last chunk */

                g_string_truncate (line, line->len - 1);

                /* join filename chunks */
                if (remain_file_name != NULL)
                {
                    g_string_append_len (remain_file_name, line->str, line->len);
                    g_string_free (line, TRUE);
                    line = remain_file_name;
                    remain_file_name = NULL;
                }
            }
            else
            {
                /* first or middle chunk of file name */

                if (remain_file_name == NULL)
                    remain_file_name = line;
                else
                {
                    g_string_append_len (remain_file_name, line->str, line->len);
                    g_string_free (line, TRUE);
                }

                continue;
            }

            name = line->str;

            if (name[0] == '.' && IS_PATH_SEP (name[1]))
                name += 2;

            if (handle_path (name, &st, &link_to_dir, &stale_link))
            {
                ok = dir_list_append (list, name, &st, link_to_dir, stale_link);

                if (ok)
                {
                    file_mark (current_panel, list->len - 1, 0);

                    if ((list->len & 31) == 0)
                        rotate_dash (TRUE);
                }
            }

            g_string_free (line, TRUE);
        }
    }

    if (remain_file_name != NULL)
        g_string_free (remain_file_name, TRUE);

    mc_pclose (external, NULL);

    current_panel->is_panelized = TRUE;
    panelize_absolutize_if_needed (current_panel);

    try_to_select (current_panel, NULL);
    panel_re_sort (current_panel);
    rotate_dash (FALSE);
}

/* --------------------------------------------------------------------------------------------- */

static void
do_panelize_cd (WPanel * panel)
{
    int i;
    dir_list *list;
    gboolean panelized_same;

    dir_list_clean (&panel->dir);
    if (panelized_panel.root_vpath == NULL)
        panelize_change_root (current_panel->cwd_vpath);

    if (panelized_panel.list.len < 1)
        dir_list_init (&panelized_panel.list);
    else if (panelized_panel.list.len > panel->dir.size)
        dir_list_grow (&panel->dir, panelized_panel.list.len - panel->dir.size);

    list = &panel->dir;
    list->len = panelized_panel.list.len;

    panelized_same = vfs_path_equal (panelized_panel.root_vpath, panel->cwd_vpath);

    for (i = 0; i < panelized_panel.list.len; i++)
    {
        if (panelized_same || DIR_IS_DOTDOT (panelized_panel.list.list[i].fname->str))
            list->list[i].fname = mc_g_string_dup (panelized_panel.list.list[i].fname);
        else
        {
            vfs_path_t *tmp_vpath;

            tmp_vpath =
                vfs_path_append_new (panelized_panel.root_vpath,
                                     panelized_panel.list.list[i].fname->str, (char *) NULL);
            list->list[i].fname = g_string_new (vfs_path_as_str (tmp_vpath));
            vfs_path_free (tmp_vpath, TRUE);
        }
        list->list[i].f.link_to_dir = panelized_panel.list.list[i].f.link_to_dir;
        list->list[i].f.stale_link = panelized_panel.list.list[i].f.stale_link;
        list->list[i].f.dir_size_computed = panelized_panel.list.list[i].f.dir_size_computed;
        list->list[i].f.marked = panelized_panel.list.list[i].f.marked;
        list->list[i].st = panelized_panel.list.list[i].st;
        list->list[i].sort_key = panelized_panel.list.list[i].sort_key;
        list->list[i].second_sort_key = panelized_panel.list.list[i].second_sort_key;
    }

    panel->is_panelized = TRUE;
    panelize_absolutize_if_needed (panel);

    try_to_select (panel, NULL);
}

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

/**
 * Change root directory of panelized content.
 * @param new_root - object with new path.
 */
void
panelize_change_root (const vfs_path_t * new_root)
{
    vfs_path_free (panelized_panel.root_vpath, TRUE);
    panelized_panel.root_vpath = vfs_path_clone (new_root);
}

/* --------------------------------------------------------------------------------------------- */

void
panelize_save_panel (WPanel * panel)
{
    int i;
    dir_list *list = &panel->dir;

    panelize_change_root (current_panel->cwd_vpath);

    if (panelized_panel.list.len > 0)
        dir_list_clean (&panelized_panel.list);
    if (panel->dir.len == 0)
        return;

    if (panel->dir.len > panelized_panel.list.size)
        dir_list_grow (&panelized_panel.list, panel->dir.len - panelized_panel.list.size);
    panelized_panel.list.len = panel->dir.len;

    for (i = 0; i < panel->dir.len; i++)
    {
        panelized_panel.list.list[i].fname = mc_g_string_dup (list->list[i].fname);
        panelized_panel.list.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
        panelized_panel.list.list[i].f.stale_link = list->list[i].f.stale_link;
        panelized_panel.list.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
        panelized_panel.list.list[i].f.marked = list->list[i].f.marked;
        panelized_panel.list.list[i].st = list->list[i].st;
        panelized_panel.list.list[i].sort_key = list->list[i].sort_key;
        panelized_panel.list.list[i].second_sort_key = list->list[i].second_sort_key;
    }
}

/* --------------------------------------------------------------------------------------------- */

/**
 * Conditionally switches a panel's directory to "/" (root).
 *
 * If a panelized panel's listing contain absolute paths, this function
 * sets the panel's directory to "/". Otherwise it does nothing.
 *
 * Rationale:
 *
 * This makes tokenized strings like "%d/%p" work. This also makes other
 * places work where such naive concatenation is done in code (e.g., when
 * pressing ctrl+shift+enter, for CK_PutCurrentFullSelected).
 *
 * When to call:
 *
 * You should always call this function after you populate the listing
 * of a panelized panel.
 */
void
panelize_absolutize_if_needed (WPanel * panel)
{
    const dir_list *const list = &panel->dir;

    /* Note: We don't support mixing of absolute and relative paths, which is
     * why it's ok for us to check only the 1st entry. */
    if (list->len > 1 && g_path_is_absolute (list->list[1].fname->str))
    {
        vfs_path_t *root;

        root = vfs_path_from_str (PATH_SEP_STR);
        panel_set_cwd (panel, root);
        if (panel == current_panel)
            mc_chdir (root);
        vfs_path_free (root, TRUE);
    }
}

/* --------------------------------------------------------------------------------------------- */

void
cd_panelize_cmd (void)
{
    if (!SELECTED_IS_PANEL)
        create_panel (MENU_PANEL_IDX, view_listing);

    do_panelize_cd (PANEL (get_panel_widget (MENU_PANEL_IDX)));
}

/* --------------------------------------------------------------------------------------------- */

void
external_panelize (void)
{
    if (!vfs_current_is_local ())
    {
        message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
        return;
    }

    init_panelize ();

    /* display file info */
    tty_setcolor (SELECTED_COLOR);

    switch (dlg_run (panelize_dlg))
    {
    case B_CANCEL:
        break;

    case B_ADD:
        add2panelize_cmd ();
        break;

    case B_REMOVE:
        {
            struct panelize *entry;

            listbox_get_current (l_panelize, NULL, (void **) &entry);
            remove_from_panelize (entry);
            break;
        }

    case B_ENTER:
        if (!input_is_empty (pname))
        {
            char *cmd;

            cmd = input_get_text (pname);
            widget_destroy (WIDGET (panelize_dlg));
            do_external_panelize (cmd);
            g_free (cmd);
            repaint_screen ();
            return;
        }
        break;

    default:
        break;
    }

    panelize_done ();
}

/* --------------------------------------------------------------------------------------------- */

void
load_panelize (void)
{
    char **keys;

    keys = mc_config_get_keys (mc_global.main_config, panelize_section, NULL);

    add2panelize (g_strdup (_("Other command")), g_strdup (""));

    if (*keys == NULL)
    {
        add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
        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"));
    }
    else
    {
        GIConv conv;
        char **profile_keys;

        conv = str_crt_conv_from ("UTF-8");

        for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
        {
            GString *buffer;

            if (mc_global.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_assign (buffer, *profile_keys);
            }

            add2panelize (g_string_free (buffer, FALSE),
                          mc_config_get_string (mc_global.main_config, panelize_section,
                                                *profile_keys, ""));
        }

        str_close_conv (conv);
    }

    g_strfreev (keys);
}

/* --------------------------------------------------------------------------------------------- */

void
save_panelize (void)
{
    struct panelize *current;

    mc_config_del_group (mc_global.main_config, panelize_section);

    for (current = panelize; current != NULL; current = current->next)
        if (strcmp (current->label, _("Other command")) != 0)
            mc_config_set_string (mc_global.main_config,
                                  panelize_section, current->label, current->command);
}

/* --------------------------------------------------------------------------------------------- */

void
done_panelize (void)
{
    struct panelize *current, *next;

    for (current = panelize; current != NULL; current = next)
    {
        next = current->next;
        g_free (current->label);
        g_free (current->command);
        g_free (current);
    }
}

/* --------------------------------------------------------------------------------------------- */