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-2020
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2013
   9    Andrew Borodin <aborodin@vmail.ru>, 2013
  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 <http://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 #ifdef ENABLE_EXT2FS_ATTR
  38 #include <e2p/e2p.h>            /* fgetflags() */
  39 #endif
  40 
  41 #include "lib/global.h"
  42 #include "lib/unixcompat.h"
  43 #include "lib/tty/tty.h"
  44 #include "lib/tty/key.h"        /* is_idle() */
  45 #include "lib/skin.h"
  46 #include "lib/strutil.h"
  47 #include "lib/timefmt.h"        /* file_date() */
  48 #include "lib/util.h"
  49 #include "lib/widget.h"
  50 
  51 #include "src/setup.h"          /* panels_options */
  52 
  53 #include "midnight.h"           /* the_menubar */
  54 #include "layout.h"
  55 #include "mountlist.h"
  56 #ifdef ENABLE_EXT2FS_ATTR
  57 #include "chattr.h"
  58 #endif
  59 
  60 #include "info.h"
  61 
  62 /*** global variables ****************************************************************************/
  63 
  64 /*** file scope macro definitions ****************************************************************/
  65 
  66 #ifndef VERSION
  67 #define VERSION "undefined"
  68 #endif
  69 
  70 /*** file scope type declarations ****************************************************************/
  71 
  72 struct WInfo
  73 {
  74     Widget widget;
  75     gboolean ready;
  76 };
  77 
  78 /*** file scope variables ************************************************************************/
  79 
  80 static struct my_statfs myfs_stats;
  81 
  82 /*** file scope functions ************************************************************************/
  83 /* --------------------------------------------------------------------------------------------- */
  84 
  85 static void
  86 info_box (WInfo * info)
     /* [previous][next][first][last][top][bottom][index][help]  */
  87 {
  88     Widget *w = WIDGET (info);
  89 
  90     const char *title = _("Information");
  91     const int len = str_term_width1 (title);
  92 
  93     tty_set_normal_attrs ();
  94     tty_setcolor (NORMAL_COLOR);
  95     widget_erase (w);
  96     tty_draw_box (w->y, w->x, w->lines, w->cols, FALSE);
  97 
  98     widget_gotoyx (w, 0, (w->cols - len - 2) / 2);
  99     tty_printf (" %s ", title);
 100 
 101     widget_gotoyx (w, 2, 0);
 102     tty_print_alt_char (ACS_LTEE, FALSE);
 103     widget_gotoyx (w, 2, w->cols - 1);
 104     tty_print_alt_char (ACS_RTEE, FALSE);
 105     tty_draw_hline (w->y + 2, w->x + 1, ACS_HLINE, w->cols - 2);
 106 }
 107 
 108 /* --------------------------------------------------------------------------------------------- */
 109 
 110 static void
 111 info_show_info (WInfo * info)
     /* [previous][next][first][last][top][bottom][index][help]  */
 112 {
 113     Widget *w = WIDGET (info);
 114     static int i18n_adjust = 0;
 115     static const char *file_label;
 116     GString *buff;
 117     struct stat st;
 118     char rp_cwd[PATH_MAX];
 119     const char *p_rp_cwd;
 120 
 121     if (!is_idle ())
 122         return;
 123 
 124     info_box (info);
 125 
 126     tty_setcolor (MARKED_COLOR);
 127     widget_gotoyx (w, 1, 3);
 128     tty_printf (_("Midnight Commander %s"), VERSION);
 129 
 130     if (!info->ready)
 131         return;
 132 
 133     if (get_current_type () != view_listing)
 134         return;
 135 
 136     /* don't rely on vpath CWD when cd_symlinks enabled */
 137     p_rp_cwd = mc_realpath (vfs_path_as_str (current_panel->cwd_vpath), rp_cwd);
 138     if (p_rp_cwd == NULL)
 139         p_rp_cwd = vfs_path_as_str (current_panel->cwd_vpath);
 140 
 141     my_statfs (&myfs_stats, p_rp_cwd);
 142 
 143     st = current_panel->dir.list[current_panel->selected].st;
 144 
 145     /* Print only lines which fit */
 146 
 147     if (i18n_adjust == 0)
 148     {
 149         /* This printf pattern string is used as a reference for size */
 150         file_label = _("File: %s");
 151         i18n_adjust = str_term_width1 (file_label) + 2;
 152     }
 153 
 154     tty_setcolor (NORMAL_COLOR);
 155 
 156     buff = g_string_new ("");
 157 
 158     switch (w->lines - 2)
 159     {
 160         /* Note: all cases are fall-throughs */
 161 
 162     default:
 163         MC_FALLTHROUGH;
 164     case 17:
 165         widget_gotoyx (w, 17, 3);
 166         if ((myfs_stats.nfree == 0 && myfs_stats.nodes == 0) ||
 167             (myfs_stats.nfree == (uintmax_t) (-1) && myfs_stats.nodes == (uintmax_t) (-1)))
 168             tty_print_string (_("No node information"));
 169         else if (myfs_stats.nfree == (uintmax_t) (-1))
 170             tty_printf ("%s -/%" PRIuMAX, _("Free nodes:"), myfs_stats.nodes);
 171         else if (myfs_stats.nodes == (uintmax_t) (-1))
 172             tty_printf ("%s %" PRIuMAX "/-", _("Free nodes:"), myfs_stats.nfree);
 173         else
 174             tty_printf ("%s %" PRIuMAX "/%" PRIuMAX " (%d%%)",
 175                         _("Free nodes:"),
 176                         myfs_stats.nfree, myfs_stats.nodes,
 177                         myfs_stats.nodes == 0 ? 0 :
 178                         (int) (100 * (long double) myfs_stats.nfree / myfs_stats.nodes));
 179         MC_FALLTHROUGH;
 180     case 16:
 181         widget_gotoyx (w, 16, 3);
 182         if (myfs_stats.avail == 0 && myfs_stats.total == 0)
 183             tty_print_string (_("No space information"));
 184         else
 185         {
 186             char buffer1[6], buffer2[6];
 187 
 188             size_trunc_len (buffer1, 5, myfs_stats.avail, 1, panels_options.kilobyte_si);
 189             size_trunc_len (buffer2, 5, myfs_stats.total, 1, panels_options.kilobyte_si);
 190             tty_printf (_("Free space: %s/%s (%d%%)"), buffer1, buffer2,
 191                         myfs_stats.total == 0 ? 0 :
 192                         (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
 193         }
 194         MC_FALLTHROUGH;
 195     case 15:
 196         widget_gotoyx (w, 15, 3);
 197         tty_printf (_("Type:       %s"),
 198                     myfs_stats.typename ? myfs_stats.typename : _("non-local vfs"));
 199         if (myfs_stats.type != 0xffff && myfs_stats.type != -1)
 200             tty_printf (" (%Xh)", (unsigned int) myfs_stats.type);
 201         MC_FALLTHROUGH;
 202     case 14:
 203         widget_gotoyx (w, 14, 3);
 204         str_printf (buff, _("Device:     %s"),
 205                     str_trunc (myfs_stats.device, w->cols - i18n_adjust));
 206         tty_print_string (buff->str);
 207         g_string_set_size (buff, 0);
 208         MC_FALLTHROUGH;
 209     case 13:
 210         widget_gotoyx (w, 13, 3);
 211         str_printf (buff, _("Filesystem: %s"),
 212                     str_trunc (myfs_stats.mpoint, w->cols - i18n_adjust));
 213         tty_print_string (buff->str);
 214         g_string_set_size (buff, 0);
 215         MC_FALLTHROUGH;
 216     case 12:
 217         widget_gotoyx (w, 12, 3);
 218         str_printf (buff, _("Accessed:   %s"), file_date (st.st_atime));
 219         tty_print_string (buff->str);
 220         g_string_set_size (buff, 0);
 221         MC_FALLTHROUGH;
 222     case 11:
 223         widget_gotoyx (w, 11, 3);
 224         str_printf (buff, _("Modified:   %s"), file_date (st.st_mtime));
 225         tty_print_string (buff->str);
 226         g_string_set_size (buff, 0);
 227         MC_FALLTHROUGH;
 228     case 10:
 229         widget_gotoyx (w, 10, 3);
 230         /* The field st_ctime is changed by writing or by setting inode
 231            information (i.e., owner, group, link count, mode, etc.).  */
 232         /* TRANSLATORS: Time of last status change as in stat(2) man. */
 233         str_printf (buff, _("Changed:    %s"), file_date (st.st_ctime));
 234         tty_print_string (buff->str);
 235         g_string_set_size (buff, 0);
 236         MC_FALLTHROUGH;
 237     case 9:
 238         widget_gotoyx (w, 9, 3);
 239 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 240         if (S_ISCHR (st.st_mode) || S_ISBLK (st.st_mode))
 241             tty_printf (_("Dev. type: major %lu, minor %lu"),
 242                         (unsigned long) major (st.st_rdev), (unsigned long) minor (st.st_rdev));
 243         else
 244 #endif
 245         {
 246             char buffer[10];
 247             size_trunc_len (buffer, 9, st.st_size, 0, panels_options.kilobyte_si);
 248             tty_printf (_("Size:       %s"), buffer);
 249 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
 250             tty_printf (ngettext (" (%lu block)", " (%lu blocks)",
 251                                   (unsigned long) st.st_blocks), (unsigned long) st.st_blocks);
 252 #endif
 253         }
 254         MC_FALLTHROUGH;
 255     case 8:
 256         widget_gotoyx (w, 8, 3);
 257         tty_printf (_("Owner:      %s/%s"), get_owner (st.st_uid), get_group (st.st_gid));
 258         MC_FALLTHROUGH;
 259     case 7:
 260         widget_gotoyx (w, 7, 3);
 261         tty_printf (_("Links:      %d"), (int) st.st_nlink);
 262         MC_FALLTHROUGH;
 263     case 6:
 264         widget_gotoyx (w, 6, 3);
 265 #ifdef ENABLE_EXT2FS_ATTR
 266         if (!vfs_current_is_local ())
 267             tty_print_string (_("Attributes: not supported"));
 268         else
 269         {
 270             vfs_path_t *vpath;
 271             unsigned long attr;
 272 
 273             vpath = vfs_path_from_str (current_panel->dir.list[current_panel->selected].fname);
 274 
 275             if (fgetflags (vfs_path_as_str (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);
 281         }
 282 #else
 283         tty_print_string (_("Attributes: not supported"));
 284 #endif
 285         MC_FALLTHROUGH;
 286     case 5:
 287         widget_gotoyx (w, 5, 3);
 288         tty_printf (_("Mode:       %s (%04o)"),
 289                     string_perm (st.st_mode), (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 = current_panel->dir.list[current_panel->selected].fname;
 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         ;
 311     }                           /* switch */
 312     g_string_free (buff, TRUE);
 313 }
 314 
 315 /* --------------------------------------------------------------------------------------------- */
 316 
 317 static void
 318 info_hook (void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 319 {
 320     WInfo *info = (WInfo *) data;
 321     Widget *other_widget;
 322 
 323     other_widget = get_panel_widget (get_current_index ());
 324     if (!other_widget)
 325         return;
 326     if (widget_overlapped (WIDGET (info), other_widget))
 327         return;
 328 
 329     info->ready = TRUE;
 330     info_show_info (info);
 331 }
 332 
 333 /* --------------------------------------------------------------------------------------------- */
 334 
 335 static cb_ret_t
 336 info_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 337 {
 338     WInfo *info = (WInfo *) w;
 339 
 340     switch (msg)
 341     {
 342     case MSG_INIT:
 343         init_my_statfs ();
 344         add_hook (&select_file_hook, info_hook, info);
 345         info->ready = FALSE;
 346         return MSG_HANDLED;
 347 
 348     case MSG_DRAW:
 349         info_hook (info);
 350         return MSG_HANDLED;
 351 
 352     case MSG_DESTROY:
 353         delete_hook (&select_file_hook, info_hook);
 354         free_my_statfs ();
 355         return MSG_HANDLED;
 356 
 357     default:
 358         return widget_default_callback (w, sender, msg, parm, data);
 359     }
 360 }
 361 
 362 /* --------------------------------------------------------------------------------------------- */
 363 /*** public functions ****************************************************************************/
 364 /* --------------------------------------------------------------------------------------------- */
 365 
 366 WInfo *
 367 info_new (int y, int x, int lines, int cols)
     /* [previous][next][first][last][top][bottom][index][help]  */
 368 {
 369     WInfo *info;
 370     Widget *w;
 371 
 372     info = g_new (struct WInfo, 1);
 373     w = WIDGET (info);
 374     widget_init (w, y, x, lines, cols, info_callback, NULL);
 375 
 376     return info;
 377 }
 378 
 379 /* --------------------------------------------------------------------------------------------- */

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