NetSurf
dirlist.c
Go to the documentation of this file.
1/*
2 * Copyright 2010 Michael Drake <tlsa@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf 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, see <http://www.gnu.org/licenses/>.
17 */
18
19/** \file
20 * Generate HTML content for displaying directory listings (implementation).
21 */
22
23#include <stdbool.h>
24#include <string.h>
25#include <stdio.h>
26#include <stdlib.h>
27
28#include "utils/nsurl.h"
29#include "utils/messages.h"
30#include "utils/nscolour.h"
31
32#include "netsurf/inttypes.h"
33#include "netsurf/types.h"
34#include "netsurf/plot_style.h"
35
36#include "dirlist.h"
38
39static int dirlist_filesize_calculate(unsigned long *bytesize);
40static int dirlist_filesize_value(unsigned long bytesize);
41static char* dirlist_filesize_unit(unsigned long bytesize);
42
43
44/**
45 * Generates the top part of an HTML directory listing page
46 *
47 * \return Top of directory listing HTML
48 *
49 * This is part of a series of functions. To generate a complete page,
50 * call the following functions in order:
51 *
52 * dirlist_generate_top()
53 * dirlist_generate_hide_columns() -- optional
54 * dirlist_generate_title()
55 * dirlist_generate_parent_link() -- optional
56 * dirlist_generate_headings()
57 * dirlist_generate_row() -- call 'n' times for 'n' rows
58 * dirlist_generate_bottom()
59 */
60
61bool dirlist_generate_top(char *buffer, int buffer_length)
62{
63 int error = snprintf(buffer, buffer_length,
64 "<html>\n"
65 "<head>\n"
66 "<link rel=\"stylesheet\" title=\"Standard\" "
67 "type=\"text/css\" href=\"resource:internal.css\">\n"
68 "<style>\n");
69 if (error < 0 || error >= buffer_length)
70 /* Error or buffer too small */
71 return false;
72 else
73 /* OK */
74 return true;
75
76}
77
78
79/**
80 * Generates the part of an HTML directory listing page that can suppress
81 * particular columns
82 *
83 * \param flags flags for which cols to suppress. 0 to suppress none
84 * \param buffer buffer to fill with generated HTML
85 * \param buffer_length maximum size of buffer
86 * \return true iff buffer filled without error
87 *
88 * This is part of a series of functions. To generate a complete page,
89 * call the following functions in order:
90 *
91 * dirlist_generate_top()
92 * dirlist_generate_hide_columns() -- optional
93 * dirlist_generate_title()
94 * dirlist_generate_parent_link() -- optional
95 * dirlist_generate_headings()
96 * dirlist_generate_row() -- call 'n' times for 'n' rows
97 * dirlist_generate_bottom()
98 */
99
100bool dirlist_generate_hide_columns(int flags, char *buffer, int buffer_length)
101{
102 int error = snprintf(buffer, buffer_length,
103 "%s\n%s\n%s\n%s\n%s\n",
104 (flags & DIRLIST_NO_NAME_COLUMN) ?
105 "span.name { display: none; }\n" : "",
106 (flags & DIRLIST_NO_TYPE_COLUMN) ?
107 "span.type { display: none; }\n" : "",
108 (flags & DIRLIST_NO_SIZE_COLUMN) ?
109 "span.size { display: none; }\n" : "",
110 (flags & DIRLIST_NO_DATE_COLUMN) ?
111 "span.date { display: none; }\n" : "",
112 (flags & DIRLIST_NO_TIME_COLUMN) ?
113 "span.time { display: none; }\n" : "");
114 if (error < 0 || error >= buffer_length)
115 /* Error or buffer too small */
116 return false;
117 else
118 /* OK */
119 return true;
120}
121
122
123/**
124 * Generates the part of an HTML directory listing page that contains the title
125 *
126 * \param title title to use
127 * \param buffer buffer to fill with generated HTML
128 * \param buffer_length maximum size of buffer
129 * \return true iff buffer filled without error
130 *
131 * This is part of a series of functions. To generate a complete page,
132 * call the following functions in order:
133 *
134 * dirlist_generate_top()
135 * dirlist_generate_hide_columns() -- optional
136 * dirlist_generate_title()
137 * dirlist_generate_parent_link() -- optional
138 * dirlist_generate_headings()
139 * dirlist_generate_row() -- call 'n' times for 'n' rows
140 * dirlist_generate_bottom()
141 */
142
143bool dirlist_generate_title(const char *title, char *buffer, int buffer_length)
144{
145 const char *stylesheet;
146 nserror err;
147 int error;
148
149 if (title == NULL)
150 title = "";
151
152 err = nscolour_get_stylesheet(&stylesheet);
153 if (err != NSERROR_OK) {
154 return false;
155 }
156
157 error = snprintf(buffer, buffer_length,
158 "</style>\n"
159 "<title>%s</title>\n"
160 "<style>\n"
161 "html {\n"
162 "\tbackground-color: #%06"PRIx32";\n"
163 "}\n"
164 "%s"
165 "</style>\n"
166 "</head>\n"
167 "<body id=\"dirlist\" class=\"ns-even-bg ns-even-fg ns-border\">\n"
168 "<h1 class=\"ns-border\">%s</h1>\n",
169 title,
171 stylesheet, title);
172 if (error < 0 || error >= buffer_length)
173 /* Error or buffer too small */
174 return false;
175 else
176 /* OK */
177 return true;
178}
179
180
181/**
182 * Generates the part of an HTML directory listing page that links to the parent
183 * directory
184 *
185 * \param parent url of parent directory
186 * \param buffer buffer to fill with generated HTML
187 * \param buffer_length maximum size of buffer
188 * \return true iff buffer filled without error
189 *
190 * This is part of a series of functions. To generate a complete page,
191 * call the following functions in order:
192 *
193 * dirlist_generate_top()
194 * dirlist_generate_hide_columns() -- optional
195 * dirlist_generate_title()
196 * dirlist_generate_parent_link() -- optional
197 * dirlist_generate_headings()
198 * dirlist_generate_row() -- call 'n' times for 'n' rows
199 * dirlist_generate_bottom()
200 */
201
203 int buffer_length)
204{
205 int error = snprintf(buffer, buffer_length,
206 "<p><a href=\"%s\">%s</a></p>",
207 parent, messages_get("FileParent"));
208 if (error < 0 || error >= buffer_length)
209 /* Error or buffer too small */
210 return false;
211 else
212 /* OK */
213 return true;
214}
215
216
217/**
218 * Generates the part of an HTML directory listing page that displays the column
219 * headings
220 *
221 * \param buffer buffer to fill with generated HTML
222 * \param buffer_length maximum size of buffer
223 * \return true iff buffer filled without error
224 *
225 * This is part of a series of functions. To generate a complete page,
226 * call the following functions in order:
227 *
228 * dirlist_generate_top()
229 * dirlist_generate_hide_columns() -- optional
230 * dirlist_generate_title()
231 * dirlist_generate_parent_link() -- optional
232 * dirlist_generate_headings()
233 * dirlist_generate_row() -- call 'n' times for 'n' rows
234 * dirlist_generate_bottom()
235 */
236
237bool dirlist_generate_headings(char *buffer, int buffer_length)
238{
239 int error = snprintf(buffer, buffer_length,
240 "<div>\n"
241 "<strong>\n"
242 "\t<span class=\"name\">%s</span>\n"
243 "\t<span class=\"type\">%s</span>\n"
244 "\t<span class=\"size\">%s</span>"
245 "<span class=\"size\"></span>\n"
246 "\t<span class=\"date\">%s</span>\n"
247 "\t<span class=\"time\">%s</span>\n"
248 "</strong>\n",
249 messages_get("FileName"), messages_get("FileType"),
250 messages_get("FileSize"), messages_get("FileDate"),
251 messages_get("FileTime"));
252 if (error < 0 || error >= buffer_length)
253 /* Error or buffer too small */
254 return false;
255 else
256 /* OK */
257 return true;
258}
259
260
261/**
262 * Generates the part of an HTML directory listing page that displays a row
263 * in the directory contents table
264 *
265 * \param even evenness of row number, for alternate row colouring
266 * \param directory whether this row is for a directory (or a file)
267 * \param url url for row entry
268 * \param name name of row entry
269 * \param mimetype MIME type of row entry
270 * \param size size of row entry. If negative, size is left blank
271 * \param date date row entry was last modified
272 * \param time time row entry was last modified
273 * \param buffer buffer to fill with generated HTML
274 * \param buffer_length maximum size of buffer
275 * \return true iff buffer filled without error
276 *
277 * This is part of a series of functions. To generate a complete page,
278 * call the following functions in order:
279 *
280 * dirlist_generate_top()
281 * dirlist_generate_hide_columns() -- optional
282 * dirlist_generate_title()
283 * dirlist_generate_parent_link() -- optional
284 * dirlist_generate_headings()
285 * dirlist_generate_row() -- call 'n' times for 'n' rows
286 * dirlist_generate_bottom()
287 */
288
289bool dirlist_generate_row(bool even, bool directory, nsurl *url, char *name,
290 const char *mimetype, long long size, char *date, char *time,
291 char *buffer, int buffer_length)
292{
293 const char *unit;
294 char size_string[100];
295 int error;
296
297 if (size < 0) {
298 unit = "";
299 strncpy(size_string, "", sizeof size_string);
300 } else {
301 unit = messages_get(dirlist_filesize_unit((unsigned long)size));
302 snprintf(size_string, sizeof size_string, "%d",
303 dirlist_filesize_value((unsigned long)size));
304 }
305
306 error = snprintf(buffer, buffer_length,
307 "<a href=\"%s\" class=\"%s %s\">\n"
308 "\t<span class=\"name ns-border\">%s</span>\n"
309 "\t<span class=\"type ns-border\">%s</span>\n"
310 "\t<span class=\"size ns-border\">%s</span>"
311 "<span class=\"size ns-border\">%s</span>\n"
312 "\t<span class=\"date ns-border\">%s</span>\n"
313 "\t<span class=\"time ns-border\">%s</span>\n"
314 "</a>\n", nsurl_access(url),
315 even ? "even ns-even-bg" : "odd ns-odd-bg",
316 directory ? "dir" : "file",
317 name, mimetype, size_string, unit, date, time);
318 if (error < 0 || error >= buffer_length)
319 /* Error or buffer too small */
320 return false;
321 else
322 /* OK */
323 return true;
324}
325
326
327/**
328 * Generates the bottom part of an HTML directory listing page
329 *
330 * \return Bottom of directory listing HTML
331 *
332 * This is part of a series of functions. To generate a complete page,
333 * call the following functions in order:
334 *
335 * dirlist_generate_top()
336 * dirlist_generate_hide_columns() -- optional
337 * dirlist_generate_title()
338 * dirlist_generate_parent_link() -- optional
339 * dirlist_generate_headings()
340 * dirlist_generate_row() -- call 'n' times for 'n' rows
341 * dirlist_generate_bottom()
342 */
343
344bool dirlist_generate_bottom(char *buffer, int buffer_length)
345{
346 int error = snprintf(buffer, buffer_length,
347 "</div>\n"
348 "</body>\n"
349 "</html>\n");
350 if (error < 0 || error >= buffer_length)
351 /* Error or buffer too small */
352 return false;
353 else
354 /* OK */
355 return true;
356}
357
358
359/**
360 * Obtain display value and units for filesize after conversion to B/kB/MB/GB,
361 * as appropriate.
362 *
363 * \param bytesize file size in bytes, updated to filesize in output units
364 * \return number of times bytesize has been divided by 1024
365 */
366
367int dirlist_filesize_calculate(unsigned long *bytesize)
368{
369 int i = 0;
370 while (*bytesize > 1024 * 4) {
371 *bytesize /= 1024;
372 i++;
373 if (i == 3)
374 break;
375 }
376 return i;
377}
378
379
380/**
381 * Obtain display value for filesize after conversion to B/kB/MB/GB,
382 * as appropriate
383 *
384 * \param bytesize file size in bytes
385 * \return Value to display for file size, in units given by filesize_unit()
386 */
387
388int dirlist_filesize_value(unsigned long bytesize)
389{
391 return (int)bytesize;
392}
393
394
395/**
396 * Obtain display units for filesize after conversion to B/kB/MB/GB,
397 * as appropriate
398 *
399 * \param bytesize file size in bytes
400 * \return Units to display for file size, for value given by filesize_value()
401 */
402
403char* dirlist_filesize_unit(unsigned long bytesize)
404{
405 const char* units[] = { "Bytes", "kBytes", "MBytes", "GBytes" };
406 return (char*)units[dirlist_filesize_calculate(&bytesize)];
407}
static osspriteop_area * buffer
The buffer characteristics.
Definition: buffer.c:55
wimp_w parent
Definition: dialog.c:88
bool dirlist_generate_row(bool even, bool directory, nsurl *url, char *name, const char *mimetype, long long size, char *date, char *time, char *buffer, int buffer_length)
Generates the part of an HTML directory listing page that displays a row in the directory contents ta...
Definition: dirlist.c:289
bool dirlist_generate_bottom(char *buffer, int buffer_length)
Generates the bottom part of an HTML directory listing page.
Definition: dirlist.c:344
static char * dirlist_filesize_unit(unsigned long bytesize)
Obtain display units for filesize after conversion to B/kB/MB/GB, as appropriate.
Definition: dirlist.c:403
bool dirlist_generate_title(const char *title, char *buffer, int buffer_length)
Generates the part of an HTML directory listing page that contains the title.
Definition: dirlist.c:143
bool dirlist_generate_parent_link(const char *parent, char *buffer, int buffer_length)
Generates the part of an HTML directory listing page that links to the parent directory.
Definition: dirlist.c:202
bool dirlist_generate_top(char *buffer, int buffer_length)
Generates the top part of an HTML directory listing page.
Definition: dirlist.c:61
bool dirlist_generate_hide_columns(int flags, char *buffer, int buffer_length)
Generates the part of an HTML directory listing page that can suppress particular columns.
Definition: dirlist.c:100
static int dirlist_filesize_calculate(unsigned long *bytesize)
Obtain display value and units for filesize after conversion to B/kB/MB/GB, as appropriate.
Definition: dirlist.c:367
static int dirlist_filesize_value(unsigned long bytesize)
Obtain display value for filesize after conversion to B/kB/MB/GB, as appropriate.
Definition: dirlist.c:388
bool dirlist_generate_headings(char *buffer, int buffer_length)
Generates the part of an HTML directory listing page that displays the column headings.
Definition: dirlist.c:237
interface to generate HTML content for displaying directory listings.
#define DIRLIST_NO_TYPE_COLUMN
Definition: dirlist.h:32
#define DIRLIST_NO_DATE_COLUMN
Definition: dirlist.h:34
#define DIRLIST_NO_SIZE_COLUMN
Definition: dirlist.h:33
#define DIRLIST_NO_TIME_COLUMN
Definition: dirlist.h:35
#define DIRLIST_NO_NAME_COLUMN
Definition: dirlist.h:31
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
Netsurf additional integer type formatting macros.
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:241
Localised message support (interface).
colour nscolours[NSCOLOUR__COUNT]
NetSurf UI colour table.
Definition: nscolour.c:38
nserror nscolour_get_stylesheet(const char **stylesheet_out)
Get a pointer to a stylesheet for nscolours.
Definition: nscolour.c:206
NetSurf UI colours (interface).
@ NSCOLOUR_WIN_ODD_BG
Definition: nscolour.h:34
NetSurf URL handling (interface).
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
plotter style interfaces, generic styles and style colour helpers.
#define colour_rb_swap(c)
Definition: plot_style.h:217
Interface to utility string handling.
Interface to system colour values.
NetSurf types.