1 /* vifm
2  * Copyright (C) 2001 Ken Steen.
3  * Copyright (C) 2011 xaizek.
4  *
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.
9  *
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.
14  *
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
18  */
19 
20 #ifndef VIFM__FOPS_COMMON_H__
21 #define VIFM__FOPS_COMMON_H__
22 
23 #include "compat/fs_limits.h"
24 #include "background.h"
25 #include "ops.h"
26 
27 struct dir_entry_t;
28 struct view_t;
29 
30 /* Path roles for fops_is_dir_writable() function. */
31 typedef enum
32 {
33 	DR_CURRENT,     /* Current (source) path. */
34 	DR_DESTINATION, /* Destination path. */
35 }
36 DirRole;
37 
38 /* Type of reaction on an error. */
39 typedef enum
40 {
41 	ST_NONE,       /* Ignore message. */
42 	ST_STATUS_BAR, /* Show message in the status bar. */
43 	ST_DIALOG,     /* Shows error dialog. */
44 }
45 SignalType;
46 
47 /* Pack of arguments supplied to procedures implementing file operations in
48  * background. */
49 typedef struct
50 {
51 	char **list;             /* User supplied list of new file names. */
52 	int nlines;              /* Number of user supplied file names (list size). */
53 	int move;                /* Whether this is a move operation. */
54 	int force;               /* Whether destination files should be removed. */
55 	char **sel_list;         /* Full paths of files to be processed. */
56 	size_t sel_list_len;     /* Number of files to process (sel_list size). */
57 	char path[PATH_MAX + 1]; /* Path at which processing should take place. */
58 	int from_file;           /* Whether list was read from a file. */
59 	int use_trash;           /* Whether either source or destination is trash
60 	                            directory. */
61 	char *is_in_trash;       /* Flags indicating whether i-th file is in trash.
62 	                            Can be NULL when unused. */
63 	ops_t *ops;              /* Pointer to pre-allocated operation description. */
64 }
65 bg_args_t;
66 
67 struct dirent;
68 struct response_variant;
69 
70 /* Callback for returning edited filename. */
71 typedef void (*fo_prompt_cb)(const char new_filename[]);
72 
73 /* Line completion function.  arg is user supplied value, which is passed
74  * through.  Should return completion offset. */
75 typedef int (*fo_complete_cmd_func)(const char cmd[], void *arg);
76 
77 /* Function to request filename editing. */
78 typedef void (*line_prompt_func)(const char prompt[], const char filename[],
79 		fo_prompt_cb cb, fo_complete_cmd_func complete, int allow_ee);
80 
81 /* Function to choose an option.  Returns choice. */
82 typedef char (*options_prompt_func)(const char title[], const char message[],
83 		const struct response_variant *variants);
84 
85 /* Filename editing function. */
86 extern line_prompt_func fops_line_prompt;
87 /* Function to choose from one of options. */
88 extern options_prompt_func fops_options_prompt;
89 
90 /* Initializes file operations. */
91 void fops_init(line_prompt_func line_func, options_prompt_func options_func);
92 
93 /* Whether set of view files can be altered (renamed, deleted, but not added).
94  * Returns non-zero if so, otherwise zero is returned. */
95 int fops_view_can_be_changed(const struct view_t *view);
96 
97 /* Checks if name list is consistent.  Returns non-zero is so, otherwise zero is
98  * returned. */
99 int fops_is_name_list_ok(int count, int nlines, char *list[], char *files[]);
100 
101 /* Checks rename correctness and forms an array of duplication marks.
102  * Directory names in files array should be without trailing slash. */
103 int fops_is_rename_list_ok(char *files[], char is_dup[], int len, char *list[]);
104 
105 /* Returns value > 0 if rename is correct, < 0 if rename isn't needed and 0
106  * when rename operation should be aborted.  silent parameter controls whether
107  * error dialog or status bar message should be shown, 0 means dialog. */
108 int fops_check_file_rename(const char dir[], const char old[], const char new[],
109 		SignalType signal_type);
110 
111 /* Makes list of marked filenames.  *nmarked is always set (0 for empty list).
112  * Returns pointer to the list, NULL for empty list. */
113 char ** fops_grab_marked_files(struct view_t *view, size_t *nmarked);
114 
115 /* Uses dentry to check file type and falls back to lstat() if dentry contains
116  * unknown type. */
117 int fops_is_dir_entry(const char full_path[], const struct dirent* dentry);
118 
119 /* Adds marked files to the ops.  Considers UI cancellation.  dst_hint can be
120  * NULL.  Returns number of files enqueued. */
121 int fops_enqueue_marked_files(ops_t *ops, struct view_t *view,
122 		const char dst_hint[], int to_trash);
123 
124 /* Allocates opt_t structure and configures it as needed.  Returns pointer to
125  * newly allocated structure, which should be freed by free_ops(). */
126 ops_t * fops_get_ops(OPS main_op, const char descr[], const char base_dir[],
127 		const char target_dir[]);
128 
129 /* Displays simple operation progress message.  The ready is zero based. */
130 void fops_progress_msg(const char text[], int ready, int total);
131 
132 /* Makes name of destination file from name of the source file.  Returns the
133  * name. */
134 const char * fops_get_dst_name(const char src_path[], int from_trash);
135 
136 /* Checks that all marked files can be read.  Returns non-zero if so, otherwise
137  * zero is returned. */
138 int fops_can_read_marked_files(struct view_t *view);
139 
140 /* Checks path argument and resolves target directory either to the argument or
141  * current directory of the view.  Returns non-zero if value of the path was
142  * used, otherwise zero is returned. */
143 int fops_check_dir_path(const struct view_t *view, const char path[],
144 		char buf[], size_t buf_len);
145 
146 /* Prompts user with a file containing lines from orig array of length count and
147  * returns modified list of strings of length *nlines or NULL on error or
148  * unchanged list unless load_always is non-zero. */
149 char ** fops_edit_list(size_t count, char *orig[], int *nlines,
150 		int load_always);
151 
152 /* Finishes initialization of ops for background processes. */
153 void fops_bg_ops_init(ops_t *ops, bg_op_t *bg_op);
154 
155 /* Allocates opt_t structure and configures it as needed.  Returns pointer to
156  * newly allocated structure, which should be freed by fops_free_ops(). */
157 ops_t * fops_get_bg_ops(OPS main_op, const char descr[], const char dir[]);
158 
159 /* Frees ops structure previously obtained by call to get_ops().  ops can be
160  * NULL. */
161 void fops_free_ops(ops_t *ops);
162 
163 /* Adapter for fops_mv_file_f() that accepts paths broken into directory/file
164  * parts. */
165 int fops_mv_file(const char src[], const char src_dir[], const char dst[],
166 		const char dst_dir[], OPS op, int cancellable, ops_t *ops);
167 
168 /* Moves file from one location to another.  Returns zero on success, otherwise
169  * non-zero is returned. */
170 int fops_mv_file_f(const char src[], const char dst[], OPS op, int bg,
171 		int cancellable, ops_t *ops);
172 
173 /* Frees background arguments structure with all its data. */
174 void fops_free_bg_args(bg_args_t *args);
175 
176 /* Fills basic fields of the args structure. */
177 void fops_prepare_for_bg_task(struct view_t *view, bg_args_t *args);
178 
179 /* Fills undo message buffer with names of marked files.  buf should be at least
180  * COMMAND_GROUP_INFO_LEN characters length.  fnames can be NULL. */
181 void fops_append_marked_files(struct view_t *view, char buf[], char **fnames);
182 
183 /* Appends file name to undo message buffer.  buf should be at least
184  * COMMAND_GROUP_INFO_LEN characters length. */
185 void fops_append_fname(char buf[], size_t len, const char fname[]);
186 
187 /* Provides different suffixes depending on whether cancellation was requested
188  * or not.  Returns pointer to a string literal. */
189 const char * fops_get_cancellation_suffix(void);
190 
191 /* Whether set of view files can be extended via addition of new elements.  at
192  * parameter is the same as for fops_get_dst_dir().  Returns non-zero if so,
193  * otherwise zero is returned. */
194 int fops_view_can_be_extended(const struct view_t *view, int at);
195 
196 /* Retrieves current target directory of file system sub-tree.  Root for regular
197  * and regular custom views and origin of either active (when at < 0) or
198  * specified by its index entry for tree views.  Returns the path. */
199 const char * fops_get_dst_dir(const struct view_t *view, int at);
200 
201 /* This is a wrapper for is_dir_writable() function, which adds message
202  * dialogs.  Returns non-zero if directory can be changed, otherwise zero is
203  * returned. */
204 int fops_is_dir_writable(DirRole dir_role, const char path[]);
205 
206 #endif /* VIFM__FOPS_COMMON_H__ */
207 
208 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
209 /* vim: set cinoptions+=t0 filetype=c : */
210