Cast to (const char *) in strrchr calls
[elinks.git] / src / session / download.h
bloba4de701e0442dd59ff008382bef81d00ad917c30
1 #ifndef EL__SESSION_DOWNLOAD_H
2 #define EL__SESSION_DOWNLOAD_H
4 #include "main/object.h"
5 #include "network/state.h"
6 #include "util/lists.h"
7 #include "util/time.h"
9 /* Silly BFU stuff */
10 struct dialog_data;
11 struct listbox_item;
12 struct terminal;
14 struct cache_entry;
15 struct connection;
16 struct session;
17 struct uri;
19 struct download;
21 typedef void (download_callback_T)(struct download *, void *);
23 /** Flags controlling how to download a file. This is a bit mask.
24 * Unrecognized bits should be preserved and ignored. */
25 enum download_flags {
26 /** Downloading cannot be resumed; do not offer such an option
27 * to the user. All bits clear. */
28 DOWNLOAD_RESUME_DISABLED = 0,
30 /** Downloading can be resumed. This is the usual value. */
31 DOWNLOAD_RESUME_ALLOWED = 1,
33 /** The user wants to resume downloading. This must not occur
34 * without #DOWNLOAD_RESUME_ALLOWED. */
35 DOWNLOAD_RESUME_SELECTED = 2,
37 /** The file will be opened in an external handler. */
38 DOWNLOAD_EXTERNAL = 4
41 struct download {
42 /* XXX: order matters there, there's some hard initialization in
43 * src/session/session.c and src/viewer/text/view.c */
44 LIST_HEAD(struct download);
46 struct connection *conn;
47 struct cache_entry *cached;
48 /** The callback is called when connection gets into a progress state,
49 * after it's over (in a result state), and also periodically after
50 * the download starts receiving some data. */
51 download_callback_T *callback;
52 void *data;
53 struct progress *progress;
55 struct connection_state state;
56 struct connection_state prev_error;
57 enum connection_priority pri;
60 /** The user has navigated to a resource that ELinks does not display
61 * automatically because of its MIME type, and ELinks is asking what
62 * to do.
64 * These structures are kept in the session.type_queries list, and
65 * destroy_session() calls done_type_query() to destroy them too. */
66 struct type_query {
67 LIST_HEAD(struct type_query);
69 /** After ELinks has downloaded enough of the resource to see
70 * that a type query is needed, it moves the download here and
71 * continues it while the user decides what to do. */
72 struct download download;
74 /** Cache entry loaded from #uri. Apparently used only for
75 * displaying the header. */
76 struct cache_entry *cached;
78 /** The session in which the user navigated to #uri. The
79 * type_query is in the session.type_queries list of this
80 * session. */
81 struct session *ses;
83 /** The URI of the resource about which ELinks is asking.
84 * This reference must be released with done_uri(). */
85 struct uri *uri;
87 /** The name of the frame in which the user navigated to #uri.
88 * If the user chooses to display the resource, it goes into
89 * this frame. This string must be freed with mem_free(). */
90 unsigned char *target_frame;
92 /** Command line for an external handler, to be run when the
93 * download finishes. When ELinks displays the type query,
94 * it copies this from mime_handler.program of the default
95 * handler of the type. The user can then edit the string.
96 * This string must be freed with mem_free(). */
97 unsigned char *external_handler;
99 /** Whether the external handler is going to use the terminal.
100 * When ELinks displays the type query, it copies this from
101 * mime_handler.block of the default handler of the type.
102 * The user can then change the flag with a checkbox. */
103 int block;
105 /** Whether the resource was generated by ELinks running
106 * a local CGI program. If the user chooses to open the
107 * resource with an external handler, ELinks normally saves
108 * the resource to a temporary file and passes the name of
109 * that to the external handler. However, if the resource is
110 * from a "file" URI that does not refer to a local CGI, then
111 * Elinks need not copy the file. */
112 unsigned int cgi:1;
114 /** mailcap entry with copiousoutput */
115 unsigned int copiousoutput:1;
116 /* int frame; */
119 struct file_download {
120 OBJECT_HEAD(struct file_download);
122 struct uri *uri;
123 unsigned char *file;
124 unsigned char *external_handler;
125 struct session *ses;
127 /** The terminal in which message boxes about the download
128 * should be displayed. If this terminal is closed, then
129 * detach_downloads_from_terminal() changes the pointer to
130 * NULL, and get_default_terminal() will be used if a
131 * terminal is needed later. However, if the download has
132 * an external handler, then detach_downloads_from_terminal()
133 * aborts it right away; external handlers always run in the
134 * original terminal, if anywhere. */
135 struct terminal *term;
137 time_t remotetime;
138 off_t seek;
139 int handle;
140 int redirect_cnt;
141 int notify;
142 struct download download;
144 /** Should the file be deleted when destroying the structure */
145 unsigned int delete_:1;
147 /** Should the download be stopped/interrupted when destroying the structure */
148 unsigned int stop:1;
150 /** Whether to block the terminal when running the external handler. */
151 unsigned int block:1;
153 /** Mailcap entry with copiousoutput */
154 unsigned int copiousoutput:1;
156 /** The current dialog for this download. Can be NULL. */
157 struct dialog_data *dlg_data;
158 struct listbox_item *box_item;
161 /** Stack of all running downloads */
162 extern LIST_OF(struct file_download) downloads;
164 static inline int
165 is_in_downloads_list(struct file_download *file_download)
167 struct file_download *down;
169 foreach (down, downloads)
170 if (file_download == down) return 1;
172 return 0;
175 int download_is_progressing(struct download *download);
177 int are_there_downloads(void);
179 /** Type of the callback function that will be called when the file
180 * has been opened, or when it is known that the file will not be
181 * opened.
183 * @param term
184 * The terminal on which the callback should display any windows.
185 * Comes directly from the @a term argument of create_download_file().
187 * @param fd
188 * A file descriptor to the opened file, or -1 if the file will not be
189 * opened. If the @a real_file argument of create_download_file()
190 * was not NULL, the callback may read the name of this file from
191 * *@a real_file.
193 * @param data
194 * A pointer to any data that the callback cares about.
195 * Comes directly from the @a data argument of create_download_file().
197 * @param flags
198 * The same as the @a flags argument of create_download_file(),
199 * except the ::DOWNLOAD_RESUME_SELECTED bit will be changed to match
200 * what the user chose.
202 * @relates cdf_hop */
203 typedef void cdf_callback_T(struct terminal *term, int fd,
204 void *data, enum download_flags flags);
206 void start_download(void *, unsigned char *);
207 void resume_download(void *, unsigned char *);
208 void create_download_file(struct terminal *, unsigned char *, unsigned char **,
209 enum download_flags, cdf_callback_T *, void *);
211 void abort_all_downloads(void);
212 void destroy_downloads(struct session *);
213 void detach_downloads_from_terminal(struct terminal *);
215 int setup_download_handler(struct session *, struct download *, struct cache_entry *, int);
217 void abort_download(struct file_download *file_download);
218 void done_type_query(struct type_query *type_query);
220 void tp_display(struct type_query *type_query);
221 void tp_save(struct type_query *type_query);
222 void tp_cancel(void *data);
223 struct file_download *init_file_download(struct uri *uri, struct session *ses, unsigned char *file, int fd);
225 #endif