FindFileApplication.st
changeset 5723 dadcb827b659
parent 5677 4aea2e3fd1e3
child 5726 464df1753187
equal deleted inserted replaced
5722:aab6fc297cd1 5723:dadcb827b659
    14 
    14 
    15 AbstractFileApplicationNoteBookComponent subclass:#FindFileApplication
    15 AbstractFileApplicationNoteBookComponent subclass:#FindFileApplication
    16 	instanceVariableNames:'searchDirectories searchDirectoryHolder notSearchForSameContents
    16 	instanceVariableNames:'searchDirectories searchDirectoryHolder notSearchForSameContents
    17 		namePatternHolder ignoreCaseInName contentsPatternHolder
    17 		namePatternHolder ignoreCaseInName contentsPatternHolder
    18 		ignoreCaseInContents notContentsPatternHolder
    18 		ignoreCaseInContents notContentsPatternHolder
    19 		ignoreCaseInNotContents findFileView searchResultTable resultList
    19 		ignoreCaseInNotContents sameContentsAsHolder findFileView
    20 		enableStop enableSearch stopSignal accessLock searchTask expanded
    20 		searchResultTable resultList enableStop enableSearch stopSignal
    21 		searchRecursively selectionHolder hasListEntries
    21 		accessLock searchTask expanded searchRecursively selectionHolder
    22 		targetApplication useLocate useGrep'
    22 		hasListEntries targetApplication useLocate useGrep
    23 	classVariableNames:''
    23 		rememberInCache'
       
    24 	classVariableNames:'ContentsInfoCache'
    24 	poolDictionaries:''
    25 	poolDictionaries:''
    25 	category:'Interface-Tools-File'
    26 	category:'Interface-Tools-File'
    26 !
    27 !
    27 
    28 
    28 !FindFileApplication class methodsFor:'documentation'!
    29 !FindFileApplication class methodsFor:'documentation'!
    42 ! !
    43 ! !
    43 
    44 
    44 !FindFileApplication class methodsFor:'instance creation'!
    45 !FindFileApplication class methodsFor:'instance creation'!
    45 
    46 
    46 open
    47 open
    47 
    48     ^ self openInDirectory:(Filename currentDirectory)
    48     ^ self openOnFileName:(Filename currentDirectory asAbsoluteFilename)
    49 
       
    50     "
       
    51      self open
       
    52     "
       
    53 !
       
    54 
       
    55 openInDirectory:aFilename
       
    56     ^ self openOnFileName:(aFilename asFilename asAbsoluteFilename)
       
    57 
       
    58     "
       
    59      self openInDirectory:'/etc'
       
    60     "
    49 !
    61 !
    50 
    62 
    51 openOnFileName:aFileName
    63 openOnFileName:aFileName
       
    64     ^ self openOnFileName:aFileName for:nil
       
    65 !
       
    66 
       
    67 openOnFileName:aFileName for:aTargetApplicationOrNil
    52 
    68 
    53     | instance builder|
    69     | instance builder|
    54 
    70 
    55     builder := super open.
    71     builder := super open.
    56     instance := builder application.
    72     instance := builder application.
    57     instance item:(DirectoryContentsBrowser itemClass fileName:aFileName).
    73     instance item:(DirectoryContentsBrowser itemClass fileName:aFileName).
    58     ^ builder
    74     aTargetApplicationOrNil notNil ifTrue:[
    59 !
    75         instance targetApplication:aTargetApplicationOrNil.
    60 
    76     ].
    61 openOnFileName:aFileName for:aTargetApplication
       
    62 
       
    63     | instance builder|
       
    64 
       
    65     builder := super open.
       
    66     instance := builder application.
       
    67     instance item:(DirectoryContentsBrowser itemClass fileName:aFileName).
       
    68     instance targetApplication:aTargetApplication.
       
    69     ^ builder
    77     ^ builder
    70 ! !
    78 ! !
    71 
    79 
    72 !FindFileApplication class methodsFor:'defaults'!
    80 !FindFileApplication class methodsFor:'defaults'!
    73 
    81 
   114               menu: searchMenu
   122               menu: searchMenu
   115               textDefault: true
   123               textDefault: true
   116             )
   124             )
   117            (ViewSpec
   125            (ViewSpec
   118               name: 'Box1'
   126               name: 'Box1'
   119               layout: (LayoutFrame 0 0.0 32 0 0 1.0 132 0)
   127               layout: (LayoutFrame 0 0.0 32 0 0 1.0 156 0)
   120               component: 
   128               component: 
   121              (SpecCollection
   129              (SpecCollection
   122                 collection: (
   130                 collection: (
   123                  (LabelSpec
   131                  (LabelSpec
   124                     label: 'Directory:'
   132                     label: 'Directory:'
   152                     translateLabel: true
   160                     translateLabel: true
   153                   )
   161                   )
   154                  (LabelSpec
   162                  (LabelSpec
   155                     label: 'Search Files Named:'
   163                     label: 'Search Files Named:'
   156                     name: 'FileNameLabel'
   164                     name: 'FileNameLabel'
   157                     layout: (LayoutFrame 2 0 30 0 154 0 47 0)
   165                     layout: (LayoutFrame 2 0 31 0 154 0 48 0)
   158                     translateLabel: true
   166                     translateLabel: true
   159                     adjust: right
   167                     adjust: right
   160                   )
   168                   )
   161                  (InputFieldSpec
   169                  (InputFieldSpec
   162                     name: 'FileNameEntryField'
   170                     name: 'FileNameEntryField'
   169                     acceptOnPointerLeave: false
   177                     acceptOnPointerLeave: false
   170                   )
   178                   )
   171                  (CheckBoxSpec
   179                  (CheckBoxSpec
   172                     label: 'Directories'
   180                     label: 'Directories'
   173                     name: 'SearchDirectoriesCheckBox'
   181                     name: 'SearchDirectoriesCheckBox'
   174                     layout: (LayoutFrame -300 1 29 0 -158 1 49 0)
   182                     layout: (LayoutFrame -300 1 29 0 -158 1 52 0)
   175                     tabable: true
   183                     tabable: true
   176                     model: searchDirectories
   184                     model: searchDirectories
   177                     translateLabel: true
   185                     translateLabel: true
   178                   )
   186                   )
   179                  (CheckBoxSpec
   187                  (CheckBoxSpec
   180                     label: 'Ignore Case'
   188                     label: 'Ignore Case'
   181                     name: 'IgnoreCaseInNameCheckBox'
   189                     name: 'IgnoreCaseInNameCheckBox'
   182                     layout: (LayoutFrame -160 1 29 0 -4 1 49 0)
   190                     layout: (LayoutFrame -160 1 29 0 -4 1 52 0)
   183                     tabable: true
   191                     tabable: true
   184                     model: ignoreCaseInName
   192                     model: ignoreCaseInName
   185                     translateLabel: true
   193                     translateLabel: true
   186                   )
   194                   )
   187                  (LabelSpec
   195                  (LabelSpec
   201                     acceptOnPointerLeave: false
   209                     acceptOnPointerLeave: false
   202                   )
   210                   )
   203                  (CheckBoxSpec
   211                  (CheckBoxSpec
   204                     label: 'Use ''grep'' Cmd'
   212                     label: 'Use ''grep'' Cmd'
   205                     name: 'UseGrepCheckBox'
   213                     name: 'UseGrepCheckBox'
   206                     layout: (LayoutFrame -300 1 52 0 -158 1 75 0)
   214                     layout: (LayoutFrame -300 1 53 0 -158 1 76 0)
   207                     visibilityChannel: canUseGrep
   215                     visibilityChannel: canUseGrep
   208                     tabable: true
   216                     tabable: true
   209                     model: useGrep
   217                     model: useGrep
   210                     translateLabel: true
   218                     translateLabel: true
   211                   )
   219                   )
   212                  (CheckBoxSpec
   220                  (CheckBoxSpec
   213                     label: 'Ignore Case'
   221                     label: 'Ignore Case'
   214                     name: 'IgnoreCaseInContentsCheckBox'
   222                     name: 'IgnoreCaseInContentsCheckBox'
   215                     layout: (LayoutFrame -160 1 52 0 -4 1 75 0)
   223                     layout: (LayoutFrame -160 1 53 0 -4 1 76 0)
   216                     enableChannel: notSearchForSameContents
   224                     enableChannel: notSearchForSameContents
   217                     tabable: true
   225                     tabable: true
   218                     model: ignoreCaseInContents
   226                     model: ignoreCaseInContents
   219                     translateLabel: true
   227                     translateLabel: true
   220                   )
   228                   )
   235                     acceptOnPointerLeave: false
   243                     acceptOnPointerLeave: false
   236                   )
   244                   )
   237                  (CheckBoxSpec
   245                  (CheckBoxSpec
   238                     label: 'Ignore Case'
   246                     label: 'Ignore Case'
   239                     name: 'IgnoreCaseInNotContentsCheckBox'
   247                     name: 'IgnoreCaseInNotContentsCheckBox'
   240                     layout: (LayoutFrame -160 1 76 0 -4 1 99 0)
   248                     layout: (LayoutFrame -160 1 77 0 -4 1 100 0)
   241                     enableChannel: notSearchForSameContents
   249                     enableChannel: notSearchForSameContents
   242                     tabable: true
   250                     tabable: true
   243                     model: ignoreCaseInNotContents
   251                     model: ignoreCaseInNotContents
       
   252                     translateLabel: true
       
   253                   )
       
   254                  (LabelSpec
       
   255                     label: 'Same Contents As:'
       
   256                     name: 'SameContentsAsLabel'
       
   257                     layout: (LayoutFrame 2 0 103 0 154 0 120 0)
       
   258                     translateLabel: true
       
   259                     adjust: right
       
   260                   )
       
   261                  (InputFieldSpec
       
   262                     name: 'SameContentsAsEntryField'
       
   263                     layout: (LayoutFrame 156 0 100 0 -315 1 120 0)
       
   264                     enableChannel: notSearchForSameContents
       
   265                     tabable: true
       
   266                     model: sameContentsAsHolder
       
   267                     formatString: ''
       
   268                     immediateAccept: true
       
   269                     acceptOnPointerLeave: false
       
   270                   )
       
   271                  (CheckBoxSpec
       
   272                     label: 'Cache Info'
       
   273                     name: 'RememberInCacheCheckBox'
       
   274                     layout: (LayoutFrame -300 1 101 0 -158 1 124 0)
       
   275                     visibilityChannel: canUseGrep
       
   276                     tabable: true
       
   277                     model: rememberInCache
   244                     translateLabel: true
   278                     translateLabel: true
   245                   )
   279                   )
   246                  )
   280                  )
   247                
   281                
   248               )
   282               )
   249             )
   283             )
   250            (SequenceViewSpec
   284            (SequenceViewSpec
   251               name: 'List1'
   285               name: 'List1'
   252               layout: (LayoutFrame 0 0.0 132 0 0 1.0 0 1)
   286               layout: (LayoutFrame 0 0.0 156 0 0 1.0 0 1)
   253               model: selectionHolder
   287               model: selectionHolder
   254               menu: menu
   288               menu: menu
   255               hasHorizontalScrollBar: true
   289               hasHorizontalScrollBar: true
   256               hasVerticalScrollBar: true
   290               hasVerticalScrollBar: true
   257               isMultiSelect: true
   291               isMultiSelect: true
   469     ].
   503     ].
   470 !
   504 !
   471 
   505 
   472 doSearch
   506 doSearch
   473 
   507 
   474     | namePattern namePatterns contentsPattern notContentsPattern dir|
   508     | namePattern namePatterns contentsPattern notContentsPattern dir fileToCompareAgainst|
   475 
   509 
   476 "/    self changeExtentToSeeSearchResult.
   510 "/    self changeExtentToSeeSearchResult.
   477 
   511 
   478     dir := self searchDirectoryHolder value.
   512     dir := self searchDirectoryHolder value.
   479     dir isNil ifTrue:[
   513     dir isNil ifTrue:[
   517     ] ifFalse:[
   551     ] ifFalse:[
   518         self ignoreCaseInNotContents value ifTrue:[
   552         self ignoreCaseInNotContents value ifTrue:[
   519             notContentsPattern := notContentsPattern asLowercase
   553             notContentsPattern := notContentsPattern asLowercase
   520         ]
   554         ]
   521     ].
   555     ].
       
   556     fileToCompareAgainst := sameContentsAsHolder value withoutSeparators.
       
   557     fileToCompareAgainst isEmpty ifTrue:[
       
   558         fileToCompareAgainst := nil.
       
   559     ] ifFalse:[
       
   560         fileToCompareAgainst := fileToCompareAgainst asFilename
       
   561     ].
       
   562 
   522     searchTask := Process for:[
   563     searchTask := Process for:[
   523         |message t|
   564         |message t|
   524 
   565 
   525         [    
   566         [    
   526             (self stopSignal) catch:[
   567             (self stopSignal) catch:[
   535                         ignoreCase:(self ignoreCaseInName value)
   576                         ignoreCase:(self ignoreCaseInName value)
   536                         containingString:contentsPattern
   577                         containingString:contentsPattern
   537                         ignoreCaseInContents:(self ignoreCaseInContents value)
   578                         ignoreCaseInContents:(self ignoreCaseInContents value)
   538                         notContainingString:notContentsPattern
   579                         notContainingString:notContentsPattern
   539                         ignoreCaseInNotContents:(self ignoreCaseInNotContents value)
   580                         ignoreCaseInNotContents:(self ignoreCaseInNotContents value)
   540                         sameContentsAsFile:nil 
   581                         sameContentsAsFile:fileToCompareAgainst  
   541                         sameContentsAs:nil 
   582                         sameContentsAs:nil 
   542                         in:(self searchDirectoryHolder value).
   583                         in:(self searchDirectoryHolder value).
   543                 ].
   584                 ].
   544                 t > 100 ifTrue:[
   585                 t > 100 ifTrue:[
   545                     t := ((t / 1000) asFixedPoint:2) printString , ' s'
   586                     t := ((t / 1000) asFixedPoint:2) printString , ' s'
   727         notSearchForSameContents := true asValue.
   768         notSearchForSameContents := true asValue.
   728     ].
   769     ].
   729     ^ notSearchForSameContents.
   770     ^ notSearchForSameContents.
   730 !
   771 !
   731 
   772 
       
   773 rememberInCache
       
   774     rememberInCache isNil ifTrue:[
       
   775         rememberInCache := false asValue.
       
   776     ].
       
   777     ^ rememberInCache.
       
   778 !
       
   779 
   732 resultList
   780 resultList
   733 
   781 
   734     resultList isNil ifTrue:[
   782     resultList isNil ifTrue:[
   735         resultList := List new.
   783         resultList := List new.
   736         resultList addDependent:self.
   784         resultList addDependent:self.
   737     ].
   785     ].
   738     ^ resultList.
   786     ^ resultList.
       
   787 !
       
   788 
       
   789 sameContentsAsHolder
       
   790     |sel|
       
   791 
       
   792     sameContentsAsHolder isNil ifTrue:[
       
   793         sameContentsAsHolder := ValueHolder new.
       
   794         masterApplication notNil ifTrue:[
       
   795             sel := masterApplication currentSelectedFiles.
       
   796             sel size > 0 ifTrue:[
       
   797                 sameContentsAsHolder value:(sel first asFilename pathName).
       
   798             ].
       
   799         ].
       
   800     ].
       
   801     ^ sameContentsAsHolder.
   739 !
   802 !
   740 
   803 
   741 searchDirectories
   804 searchDirectories
   742     searchDirectories isNil ifTrue:[
   805     searchDirectories isNil ifTrue:[
   743         searchDirectories := false asValue.
   806         searchDirectories := false asValue.
   893     notContainingString:notContentsStringArg ignoreCaseInNotContents:ignCaseInNotContents 
   956     notContainingString:notContentsStringArg ignoreCaseInNotContents:ignCaseInNotContents 
   894     sameContentsAsFile:filenameToCompareContentsOrNil sameContentsAs:bytesToCompareContentsOrNil in:aDirectory
   957     sameContentsAsFile:filenameToCompareContentsOrNil sameContentsAs:bytesToCompareContentsOrNil in:aDirectory
   895 
   958 
   896     |dir nameMatches lines contentsToCompare list inStream
   959     |dir nameMatches lines contentsToCompare list inStream
   897      doesFileMatch contentsString notContentsString check checkNot 
   960      doesFileMatch contentsString notContentsString check checkNot 
   898      grepCommand nameMatch|
   961      grepCommand nameMatch fileSizeSearchFor fileSearchedFor fileMD5SearchedFor remember
       
   962      cache|
   899 
   963 
   900     contentsString := contentsStringArg.
   964     contentsString := contentsStringArg.
   901     ignCaseInContents ifTrue:[ contentsString := contentsString asLowercase ].
   965     ignCaseInContents ifTrue:[ contentsString := contentsString asLowercase ].
   902     notContentsString := notContentsStringArg.
   966     notContentsString := notContentsStringArg.
   903     ignCaseInNotContents ifTrue:[ notContentsString := notContentsString asLowercase ].    
   967     ignCaseInNotContents ifTrue:[ notContentsString := notContentsString asLowercase ].    
   904 
   968 
       
   969     filenameToCompareContentsOrNil notNil ifTrue:[
       
   970         fileSizeSearchFor := filenameToCompareContentsOrNil fileSize.
       
   971         fileSearchedFor := filenameToCompareContentsOrNil pathName.
       
   972         fileMD5SearchedFor := MD5Stream hashValueOfFile:filenameToCompareContentsOrNil.
       
   973         remember := self rememberInCache value.
       
   974         remember ifTrue:[
       
   975             ContentsInfoCache isNil ifTrue:[
       
   976                 ContentsInfoCache := Dictionary new.
       
   977             ]
       
   978         ].
       
   979     ].
       
   980     cache := ContentsInfoCache.
       
   981 
   905     dir := aDirectory asFilename.
   982     dir := aDirectory asFilename.
   906     self changeInformationTo:'Find File ' , '- searching .' , ((dir name) copyFrom:(self searchDirectoryHolder value asString size + 1)) toTab:false.
   983     self changeInformationTo:'Find File ' , '- searching .' , ((dir name) copyFrom:(self searchDirectoryHolder value asString size + 1)) toTab:false.
   907 
   984 
   908     list := self resultList.
   985     list := self resultList.
   909 
   986 
   910     filenameToCompareContentsOrNil notNil ifTrue:[
   987     filenameToCompareContentsOrNil notNil ifTrue:[
   911         doesFileMatch := 
   988         doesFileMatch := 
   912             [:f |
   989             [:f |
   913                 |contentsMatches|
   990                 |contentsMatches fileMD5 fileName fileSize cacheLine|
   914 
   991 
   915                 "/ contents compare ...
   992                 "/ contents compare ...
   916                 contentsMatches := false.
   993                 contentsMatches := false.
   917                 f pathName ~= filenameToCompareContentsOrNil pathName ifTrue:[
   994                 fileName := f pathName.
   918                     f fileSize == filenameToCompareContentsOrNil fileSize ifTrue:[
   995                 fileName ~= fileSearchedFor ifTrue:[
   919                         contentsToCompare isNil ifTrue:[
   996                     cacheLine := cache at:fileName ifAbsent:nil.
   920                             filenameToCompareContentsOrNil fileSize < (512*1024) ifTrue:[
   997                     cacheLine notNil ifTrue:[
   921                                 contentsToCompare := filenameToCompareContentsOrNil binaryContentsOfEntireFile
   998                         fileSize := cacheLine at:1.    
   922                             ]
   999                         fileMD5 := cacheLine at:2.    
       
  1000                     ].
       
  1001                     fileSize isNil ifTrue:[
       
  1002                         fileSize := f fileSize.
       
  1003                     ].
       
  1004                     remember ifTrue:[
       
  1005                         cacheLine := cache at:fileName ifAbsentPut:[Array new:2].
       
  1006                         cacheLine at:1 put:fileSize.
       
  1007                     ].
       
  1008                     fileSize == fileSizeSearchFor ifTrue:[
       
  1009                         fileMD5 isNil ifTrue:[
       
  1010                             fileMD5 := MD5Stream hashValueOfFile:f.
       
  1011                             remember ifTrue:[
       
  1012                                 cacheLine at:2 put:fileMD5
       
  1013                             ].
   923                         ].
  1014                         ].
   924                         contentsToCompare isNil ifTrue:[
  1015                         contentsMatches := (fileMD5 = fileMD5SearchedFor).
   925                             "/ too large - compare block-wise ...
  1016 "/                        contentsToCompare isNil ifTrue:[
   926                             contentsMatches := (filenameToCompareContentsOrNil sameContentsAs:f).
  1017 "/                            filenameToCompareContentsOrNil fileSize < (512*1024) ifTrue:[
   927                         ] ifFalse:[
  1018 "/                                contentsToCompare := filenameToCompareContentsOrNil binaryContentsOfEntireFile
   928                             contentsMatches := contentsToCompare = (f binaryContentsOfEntireFile).
  1019 "/                            ]
   929                         ]
  1020 "/                        ].
       
  1021 "/                        contentsToCompare isNil ifTrue:[
       
  1022 "/                            "/ too large - compare block-wise ...
       
  1023 "/                            contentsMatches := (filenameToCompareContentsOrNil sameContentsAs:f).
       
  1024 "/                        ] ifFalse:[
       
  1025 "/                            contentsMatches := contentsToCompare = (f binaryContentsOfEntireFile).
       
  1026 "/                        ]
   930                     ].
  1027                     ].
   931                 ] ifFalse:[
  1028                 ] ifFalse:[
   932                     f isSymbolicLink ifTrue:[
  1029                     f isSymbolicLink ifTrue:[
   933                         list add: (f name , ' is a symbolic link to ' , f pathName).
  1030                         list add: (f name , ' is a symbolic link to ' , f pathName).
   934                     ]
  1031                     ]
  1184 !
  1281 !
  1185 
  1282 
  1186 release
  1283 release
  1187 
  1284 
  1188     self stopSearchTask.
  1285     self stopSearchTask.
       
  1286     ContentsInfoCache := nil.
  1189     ^ super release
  1287     ^ super release
  1190 ! !
  1288 ! !
  1191 
  1289 
  1192 !FindFileApplication class methodsFor:'documentation'!
  1290 !FindFileApplication class methodsFor:'documentation'!
  1193 
  1291 
  1194 version
  1292 version
  1195     ^ '$Header: /cvs/stx/stx/libtool/FindFileApplication.st,v 1.25 2004-03-09 13:45:15 cg Exp $'
  1293     ^ '$Header: /cvs/stx/stx/libtool/FindFileApplication.st,v 1.26 2004-03-15 22:57:47 cg Exp $'
  1196 ! !
  1294 ! !