MIMETypeIconLibrary.st
author Claus Gittinger <cg@exept.de>
Wed, 03 Sep 2008 11:52:12 +0200
changeset 2539 9ed4a786aec2
parent 2359 283d81512850
child 2847 82b42231b3c6
permissions -rw-r--r--
category change

"
 COPYRIGHT (c) 2004 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libview2' }"

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

!MIMETypeIconLibrary class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2004 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
! !

!MIMETypeIconLibrary class methodsFor:'initialization'!

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

initializeIcons
    |icons matchedIcons defaults resources addMapping fileNameToImageMap|

    icons := Dictionary new.
    matchedIcons := OrderedCollection new.
    fileNameToImageMap := Dictionary new.

    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'
        'application/x-waltop-digital-notepad'          'tiny_file_digital_notepad.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.
            ]
        ]
    ].

    resources := self classResources.

    "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
        ].
    ].

    Icons := icons.
    MatchedIcons := matchedIcons.

    "
     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.15 2007-12-11 10:45:58 cg Exp $'
! !

MIMETypeIconLibrary initialize!