Ticket #1790: mc crashes on start
[midnight-commander.git] / src / info.c
blob2cbc76dd44b08bcac983f8f7e828a755ea027d61
1 /* Panel managing.
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /** \file info.c
20 * \brief Source: panel managing
23 #include <config.h>
25 #include <stdio.h>
27 #include "global.h"
29 #include "../src/tty/tty.h"
30 #include "../src/tty/key.h" /* is_idle() */
31 #include "../src/tty/mouse.h" /* Gpm_Event */
32 #include "../src/skin/skin.h"
34 #include "dialog.h"
35 #include "widget.h" /* default_proc*/
36 #include "main-widgets.h" /* the_menubar*/
37 #include "dir.h" /* required by panel */
38 #include "panel.h" /* for the panel structure */
39 #include "main.h" /* other_panel, current_panel definitions */
40 #include "menu.h" /* menubar_visible */
41 #include "util.h" /* size_trunc_len */
42 #include "layout.h"
43 #include "mountlist.h"
44 #include "unixcompat.h"
45 #include "strutil.h"
46 #include "info.h"
48 #ifndef VERSION
49 # define VERSION "undefined"
50 #endif
52 struct WInfo {
53 Widget widget;
54 int ready;
57 /* Have we called the init_my_statfs routine? */
58 static gboolean initialized = FALSE;
59 static struct my_statfs myfs_stats;
61 static void info_box (Dlg_head *h, struct WInfo *info)
63 tty_set_normal_attrs ();
64 tty_setcolor (NORMAL_COLOR);
65 widget_erase (&info->widget);
66 draw_box (h, info->widget.y, info->widget.x,
67 info->widget.lines, info->widget.cols);
70 static void
71 info_show_info (struct WInfo *info)
73 static int i18n_adjust = 0;
74 static const char *file_label;
75 GString *buff;
76 struct stat st;
78 if (!is_idle ())
79 return;
81 info_box (info->widget.parent, info);
82 tty_setcolor (MARKED_COLOR);
83 widget_move (&info->widget, 1, 3);
84 tty_printf (_("Midnight Commander %s"), VERSION);
85 tty_setcolor (NORMAL_COLOR);
86 tty_draw_hline (info->widget.y + 2, info->widget.x + 1,
87 ACS_HLINE, info->widget.cols - 2);
88 if (get_current_type () != view_listing)
89 return;
91 if (!info->ready)
92 return;
94 my_statfs (&myfs_stats, current_panel->cwd);
95 st = current_panel->dir.list [current_panel->selected].st;
97 /* Print only lines which fit */
99 if (i18n_adjust == 0) {
100 /* This printf pattern string is used as a reference for size */
101 file_label = _("File: %s");
102 i18n_adjust = str_term_width1 (file_label) + 2;
105 buff = g_string_new ("");
107 switch (info->widget.lines-2){
108 /* Note: all cases are fall-throughs */
110 default:
112 case 16:
113 widget_move (&info->widget, 16, 3);
114 if (myfs_stats.nfree >0 || myfs_stats.nodes > 0)
115 tty_printf (_("Free nodes: %d (%d%%) of %d"),
116 myfs_stats.nfree,
117 myfs_stats.total
118 ? 100 * myfs_stats.nfree / myfs_stats.nodes : 0,
119 myfs_stats.nodes);
120 else
121 tty_print_string (_("No node information"));
123 case 15:
124 widget_move (&info->widget, 15, 3);
125 if (myfs_stats.avail > 0 || myfs_stats.total > 0){
126 char buffer1 [6], buffer2[6];
127 size_trunc_len (buffer1, 5, myfs_stats.avail, 1);
128 size_trunc_len (buffer2, 5, myfs_stats.total, 1);
129 tty_printf (_("Free space: %s (%d%%) of %s"), buffer1, myfs_stats.total ?
130 (int)(100 * (double)myfs_stats.avail / myfs_stats.total) : 0,
131 buffer2);
132 } else
133 tty_print_string (_("No space information"));
135 case 14:
136 widget_move (&info->widget, 14, 3);
137 tty_printf (_("Type: %s "),
138 myfs_stats.typename ? myfs_stats.typename : _("non-local vfs"));
139 if (myfs_stats.type != 0xffff && myfs_stats.type != -1)
140 tty_printf (" (%Xh)", myfs_stats.type);
142 case 13:
143 widget_move (&info->widget, 13, 3);
144 str_printf (buff, _("Device: %s"),
145 str_trunc (myfs_stats.device, info->widget.cols - i18n_adjust));
146 tty_print_string (buff->str);
147 g_string_set_size(buff, 0);
148 case 12:
149 widget_move (&info->widget, 12, 3);
150 str_printf (buff, _("Filesystem: %s"),
151 str_trunc (myfs_stats.mpoint, info->widget.cols - i18n_adjust));
152 tty_print_string (buff->str);
153 g_string_set_size(buff, 0);
154 case 11:
155 widget_move (&info->widget, 11, 3);
156 str_printf (buff, _("Accessed: %s"), file_date (st.st_atime));
157 tty_print_string (buff->str);
158 g_string_set_size(buff, 0);
159 case 10:
160 widget_move (&info->widget, 10, 3);
161 str_printf (buff, _("Modified: %s"), file_date (st.st_mtime));
162 tty_print_string (buff->str);
163 g_string_set_size(buff, 0);
164 case 9:
165 widget_move (&info->widget, 9, 3);
166 /* TRANSLATORS: "Status changed", like in the stat(2) man page */
167 str_printf (buff, _("Status: %s"), file_date (st.st_ctime));
168 tty_print_string (buff->str);
169 g_string_set_size(buff, 0);
171 case 8:
172 widget_move (&info->widget, 8, 3);
173 #ifdef HAVE_STRUCT_STAT_ST_RDEV
174 if (S_ISCHR (st.st_mode) || S_ISBLK(st.st_mode))
175 tty_printf (_("Dev. type: major %lu, minor %lu"),
176 (unsigned long) major (st.st_rdev),
177 (unsigned long) minor (st.st_rdev));
178 else
179 #endif
181 char buffer[10];
182 size_trunc_len(buffer, 9, st.st_size, 0);
183 tty_printf (_("Size: %s"), buffer);
184 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
185 tty_printf (ngettext(" (%ld block)", " (%ld blocks)",
186 (unsigned long int) st.st_blocks),
187 (long int) st.st_blocks);
188 #endif
191 case 7:
192 widget_move (&info->widget, 7, 3);
193 tty_printf (_("Owner: %s/%s"),
194 get_owner (st.st_uid),
195 get_group (st.st_gid));
197 case 6:
198 widget_move (&info->widget, 6, 3);
199 tty_printf (_("Links: %d"), (int) st.st_nlink);
201 case 5:
202 widget_move (&info->widget, 5, 3);
203 tty_printf (_("Mode: %s (%04o)"),
204 string_perm (st.st_mode), (unsigned) st.st_mode & 07777);
206 case 4:
207 widget_move (&info->widget, 4, 3);
208 tty_printf (_("Location: %Xh:%Xh"), (int)st.st_dev, (int)st.st_ino);
210 case 3:
212 const char *fname;
214 widget_move (&info->widget, 3, 2);
215 fname = current_panel->dir.list [current_panel->selected].fname;
216 str_printf (buff, file_label,
217 str_trunc (fname, info->widget.cols - i18n_adjust));
218 tty_print_string (buff->str);
221 case 2:
222 case 1:
223 case 0:
225 } /* switch */
226 g_string_free (buff, TRUE);
229 static void info_hook (void *data)
231 struct WInfo *info = (struct WInfo *) data;
232 Widget *other_widget;
234 other_widget = get_panel_widget (get_current_index ());
235 if (!other_widget)
236 return;
237 if (dlg_overlap (&info->widget, other_widget))
238 return;
240 info->ready = 1;
241 info_show_info (info);
244 static cb_ret_t
245 info_callback (Widget *w, widget_msg_t msg, int parm)
247 struct WInfo *info = (struct WInfo *) w;
249 switch (msg) {
251 case WIDGET_INIT:
252 add_hook (&select_file_hook, info_hook, info);
253 info->ready = 0;
254 return MSG_HANDLED;
256 case WIDGET_DRAW:
257 info_hook (info);
258 info_show_info (info);
259 return MSG_HANDLED;
261 case WIDGET_FOCUS:
262 return MSG_NOT_HANDLED;
264 case WIDGET_DESTROY:
265 delete_hook (&select_file_hook, info_hook);
266 return MSG_HANDLED;
268 default:
269 return default_proc (msg, parm);
273 static int
274 info_event (Gpm_Event *event, void *data)
276 Widget *w = &((WInfo *) data)->widget;
278 /* rest of the upper frame, the menu is invisible - call menu */
279 if (event->type & GPM_DOWN && event->y == 1 && !menubar_visible) {
280 event->x += w->x;
281 return the_menubar->widget.mouse (event, the_menubar);
284 return MOU_NORMAL;
287 WInfo *
288 info_new (void)
290 struct WInfo *info = g_new (struct WInfo, 1);
292 init_widget (&info->widget, 0, 0, 0, 0, info_callback, info_event);
294 /* We do not want the cursor */
295 widget_want_cursor (info->widget, 0);
297 if (!initialized) {
298 initialized = TRUE;
299 init_my_statfs ();
302 return info;