HTIcons.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:9k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /*        HTIcon.c
  2. ** ICON MANAGEMENT
  3. **
  4. ** (c) COPYRIGHT MIT 1995.
  5. ** Please first read the full copyright statement in the file COPYRIGH.
  6. **
  7. ** This module contains the functions for initializing, adding
  8. ** and selecting the icon for local directory listings, FTP and Gopher.
  9. **
  10. ** History:
  11. **    Mar 94 Written by Ari Luotonen, luotonen@dxcern.cern.ch
  12. **
  13. */
  14. /* Library include files */
  15. #include "tcp.h"
  16. #include "HTUtils.h"
  17. #include "HTString.h"
  18. #include "HTAnchor.h"
  19. #include "HTParse.h"
  20. #include "HTFormat.h"
  21. #include "HTChunk.h"
  22. #include "HTIcons.h"  /* Implemented here */
  23. /* Globals */
  24. PRIVATE BOOL HTDirShowBrackets = YES;
  25. PRIVATE HTIconNode * icon_unknown = NULL; /* Unknown file type */
  26. PRIVATE HTIconNode * icon_blank = NULL; /* Blank icon in heading */
  27. PRIVATE HTIconNode * icon_parent = NULL; /* Parent directory icon */
  28. PRIVATE HTIconNode * icon_dir = NULL; /* Directory icon */
  29. /* Type definitions and global variables etc. local to this module */
  30. PRIVATE HTList * icons = NULL;
  31. PRIVATE int alt_len = 0; /* Longest ALT text */
  32. /* 
  33.  * Global variable for the AddHref nodes
  34.  * AddHref URL suff1 suff2 ...
  35.  */
  36. PRIVATE HTList * hrefs = NULL;
  37. /* ------------------------------------------------------------------------- */
  38. PRIVATE void alt_resize (char * alt)
  39. {
  40.     if (alt) {
  41. int len = strlen(alt);
  42. if (len > alt_len) alt_len = len;
  43.     }
  44. }
  45. PUBLIC char * HTIcon_alt_string (char * alt,
  46.       BOOL brackets)
  47. {
  48.     static char * ret = NULL;
  49.     char * p = NULL;
  50.     int len = alt ? strlen(alt) : 0;
  51.     if (ret) HT_FREE(ret); /* from previous call */
  52.     if ((p = ret = (char*) HT_MALLOC(alt_len + 3)) == NULL)
  53. HT_OUTOFMEM("HTIcon_alt_string");
  54.     if (HTDirShowBrackets)
  55. *p++ = brackets ? '[' : ' ';
  56.     if (alt) strcpy(p,alt);
  57.     p += len;
  58.     while (len++ < alt_len) *p++=' ';
  59.     if (HTDirShowBrackets)
  60. *p++ = brackets ? ']' : ' ';
  61.     *p = 0;
  62.     return ret;
  63. }
  64. /*
  65. ** HTAddIcon(url, alt, type_templ) adds icon:
  66. **
  67. ** <IMG SRC="url" ALT="[alt]">
  68. **
  69. ** for files for which content-type or content-encoding matches
  70. ** type_templ.  If type_templ contains a slash, it is taken to be
  71. ** a content-type template.  Otherwise, it is a content-encoding
  72. ** template.
  73. */
  74. PUBLIC void HTAddIcon (char * url,
  75.     char * alt,
  76.     char * type_templ)
  77. {
  78.     HTIconNode * node;
  79.     if (!url || !type_templ) return;
  80.     if ((node = (HTIconNode *) HT_CALLOC(1,sizeof(HTIconNode))) == NULL)
  81.         HT_OUTOFMEM("HTAddIcon");
  82.     if (url) StrAllocCopy(node->icon_url, url);
  83.     if (alt) StrAllocCopy(node->icon_alt, alt);
  84.     if (type_templ) StrAllocCopy(node->type_templ, type_templ);
  85.     if (!icons) icons = HTList_new();
  86.     HTList_addObject(icons, (void*)node);
  87.     alt_resize(alt);
  88.     if (PROT_TRACE)
  89. TTYPrint(TDEST, "AddIcon..... %s => SRC="%s" ALT="%s"n",
  90. type_templ,url, alt ? alt : "");
  91. }
  92. /*
  93.  * Put the AddHrefs in a list. It can be used for indexing to
  94.  * present special filetypes through a CGI.
  95.  */
  96. PUBLIC void HTAddHref (char *     url,
  97.                             char *     type_templ)
  98. {
  99.     HTHrefNode * node;
  100.     if (!url || !type_templ) return;
  101.     if ((node = (HTHrefNode *) HT_CALLOC(1,sizeof(HTHrefNode))) == NULL)
  102.         HT_OUTOFMEM("HTAddHref");
  103.     if (url) StrAllocCopy(node->href_url, url);
  104.     if (type_templ) StrAllocCopy(node->type_templ, type_templ);
  105.     if (!hrefs) hrefs = HTList_new();
  106.     HTList_addObject(hrefs, (void*)node);
  107.     if (PROT_TRACE)
  108. TTYPrint(TDEST, "AddHref..... %s => URL="%s" n",type_templ,url);
  109. }
  110. /*
  111. ** HTAddUnknownIcon(url,alt) adds the icon used for files for which
  112. ** no other icon seems appropriate (unknown type).
  113. */
  114. PUBLIC void HTAddUnknownIcon (char * url,
  115.    char * alt)
  116. {
  117.     if ((icon_unknown = (HTIconNode *) HT_CALLOC(1,sizeof(HTIconNode))) == NULL)
  118.         HT_OUTOFMEM("HTAddUnknownIcon");
  119.     if (url) StrAllocCopy(icon_unknown->icon_url, url);
  120.     if (alt) StrAllocCopy(icon_unknown->icon_alt, alt);
  121.     alt_resize(alt);
  122.     if (PROT_TRACE)
  123. TTYPrint(TDEST,"AddIcon..... UNKNOWN => SRC="%s" ALT="%s"n",url,
  124. alt ? alt : "");
  125. }
  126. /*
  127. ** HTAddBlankIcon(url,alt) adds the blank icon used in the
  128. ** heading of the listing.
  129. */
  130. PUBLIC void HTAddBlankIcon (char * url,
  131.  char * alt)
  132. {
  133.     if ((icon_blank = (HTIconNode *) HT_CALLOC(1,sizeof(HTIconNode))) == NULL)
  134.         HT_OUTOFMEM("HTAddBlankIcon");
  135.     if (url) StrAllocCopy(icon_blank->icon_url, url);
  136.     if (alt) StrAllocCopy(icon_blank->icon_alt, alt);
  137.     alt_resize(alt);
  138.     if (PROT_TRACE)
  139. TTYPrint(TDEST,"AddIcon..... BLANK => SRC="%s" ALT="%s"n",url,
  140. alt ? alt : "");
  141. }
  142. /*
  143. ** HTAddParentIcon(url,alt) adds the parent directory icon.
  144. */
  145. PUBLIC void HTAddParentIcon (char * url,
  146.   char * alt)
  147. {
  148.     if ((icon_parent = (HTIconNode *) HT_CALLOC(1,sizeof(HTIconNode))) == NULL)
  149.         HT_OUTOFMEM("HTAddBlankIcon");
  150.     if (url) StrAllocCopy(icon_parent->icon_url, url);
  151.     if (alt) StrAllocCopy(icon_parent->icon_alt, alt);
  152.     alt_resize(alt);
  153.     if (PROT_TRACE)
  154. TTYPrint(TDEST,"AddIcon..... PARENT => SRC="%s" ALT="%s"n",url,
  155. alt ? alt : "");
  156. }
  157. /*
  158. ** HTAddDirIcon(url,alt) adds the directory icon.
  159. */
  160. PUBLIC void HTAddDirIcon (char * url,
  161.        char * alt)
  162. {
  163.     if ((icon_dir = (HTIconNode *) HT_CALLOC(1,sizeof(HTIconNode))) == NULL)
  164.         HT_OUTOFMEM("HTAddBlankIcon");
  165.     if (url) StrAllocCopy(icon_dir->icon_url, url);
  166.     if (alt) StrAllocCopy(icon_dir->icon_alt, alt);
  167.     alt_resize(alt);
  168.     if (PROT_TRACE)
  169. TTYPrint(TDEST,"AddIcon..... DIRECTORY => SRC="%s" ALT="%s"n",url,
  170. alt ? alt : "");
  171. }
  172. PRIVATE BOOL match (char * templ,
  173.  char * actual)
  174. {
  175.     static char * c1 = NULL;
  176.     static char * c2 = NULL;
  177.     char * slash1;
  178.     char * slash2;
  179.     StrAllocCopy(c1,templ);
  180.     StrAllocCopy(c2,actual);
  181.     slash1 = strchr(c1,'/');
  182.     slash2 = strchr(c2,'/');
  183.     if (slash1 && slash2) {
  184. *slash1++ = 0;
  185. *slash2++ = 0;
  186. return HTStrMatch(c1,c2) && HTStrMatch(slash1,slash2);
  187.     }
  188.     else if (!slash1 && !slash2)
  189. return HTStrMatch(c1,c2) ? YES : NO;
  190.     else
  191. return NO;
  192. }
  193. PRIVATE char * prefixed (CONST char * prefix,
  194.       char * name)
  195. {
  196.     static char * ret = NULL;
  197.     HT_FREE(ret); /* from previous call */
  198.     if ((ret = (char  *) HT_MALLOC(strlen(prefix) + strlen(name) + 2)) == NULL)
  199.         HT_OUTOFMEM("prefixed");
  200.     strcpy(ret,prefix);
  201.     if (*prefix && prefix[strlen(prefix)-1] != '/')
  202. strcat(ret,"/");
  203.     strcat(ret,name);
  204.     return ret;
  205. }
  206. PUBLIC void HTStdIconInit (CONST char * url_prefix)
  207. {
  208.     CONST char * p = url_prefix ? url_prefix : "/internal-icon/";
  209.     HTAddBlankIcon  (prefixed(p,"blank.xbm"), NULL );
  210.     HTAddDirIcon    (prefixed(p,"directory.xbm"),"DIR" );
  211.     HTAddParentIcon (prefixed(p,"back.xbm"), "UP" );
  212.     HTAddUnknownIcon(prefixed(p,"unknown.xbm"), NULL );
  213.     HTAddIcon(prefixed(p,"unknown.xbm"), NULL, "*/*");
  214.     HTAddIcon(prefixed(p,"binary.xbm"), "BIN", "binary");
  215.     HTAddIcon(prefixed(p,"unknown.xbm"), NULL, "www/unknown");
  216.     HTAddIcon(prefixed(p,"text.xbm"), "TXT", "text/*");
  217.     HTAddIcon(prefixed(p,"image.xbm"), "IMG", "image/*");
  218.     HTAddIcon(prefixed(p,"movie.xbm"), "MOV", "video/*");
  219.     HTAddIcon(prefixed(p,"sound.xbm"), "AU", "audio/*");
  220.     HTAddIcon(prefixed(p,"tar.xbm"), "TAR", "multipart/x-tar");
  221.     HTAddIcon(prefixed(p,"tar.xbm"), "TAR", "multipart/x-gtar");
  222.     HTAddIcon(prefixed(p,"compressed.xbm"), "CMP", "x-compress");
  223.     HTAddIcon(prefixed(p,"compressed.xbm"), "GZP", "x-gzip");
  224.     HTAddIcon(prefixed(p,"index.xbm"), "IDX", "application/x-gopher-index");
  225.     HTAddIcon(prefixed(p,"index2.xbm"), "CSO", "application/x-gopher-cso");
  226.     HTAddIcon(prefixed(p,"telnet.xbm"), "TEL", "application/x-gopher-telnet");
  227.     HTAddIcon(prefixed(p,"unknown.xbm"),        "DUP", "application/x-gopher-duplicate");
  228.     HTAddIcon(prefixed(p,"unknown.xbm"), "TN", "application/x-gopher-tn3270");
  229. }
  230. /*  HTGetIcon()
  231. ** returns the icon corresponding to content_type or content_encoding.
  232. */
  233. PUBLIC HTIconNode * HTGetIcon (HTFileMode mode,
  234.        HTFormat content_type,
  235.        HTEncoding content_encoding)
  236. {
  237.     if (!icon_unknown) icon_unknown = icon_blank;
  238.     if (mode == HT_IS_FILE) {
  239. char * ct = content_type ? HTAtom_name(content_type) : NULL;
  240. char * ce = content_encoding ? HTAtom_name(content_encoding) : NULL;
  241. HTList * cur = icons;
  242. HTIconNode * node;
  243. while ((node = (HTIconNode*)HTList_nextObject(cur))) {
  244.     char * slash = strchr(node->type_templ,'/');
  245.     if ((ct && slash && match(node->type_templ,ct)) ||
  246. (ce && !slash && HTStrMatch(node->type_templ,ce))) {
  247. return node;
  248.     }
  249. }
  250.     } else if (mode == HT_IS_DIR) {
  251. return icon_dir ? icon_dir : icon_unknown;
  252.     } else if (mode == HT_IS_BLANK) {
  253. return icon_blank ? icon_blank : icon_unknown;
  254.     } else if (mode == HT_IS_PARENT) {
  255. return icon_parent ? icon_parent : icon_unknown;
  256.     }
  257.     return icon_unknown;
  258. }
  259. /*
  260.  * Find the URL for a given type. Called from HTDirBrw.c
  261.  */
  262. PUBLIC HTHrefNode * HTGetHref ( char * filename)
  263. {
  264.     HTHrefNode * node;
  265.     char *c;
  266.     HTList * cur = hrefs;
  267.     c = strrchr(filename, '.');
  268.     if (c) {
  269. while ((node = (HTHrefNode*)HTList_nextObject(cur))) {
  270.     if ((!strcmp(node->type_templ,c)) ) {
  271. return node;
  272.     }
  273. }
  274.     }
  275.     return NULL;
  276. }
  277. /* END OF MODULE */