home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 December / CHIP_CD_2004-12.iso / bonus / oo / OOo_1.1.3_ru_RU_infra_WinIntel_install.exe / $PLUGINSDIR / f_0218 / UCB.xba < prev   
Extensible Markup Language  |  2003-03-27  |  9KB  |  281 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="UCB" script:language="StarBasic">'Option explicit
  4. Public oDocument
  5. Dim oDocInfo as object
  6. Const SBMAXDIRCOUNT = 10
  7. Dim CurDirMaxCount as Integer
  8. Dim sDirArray(SBMAXDIRCOUNT-1) as String
  9. Dim DirIndex As Integer
  10. Dim iDirCount as Integer
  11. Dim bInterruptSearch as Boolean
  12.  
  13. Sub Main()
  14. Dim LocsfileContent(0) as String
  15.     LocsfileContent(0) = "*"
  16.     ReadDirectories("file:///space", LocsfileContent(), True, False, false)
  17. End Sub
  18.  
  19. '        ReadDirectories(      sSourceDir,          bRecursive,          bCheckRealType, False, sFileContent(), sLocExtension)
  20.  
  21. Function ReadDirectories(ByVal AnchorDir As String, bRecursive as Boolean,  bcheckFileType as Boolean, bGetByTitle as Boolean, Optional sFileContent(), Optional sExtension as String)
  22. Dim i as integer
  23. Dim Status as Object
  24. Dim FileCountinDir as Integer
  25. Dim RealFileContent as String
  26. Dim FileName as string
  27. Dim oUcbObject as Object
  28. Dim DirContent()
  29. Dim CurIndex as Integer
  30. Dim MaxIndex as Integer
  31. Dim StartUbound as Integer
  32. Dim FileExtension as String
  33.     StartUbound = 5
  34.     MaxIndex = StartUBound
  35.     CurDirMaxCount = SBMAXDIRCOUNT
  36. Dim sFileArray(StartUbound,1) as String
  37.     CurIndex = -1
  38.     ' Todo: Is the last separator valid?
  39.     DirIndex = 0
  40.     sDirArray(iDirIndex) = AnchorDir
  41.     iDirCount = 1
  42.     oDocInfo = CreateUnoService("com.sun.star.document.DocumentProperties")
  43.     oUcbObject = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  44.     If oUcbObject.Exists(AnchorDir) Then
  45.         Do
  46.             AnchorDir = sDirArray(DirIndex)
  47.             DirContent() = oUcbObject.GetFolderContents(AnchorDir,True)
  48.             DirIndex = DirIndex + 1
  49.             If Ubound(DirContent()) <> -1 Then
  50.                 FileCountinDir = Ubound(DirContent())+ 1
  51.                 For i = 0 to FilecountinDir -1
  52.                     If bInterruptSearch = True Then
  53.                         Exit Do
  54.                     End If
  55.                     
  56.                     Filename = DirContent(i)
  57.                     If oUcbObject.IsFolder(FileName) Then
  58.                         If brecursive Then
  59.                             AddFoldertoList(FileName, DirIndex)
  60.                         End If
  61.                     Else
  62.                         If bcheckFileType Then
  63.                             RealFileContent  = GetRealFileContent(oDocInfo, FileName)
  64.                         Else
  65.                             RealFileContent = GetFileNameExtension(FileName)
  66.                         End If
  67.                         If RealFileContent <> "" Then
  68.                             ' Retrieve the Index in the Array, where a Filename is positioned
  69.                             If Not IsMissing(sFileContent()) Then
  70.                                 If (FieldinArray(sFileContent(), Ubound(sFileContent), RealFileContent)) Then
  71.                                     ' The extension of the current file passes the filter and is therefor admitted to the
  72.                                     ' fileList
  73.                                     If Not IsMissing(sExtension) Then
  74.                                         If sExtension <> "" Then
  75.                                             ' Consider that some Formats like old StarOffice Templates with the extension ".vor" can only be
  76.                                             ' precisely identified by their mimetype and their extension
  77.                                             FileExtension = GetFileNameExtension(FileName)
  78.                                             If FileExtension = sExtension Then
  79.                                                 AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
  80.                                             End If
  81.                                         Else
  82.                                             AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
  83.                                         End If
  84.                                     Else
  85.                                         AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
  86.                                     End If
  87.                                 End If
  88.                             Else
  89.                                 AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
  90.                             End If
  91.                             If CurIndex = MaxIndex Then
  92.                                 MaxIndex = MaxIndex + StartUbound
  93.                                 ReDim Preserve sFileArray(MaxIndex,1) as String
  94.                             End If
  95.                         End If
  96.                     End If
  97.                 Next i
  98.             End If
  99.         Loop Until DirIndex >= iDirCount
  100.         If CurIndex > -1 Then
  101.             ReDim Preserve sFileArray(CurIndex,1) as String
  102.         Else
  103.             ReDim sFileArray() as String
  104.         End If
  105.     Else
  106.         Msgbox("Directory '" & ConvertFromUrl(AnchorDir) & "' does not exist!", 16, GetProductName())
  107.     End If
  108.     ReadDirectories() = sFileArray()
  109. End Function
  110.  
  111.  
  112. Sub AddFoldertoList(sDirURL as String, iDirIndex)
  113.     iDirCount = iDirCount + 1
  114.     If iDirCount = CurDirMaxCount Then
  115.         CurDirMaxCount = CurDirMaxCount + SBMAXDIRCOUNT
  116.         ReDim Preserve sDirArray(CurDirMaxCount) as String
  117.     End If
  118.     sDirArray(iDirCount-1) = sDirURL
  119. End Sub
  120.  
  121.  
  122. Sub AddFileNameToList(sFileArray(), FileName as String, FileContent as String, bGetByTitle as Boolean, CurIndex)
  123. Dim FileCount As Integer
  124.     CurIndex = CurIndex + 1
  125.     sFileArray(CurIndex,0) = FileName
  126.     If bGetByTitle Then
  127.         sFileArray(CurIndex,1) = RetrieveDocTitle(oDocInfo, FileName)
  128.         ' Add the documenttitles to the Filearray
  129.     Else
  130.         sFileArray(CurIndex,1) = FileContent
  131.     End If
  132. End Sub
  133.  
  134.  
  135. Function RetrieveDocTitle(oDocInfo as Object, sFileName as String) As String
  136. Dim sDocTitle as String
  137.     On Local Error Goto NOFILE
  138.     oDocInfo.Read(sFileName)
  139.     sDocTitle = oDocInfo.Title
  140.     NOFILE:
  141.     If Err <> 0 Then
  142.         GetRealFileContent = ""
  143.         RESUME CLR_ERROR
  144.     End If
  145.     CLR_ERROR:
  146.     If sDocTitle = "" Then
  147.         sDocTitle = GetFileNameWithoutExtension(sFilename, "/")
  148.     End If
  149.     RetrieveDocTitle = sDocTitle
  150. End Function
  151.  
  152.  
  153. ' Retrieves The Filecontent of a Document by extracting the content
  154. ' from the Header of the document
  155. Function GetRealFileContent(oDocInfo as Object, FileName as String) As String
  156.     On Local Error Goto NOFILE
  157.     oDocInfo.Read(FileName)
  158.     GetRealFileContent = oDocInfo.MIMEType
  159.     NOFILE:
  160.     If Err <> 0 Then
  161.         GetRealFileContent = ""
  162.         resume CLR_ERROR
  163.     End If
  164.     CLR_ERROR:
  165. End Function
  166.  
  167.  
  168. Function CopyRecursively(SourceFilePath as String, SourceStemDir as String, TargetStemDir as String)
  169. Dim TargetDir as String
  170. Dim TargetFile as String
  171.  
  172.     TargetFile= ReplaceString(SourceFilePath, TargetStemDir, SourceStemDir)
  173.     TargetFileName = FileNameoutofPath(TargetFile,"/")
  174.     TargetDir = DeleteStr(TargetFile, TargetFileName)
  175.     CreateFolder(TargetDir)
  176.     CopyRecursively() = TargetFile
  177. End Function
  178.  
  179.  
  180. ' Opens a help url referenced by a Help ID that is retrieved from the calling button tag
  181. Sub ShowHelperDialog(aEvent)
  182. Dim oSystemNode as Object
  183. Dim sSystem as String
  184. Dim oLanguageNode as Object
  185. Dim sLocale as String
  186. Dim sLocaleList() as String
  187. Dim sLanguage as String
  188. Dim sHelpUrl as String
  189. Dim sDocType as String
  190.     HelpID = aEvent.Source.Model.Tag
  191.     oLocDocument = StarDesktop.ActiveFrame.Controller.Model
  192.     sDocType = GetDocumentType(oLocDocument)
  193.     oSystemNode = GetRegistryKeyContent("org.openoffice.Office.Common/Help")
  194.     sSystem = oSystemNode.GetByName("System")
  195.     oLanguageNode = GetRegistryKeyContent("org.openoffice.Setup/L10N/")
  196.     sLocale = oLanguageNode.getByName("ooLocale")
  197.     sLocaleList() = ArrayoutofString(sLocale, "-")
  198.     sLanguage = sLocaleList(0)
  199.     sHelpUrl = "vnd.sun.star.help://" & sDocType & "/" & HelpID & "?Language=" & sLanguage & "&System=" & sSystem
  200.     StarDesktop.LoadComponentfromUrl(sHelpUrl, "OFFICE_HELP", 63, NoArgs())
  201. End Sub
  202.  
  203.  
  204. Sub SaveDataToFile(FilePath as String, DataList())
  205. Dim FileChannel as Integer
  206. Dim i as Integer
  207. Dim oFile as Object
  208. Dim oOutputStream as Object
  209. Dim oStreamString as Object
  210. Dim oUcb as Object
  211. Dim sCRLF as String
  212.  
  213.     sCRLF = CHR(13) & CHR(10)
  214.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  215.     oOutputStream = createUnoService("com.sun.star.io.TextOutputStream")
  216.     If oUcb.Exists(FilePath) Then
  217.         oUcb.Kill(FilePath)
  218.     End If
  219.     oFile = oUcb.OpenFileReadWrite(FilePath)
  220.     oOutputStream.SetOutputStream(oFile.GetOutputStream)
  221.     For i = 0 To Ubound(DataList())
  222.         oOutputStream.WriteString(DataList(i) & sCRLF)
  223.     Next i
  224.     oOutputStream.CloseOutput()
  225. End Sub
  226.  
  227.  
  228. Function LoadDataFromFile(FilePath as String, DataList()) as Boolean
  229. Dim oInputStream as Object
  230. Dim i as Integer
  231. Dim oUcb as Object
  232. Dim oFile as Object
  233. Dim MaxIndex as Integer
  234.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  235.     If oUcb.Exists(FilePath) Then
  236.         MaxIndex = 10
  237.         oInputStream = createUnoService("com.sun.star.io.TextInputStream")
  238.         oFile = oUcb.OpenFileReadWrite(FilePath)
  239.         oInputStream.SetInputStream(oFile.GetInputStream)
  240.         i = -1
  241.         Redim Preserve DataList(MaxIndex)
  242.         While Not oInputStream.IsEOF
  243.             i = i + 1
  244.             If i > MaxIndex Then
  245.                 MaxIndex = MaxIndex + 10
  246.                 Redim Preserve DataList(MaxIndex)
  247.             End If
  248.             DataList(i) = oInputStream.ReadLine
  249.         Wend
  250.         If i > -1 And i <> MaxIndex Then
  251.             Redim Preserve DataList(i)
  252.         End If
  253.         LoadDataFromFile() = True
  254.         oInputStream.CloseInput()
  255.     Else
  256.         LoadDataFromFile() = False
  257.     End If
  258. End Function
  259.  
  260.  
  261. Function CreateFolder(sNewFolder) as Boolean
  262. Dim oUcb as Object
  263.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  264.     On Local Error Goto NOSPACEONDRIVE
  265.     If Not oUcb.Exists(sNewFolder) Then
  266.         oUcb.CreateFolder(sNewFolder)
  267.     End If
  268.     CreateFolder = True
  269. NOSPACEONDRIVE:
  270.     If Err <> 0 Then
  271.         If InitResources("", "com") Then
  272.             ErrMsg = GetResText(1000)
  273.             ErrMsg = ReplaceString(ErrMsg, chr(13), "<BR>")
  274.             ErrMsg = ReplaceString(ErrMsg, sNewFolder, "%1")
  275.             Msgbox(ErrMsg, 48, GetProductName())
  276.         End If
  277.         CreateFolder = False
  278.         Resume GOON
  279.     End If
  280. GOON:
  281. End Function</script:module>