UCB.xba 10 KB

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