MIMETypeIconLibrary.st
author Claus Gittinger <cg@exept.de>
Thu, 04 Dec 2003 16:46:52 +0100
changeset 1875 8d2f35d8c1f2
parent 1872 a1b2eb1649df
child 1876 efbef37f8dce
permissions -rw-r--r--
*** empty log message ***

"{ Package: 'stx:libview2' }"

Object subclass:#MIMETypeIconLibrary
	instanceVariableNames:''
	classVariableNames:'Icons MatchedIcons'
	poolDictionaries:''
	category:'Interface-Smalltalk'
!


!MIMETypeIconLibrary class methodsFor:'initialization'!

initialize
    "/ now done lazily, when trhe first icon is asked for
    "/ self initializeIcons
!

initializeIcons
    |defaults resources addMapping fileNameToImageMap|

    Icons := Dictionary new.
    MatchedIcons := OrderedCollection new.
    fileNameToImageMap := Dictionary new.

    resources := ResourcePack for:self.

    defaults := Dictionary new.
    #(
        "/ internal-type to icon mappings.
        #directory             'tiny_yellow_dir.xpm'            
        #directoryOpen         'tiny_yellow_dir_open.xpm'       
        #directoryLocked       'tiny_yellow_dir_locked.xpm'     
        #directoryLink         'tiny_yellow_dir_link.xpm'       
        #directoryNetwork      'tiny_yellow_dir_network.xpm'    
        #file                  'tiny_file_plain.xpm'            
        #fileLink              'tiny_file_link.xpm'             
        #fileLocked            'tiny_file_lock.xpm'             

        #executableFile        'tiny_file_exec.xpm'             
        #deviceFile            'tiny_file_device.xpm'           
        #binaryFile            'tiny_file_binary.xpm'           
        #specialFile           'tiny_file_special.xpm'          

        #addOnLocked           'tiny_addOn_lock.xpm'            
        #addOnLinked           'tiny_addOn_link.xpm'            

        #directoryGray         'tiny_yellow_dir_gray.xpm'       
        #directoryOpenGray     'tiny_yellow_dir_open_gray.xpm'  

        "/ these are soon obsolete
        #imageFile             'tiny_file_pix.xpm'              
        #textFile              'tiny_file_text.xpm'             

        "/ mime-type to icon mappings.
        'image/*'                                       'tiny_file_pix.xpm'

        "/ misc text ...

        'text/html'                                     'tiny_file_html.xpm'
"/        'text/plain'                                    'tiny_file_text.xpm'
        'text/*'                                        'tiny_file_text.xpm'

        'application/postscript'                        'tiny_file_postscript.xpm'
        'application/pdf'                               'tiny_file_pdf.xpm'
        'application/rtf'                               'tiny_file_rtf.xpm'

        "/ video formats ...

"/        'video/x-sgi-movie'                             'tiny_file_movie.xpm'
"/        'video/x-msvideo'                               'tiny_file_movie.xpm'
"/        'video/quicktime'                               'tiny_file_movie.xpm'
"/        'video/x-mpeg2'                                 'tiny_file_movie.xpm'
"/        'video/mpeg'                                    'tiny_file_movie.xpm'
        'video/*'                                       'tiny_file_movie2.xpm'
        'application/vnd.rn-realmedia'                  'tiny_file_movie2.xpm'

        "/ audio formats ...

"/        'audio/x-pn-realaudio'                          'tiny_file_sound.xpm'
"/        'audio/x-mpeg'                                  'tiny_file_sound.xpm'
"/        'audio/x-mp3'                                   'tiny_file_sound.xpm'
"/        'audio/x-wav'                                   'tiny_file_sound.xpm'
"/        'audio/x-aiff'                                  'tiny_file_sound.xpm'
"/        'audio/basic'                                   'tiny_file_sound.xpm'
"/        'audio/x-shockwave-flash'                       'tiny_file_sound.xpm'
        'audio/*'                                       'tiny_file_sound.xpm'

        "/ misc stuff
        'application/winword'                           'tiny_file_text.xpm'

        "/ binary formats
        'application/binary'                            'tiny_file_binary.xpm'
        'application/library'                           'tiny_file_binary.xpm'
        'application/shared-library'                    'tiny_file_binary.xpm'

        "/ progr. languages
        'application/x-smalltalk-source'                'tiny_file_st.xpm'
        'application/x-smalltalk-source-sif'            'tiny_file_text.xpm'
        'application/x-c-source'                        'tiny_file_text.xpm'
        'application/x-c-header'                        'tiny_file_text.xpm'
        'application/x-cpp-source'                      'tiny_file_text.xpm'
        'application/x-javascript'                      'tiny_file_text.xpm'
        'application/x-java-source'                     'tiny_file_java.xpm'

        'application/x-sh'                              'tiny_file_text.xpm'
        'application/x-csh'                             'tiny_file_text.xpm'
        'application/x-tcl'                             'tiny_file_text.xpm'
        'application/x-perl'                            'tiny_file_text.xpm'
        'application/x-make'                            'tiny_file_text.xpm'
        'application/x-batch-script'                    'tiny_file_text.xpm'
        'application/x-asn1-source'                     'tiny_file_text.xpm'

        "/ compressors / archivers
        'application/x-tar'                             'tiny_file_archive.xpm'
        'application/x-gtar'                            'tiny_file_archive.xpm'
        'application/x-tar-compressed'                  'tiny_file_compressed_archive.xpm'
        'application/x-zip-compressed'                  'tiny_file_compressed_archive.xpm'
        'application/x-gzip-compressed'                 'tiny_file_compressed_archive.xpm'
        'application/x-cpio'                            'tiny_file_archive.xpm'
        'application/x-shar'                            'tiny_file_archive.xpm'
        'application/java-archive'                      'tiny_file_archive.xpm'

        '*'                                             'tiny_file_plain.xpm'
     ) pairWiseDo:[:fileTypeOrMimeTypePattern :baseName |
        defaults at:fileTypeOrMimeTypePattern put:baseName.
    ].

    addMapping := [:key :baseNameOrName |
        |fileName image|

        baseNameOrName notNil ifTrue:[
            image := fileNameToImageMap at:baseNameOrName ifAbsent:nil.
            image isNil ifTrue:[
                image := Smalltalk imageFromFileNamed:baseNameOrName forClass:self.
                image isNil ifTrue:[
                    fileName := 'bitmaps/xpmBitmaps/document_images/' , baseNameOrName.
                    image := Smalltalk imageFromFileNamed:fileName forClass:self.
                ].
            ].
            fileNameToImageMap at:baseNameOrName put:image.
            key includesMatchCharacters ifTrue:[
                MatchedIcons add:(key -> image).
            ] ifFalse:[    
                Icons at:key put:image.
            ]
        ]
    ].

    "all mappings from the resource file"
    resources keysAndValuesDo:addMapping.

    "missing mappings from above defaults"
    defaults keysAndValuesDo:[:key :baseName |
        (resources includesKey:key) ifFalse:[
            addMapping value:key value:baseName
        ].
    ].

    "
     self initializeIcons
    "
! !

!MIMETypeIconLibrary class methodsFor:'accessing'!

fileTypeIconKeyFor:aFilename
    aFilename isSymbolicLink ifTrue:[
        ^ #symbolicLink.
    ].
    aFilename isDirectory ifTrue:[
        ^ #directory.
    ].
    aFilename isSpecialFile ifTrue:[
        ^ #specialFile.
    ].
    aFilename isExecutableProgram ifTrue:[
        ^ #executableFile
    ].
    ^ #file.
!

iconForKey:mimeTypeOrKey

    ^ self icons at:mimeTypeOrKey ifAbsent:nil.
!

iconForKeyMatching:mimeTypeOrKey

    |icn assoc|

    icn := self iconForKey:mimeTypeOrKey.
    icn notNil ifTrue:[^icn].
    assoc := self  iconForMatchKey:mimeTypeOrKey.
    assoc notNil ifTrue:[^ assoc value].
    ^ nil.
!

iconForMatchKey:matchKey

    ^ self matchedIcons detect:[:assoc | assoc key match:matchKey] ifNone:nil.
!

iconKeyForFile:aFilenameArg
    "given a fileName, return an appropriate icon"

    |aFilename fileType mimeType suffix|

    aFilename := aFilenameArg.

    aFilenameArg isFilename ifTrue:[
        fileType := self fileTypeIconKeyFor:aFilename.
    ] ifFalse:[
        fileType := #file
    ].

    (fileType == #directory) ifFalse:[
        suffix := aFilename suffix.
        (suffix = 'bak' or:[suffix = 'sav']) ifTrue:[
            aFilename := aFilename withoutSuffix.
        ].
        mimeType := MIMETypes mimeTypeForFilename:aFilename.
    ].

    ^ (mimeType ? fileType).
!

iconKeyForRemoteDirectory:aFilenameArg
    "given a fileName, return an appropriate icon"

    ^ #directoryNetwork
!

icons
    Icons isNil ifTrue:[
        self initializeIcons
    ].
    ^ Icons
!

matchedIcons
    MatchedIcons isNil ifTrue:[
        self initializeIcons
    ].
    ^ MatchedIcons
! !

!MIMETypeIconLibrary class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/MIMETypeIconLibrary.st,v 1.9 2003-12-04 15:46:52 cg Exp $'
! !

MIMETypeIconLibrary initialize!