Init/deinit clean up
[pantumic.git] / src / info.c
blob50ef190228912fb8e6d16c6b64ea9b5bacc17086
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>
26 #include <sys/stat.h>
28 #include "lib/global.h"
29 #include "lib/unixcompat.h"
30 #include "lib/tty/tty.h"
31 #include "lib/tty/key.h" /* is_idle() */
32 #include "lib/tty/mouse.h" /* Gpm_Event */
33 #include "lib/skin.h"
34 #include "lib/strutil.h"
35 #include "lib/timefmt.h" /* file_date() */
36 #include "lib/util.h"
37 #include "lib/widget.h"
39 #include "midnight.h" /* the_menubar */
40 #include "layout.h"
41 #include "mountlist.h"
42 #include "setup.h" /* panels_options */
43 #include "info.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 #ifndef VERSION
50 #define VERSION "undefined"
51 #endif
53 /*** file scope type declarations ****************************************************************/
55 struct WInfo
57 Widget widget;
58 int ready;
61 /*** file scope variables ************************************************************************/
63 static struct my_statfs myfs_stats;
65 /*** file scope functions ************************************************************************/
66 /* --------------------------------------------------------------------------------------------- */
68 static void
69 info_box (struct WInfo *info)
71 const char *title = _("Information");
72 const int len = str_term_width1 (title);
74 tty_set_normal_attrs ();
75 tty_setcolor (NORMAL_COLOR);
76 widget_erase (&info->widget);
77 draw_box (info->widget.owner, info->widget.y, info->widget.x,
78 info->widget.lines, info->widget.cols, FALSE);
80 widget_move (&info->widget, 0, (info->widget.cols - len - 2) / 2);
81 tty_printf (" %s ", title);
83 widget_move (&info->widget, 2, 0);
84 tty_print_alt_char (ACS_LTEE, FALSE);
85 widget_move (&info->widget, 2, info->widget.cols - 1);
86 tty_print_alt_char (ACS_RTEE, FALSE);
87 tty_draw_hline (info->widget.y + 2, info->widget.x + 1, ACS_HLINE, info->widget.cols - 2);
90 /* --------------------------------------------------------------------------------------------- */
92 static void
93 info_show_info (struct WInfo *info)
95 static int i18n_adjust = 0;
96 static const char *file_label;
97 GString *buff;
98 struct stat st;
100 if (!is_idle ())
101 return;
103 info_box (info);
105 tty_setcolor (MARKED_COLOR);
106 widget_move (&info->widget, 1, 3);
107 tty_printf (_("Midnight Commander %s"), VERSION);
109 if (!info->ready)
110 return;
112 if (get_current_type () != view_listing)
113 return;
115 my_statfs (&myfs_stats, current_panel->cwd);
116 st = current_panel->dir.list[current_panel->selected].st;
118 /* Print only lines which fit */
120 if (i18n_adjust == 0)
122 /* This printf pattern string is used as a reference for size */
123 file_label = _("File: %s");
124 i18n_adjust = str_term_width1 (file_label) + 2;
127 tty_setcolor (NORMAL_COLOR);
129 buff = g_string_new ("");
131 switch (info->widget.lines - 2)
133 /* Note: all cases are fall-throughs */
135 default:
137 case 16:
138 widget_move (&info->widget, 16, 3);
139 if (myfs_stats.nfree > 0 || myfs_stats.nodes > 0)
140 tty_printf (_("Free nodes: %ld (%ld%%) of %ld"),
141 (size_t) myfs_stats.nfree,
142 myfs_stats.nodes != 0
143 ? 100 * (size_t) myfs_stats.nfree / (size_t) myfs_stats.nodes : 0,
144 (size_t) myfs_stats.nodes);
145 else
146 tty_print_string (_("No node information"));
148 case 15:
149 widget_move (&info->widget, 15, 3);
150 if (myfs_stats.avail > 0 || myfs_stats.total > 0)
152 char buffer1[6], buffer2[6];
153 size_trunc_len (buffer1, 5, myfs_stats.avail, 1, panels_options.kilobyte_si);
154 size_trunc_len (buffer2, 5, myfs_stats.total, 1, panels_options.kilobyte_si);
155 tty_printf (_("Free space: %s (%d%%) of %s"), buffer1, myfs_stats.total ?
156 (int) (100 * (double) myfs_stats.avail / myfs_stats.total) : 0, buffer2);
158 else
159 tty_print_string (_("No space information"));
161 case 14:
162 widget_move (&info->widget, 14, 3);
163 tty_printf (_("Type: %s"),
164 myfs_stats.typename ? myfs_stats.typename : _("non-local vfs"));
165 if (myfs_stats.type != 0xffff && myfs_stats.type != -1)
166 tty_printf (" (%Xh)", myfs_stats.type);
168 case 13:
169 widget_move (&info->widget, 13, 3);
170 str_printf (buff, _("Device: %s"),
171 str_trunc (myfs_stats.device, info->widget.cols - i18n_adjust));
172 tty_print_string (buff->str);
173 g_string_set_size (buff, 0);
174 case 12:
175 widget_move (&info->widget, 12, 3);
176 str_printf (buff, _("Filesystem: %s"),
177 str_trunc (myfs_stats.mpoint, info->widget.cols - i18n_adjust));
178 tty_print_string (buff->str);
179 g_string_set_size (buff, 0);
180 case 11:
181 widget_move (&info->widget, 11, 3);
182 str_printf (buff, _("Accessed: %s"), file_date (st.st_atime));
183 tty_print_string (buff->str);
184 g_string_set_size (buff, 0);
185 case 10:
186 widget_move (&info->widget, 10, 3);
187 str_printf (buff, _("Modified: %s"), file_date (st.st_mtime));
188 tty_print_string (buff->str);
189 g_string_set_size (buff, 0);
190 case 9:
191 widget_move (&info->widget, 9, 3);
192 /* The field st_ctime is changed by writing or by setting inode
193 information (i.e., owner, group, link count, mode, etc.). */
194 /* TRANSLATORS: Time of last status change as in stat(2) man. */
195 str_printf (buff, _("Changed: %s"), file_date (st.st_ctime));
196 tty_print_string (buff->str);
197 g_string_set_size (buff, 0);
199 case 8:
200 widget_move (&info->widget, 8, 3);
201 #ifdef HAVE_STRUCT_STAT_ST_RDEV
202 if (S_ISCHR (st.st_mode) || S_ISBLK (st.st_mode))
203 tty_printf (_("Dev. type: major %lu, minor %lu"),
204 (unsigned long) major (st.st_rdev), (unsigned long) minor (st.st_rdev));
205 else
206 #endif
208 char buffer[10];
209 size_trunc_len (buffer, 9, st.st_size, 0, panels_options.kilobyte_si);
210 tty_printf (_("Size: %s"), buffer);
211 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
212 tty_printf (ngettext (" (%ld block)", " (%ld blocks)",
213 (unsigned long int) st.st_blocks), (long int) st.st_blocks);
214 #endif
217 case 7:
218 widget_move (&info->widget, 7, 3);
219 tty_printf (_("Owner: %s/%s"), get_owner (st.st_uid), get_group (st.st_gid));
221 case 6:
222 widget_move (&info->widget, 6, 3);
223 tty_printf (_("Links: %d"), (int) st.st_nlink);
225 case 5:
226 widget_move (&info->widget, 5, 3);
227 tty_printf (_("Mode: %s (%04o)"),
228 string_perm (st.st_mode), (unsigned) st.st_mode & 07777);
230 case 4:
231 widget_move (&info->widget, 4, 3);
232 tty_printf (_("Location: %Xh:%Xh"), (int) st.st_dev, (int) st.st_ino);
234 case 3:
236 const char *fname;
238 widget_move (&info->widget, 3, 2);
239 fname = current_panel->dir.list[current_panel->selected].fname;
240 str_printf (buff, file_label, str_trunc (fname, info->widget.cols - i18n_adjust));
241 tty_print_string (buff->str);
244 case 2:
245 case 1:
246 case 0:
248 } /* switch */
249 g_string_free (buff, TRUE);
252 /* --------------------------------------------------------------------------------------------- */
254 static void
255 info_hook (void *data)
257 struct WInfo *info = (struct WInfo *) data;
258 Widget *other_widget;
260 other_widget = get_panel_widget (get_current_index ());
261 if (!other_widget)
262 return;
263 if (dlg_overlap (&info->widget, other_widget))
264 return;
266 info->ready = 1;
267 info_show_info (info);
270 /* --------------------------------------------------------------------------------------------- */
272 static cb_ret_t
273 info_callback (Widget * w, widget_msg_t msg, int parm)
275 struct WInfo *info = (struct WInfo *) w;
277 switch (msg)
280 case WIDGET_INIT:
281 init_my_statfs ();
282 add_hook (&select_file_hook, info_hook, info);
283 info->ready = 0;
284 return MSG_HANDLED;
286 case WIDGET_DRAW:
287 info_hook (info);
288 return MSG_HANDLED;
290 case WIDGET_FOCUS:
291 return MSG_NOT_HANDLED;
293 case WIDGET_DESTROY:
294 delete_hook (&select_file_hook, info_hook);
295 free_my_statfs ();
296 return MSG_HANDLED;
298 default:
299 return default_proc (msg, parm);
303 /* --------------------------------------------------------------------------------------------- */
305 static int
306 info_event (Gpm_Event * event, void *data)
308 Widget *w = &((WInfo *) data)->widget;
310 /* rest of the upper frame, the menu is invisible - call menu */
311 if (event->type & GPM_DOWN && event->y == 1 && !menubar_visible)
313 event->x += w->x;
314 return the_menubar->widget.mouse (event, the_menubar);
317 return MOU_NORMAL;
320 /* --------------------------------------------------------------------------------------------- */
321 /*** public functions ****************************************************************************/
322 /* --------------------------------------------------------------------------------------------- */
324 WInfo *
325 info_new (int y, int x, int lines, int cols)
327 struct WInfo *info = g_new (struct WInfo, 1);
329 init_widget (&info->widget, y, x, lines, cols, info_callback, info_event);
331 /* We do not want the cursor */
332 widget_want_cursor (info->widget, 0);
334 return info;
337 /* --------------------------------------------------------------------------------------------- */