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

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.25    ! frystyk     6: **     @(#) $Id: Date Author State $
2.1       frystyk     7: **
                      8: **     This module contains the functions for initializing, adding
                      9: **     and selecting the icon for local directory listings, FTP and Gopher.
                     10: **
                     11: **     History:
2.13      frystyk    12: **        Mar 94       Written by Ari Luotonen, luotonen@dxcern.cern.ch
2.1       frystyk    13: **
                     14: */
                     15: 
                     16: /* Library include files */
2.23      frystyk    17: #include "sysdep.h"
2.8       frystyk    18: #include "HTUtils.h"
                     19: #include "HTString.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 */
2.19      frystyk    27: PRIVATE 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: 
2.18      frystyk    46: PRIVATE void alt_resize (char * alt)
2.1       frystyk    47: {
                     48:     if (alt) {
                     49:        int len = strlen(alt);
                     50:        if (len > alt_len) alt_len = len;
                     51:     }
                     52: }
                     53: 
                     54: 
2.18      frystyk    55: PUBLIC char * HTIcon_alt_string (char *        alt,
                     56:                                      BOOL      brackets)
2.1       frystyk    57: {
                     58:     static char * ret = NULL;
                     59:     char * p = NULL;
                     60:     int len = alt ? strlen(alt) : 0;
                     61: 
2.24      frystyk    62:     HT_FREE(ret);              /* from previous call */
2.21      frystyk    63:     if ((p = ret = (char*) HT_MALLOC(alt_len + 3)) == NULL)
                     64:        HT_OUTOFMEM("HTIcon_alt_string");
2.1       frystyk    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: */
2.18      frystyk    89: PUBLIC void HTAddIcon (char *  url,
                     90:                            char *      alt,
                     91:                            char *      type_templ)
2.1       frystyk    92: {
                     93:     HTIconNode * node;
                     94: 
                     95:     if (!url || !type_templ) return;
                     96: 
2.21      frystyk    97:     if ((node = (HTIconNode *) HT_CALLOC(1,sizeof(HTIconNode))) == NULL)
                     98:         HT_OUTOFMEM("HTAddIcon");
2.1       frystyk    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.22      eric      108:        HTTrace("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:  */
2.18      frystyk   117: PUBLIC void HTAddHref (char *     url,
                    118:                             char *     type_templ)
2.2       luotonen  119: {
                    120:     HTHrefNode * node;
                    121: 
                    122:     if (!url || !type_templ) return;
                    123: 
2.21      frystyk   124:     if ((node = (HTHrefNode *) HT_CALLOC(1,sizeof(HTHrefNode))) == NULL)
                    125:         HT_OUTOFMEM("HTAddHref");
2.2       luotonen  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.22      eric      133:        HTTrace("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: */
2.18      frystyk   142: PUBLIC void HTAddUnknownIcon (char * url,
                    143:                                   char * alt)
2.1       frystyk   144: {
2.21      frystyk   145:     if ((icon_unknown = (HTIconNode *) HT_CALLOC(1,sizeof(HTIconNode))) == NULL)
                    146:         HT_OUTOFMEM("HTAddUnknownIcon");
2.1       frystyk   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.22      eric      152:        HTTrace("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: */
2.18      frystyk   161: PUBLIC void HTAddBlankIcon (char * url,
                    162:                                 char * alt)
2.1       frystyk   163: {
2.21      frystyk   164:     if ((icon_blank = (HTIconNode *) HT_CALLOC(1,sizeof(HTIconNode))) == NULL)
                    165:         HT_OUTOFMEM("HTAddBlankIcon");
2.1       frystyk   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.22      eric      171:        HTTrace("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: */
2.18      frystyk   179: PUBLIC void HTAddParentIcon (char * url,
                    180:                                  char * alt)
2.1       frystyk   181: {
2.21      frystyk   182:     if ((icon_parent = (HTIconNode *) HT_CALLOC(1,sizeof(HTIconNode))) == NULL)
                    183:         HT_OUTOFMEM("HTAddBlankIcon");
2.1       frystyk   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.22      eric      189:        HTTrace("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: */
2.18      frystyk   197: PUBLIC void HTAddDirIcon (char * url,
                    198:                               char * alt)
2.1       frystyk   199: {
2.21      frystyk   200:     if ((icon_dir = (HTIconNode *) HT_CALLOC(1,sizeof(HTIconNode))) == NULL)
                    201:         HT_OUTOFMEM("HTAddBlankIcon");
2.1       frystyk   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.22      eric      207:        HTTrace("AddIcon..... DIRECTORY => SRC=\"%s\" ALT=\"%s\"\n",url,
2.8       frystyk   208:                alt ? alt : "");
2.1       frystyk   209: }
                    210: 
                    211: 
2.18      frystyk   212: PRIVATE BOOL match (char * templ,
                    213:                         char * actual)
2.1       frystyk   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;
2.20      frystyk   229:        return HTStrMatch(c1,c2) && HTStrMatch(slash1,slash2);
2.1       frystyk   230:     }
                    231:     else if (!slash1 && !slash2)
2.20      frystyk   232:        return HTStrMatch(c1,c2) ? YES : NO;
2.1       frystyk   233:     else
                    234:        return NO;
                    235: }
                    236: 
                    237: 
2.23      frystyk   238: PRIVATE char * prefixed (const char *  prefix,
2.18      frystyk   239:                              char *            name)
2.1       frystyk   240: {
                    241:     static char * ret = NULL;
2.21      frystyk   242:     HT_FREE(ret);              /* from previous call */
2.1       frystyk   243: 
2.21      frystyk   244:     if ((ret = (char  *) HT_MALLOC(strlen(prefix) + strlen(name) + 2)) == NULL)
                    245:         HT_OUTOFMEM("prefixed");
2.1       frystyk   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.23      frystyk   255: PUBLIC void HTStdIconInit (const char * url_prefix)
2.1       frystyk   256: {
2.23      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.18      frystyk   285: PUBLIC HTIconNode * HTGetIcon (HTFileMode      mode,
                    286:                               HTFormat         content_type,
                    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)) ||
2.20      frystyk   299:                (ce && !slash && HTStrMatch(node->type_templ,ce))) {
2.1       frystyk   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:  */
2.18      frystyk   317: PUBLIC HTHrefNode * HTGetHref ( char * filename)
2.2       luotonen  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