1 /* vifm
2  * Copyright (C) 2014 xaizek.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include "trashes_menu.h"
20 
21 #include <stdint.h> /* uint64_t */
22 #include <string.h> /* strchr() strdup() */
23 
24 #include "../ui/ui.h"
25 #include "../utils/cancellation.h"
26 #include "../utils/str.h"
27 #include "../utils/string_array.h"
28 #include "../utils/utils.h"
29 #include "../fops_misc.h"
30 #include "../trash.h"
31 #include "menus.h"
32 
33 static char * format_item(const char trash_dir[], int calc_size);
34 static int execute_trashes_cb(view_t *view, menu_data_t *m);
35 static KHandlerResponse trashes_khandler(view_t *view, menu_data_t *m,
36 		const wchar_t keys[]);
37 
38 int
show_trashes_menu(view_t * view,int calc_size)39 show_trashes_menu(view_t *view, int calc_size)
40 {
41 	char **trashes;
42 	int ntrashes;
43 	int i;
44 
45 	static menu_data_t m;
46 	menus_init_data(&m, view,
47 			format_str("%sNon-empty trash directories", calc_size ? "[  size] " : ""),
48 			strdup("No non-empty trash directories found"));
49 
50 	m.execute_handler = &execute_trashes_cb;
51 	m.key_handler = &trashes_khandler;
52 	m.extra_data = calc_size;
53 
54 	trashes = trash_list_trashes(&ntrashes);
55 
56 	show_progress(NULL, 0);
57 	for(i = 0; i < ntrashes; i++)
58 	{
59 		char *const item = format_item(trashes[i], calc_size);
60 		m.len = put_into_string_array(&m.items, m.len, item);
61 	}
62 
63 	free_string_array(trashes, ntrashes);
64 
65 	return menus_enter(m.state, view);
66 }
67 
68 /* Formats single menu item.  Returns pointer to newly allocated string. */
69 static char *
format_item(const char trash_dir[],int calc_size)70 format_item(const char trash_dir[], int calc_size)
71 {
72 	char msg[PATH_MAX + 1];
73 	uint64_t size;
74 	char size_str[64];
75 
76 	if(!calc_size)
77 	{
78 		return strdup(trash_dir);
79 	}
80 
81 	snprintf(msg, sizeof(msg), "Calculating size of %s...", trash_dir);
82 	show_progress(msg, 1);
83 
84 	size = fops_dir_size(trash_dir, 1, &no_cancellation);
85 
86 	size_str[0] = '\0';
87 	friendly_size_notation(size, sizeof(size_str), size_str);
88 
89 	return format_str("[%8s] %s", size_str, trash_dir);
90 }
91 
92 /* Callback that is called when menu item is selected.  Return non-zero to stay
93  * in menu mode and zero otherwise. */
94 static int
execute_trashes_cb(view_t * view,menu_data_t * m)95 execute_trashes_cb(view_t *view, menu_data_t *m)
96 {
97 	const char *const item = m->items[m->pos];
98 	const char *const trash_dir = m->extra_data ? strchr(item, ']') + 2 : item;
99 	menus_goto_dir(view, trash_dir);
100 	return 0;
101 }
102 
103 /* Menu-specific shortcut handler.  Returns code that specifies both taken
104  * actions and what should be done next. */
105 static KHandlerResponse
trashes_khandler(view_t * view,menu_data_t * m,const wchar_t keys[])106 trashes_khandler(view_t *view, menu_data_t *m, const wchar_t keys[])
107 {
108 	if(wcscmp(keys, L"dd") == 0)
109 	{
110 		const char *const item = m->items[m->pos];
111 		const char *trash_dir = m->extra_data ? strchr(item, ']') + 2 : item;
112 		trash_empty(trash_dir);
113 		menus_remove_current(m->state);
114 		return KHR_REFRESH_WINDOW;
115 	}
116 	return KHR_UNHANDLED;
117 }
118 
119 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
120 /* vim: set cinoptions+=t0 filetype=c : */
121