root/src/filemanager/panelize.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. panelize_entry_free
  2. panelize_entry_cmp_by_label
  3. panelize_entry_add_to_listbox
  4. update_command
  5. panelize_callback
  6. external_panelize_init
  7. external_panelize_done
  8. add2panelize
  9. add2panelize_cmd
  10. remove_from_panelize
  11. do_external_panelize
  12. external_panelize_cmd
  13. external_panelize_load
  14. external_panelize_save
  15. external_panelize_free

   1 /*
   2    External panelize
   3 
   4    Copyright (C) 1995-2024
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Janne Kukonlehto, 1995
   9    Jakub Jelinek, 1995
  10    Andrew Borodin <aborodin@vmail.ru> 2011-2023
  11 
  12    This file is part of the Midnight Commander.
  13 
  14    The Midnight Commander is free software: you can redistribute it
  15    and/or modify it under the terms of the GNU General Public License as
  16    published by the Free Software Foundation, either version 3 of the License,
  17    or (at your option) any later version.
  18 
  19    The Midnight Commander is distributed in the hope that it will be useful,
  20    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22    GNU General Public License for more details.
  23 
  24    You should have received a copy of the GNU General Public License
  25    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  26  */
  27 
  28 /** \file panelize.c
  29  *  \brief Source: External panelization module
  30  */
  31 
  32 #include <config.h>
  33 
  34 #include "lib/global.h"
  35 
  36 #include "lib/skin.h"
  37 #include "lib/tty/tty.h"
  38 #include "lib/vfs/vfs.h"
  39 #include "lib/mcconfig.h"       /* Load/save directories panelize */
  40 #include "lib/strutil.h"
  41 #include "lib/widget.h"
  42 #include "lib/util.h"           /* mc_pipe_t */
  43 
  44 #include "src/history.h"
  45 
  46 #include "filemanager.h"        /* current_panel */
  47 #include "layout.h"             /* rotate_dash() */
  48 #include "panel.h"              /* WPanel, dir.h */
  49 
  50 #include "panelize.h"
  51 
  52 /*** global variables ****************************************************************************/
  53 
  54 /*** file scope macro definitions ****************************************************************/
  55 
  56 #define UX 3
  57 #define UY 2
  58 
  59 #define B_ADD    B_USER
  60 #define B_REMOVE (B_USER + 1)
  61 
  62 /*** file scope type declarations ****************************************************************/
  63 
  64 typedef struct
  65 {
  66     char *command;
  67     char *label;
  68 } panelize_entry_t;
  69 
  70 /*** forward declarations (file scope functions) *************************************************/
  71 
  72 /*** file scope variables ************************************************************************/
  73 
  74 static WListbox *l_panelize;
  75 static WDialog *panelize_dlg;
  76 static int last_listitem;
  77 static WInput *pname;
  78 static GSList *panelize = NULL;
  79 
  80 static const char *panelize_section = "Panelize";
  81 
  82 /* --------------------------------------------------------------------------------------------- */
  83 /*** file scope functions ************************************************************************/
  84 /* --------------------------------------------------------------------------------------------- */
  85 
  86 static void
  87 panelize_entry_free (gpointer data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  88 {
  89     panelize_entry_t *entry = (panelize_entry_t *) data;
  90 
  91     g_free (entry->command);
  92     g_free (entry->label);
  93     g_free (entry);
  94 }
  95 
  96 /* --------------------------------------------------------------------------------------------- */
  97 
  98 static int
  99 panelize_entry_cmp_by_label (gconstpointer a, gconstpointer b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 100 {
 101     const panelize_entry_t *entry = (const panelize_entry_t *) a;
 102     const char *label = (const char *) b;
 103 
 104     return strcmp (entry->label, label);
 105 }
 106 
 107 /* --------------------------------------------------------------------------------------------- */
 108 
 109 static void
 110 panelize_entry_add_to_listbox (gpointer data, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 111 {
 112     panelize_entry_t *entry = (panelize_entry_t *) data;
 113 
 114     (void) user_data;
 115 
 116     listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, entry->label, entry, FALSE);
 117 }
 118 
 119 /* --------------------------------------------------------------------------------------------- */
 120 
 121 static void
 122 update_command (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 123 {
 124     if (l_panelize->current != last_listitem)
 125     {
 126         panelize_entry_t *data = NULL;
 127 
 128         last_listitem = l_panelize->current;
 129         listbox_get_current (l_panelize, NULL, (void **) &data);
 130         input_assign_text (pname, data->command);
 131         pname->point = 0;
 132         input_update (pname, TRUE);
 133     }
 134 }
 135 
 136 /* --------------------------------------------------------------------------------------------- */
 137 
 138 static cb_ret_t
 139 panelize_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 140 {
 141     switch (msg)
 142     {
 143     case MSG_INIT:
 144         group_default_callback (w, NULL, MSG_INIT, 0, NULL);
 145         MC_FALLTHROUGH;
 146 
 147     case MSG_NOTIFY:           /* MSG_NOTIFY is fired by the listbox to tell us the item has changed. */
 148         update_command ();
 149         return MSG_HANDLED;
 150 
 151     default:
 152         return dlg_default_callback (w, sender, msg, parm, data);
 153     }
 154 }
 155 
 156 /* --------------------------------------------------------------------------------------------- */
 157 
 158 static void
 159 external_panelize_init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 160 {
 161     struct
 162     {
 163         int ret_cmd;
 164         button_flags_t flags;
 165         const char *text;
 166     } panelize_but[] =
 167     {
 168         /* *INDENT-OFF* */
 169         { B_ENTER, DEFPUSH_BUTTON, N_("Pane&lize") },
 170         { B_REMOVE, NORMAL_BUTTON, N_("&Remove") },
 171         { B_ADD, NORMAL_BUTTON, N_("&Add new") },
 172         { B_CANCEL, NORMAL_BUTTON, N_("&Cancel") }
 173         /* *INDENT-ON* */
 174     };
 175 
 176     WGroup *g;
 177 
 178     size_t i;
 179     int blen;
 180     int panelize_cols;
 181     int x, y;
 182 
 183     last_listitem = 0;
 184 
 185     do_refresh ();
 186 
 187     i = G_N_ELEMENTS (panelize_but);
 188     blen = i - 1;               /* gaps between buttons */
 189     while (i-- != 0)
 190     {
 191 #ifdef ENABLE_NLS
 192         panelize_but[i].text = _(panelize_but[i].text);
 193 #endif
 194         blen += str_term_width1 (panelize_but[i].text) + 3 + 1;
 195         if (panelize_but[i].flags == DEFPUSH_BUTTON)
 196             blen += 2;
 197     }
 198 
 199     panelize_cols = COLS - 6;
 200     panelize_cols = MAX (panelize_cols, blen + 4);
 201 
 202     panelize_dlg =
 203         dlg_create (TRUE, 0, 0, 20, panelize_cols, WPOS_CENTER, FALSE, dialog_colors,
 204                     panelize_callback, NULL, "[External panelize]", _("External panelize"));
 205     g = GROUP (panelize_dlg);
 206 
 207     /* add listbox to the dialogs */
 208     y = UY;
 209     group_add_widget (g, groupbox_new (y++, UX, 12, panelize_cols - UX * 2, ""));
 210 
 211     l_panelize = listbox_new (y, UX + 1, 10, panelize_cols - UX * 2 - 2, FALSE, NULL);
 212     g_slist_foreach (panelize, panelize_entry_add_to_listbox, NULL);
 213     listbox_set_current (l_panelize, listbox_search_text (l_panelize, _("Other command")));
 214     group_add_widget (g, l_panelize);
 215 
 216     y += WIDGET (l_panelize)->rect.lines + 1;
 217     group_add_widget (g, label_new (y++, UX, _("Command")));
 218     pname =
 219         input_new (y++, UX, input_colors, panelize_cols - UX * 2, "", "in",
 220                    INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_COMMANDS |
 221                    INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES | INPUT_COMPLETE_CD |
 222                    INPUT_COMPLETE_SHELL_ESC);
 223     group_add_widget (g, pname);
 224 
 225     group_add_widget (g, hline_new (y++, -1, -1));
 226 
 227     x = (panelize_cols - blen) / 2;
 228     for (i = 0; i < G_N_ELEMENTS (panelize_but); i++)
 229     {
 230         WButton *b;
 231 
 232         b = button_new (y, x,
 233                         panelize_but[i].ret_cmd, panelize_but[i].flags, panelize_but[i].text, NULL);
 234         group_add_widget (g, b);
 235 
 236         x += button_get_len (b) + 1;
 237     }
 238 
 239     widget_select (WIDGET (l_panelize));
 240 }
 241 
 242 /* --------------------------------------------------------------------------------------------- */
 243 
 244 static void
 245 external_panelize_done (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 246 {
 247     widget_destroy (WIDGET (panelize_dlg));
 248     repaint_screen ();
 249 }
 250 
 251 /* --------------------------------------------------------------------------------------------- */
 252 
 253 static void
 254 add2panelize (char *label, char *command)
     /* [previous][next][first][last][top][bottom][index][help]  */
 255 {
 256     panelize_entry_t *entry;
 257 
 258     entry = g_try_new (panelize_entry_t, 1);
 259     if (entry != NULL)
 260     {
 261         entry->label = label;
 262         entry->command = command;
 263 
 264         panelize = g_slist_insert_sorted (panelize, entry, panelize_entry_cmp_by_label);
 265     }
 266 }
 267 
 268 /* --------------------------------------------------------------------------------------------- */
 269 
 270 static void
 271 add2panelize_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 272 {
 273     if (!input_is_empty (pname))
 274     {
 275         char *label;
 276 
 277         label = input_dialog (_("Add to external panelize"),
 278                               _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "",
 279                               INPUT_COMPLETE_NONE);
 280         if (label == NULL || *label == '\0')
 281             g_free (label);
 282         else
 283             add2panelize (label, input_get_text (pname));
 284     }
 285 }
 286 
 287 /* --------------------------------------------------------------------------------------------- */
 288 
 289 static void
 290 remove_from_panelize (panelize_entry_t * entry)
     /* [previous][next][first][last][top][bottom][index][help]  */
 291 {
 292     if (strcmp (entry->label, _("Other command")) != 0)
 293     {
 294         panelize = g_slist_remove (panelize, entry);
 295         panelize_entry_free (entry);
 296     }
 297 }
 298 
 299 /* --------------------------------------------------------------------------------------------- */
 300 
 301 static void
 302 do_external_panelize (const char *command)
     /* [previous][next][first][last][top][bottom][index][help]  */
 303 {
 304     dir_list *list = &current_panel->dir;
 305     mc_pipe_t *external;
 306     GError *error = NULL;
 307     GString *remain_file_name = NULL;
 308 
 309     external = mc_popen (command, TRUE, TRUE, &error);
 310     if (external == NULL)
 311     {
 312         message (D_ERROR, _("External panelize"), "%s", error->message);
 313         g_error_free (error);
 314         return;
 315     }
 316 
 317     /* Clear the counters and the directory list */
 318     panel_clean_dir (current_panel);
 319 
 320     panel_panelize_change_root (current_panel, current_panel->cwd_vpath);
 321 
 322     dir_list_init (list);
 323 
 324     while (TRUE)
 325     {
 326         GString *line;
 327         gboolean ok;
 328 
 329         /* init buffers before call of mc_pread() */
 330         external->out.len = MC_PIPE_BUFSIZE;
 331         external->err.len = MC_PIPE_BUFSIZE;
 332         external->err.null_term = TRUE;
 333 
 334         mc_pread (external, &error);
 335 
 336         if (error != NULL)
 337         {
 338             message (D_ERROR, MSG_ERROR, _("External panelize:\n%s"), error->message);
 339             g_error_free (error);
 340             break;
 341         }
 342 
 343         if (external->err.len > 0)
 344             message (D_ERROR, MSG_ERROR, _("External panelize:\n%s"), external->err.buf);
 345 
 346         if (external->out.len == MC_PIPE_STREAM_EOF)
 347             break;
 348 
 349         if (external->out.len == 0)
 350             continue;
 351 
 352         if (external->out.len == MC_PIPE_ERROR_READ)
 353         {
 354             message (D_ERROR, MSG_ERROR,
 355                      _("External panelize:\nfailed to read data from child stdout:\n%s"),
 356                      unix_error_string (external->out.error));
 357             break;
 358         }
 359 
 360         ok = TRUE;
 361 
 362         while (ok && (line = mc_pstream_get_string (&external->out)) != NULL)
 363         {
 364             char *name;
 365             gboolean link_to_dir, stale_link;
 366             struct stat st;
 367 
 368             /* handle a \n-separated file list */
 369 
 370             if (line->str[line->len - 1] == '\n')
 371             {
 372                 /* entire file name or last chunk */
 373 
 374                 g_string_truncate (line, line->len - 1);
 375 
 376                 /* join filename chunks */
 377                 if (remain_file_name != NULL)
 378                 {
 379                     g_string_append_len (remain_file_name, line->str, line->len);
 380                     g_string_free (line, TRUE);
 381                     line = remain_file_name;
 382                     remain_file_name = NULL;
 383                 }
 384             }
 385             else
 386             {
 387                 /* first or middle chunk of file name */
 388 
 389                 if (remain_file_name == NULL)
 390                     remain_file_name = line;
 391                 else
 392                 {
 393                     g_string_append_len (remain_file_name, line->str, line->len);
 394                     g_string_free (line, TRUE);
 395                 }
 396 
 397                 continue;
 398             }
 399 
 400             name = line->str;
 401 
 402             if (name[0] == '.' && IS_PATH_SEP (name[1]))
 403                 name += 2;
 404 
 405             if (handle_path (name, &st, &link_to_dir, &stale_link))
 406             {
 407                 ok = dir_list_append (list, name, &st, link_to_dir, stale_link);
 408 
 409                 if (ok)
 410                 {
 411                     file_mark (current_panel, list->len - 1, 0);
 412 
 413                     if ((list->len & 31) == 0)
 414                         rotate_dash (TRUE);
 415                 }
 416             }
 417 
 418             g_string_free (line, TRUE);
 419         }
 420     }
 421 
 422     if (remain_file_name != NULL)
 423         g_string_free (remain_file_name, TRUE);
 424 
 425     mc_pclose (external, NULL);
 426 
 427     current_panel->is_panelized = TRUE;
 428     panel_panelize_absolutize_if_needed (current_panel);
 429 
 430     panel_set_current_by_name (current_panel, NULL);
 431     panel_re_sort (current_panel);
 432     rotate_dash (FALSE);
 433 }
 434 
 435 /* --------------------------------------------------------------------------------------------- */
 436 /*** public functions ****************************************************************************/
 437 /* --------------------------------------------------------------------------------------------- */
 438 
 439 void
 440 external_panelize_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 441 {
 442     if (!vfs_current_is_local ())
 443     {
 444         message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
 445         return;
 446     }
 447 
 448     external_panelize_init ();
 449 
 450     /* display file info */
 451     tty_setcolor (SELECTED_COLOR);
 452 
 453     switch (dlg_run (panelize_dlg))
 454     {
 455     case B_CANCEL:
 456         break;
 457 
 458     case B_ADD:
 459         add2panelize_cmd ();
 460         break;
 461 
 462     case B_REMOVE:
 463         {
 464             panelize_entry_t *entry;
 465 
 466             listbox_get_current (l_panelize, NULL, (void **) &entry);
 467             remove_from_panelize (entry);
 468             break;
 469         }
 470 
 471     case B_ENTER:
 472         if (!input_is_empty (pname))
 473         {
 474             char *cmd;
 475 
 476             cmd = input_get_text (pname);
 477             widget_destroy (WIDGET (panelize_dlg));
 478             do_external_panelize (cmd);
 479             g_free (cmd);
 480             repaint_screen ();
 481             return;
 482         }
 483         break;
 484 
 485     default:
 486         break;
 487     }
 488 
 489     external_panelize_done ();
 490 }
 491 
 492 /* --------------------------------------------------------------------------------------------- */
 493 
 494 void
 495 external_panelize_load (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 496 {
 497     char **keys;
 498 
 499     keys = mc_config_get_keys (mc_global.main_config, panelize_section, NULL);
 500 
 501     add2panelize (g_strdup (_("Other command")), g_strdup (""));
 502 
 503     if (*keys == NULL)
 504     {
 505         add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
 506         add2panelize (g_strdup (_("Find rejects after patching")),
 507                       g_strdup ("find . -name \\*.rej -print"));
 508         add2panelize (g_strdup (_("Find *.orig after patching")),
 509                       g_strdup ("find . -name \\*.orig -print"));
 510         add2panelize (g_strdup (_("Find SUID and SGID programs")),
 511                       g_strdup
 512                       ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 -a -perm /01 \\) \\) -print"));
 513     }
 514     else
 515     {
 516         GIConv conv;
 517         char **profile_keys;
 518 
 519         conv = str_crt_conv_from ("UTF-8");
 520 
 521         for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
 522         {
 523             GString *buffer;
 524 
 525             if (mc_global.utf8_display || conv == INVALID_CONV)
 526                 buffer = g_string_new (*profile_keys);
 527             else
 528             {
 529                 buffer = g_string_new ("");
 530                 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
 531                     g_string_assign (buffer, *profile_keys);
 532             }
 533 
 534             add2panelize (g_string_free (buffer, FALSE),
 535                           mc_config_get_string (mc_global.main_config, panelize_section,
 536                                                 *profile_keys, ""));
 537         }
 538 
 539         str_close_conv (conv);
 540     }
 541 
 542     g_strfreev (keys);
 543 }
 544 
 545 /* --------------------------------------------------------------------------------------------- */
 546 
 547 void
 548 external_panelize_save (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 549 {
 550     GSList *l;
 551 
 552     mc_config_del_group (mc_global.main_config, panelize_section);
 553 
 554     for (l = panelize; l != NULL; l = g_slist_next (l))
 555     {
 556         panelize_entry_t *current = (panelize_entry_t *) l->data;
 557 
 558         if (strcmp (current->label, _("Other command")) != 0)
 559             mc_config_set_string (mc_global.main_config,
 560                                   panelize_section, current->label, current->command);
 561     }
 562 }
 563 
 564 /* --------------------------------------------------------------------------------------------- */
 565 
 566 void
 567 external_panelize_free (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 568 {
 569     g_clear_slist (&panelize, panelize_entry_free);
 570     panelize = NULL;
 571 }
 572 
 573 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */