Revert "Try fix of compile warnings about assigned but unused variables"
[midnight-commander.git] / src / filemanager / info.c
bloba9899f8271463842b4725088da1e006148eafddc
1 /*
2 Panel managing.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2007, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file info.c
25 * \brief Source: panel managing
28 #include <config.h>
30 #include <stdio.h>
31 #include <sys/stat.h>
32 #include <inttypes.h> /* PRIuMAX */
34 #include "lib/global.h"
35 #include "lib/unixcompat.h"
36 #include "lib/tty/tty.h"
37 #include "lib/tty/key.h" /* is_idle() */
38 #include "lib/tty/mouse.h" /* Gpm_Event */
39 #include "lib/skin.h"
40 #include "lib/strutil.h"
41 #include "lib/timefmt.h" /* file_date() */
42 #include "lib/util.h"
43 #include "lib/widget.h"
45 #include "src/setup.h" /* panels_options */
47 #include "midnight.h" /* the_menubar */
48 #include "layout.h"
49 #include "mountlist.h"
50 #include "info.h"
52 /*** global variables ****************************************************************************/
54 /*** file scope macro definitions ****************************************************************/
56 #ifndef VERSION
57 #define VERSION "undefined"
58 #endif
60 /*** file scope type declarations ****************************************************************/
62 struct WInfo
64 Widget widget;
65 int ready;
68 /*** file scope variables ************************************************************************/
70 static struct my_statfs myfs_stats;
72 /*** file scope functions ************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
75 static void
76 info_box (struct WInfo *info)
78 const char *title = _("Information");
79 const int len = str_term_width1 (title);
81 tty_set_normal_attrs ();
82 tty_setcolor (NORMAL_COLOR);
83 widget_erase (&info->widget);
84 draw_box (info->widget.owner, info->widget.y, info->widget.x,
85 info->widget.lines, info->widget.cols, FALSE);
87 widget_move (&info->widget, 0, (info->widget.cols - len - 2) / 2);
88 tty_printf (" %s ", title);
90 widget_move (&info->widget, 2, 0);
91 tty_print_alt_char (ACS_LTEE, FALSE);
92 widget_move (&info->widget, 2, info->widget.cols - 1);
93 tty_print_alt_char (ACS_RTEE, FALSE);
94 tty_draw_hline (info->widget.y + 2, info->widget.x + 1, ACS_HLINE, info->widget.cols - 2);
97 /* --------------------------------------------------------------------------------------------- */
99 static void
100 info_show_info (struct WInfo *info)
102 static int i18n_adjust = 0;
103 static const char *file_label;
104 GString *buff;
105 struct stat st;
107 if (!is_idle ())
108 return;
110 info_box (info);
112 tty_setcolor (MARKED_COLOR);
113 widget_move (&info->widget, 1, 3);
114 tty_printf (_("Midnight Commander %s"), VERSION);
116 if (!info->ready)
117 return;
119 if (get_current_type () != view_listing)
120 return;
123 char *cwd_str;
125 cwd_str = vfs_path_to_str (current_panel->cwd_vpath);
126 my_statfs (&myfs_stats, cwd_str);
127 g_free (cwd_str);
130 st = current_panel->dir.list[current_panel->selected].st;
132 /* Print only lines which fit */
134 if (i18n_adjust == 0)
136 /* This printf pattern string is used as a reference for size */
137 file_label = _("File: %s");
138 i18n_adjust = str_term_width1 (file_label) + 2;
141 tty_setcolor (NORMAL_COLOR);
143 buff = g_string_new ("");
145 switch (info->widget.lines - 2)
147 /* Note: all cases are fall-throughs */
149 default:
151 case 16:
152 widget_move (&info->widget, 16, 3);
153 if (myfs_stats.nfree == 0 && myfs_stats.nodes == 0)
154 tty_print_string (_("No node information"));
155 else
156 tty_printf ("%s %" PRIuMAX "/%" PRIuMAX " (%d%%)",
157 _("Free nodes:"),
158 myfs_stats.nfree, myfs_stats.nodes,
159 myfs_stats.nodes == 0 ? 0 :
160 (int) (100 * (long double) myfs_stats.nfree / myfs_stats.nodes));
162 case 15:
163 widget_move (&info->widget, 15, 3);
164 if (myfs_stats.avail == 0 && myfs_stats.total == 0)
165 tty_print_string (_("No space information"));
166 else
168 char buffer1[6], buffer2[6];
170 size_trunc_len (buffer1, 5, myfs_stats.avail, 1, panels_options.kilobyte_si);
171 size_trunc_len (buffer2, 5, myfs_stats.total, 1, panels_options.kilobyte_si);
172 tty_printf (_("Free space: %s/%s (%d%%)"), buffer1, buffer2,
173 myfs_stats.total == 0 ? 0 :
174 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
177 case 14:
178 widget_move (&info->widget, 14, 3);
179 tty_printf (_("Type: %s"),
180 myfs_stats.typename ? myfs_stats.typename : _("non-local vfs"));
181 if (myfs_stats.type != 0xffff && myfs_stats.type != -1)
182 tty_printf (" (%Xh)", myfs_stats.type);
184 case 13:
185 widget_move (&info->widget, 13, 3);
186 str_printf (buff, _("Device: %s"),
187 str_trunc (myfs_stats.device, info->widget.cols - i18n_adjust));
188 tty_print_string (buff->str);
189 g_string_set_size (buff, 0);
190 case 12:
191 widget_move (&info->widget, 12, 3);
192 str_printf (buff, _("Filesystem: %s"),
193 str_trunc (myfs_stats.mpoint, info->widget.cols - i18n_adjust));
194 tty_print_string (buff->str);
195 g_string_set_size (buff, 0);
196 case 11:
197 widget_move (&info->widget, 11, 3);
198 str_printf (buff, _("Accessed: %s"), file_date (st.st_atime));
199 tty_print_string (buff->str);
200 g_string_set_size (buff, 0);
201 case 10:
202 widget_move (&info->widget, 10, 3);
203 str_printf (buff, _("Modified: %s"), file_date (st.st_mtime));
204 tty_print_string (buff->str);
205 g_string_set_size (buff, 0);
206 case 9:
207 widget_move (&info->widget, 9, 3);
208 /* The field st_ctime is changed by writing or by setting inode
209 information (i.e., owner, group, link count, mode, etc.). */
210 /* TRANSLATORS: Time of last status change as in stat(2) man. */
211 str_printf (buff, _("Changed: %s"), file_date (st.st_ctime));
212 tty_print_string (buff->str);
213 g_string_set_size (buff, 0);
215 case 8:
216 widget_move (&info->widget, 8, 3);
217 #ifdef HAVE_STRUCT_STAT_ST_RDEV
218 if (S_ISCHR (st.st_mode) || S_ISBLK (st.st_mode))
219 tty_printf (_("Dev. type: major %lu, minor %lu"),
220 (unsigned long) major (st.st_rdev), (unsigned long) minor (st.st_rdev));
221 else
222 #endif
224 char buffer[10];
225 size_trunc_len (buffer, 9, st.st_size, 0, panels_options.kilobyte_si);
226 tty_printf (_("Size: %s"), buffer);
227 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
228 tty_printf (ngettext (" (%ld block)", " (%ld blocks)",
229 (unsigned long) st.st_blocks), (unsigned long) st.st_blocks);
230 #endif
233 case 7:
234 widget_move (&info->widget, 7, 3);
235 tty_printf (_("Owner: %s/%s"), get_owner (st.st_uid), get_group (st.st_gid));
237 case 6:
238 widget_move (&info->widget, 6, 3);
239 tty_printf (_("Links: %d"), (int) st.st_nlink);
241 case 5:
242 widget_move (&info->widget, 5, 3);
243 tty_printf (_("Mode: %s (%04o)"),
244 string_perm (st.st_mode), (unsigned) st.st_mode & 07777);
246 case 4:
247 widget_move (&info->widget, 4, 3);
248 tty_printf (_("Location: %Xh:%Xh"), (int) st.st_dev, (int) st.st_ino);
250 case 3:
252 const char *fname;
254 widget_move (&info->widget, 3, 2);
255 fname = current_panel->dir.list[current_panel->selected].fname;
256 str_printf (buff, file_label, str_trunc (fname, info->widget.cols - i18n_adjust));
257 tty_print_string (buff->str);
260 case 2:
261 case 1:
262 case 0:
264 } /* switch */
265 g_string_free (buff, TRUE);
268 /* --------------------------------------------------------------------------------------------- */
270 static void
271 info_hook (void *data)
273 struct WInfo *info = (struct WInfo *) data;
274 Widget *other_widget;
276 other_widget = get_panel_widget (get_current_index ());
277 if (!other_widget)
278 return;
279 if (dlg_overlap (&info->widget, other_widget))
280 return;
282 info->ready = 1;
283 info_show_info (info);
286 /* --------------------------------------------------------------------------------------------- */
288 static cb_ret_t
289 info_callback (Widget * w, widget_msg_t msg, int parm)
291 struct WInfo *info = (struct WInfo *) w;
293 switch (msg)
296 case WIDGET_INIT:
297 init_my_statfs ();
298 add_hook (&select_file_hook, info_hook, info);
299 info->ready = 0;
300 return MSG_HANDLED;
302 case WIDGET_DRAW:
303 info_hook (info);
304 return MSG_HANDLED;
306 case WIDGET_FOCUS:
307 return MSG_NOT_HANDLED;
309 case WIDGET_DESTROY:
310 delete_hook (&select_file_hook, info_hook);
311 free_my_statfs ();
312 return MSG_HANDLED;
314 default:
315 return default_proc (msg, parm);
319 /* --------------------------------------------------------------------------------------------- */
321 static int
322 info_event (Gpm_Event * event, void *data)
324 Widget *w = &((WInfo *) data)->widget;
326 /* rest of the upper frame, the menu is invisible - call menu */
327 if (event->type & GPM_DOWN && event->y == 1 && !menubar_visible)
329 event->x += w->x;
330 return the_menubar->widget.mouse (event, the_menubar);
333 return MOU_NORMAL;
336 /* --------------------------------------------------------------------------------------------- */
337 /*** public functions ****************************************************************************/
338 /* --------------------------------------------------------------------------------------------- */
340 WInfo *
341 info_new (int y, int x, int lines, int cols)
343 struct WInfo *info = g_new (struct WInfo, 1);
345 init_widget (&info->widget, y, x, lines, cols, info_callback, info_event);
347 /* We do not want the cursor */
348 widget_want_cursor (info->widget, 0);
350 return info;
353 /* --------------------------------------------------------------------------------------------- */