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