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 (NORMAL_COLOR);
  91     widget_erase (w);
  92     tty_draw_box (w->rect.y, w->rect.x, w->rect.lines, w->rect.cols, FALSE);
  93 
  94     widget_gotoyx (w, 0, (w->rect.cols - len - 2) / 2);
  95     tty_printf (" %s ", title);
  96 
  97     widget_gotoyx (w, 2, 0);
  98     tty_print_alt_char (ACS_LTEE, FALSE);
  99     widget_gotoyx (w, 2, w->rect.cols - 1);
 100     tty_print_alt_char (ACS_RTEE, FALSE);
 101     tty_draw_hline (w->rect.y + 2, w->rect.x + 1, ACS_HLINE, w->rect.cols - 2);
 102 }
 103 
 104 /* --------------------------------------------------------------------------------------------- */
 105 
 106 static void
 107 info_show_info (WInfo *info)
     /* [previous][next][first][last][top][bottom][index][help]  */
 108 {
 109     const WRect *w = &CONST_WIDGET (info)->rect;
 110     const file_entry_t *fe;
 111     static int i18n_adjust = 0;
 112     static const char *file_label;
 113     GString *buff;
 114     struct stat st;
 115     char rp_cwd[PATH_MAX];
 116     const char *p_rp_cwd;
 117 
 118     if (!is_idle ())
 119         return;
 120 
 121     info_box (info);
 122 
 123     tty_setcolor (MARKED_COLOR);
 124     widget_gotoyx (w, 1, 3);
 125     tty_printf (_ ("Midnight Commander %s"), mc_global.mc_version);
 126 
 127     if (!info->ready)
 128         return;
 129 
 130     if (get_current_type () != view_listing)
 131         return;
 132 
 133     // don't rely on vpath CWD when cd_symlinks enabled
 134     p_rp_cwd = mc_realpath (vfs_path_as_str (current_panel->cwd_vpath), rp_cwd);
 135     if (p_rp_cwd == NULL)
 136         p_rp_cwd = vfs_path_as_str (current_panel->cwd_vpath);
 137 
 138     my_statfs (&myfs_stats, p_rp_cwd);
 139 
 140     fe = panel_current_entry (current_panel);
 141     if (fe == NULL)
 142         return;
 143 
 144     st = fe->st;
 145 
 146     // Print only lines which fit
 147 
 148     if (i18n_adjust == 0)
 149     {
 150         // This printf pattern string is used as a reference for size
 151         file_label = _ ("File: %s");
 152         i18n_adjust = str_term_width1 (file_label) + 2;
 153     }
 154 
 155     tty_setcolor (NORMAL_COLOR);
 156 
 157     buff = g_string_new ("");
 158 
 159     switch (w->lines - 2)
 160     {
 161         // Note: all cases are fall-throughs
 162 
 163     default:
 164         MC_FALLTHROUGH;
 165     case 17:
 166         widget_gotoyx (w, 17, 3);
 167         if ((myfs_stats.nfree == 0 && myfs_stats.nodes == 0)
 168             || (myfs_stats.nfree == (uintmax_t) (-1) && myfs_stats.nodes == (uintmax_t) (-1)))
 169             tty_print_string (_ ("No node information"));
 170         else if (myfs_stats.nfree == (uintmax_t) (-1))
 171             tty_printf ("%s - / %" PRIuMAX, _ ("Free nodes:"), myfs_stats.nodes);
 172         else if (myfs_stats.nodes == (uintmax_t) (-1))
 173             tty_printf ("%s %" PRIuMAX " / -", _ ("Free nodes:"), myfs_stats.nfree);
 174         else
 175             tty_printf ("%s %" PRIuMAX " / %" PRIuMAX " (%d%%)", _ ("Free nodes:"),
 176                         myfs_stats.nfree, myfs_stats.nodes,
 177                         myfs_stats.nodes == 0
 178                             ? 0
 179                             : (int) (100 * (long double) myfs_stats.nfree / myfs_stats.nodes));
 180         MC_FALLTHROUGH;
 181     case 16:
 182         widget_gotoyx (w, 16, 3);
 183         if (myfs_stats.avail == 0 && myfs_stats.total == 0)
 184             tty_print_string (_ ("No space information"));
 185         else
 186         {
 187             char buffer1[6], buffer2[6];
 188 
 189             size_trunc_len (buffer1, 5, myfs_stats.avail, 1, panels_options.kilobyte_si);
 190             size_trunc_len (buffer2, 5, myfs_stats.total, 1, panels_options.kilobyte_si);
 191             tty_printf (_ ("Free space: %s / %s (%d%%)"), buffer1, buffer2,
 192                         myfs_stats.total == 0
 193                             ? 0
 194                             : (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
 195         }
 196         MC_FALLTHROUGH;
 197     case 15:
 198         widget_gotoyx (w, 15, 3);
 199         tty_printf (_ ("Type:       %s"),
 200                     myfs_stats.typename ? myfs_stats.typename : _ ("non-local vfs"));
 201         if (myfs_stats.type != 0xffff && myfs_stats.type != -1)
 202             tty_printf (" (%Xh)", (unsigned int) myfs_stats.type);
 203         MC_FALLTHROUGH;
 204     case 14:
 205         widget_gotoyx (w, 14, 3);
 206         str_printf (buff, _ ("Device:     %s"),
 207                     str_trunc (myfs_stats.device, w->cols - i18n_adjust));
 208         tty_print_string (buff->str);
 209         g_string_set_size (buff, 0);
 210         MC_FALLTHROUGH;
 211     case 13:
 212         widget_gotoyx (w, 13, 3);
 213         str_printf (buff, _ ("Filesystem: %s"),
 214                     str_trunc (myfs_stats.mpoint, w->cols - i18n_adjust));
 215         tty_print_string (buff->str);
 216         g_string_set_size (buff, 0);
 217         MC_FALLTHROUGH;
 218     case 12:
 219         widget_gotoyx (w, 12, 3);
 220         str_printf (buff, _ ("Accessed:   %s"), file_date (st.st_atime));
 221         tty_print_string (buff->str);
 222         g_string_set_size (buff, 0);
 223         MC_FALLTHROUGH;
 224     case 11:
 225         widget_gotoyx (w, 11, 3);
 226         str_printf (buff, _ ("Modified:   %s"), file_date (st.st_mtime));
 227         tty_print_string (buff->str);
 228         g_string_set_size (buff, 0);
 229         MC_FALLTHROUGH;
 230     case 10:
 231         widget_gotoyx (w, 10, 3);
 232         /* The field st_ctime is changed by writing or by setting inode
 233            information (i.e., owner, group, link count, mode, etc.).  */
 234         // TRANSLATORS: Time of last status change as in stat(2) man.
 235         str_printf (buff, _ ("Changed:    %s"), file_date (st.st_ctime));
 236         tty_print_string (buff->str);
 237         g_string_set_size (buff, 0);
 238         MC_FALLTHROUGH;
 239     case 9:
 240         widget_gotoyx (w, 9, 3);
 241 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 242         if (S_ISCHR (st.st_mode) || S_ISBLK (st.st_mode))
 243             tty_printf (_ ("Dev. type: major %lu, minor %lu"), (unsigned long) major (st.st_rdev),
 244                         (unsigned long) minor (st.st_rdev));
 245         else
 246 #endif
 247         {
 248             char buffer[10];
 249             size_trunc_len (buffer, 9, st.st_size, 0, panels_options.kilobyte_si);
 250             tty_printf (_ ("Size:       %s"), buffer);
 251 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
 252             tty_printf (ngettext (" (%lu block)", " (%lu blocks)", (unsigned long) st.st_blocks),
 253                         (unsigned long) st.st_blocks);
 254 #endif
 255         }
 256         MC_FALLTHROUGH;
 257     case 8:
 258         widget_gotoyx (w, 8, 3);
 259         tty_printf (_ ("Owner:      %s/%s"), get_owner (st.st_uid), get_group (st.st_gid));
 260         MC_FALLTHROUGH;
 261     case 7:
 262         widget_gotoyx (w, 7, 3);
 263         tty_printf (_ ("Links:      %d"), (int) st.st_nlink);
 264         MC_FALLTHROUGH;
 265     case 6:
 266         widget_gotoyx (w, 6, 3);
 267 
 268 #ifdef ENABLE_EXT2FS_ATTR
 269         {
 270             vfs_path_t *vpath;
 271             unsigned long attr;
 272 
 273             vpath = vfs_path_from_str (fe->fname->str);
 274 
 275             if (mc_fgetflags (vpath, &attr) == 0)
 276                 tty_printf (_ ("Attributes: %s"), chattr_get_as_str (attr));
 277             else
 278                 tty_print_string (_ ("Attributes: unavailable"));
 279 
 280             vfs_path_free (vpath, TRUE);
 281         }
 282 #else
 283         tty_print_string (_ ("Attributes: unavailable"));
 284 #endif
 285         MC_FALLTHROUGH;
 286     case 5:
 287         widget_gotoyx (w, 5, 3);
 288         tty_printf (_ ("Mode:       %s (%04o)"), string_perm (st.st_mode),
 289                     (unsigned) st.st_mode & 07777);
 290         MC_FALLTHROUGH;
 291     case 4:
 292         widget_gotoyx (w, 4, 3);
 293         tty_printf (_ ("Location:   %Xh:%Xh"), (unsigned int) st.st_dev, (unsigned int) st.st_ino);
 294         MC_FALLTHROUGH;
 295     case 3:
 296     {
 297         const char *fname;
 298 
 299         widget_gotoyx (w, 3, 2);
 300         fname = fe->fname->str;
 301         str_printf (buff, file_label, str_trunc (fname, w->cols - i18n_adjust));
 302         tty_print_string (buff->str);
 303     }
 304         MC_FALLTHROUGH;
 305     case 2:
 306         MC_FALLTHROUGH;
 307     case 1:
 308         MC_FALLTHROUGH;
 309     case 0:;
 310     }  // switch
 311     g_string_free (buff, TRUE);
 312 }
 313 
 314 /* --------------------------------------------------------------------------------------------- */
 315 
 316 static void
 317 info_hook (void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 318 {
 319     WInfo *info = (WInfo *) data;
 320     Widget *other_widget;
 321 
 322     other_widget = get_panel_widget (get_current_index ());
 323     if (!other_widget)
 324         return;
 325     if (widget_overlapped (WIDGET (info), other_widget))
 326         return;
 327 
 328     info->ready = TRUE;
 329     info_show_info (info);
 330 }
 331 
 332 /* --------------------------------------------------------------------------------------------- */
 333 
 334 static cb_ret_t
 335 info_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 336 {
 337     WInfo *info = (WInfo *) w;
 338 
 339     switch (msg)
 340     {
 341     case MSG_INIT:
 342         init_my_statfs ();
 343         add_hook (&select_file_hook, info_hook, info);
 344         info->ready = FALSE;
 345         return MSG_HANDLED;
 346 
 347     case MSG_DRAW:
 348         info_hook (info);
 349         return MSG_HANDLED;
 350 
 351     case MSG_DESTROY:
 352         delete_hook (&select_file_hook, info_hook);
 353         free_my_statfs ();
 354         return MSG_HANDLED;
 355 
 356     default:
 357         return widget_default_callback (w, sender, msg, parm, data);
 358     }
 359 }
 360 
 361 /* --------------------------------------------------------------------------------------------- */
 362 /*** public functions ****************************************************************************/
 363 /* --------------------------------------------------------------------------------------------- */
 364 
 365 WInfo *
 366 info_new (const WRect *r)
     /* [previous][next][first][last][top][bottom][index][help]  */
 367 {
 368     WInfo *info;
 369     Widget *w;
 370 
 371     info = g_new (struct WInfo, 1);
 372     w = WIDGET (info);
 373     widget_init (w, r, info_callback, NULL);
 374 
 375     return info;
 376 }
 377 
 378 /* --------------------------------------------------------------------------------------------- */

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