Manual pages: mcmcdiffmceditmcview

root/src/filemanager/info.c

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

DEFINITIONS

This source file includes following definitions.
  1. info_box
  2. info_show_info
  3. info_hook
  4. info_callback
  5. info_new

   1 /*
   2    Panel managing.
   3 
   4    Copyright (C) 1994-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2013
   9    Andrew Borodin <aborodin@vmail.ru>, 2013-2023
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  25  */
  26 
  27 /** \file info.c
  28  *  \brief Source: panel managing
  29  */
  30 
  31 #include <config.h>
  32 
  33 #include <stdio.h>
  34 #include <stdlib.h>
  35 #include <sys/stat.h>
  36 #include <inttypes.h>  // PRIuMAX
  37 
  38 #include "lib/global.h"
  39 #include "lib/unixcompat.h"
  40 #include "lib/tty/tty.h"
  41 #include "lib/tty/key.h"  // is_idle()
  42 #include "lib/skin.h"
  43 #include "lib/strutil.h"
  44 #include "lib/timefmt.h"  // file_date()
  45 #include "lib/util.h"
  46 #include "lib/widget.h"
  47 
  48 #include "src/setup.h"  // panels_options
  49 
  50 #include "filemanager.h"  // the_menubar
  51 #include "layout.h"
  52 #include "mountlist.h"
  53 #ifdef ENABLE_EXT2FS_ATTR
  54 #include "cmd.h"  // chattr_get_as_str()
  55 #endif
  56 
  57 #include "info.h"
  58 
  59 /*** global variables ****************************************************************************/
  60 
  61 /*** file scope macro definitions ****************************************************************/
  62 
  63 /*** file scope type declarations ****************************************************************/
  64 
  65 struct WInfo
  66 {
  67     Widget widget;
  68     gboolean ready;
  69 };
  70 
  71 /*** forward declarations (file scope functions) *************************************************/
  72 
  73 /*** file scope variables ************************************************************************/
  74 
  75 static struct my_statfs myfs_stats;
  76 
  77 /* --------------------------------------------------------------------------------------------- */
  78 /*** file scope functions ************************************************************************/
  79 /* --------------------------------------------------------------------------------------------- */
  80 
  81 static void
  82 info_box (WInfo *info)
     /* [previous][next][first][last][top][bottom][index][help]  */
  83 {
  84     Widget *w = WIDGET (info);
  85 
  86     const char *title = _ ("Information");
  87     const int len = str_term_width1 (title);
  88 
  89     tty_set_normal_attrs ();
  90     tty_setcolor (CORE_NORMAL_COLOR);
  91     widget_erase (w);
  92 
  93     tty_setcolor (CORE_FRAME_COLOR);
  94     tty_draw_box (w->rect.y, w->rect.x, w->rect.lines, w->rect.cols, FALSE);
  95     widget_gotoyx (w, 2, 0);
  96     tty_print_char (mc_tty_frm[MC_TTY_FRM_DLEFTMIDDLE]);
  97     widget_gotoyx (w, 2, w->rect.cols - 1);
  98     tty_print_char (mc_tty_frm[MC_TTY_FRM_DRIGHTMIDDLE]);
  99     tty_draw_hline (w->rect.y + 2, w->rect.x + 1, mc_tty_frm[MC_TTY_FRM_HORIZ], w->rect.cols - 2);
 100 
 101     tty_setcolor (CORE_NORMAL_COLOR);
 102     widget_gotoyx (w, 0, (w->rect.cols - len - 2) / 2);
 103     tty_printf (" %s ", title);
 104 }
 105 
 106 /* --------------------------------------------------------------------------------------------- */
 107 
 108 static void
 109 info_show_info (WInfo *info)
     /* [previous][next][first][last][top][bottom][index][help]  */
 110 {
 111     const WRect *w = &CONST_WIDGET (info)->rect;
 112     const file_entry_t *fe;
 113     static int i18n_adjust = 0;
 114     static const char *file_label;
 115     GString *buff;
 116     struct stat st;
 117     char rp_cwd[PATH_MAX];
 118     const char *p_rp_cwd;
 119 
 120     if (!is_idle ())
 121         return;
 122 
 123     info_box (info);
 124 
 125     tty_setcolor (CORE_MARKED_COLOR);
 126     widget_gotoyx (w, 1, 3);
 127     tty_printf ("%s %s", PACKAGE_NAME, mc_global.mc_version);
 128 
 129     if (!info->ready)
 130         return;
 131 
 132     if (get_current_type () != view_listing)
 133         return;
 134 
 135     // don't rely on vpath CWD when cd_symlinks enabled
 136     p_rp_cwd = mc_realpath (vfs_path_as_str (current_panel->cwd_vpath), rp_cwd);
 137     if (p_rp_cwd == NULL)
 138         p_rp_cwd = vfs_path_as_str (current_panel->cwd_vpath);
 139 
 140     my_statfs (&myfs_stats, p_rp_cwd);
 141 
 142     fe = panel_current_entry (current_panel);
 143     if (fe == NULL)
 144         return;
 145 
 146     st = fe->st;
 147 
 148     // Print only lines which fit
 149 
 150     if (i18n_adjust == 0)
 151     {
 152         // This printf pattern string is used as a reference for size
 153         file_label = _ ("File: %s");
 154         i18n_adjust = str_term_width1 (file_label) + 2;
 155     }
 156 
 157     tty_setcolor (CORE_NORMAL_COLOR);
 158 
 159     buff = g_string_new ("");
 160 
 161     switch (w->lines - 2)
 162     {
 163         // Note: all cases are fall-throughs
 164 
 165     default:
 166         MC_FALLTHROUGH;
 167     case 17:
 168         widget_gotoyx (w, 17, 3);
 169         if ((myfs_stats.nfree == 0 && myfs_stats.nodes == 0)
 170             || (myfs_stats.nfree == (uintmax_t) (-1) && myfs_stats.nodes == (uintmax_t) (-1)))
 171             tty_print_string (_ ("No node information"));
 172         else if (myfs_stats.nfree == (uintmax_t) (-1))
 173             tty_printf ("%s - / %" PRIuMAX, _ ("Free nodes:"), myfs_stats.nodes);
 174         else if (myfs_stats.nodes == (uintmax_t) (-1))
 175             tty_printf ("%s %" PRIuMAX " / -", _ ("Free nodes:"), myfs_stats.nfree);
 176         else
 177             tty_printf ("%s %" PRIuMAX " / %" PRIuMAX " (%d%%)", _ ("Free nodes:"),
 178                         myfs_stats.nfree, myfs_stats.nodes,
 179                         myfs_stats.nodes == 0
 180                             ? 0
 181                             : (int) (100 * (long double) myfs_stats.nfree / myfs_stats.nodes));
 182         MC_FALLTHROUGH;
 183     case 16:
 184         widget_gotoyx (w, 16, 3);
 185         if (myfs_stats.avail == 0 && myfs_stats.total == 0)
 186             tty_print_string (_ ("No space information"));
 187         else
 188         {
 189             char buffer1[6], buffer2[6];
 190 
 191             size_trunc_len (buffer1, 5, myfs_stats.avail, 1, panels_options.kilobyte_si);
 192             size_trunc_len (buffer2, 5, myfs_stats.total, 1, panels_options.kilobyte_si);
 193             tty_printf (_ ("Free space: %s / %s (%d%%)"), buffer1, buffer2,
 194                         myfs_stats.total == 0
 195                             ? 0
 196                             : (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
 197         }
 198         MC_FALLTHROUGH;
 199     case 15:
 200         widget_gotoyx (w, 15, 3);
 201         tty_printf (_ ("Type:       %s"),
 202                     myfs_stats.typename ? myfs_stats.typename : _ ("non-local vfs"));
 203         if (myfs_stats.type != 0xffff && myfs_stats.type != -1)
 204             tty_printf (" (%Xh)", (unsigned int) myfs_stats.type);
 205         MC_FALLTHROUGH;
 206     case 14:
 207         widget_gotoyx (w, 14, 3);
 208         str_printf (buff, _ ("Device:     %s"),
 209                     str_trunc (myfs_stats.device, w->cols - i18n_adjust));
 210         tty_print_string (buff->str);
 211         g_string_set_size (buff, 0);
 212         MC_FALLTHROUGH;
 213     case 13:
 214         widget_gotoyx (w, 13, 3);
 215         str_printf (buff, _ ("Filesystem: %s"),
 216                     str_trunc (myfs_stats.mpoint, w->cols - i18n_adjust));
 217         tty_print_string (buff->str);
 218         g_string_set_size (buff, 0);
 219         MC_FALLTHROUGH;
 220     case 12:
 221         widget_gotoyx (w, 12, 3);
 222         str_printf (buff, _ ("Accessed:   %s"), file_date (st.st_atime));
 223         tty_print_string (buff->str);
 224         g_string_set_size (buff, 0);
 225         MC_FALLTHROUGH;
 226     case 11:
 227         widget_gotoyx (w, 11, 3);
 228         str_printf (buff, _ ("Modified:   %s"), file_date (st.st_mtime));
 229         tty_print_string (buff->str);
 230         g_string_set_size (buff, 0);
 231         MC_FALLTHROUGH;
 232     case 10:
 233         widget_gotoyx (w, 10, 3);
 234         /* The field st_ctime is changed by writing or by setting inode
 235            information (i.e., owner, group, link count, mode, etc.).  */
 236         // TRANSLATORS: Time of last status change as in stat(2) man.
 237         str_printf (buff, _ ("Changed:    %s"), file_date (st.st_ctime));
 238         tty_print_string (buff->str);
 239         g_string_set_size (buff, 0);
 240         MC_FALLTHROUGH;
 241     case 9:
 242         widget_gotoyx (w, 9, 3);
 243 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 244         if (S_ISCHR (st.st_mode) || S_ISBLK (st.st_mode))
 245             tty_printf (_ ("Dev. type: major %lu, minor %lu"), (unsigned long) major (st.st_rdev),
 246                         (unsigned long) minor (st.st_rdev));
 247         else
 248 #endif
 249         {
 250             char buffer[10];
 251             size_trunc_len (buffer, 9, st.st_size, 0, panels_options.kilobyte_si);
 252             tty_printf (_ ("Size:       %s"), buffer);
 253 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
 254             tty_printf (ngettext (" (%lu block)", " (%lu blocks)", (unsigned long) st.st_blocks),
 255                         (unsigned long) st.st_blocks);
 256 #endif
 257         }
 258         MC_FALLTHROUGH;
 259     case 8:
 260         widget_gotoyx (w, 8, 3);
 261         tty_printf (_ ("Owner:      %s/%s"), get_owner (st.st_uid), get_group (st.st_gid));
 262         MC_FALLTHROUGH;
 263     case 7:
 264         widget_gotoyx (w, 7, 3);
 265         tty_printf (_ ("Links:      %d"), (int) st.st_nlink);
 266         MC_FALLTHROUGH;
 267     case 6:
 268         widget_gotoyx (w, 6, 3);
 269 
 270 #ifdef ENABLE_EXT2FS_ATTR
 271         {
 272             vfs_path_t *vpath;
 273             unsigned long attr;
 274 
 275             vpath = vfs_path_from_str (fe->fname->str);
 276 
 277             if (mc_fgetflags (vpath, &attr) == 0)
 278                 tty_printf (_ ("Attributes: %s"), chattr_get_as_str (attr));
 279             else
 280                 tty_print_string (_ ("Attributes: unavailable"));
 281 
 282             vfs_path_free (vpath, TRUE);
 283         }
 284 #else
 285         tty_print_string (_ ("Attributes: unavailable"));
 286 #endif
 287         MC_FALLTHROUGH;
 288     case 5:
 289         widget_gotoyx (w, 5, 3);
 290         tty_printf (_ ("Mode:       %s (%04o)"), string_perm (st.st_mode),
 291                     (unsigned) st.st_mode & 07777);
 292         MC_FALLTHROUGH;
 293     case 4:
 294         widget_gotoyx (w, 4, 3);
 295         tty_printf (_ ("Location:   %Xh:%Xh"), (unsigned int) st.st_dev, (unsigned int) st.st_ino);
 296         MC_FALLTHROUGH;
 297     case 3:
 298     {
 299         const char *fname;
 300 
 301         widget_gotoyx (w, 3, 2);
 302         fname = fe->fname->str;
 303         str_printf (buff, file_label, str_trunc (fname, w->cols - i18n_adjust));
 304         tty_print_string (buff->str);
 305     }
 306         MC_FALLTHROUGH;
 307     case 2:
 308         MC_FALLTHROUGH;
 309     case 1:
 310         MC_FALLTHROUGH;
 311     case 0:;
 312     }  // switch
 313     g_string_free (buff, TRUE);
 314 }
 315 
 316 /* --------------------------------------------------------------------------------------------- */
 317 
 318 static void
 319 info_hook (void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 320 {
 321     WInfo *info = (WInfo *) data;
 322     Widget *other_widget;
 323 
 324     other_widget = get_panel_widget (get_current_index ());
 325     if (!other_widget)
 326         return;
 327     if (widget_overlapped (WIDGET (info), other_widget))
 328         return;
 329 
 330     info->ready = TRUE;
 331     info_show_info (info);
 332 }
 333 
 334 /* --------------------------------------------------------------------------------------------- */
 335 
 336 static cb_ret_t
 337 info_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 338 {
 339     WInfo *info = (WInfo *) w;
 340 
 341     switch (msg)
 342     {
 343     case MSG_INIT:
 344         init_my_statfs ();
 345         add_hook (&select_file_hook, info_hook, info);
 346         info->ready = FALSE;
 347         return MSG_HANDLED;
 348 
 349     case MSG_DRAW:
 350         info_hook (info);
 351         return MSG_HANDLED;
 352 
 353     case MSG_DESTROY:
 354         delete_hook (&select_file_hook, info_hook);
 355         free_my_statfs ();
 356         return MSG_HANDLED;
 357 
 358     default:
 359         return widget_default_callback (w, sender, msg, parm, data);
 360     }
 361 }
 362 
 363 /* --------------------------------------------------------------------------------------------- */
 364 /*** public functions ****************************************************************************/
 365 /* --------------------------------------------------------------------------------------------- */
 366 
 367 WInfo *
 368 info_new (const WRect *r)
     /* [previous][next][first][last][top][bottom][index][help]  */
 369 {
 370     WInfo *info;
 371     Widget *w;
 372 
 373     info = g_new (struct WInfo, 1);
 374     w = WIDGET (info);
 375     widget_init (w, r, info_callback, NULL);
 376 
 377     return info;
 378 }
 379 
 380 /* --------------------------------------------------------------------------------------------- */

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