Annotation of libwww/Library/src/HTIcons.c, revision 2.17

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

Webmaster