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

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