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

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

Webmaster