2e891e0d48f053c03aa4ce9f8636195f7b9741b2
[midnight-commander.git] / src / filemanager / info.c
blob2e891e0d48f053c03aa4ce9f8636195f7b9741b2
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 (WInfo *info)
78 Widget *w = WIDGET (info);
80 const char *title = _("Information");
81 const int len = str_term_width1 (title);
83 tty_set_normal_attrs ();
84 tty_setcolor (NORMAL_COLOR);
85 widget_erase (w);
86 draw_box (w->owner, w->y, w->x, w->lines, w->cols, FALSE);
88 widget_move (w, 0, (w->cols - len - 2) / 2);
89 tty_printf (" %s ", title);
91 widget_move (w, 2, 0);
92 tty_print_alt_char (ACS_LTEE, FALSE);
93 widget_move (w, 2, w->cols - 1);
94 tty_print_alt_char (ACS_RTEE, FALSE);
95 tty_draw_hline (w->y + 2, w->x + 1, ACS_HLINE, w->cols - 2);
98 /* --------------------------------------------------------------------------------------------- */
100 static void
101 info_show_info (WInfo *info)
103 Widget *w = WIDGET (info);
104 static int i18n_adjust = 0;
105 static const char *file_label;
106 GString *buff;
107 struct stat st;
109 if (!is_idle ())
110 return;
112 info_box (info);
114 tty_setcolor (MARKED_COLOR);
115 widget_move (w, 1, 3);
116 tty_printf (_("Midnight Commander %s"), VERSION);
118 if (!info->ready)
119 return;
121 if (get_current_type () != view_listing)
122 return;
125 char *cwd_str;
127 cwd_str = vfs_path_to_str (current_panel->cwd_vpath);
128 my_statfs (&myfs_stats, cwd_str);
129 g_free (cwd_str);
132 st = current_panel->dir.list[current_panel->selected].st;
134 /* Print only lines which fit */
136 if (i18n_adjust == 0)
138 /* This printf pattern string is used as a reference for size */
139 file_label = _("File: %s");
140 i18n_adjust = str_term_width1 (file_label) + 2;
143 tty_setcolor (NORMAL_COLOR);
145 buff = g_string_new ("");
147 switch (w->lines - 2)
149 /* Note: all cases are fall-throughs */
151 default:
153 case 16:
154 widget_move (w, 16, 3);
155 if (myfs_stats.nfree == 0 && myfs_stats.nodes == 0)
156 tty_print_string (_("No node information"));
157 else
158 tty_printf ("%s %" PRIuMAX "/%" PRIuMAX " (%d%%)",
159 _("Free nodes:"),
160 myfs_stats.nfree, myfs_stats.nodes,
161 myfs_stats.nodes == 0 ? 0 :
162 (int) (100 * (long double) myfs_stats.nfree / myfs_stats.nodes));
164 case 15:
165 widget_move (w, 15, 3);
166 if (myfs_stats.avail == 0 && myfs_stats.total == 0)
167 tty_print_string (_("No space information"));
168 else
170 char buffer1[6], buffer2[6];
172 size_trunc_len (buffer1, 5, myfs_stats.avail, 1, panels_options.kilobyte_si);
173 size_trunc_len (buffer2, 5, myfs_stats.total, 1, panels_options.kilobyte_si);
174 tty_printf (_("Free space: %s/%s (%d%%)"), buffer1, buffer2,
175 myfs_stats.total == 0 ? 0 :
176 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
179 case 14:
180 widget_move (w, 14, 3);
181 tty_printf (_("Type: %s"),
182 myfs_stats.typename ? myfs_stats.typename : _("non-local vfs"));
183 if (myfs_stats.type != 0xffff && myfs_stats.type != -1)
184 tty_printf (" (%Xh)", myfs_stats.type);
186 case 13:
187 widget_move (w, 13, 3);
188 str_printf (buff, _("Device: %s"),
189 str_trunc (myfs_stats.device, w->cols - i18n_adjust));
190 tty_print_string (buff->str);
191 g_string_set_size (buff, 0);
192 case 12:
193 widget_move (w, 12, 3);
194 str_printf (buff, _("Filesystem: %s"),
195 str_trunc (myfs_stats.mpoint, w->cols - i18n_adjust));
196 tty_print_string (buff->str);
197 g_string_set_size (buff, 0);
198 case 11:
199 widget_move (w, 11, 3);
200 str_printf (buff, _("Accessed: %s"), file_date (st.st_atime));
201 tty_print_string (buff->str);
202 g_string_set_size (buff, 0);
203 case 10:
204 widget_move (w, 10, 3);
205 str_printf (buff, _("Modified: %s"), file_date (st.st_mtime));
206 tty_print_string (buff->str);
207 g_string_set_size (buff, 0);
208 case 9:
209 widget_move (w, 9, 3);
210 /* The field st_ctime is changed by writing or by setting inode
211 information (i.e., owner, group, link count, mode, etc.). */
212 /* TRANSLATORS: Time of last status change as in stat(2) man. */
213 str_printf (buff, _("Changed: %s"), file_date (st.st_ctime));
214 tty_print_string (buff->str);
215 g_string_set_size (buff, 0);
217 case 8:
218 widget_move (w, 8, 3);
219 #ifdef HAVE_STRUCT_STAT_ST_RDEV
220 if (S_ISCHR (st.st_mode) || S_ISBLK (st.st_mode))
221 tty_printf (_("Dev. type: major %lu, minor %lu"),
222 (unsigned long) major (st.st_rdev), (unsigned long) minor (st.st_rdev));
223 else
224 #endif
226 char buffer[10];
227 size_trunc_len (buffer, 9, st.st_size, 0, panels_options.kilobyte_si);
228 tty_printf (_("Size: %s"), buffer);
229 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
230 tty_printf (ngettext (" (%ld block)", " (%ld blocks)",
231 (unsigned long) st.st_blocks), (unsigned long) st.st_blocks);
232 #endif
235 case 7:
236 widget_move (w, 7, 3);
237 tty_printf (_("Owner: %s/%s"), get_owner (st.st_uid), get_group (st.st_gid));
239 case 6:
240 widget_move (w, 6, 3);
241 tty_printf (_("Links: %d"), (int) st.st_nlink);
243 case 5:
244 widget_move (w, 5, 3);
245 tty_printf (_("Mode: %s (%04o)"),
246 string_perm (st.st_mode), (unsigned) st.st_mode & 07777);
248 case 4:
249 widget_move (w, 4, 3);
250 tty_printf (_("Location: %Xh:%Xh"), (int) st.st_dev, (int) st.st_ino);
252 case 3:
254 const char *fname;
256 widget_move (w, 3, 2);
257 fname = current_panel->dir.list[current_panel->selected].fname;
258 str_printf (buff, file_label, str_trunc (fname, w->cols - i18n_adjust));
259 tty_print_string (buff->str);
262 case 2:
263 case 1:
264 case 0:
266 } /* switch */
267 g_string_free (buff, TRUE);
270 /* --------------------------------------------------------------------------------------------- */
272 static void
273 info_hook (void *data)
275 struct WInfo *info = (struct WInfo *) data;
276 Widget *other_widget;
278 other_widget = get_panel_widget (get_current_index ());
279 if (!other_widget)
280 return;
281 if (dlg_overlap (WIDGET (info), other_widget))
282 return;
284 info->ready = 1;
285 info_show_info (info);
288 /* --------------------------------------------------------------------------------------------- */
290 static cb_ret_t
291 info_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
293 struct WInfo *info = (struct WInfo *) w;
295 switch (msg)
297 case MSG_INIT:
298 init_my_statfs ();
299 add_hook (&select_file_hook, info_hook, info);
300 info->ready = 0;
301 return MSG_HANDLED;
303 case MSG_DRAW:
304 info_hook (info);
305 return MSG_HANDLED;
307 case MSG_FOCUS:
308 return MSG_NOT_HANDLED;
310 case MSG_DESTROY:
311 delete_hook (&select_file_hook, info_hook);
312 free_my_statfs ();
313 return MSG_HANDLED;
315 default:
316 return widget_default_callback (w, sender, msg, parm, data);
320 /* --------------------------------------------------------------------------------------------- */
321 /*** public functions ****************************************************************************/
322 /* --------------------------------------------------------------------------------------------- */
324 WInfo *
325 info_new (int y, int x, int lines, int cols)
327 WInfo *info;
328 Widget *w;
330 info = g_new (struct WInfo, 1);
331 w = WIDGET (info);
332 init_widget (w, y, x, lines, cols, info_callback, NULL);
333 /* We do not want the cursor */
334 widget_want_cursor (w, FALSE);
336 return info;
339 /* --------------------------------------------------------------------------------------------- */