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

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

Webmaster