changeset 180:0038a2943cc2

- Made file inputs free the loaded file after the page is left.
author jcid
date Tue, 15 Apr 2008 17:43:17 +0200
parents 354925511026
children da443771b424
files ChangeLog src/html.cc src/misc.c src/misc.h
diffstat 4 files changed, 51 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Apr 14 21:11:00 2008 +0200
+++ b/ChangeLog	Tue Apr 15 17:43:17 2008 +0200
@@ -90,7 +90,6 @@
  - Fixed a bug in Cookies_parse_one. Set it to a single return point too!
  - Added dStr_memmem() and dStr_printable() to dlib.
  - Split Html_append_input() into smaller functions.
- - Implemented the file input control for forms (work in progress).
  - Implemented ISINDEX.
  - Added input image for FORMS.
    Patches: place
@@ -103,6 +102,7 @@
  - Added support for "charset" in the META element.
  - Added a_Capi_get_flags(). It requests a cache entry's status as flags.
  - Switched URL_DATA type from char* to a dStr.
+ - Implemented the file input control for forms (work in progress).
    Patch: place, Jorge Arellano Cid
 +- Fixed a cookies-related dillo freeze bug happening at:
      http://www.fltk.org/newsgroups.php?gfltk.general+v:24912
--- a/src/html.cc	Mon Apr 14 21:11:00 2008 +0200
+++ b/src/html.cc	Tue Apr 15 17:43:17 2008 +0200
@@ -249,6 +249,8 @@
                          entries, it is the initial value */
    DilloHtmlSelect *select;
    bool_t init_val;   /* only meaningful for buttons */
+   Dstr *file_data;   /* only meaningful for file inputs.
+                         todo: may become a list... */
 };
 
 /*-----------------------------------------------------------------------------
@@ -923,6 +925,7 @@
          DilloHtmlInput *input = form->inputs->getRef(j);
          dFree(input->name);
          dFree(input->init_str);
+         dStr_free(input->file_data, 1);
 
          if (input->type == DILLO_HTML_INPUT_SELECT ||
              input->type == DILLO_HTML_INPUT_SEL_LIST) {
@@ -3534,9 +3537,9 @@
       char *ptr = first = dStrdup(attrbuf);
       while (ptr && !charset) {
          char *curr = dStrsep(&ptr, " ,");
-         if (!strcasecmp(curr, "utf-8")) {
+         if (!dStrcasecmp(curr, "utf-8")) {
             charset = curr;
-         } else if (!strcasecmp(curr, "UNKNOWN")) {
+         } else if (!dStrcasecmp(curr, "UNKNOWN")) {
             /* defined to be whatever encoding the document is in */
             charset = html->charset;
          }
@@ -3787,6 +3790,7 @@
    input->init_str = (init_str) ? dStrdup(init_str) : NULL;
    input->select = select;
    input->init_val = init_val;
+   input->file_data = NULL;
    Html_reset_input(input);
 
    /* some stats */
@@ -3936,9 +3940,16 @@
                                               const char *name, Dstr *file,
                                               const char *filename)
 {
+   const char *ctype, *ext;
+
    if (name && name[0]) {
-      const char *ctype;
       (void)a_Misc_get_content_type_from_data(file->str, file->len, &ctype);
+      /* Heuristic: text/plain with ".htm[l]" extension -> text/html */
+      if ((ext = strrchr(filename, '.')) &&
+          !dStrcasecmp(ctype, "text/plain") &&
+          (!dStrcasecmp(ext, ".html") || !dStrcasecmp(ext, ".htm"))) {
+         ctype = "text/html";
+      }
 
       if (data->len == 0) {
          dStr_append(data, "--");
@@ -4078,19 +4089,13 @@
          (LabelButtonResource*)((Embed*)input->widget)->getResource();
       const char *filename = lbr->getLabel();
       if (filename[0] && strcmp(filename, input->init_str)) {
-         char *buf;
-         int buf_size;
-         char *escaped_name = a_Misc_escape_chars(filename, "% ");
-         DilloUrl *url = a_Url_new(escaped_name, "file:///", 0, 0, 0);
-         if (a_Capi_get_buf(url, &buf, &buf_size)) {
-            Dstr *file = dStr_sized_new(buf_size);
-            dStr_append_l(file, buf, buf_size);
+         if (input->file_data) {
+            Dstr *file = dStr_sized_new(input->file_data->len);
+            dStr_append_l(file, input->file_data->str, input->file_data->len);
             dList_append(values, file);
          } else {
-            MSG("form file input \"%s\" not loaded.\n", filename);
+            MSG("FORM file input \"%s\" not loaded.\n", filename);
          }
-         a_Url_free(url);
-         dFree(escaped_name);
       }
       break;
    }
@@ -4333,19 +4338,6 @@
 }
 
 /*
- * Callback used when getting a file for form input.
- */
-static void Html_get_file_cb(int Op, CacheClient_t *Client)
-{
-   DilloWeb *web = (DilloWeb *)Client->Web;
-   LabelButtonResource *lbr = (LabelButtonResource *)Client->CbData;
-   if (Op) {
-      lbr->setLabel(URL_PATH(Client->Url));
-      a_UIcmd_set_msg(web->bw, "File loaded.");
-   }
-}
-
-/*
  * Handler for events related to forms.
  *
  * TODO: Currently there's "clicked" for buttons, we surely need "enter" for
@@ -4385,13 +4377,15 @@
       if (filename) {
          LabelButtonResource *lbr =
             (LabelButtonResource*)((Embed*)input->widget)->getResource();
-         char *escaped_name = a_Misc_escape_chars(filename, "% ");
-         DilloUrl *url = a_Url_new(escaped_name, "file:///",URL_E2EReload,0,0);
-         DilloWeb *web = a_Web_new(url);
-         web->bw = html->bw;
-         a_Capi_open_url(web, Html_get_file_cb, lbr);
-         a_Url_free(url);
-         dFree(escaped_name);
+         a_UIcmd_set_msg(html->bw, "Loading file...");
+         dStr_free(input->file_data, 1);
+         input->file_data = a_Misc_file2dstr(filename);
+         if (input->file_data) {
+            a_UIcmd_set_msg(html->bw, "File loaded.");
+            lbr->setLabel(filename);
+         } else {
+            a_UIcmd_set_msg(html->bw, "ERROR: can't load: %s", filename);
+         }
       }
    } else if (input->type == DILLO_HTML_INPUT_RESET) {
       Html_reset_form(form);
--- a/src/misc.c	Mon Apr 14 21:11:00 2008 +0200
+++ b/src/misc.c	Tue Apr 15 17:43:17 2008 +0200
@@ -269,3 +269,25 @@
    out[i] = '\0';
    return out;
 }
+
+/*
+ * Load a local file into a dStr.
+ * Return value: dStr on success, NULL on error.
+ * todo: a filesize threshold may be implemented.
+ */
+Dstr *a_Misc_file2dstr(const char *filename)
+{
+   FILE *F_in;
+   int n;
+   char buf[4096];
+   Dstr *dstr = NULL;
+
+   if ((F_in = fopen(filename, "r"))) {
+      dstr = dStr_sized_new(4096);
+      while ((n = fread (buf, 1, 4096, F_in)) > 0) {
+         dStr_append_l(dstr, buf, n);
+      }
+      fclose(F_in);
+   }
+   return dstr;
+}
--- a/src/misc.h	Mon Apr 14 21:11:00 2008 +0200
+++ b/src/misc.h	Tue Apr 15 17:43:17 2008 +0200
@@ -15,7 +15,7 @@
 int a_Misc_content_type_check(const char *EntryType, const char *DetectedType);
 int a_Misc_parse_geometry(char *geom, int *x, int *y, int *w, int *h);
 char *a_Misc_encode_base64(const char *in);
-
+Dstr *a_Misc_file2dstr(const char *filename);
 
 #ifdef __cplusplus
 }