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 "ioeta.h"
20 
21 #include <stddef.h> /* NULL */
22 #include <stdint.h> /* uint64_t */
23 #include <stdlib.h> /* free() */
24 #include <string.h> /* strdup() */
25 
26 #include "../../utils/fs.h"
27 #include "../../utils/str.h"
28 #include "../ioeta.h"
29 #include "ionotif.h"
30 
31 void
ioeta_release(ioeta_estim_t * estim)32 ioeta_release(ioeta_estim_t *estim)
33 {
34 	free(estim->item);
35 	free(estim->target);
36 }
37 
38 void
ioeta_add_item(ioeta_estim_t * estim,const char path[])39 ioeta_add_item(ioeta_estim_t *estim, const char path[])
40 {
41 	++estim->total_items;
42 
43 	replace_string(&estim->item, path);
44 
45 	ionotif_notify(IO_PS_ESTIMATING, estim);
46 }
47 
48 void
ioeta_add_file(ioeta_estim_t * estim,const char path[])49 ioeta_add_file(ioeta_estim_t *estim, const char path[])
50 {
51 	if(!is_symlink(path))
52 	{
53 		estim->total_bytes += get_file_size(path);
54 	}
55 
56 	ioeta_add_item(estim, path);
57 }
58 
59 void
ioeta_add_dir(ioeta_estim_t * estim,const char path[])60 ioeta_add_dir(ioeta_estim_t *estim, const char path[])
61 {
62 	/* TODO: think about counting directories, otherwise copying of large amount
63 	 *       of empty directories or hierarchy of empty directories produces weird
64 	 *       progress reports and it even might be the reason of getting more than
65 	 *       100% progress. */
66 
67 	replace_string(&estim->item, path);
68 
69 	ionotif_notify(IO_PS_ESTIMATING, estim);
70 }
71 
72 void
ioeta_update(ioeta_estim_t * estim,const char path[],const char target[],int finished,uint64_t bytes)73 ioeta_update(ioeta_estim_t *estim, const char path[], const char target[],
74 		int finished, uint64_t bytes)
75 {
76 	if(estim == NULL || estim->silent)
77 	{
78 		return;
79 	}
80 
81 	estim->current_byte += bytes;
82 	estim->current_file_byte += bytes;
83 	if(estim->current_byte > estim->total_bytes)
84 	{
85 		/* Estimations are out of date, update them. */
86 		estim->total_bytes = estim->current_byte;
87 	}
88 
89 	if(finished)
90 	{
91 		++estim->current_item;
92 		if(estim->current_item > estim->total_items)
93 		{
94 			/* Estimations are out of date, update them. */
95 			estim->total_items = estim->current_item;
96 		}
97 		estim->current_file_byte = 0U;
98 		estim->total_file_bytes = 0U;
99 	}
100 	else if(estim->inspected_items != estim->current_item + 1)
101 	{
102 		estim->inspected_items = estim->current_item + 1;
103 		estim->total_file_bytes = get_file_size(path);
104 	}
105 
106 	if(path != NULL)
107 	{
108 		replace_string(&estim->item, path);
109 	}
110 
111 	if(target != NULL)
112 	{
113 		replace_string(&estim->target, target);
114 	}
115 
116 	ionotif_notify(IO_PS_IN_PROGRESS, estim);
117 }
118 
119 int
ioeta_silent_on(ioeta_estim_t * estim)120 ioeta_silent_on(ioeta_estim_t *estim)
121 {
122 	int silent;
123 
124 	if(estim == NULL)
125 	{
126 		return 0;
127 	}
128 
129 	silent = estim->silent;
130 	estim->silent = 1;
131 	return silent;
132 }
133 
134 void
ioeta_silent_set(ioeta_estim_t * estim,int silent)135 ioeta_silent_set(ioeta_estim_t *estim, int silent)
136 {
137 	if(estim != NULL)
138 	{
139 		estim->silent = silent;
140 	}
141 }
142 
143 ioeta_estim_t
ioeta_save(const ioeta_estim_t * estim)144 ioeta_save(const ioeta_estim_t *estim)
145 {
146 	ioeta_estim_t copy = *estim;
147 	copy.item = (copy.item == NULL ? NULL : strdup(copy.item));
148 	copy.target = (copy.target == NULL ? NULL : strdup(copy.target));
149 
150 	return copy;
151 }
152 
153 void
ioeta_restore(ioeta_estim_t * estim,const ioeta_estim_t * save)154 ioeta_restore(ioeta_estim_t *estim, const ioeta_estim_t *save)
155 {
156 	char *item = estim->item;
157 	char *target = estim->target;
158 
159 	if(estim->silent)
160 	{
161 		return;
162 	}
163 
164 	update_string(&item, save->item);
165 	update_string(&target, save->target);
166 
167 	*estim = *save;
168 	estim->item = item;
169 	estim->target = target;
170 }
171 
172 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
173 /* vim: set cinoptions+=t0 filetype=c : */
174