1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
4 * This file is part of the LibreOffice project.
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/.
10 * This file incorporates work covered by the following license notice:
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 .
20 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"UCB" script:
language=
"StarBasic">'Option explicit
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
32 Dim LocsfileContent(
0) as String
33 LocsfileContent(
0) =
"*
"
34 ReadDirectories(
"file:///space
", LocsfileContent(), True, False, false)
37 ' ReadDirectories( sSourceDir, bRecursive, bCheckRealType, False, sFileContent(), sLocExtension)
39 Function ReadDirectories(ByVal AnchorDir As String, bRecursive as Boolean, bcheckFileType as Boolean, bGetByTitle as Boolean, Optional sFileContent(), Optional sExtension as String)
42 Dim FileCountinDir as Integer
43 Dim RealFileContent as String
44 Dim FileName as string
45 Dim oUcbObject as Object
47 Dim CurIndex as Integer
48 Dim MaxIndex as Integer
49 Dim StartUbound as Integer
50 Dim FileExtension as String
52 MaxIndex = StartUBound
53 CurDirMaxCount = SBMAXDIRCOUNT
54 Dim sFileArray(StartUbound,
1) as String
55 On Local Error Goto FILESYSTEMPROBLEM:
57 ' Todo: Is the last separator valid?
59 sDirArray(iDirIndex) = AnchorDir
61 oDocInfo = CreateUnoService(
"com.sun.star.document.DocumentProperties
")
62 oUcbObject = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
63 If oUcbObject.Exists(AnchorDir) Then
65 AnchorDir = sDirArray(DirIndex)
66 On Local Error Resume Next
67 DirContent() = oUcbObject.GetFolderContents(AnchorDir,True)
68 DirIndex = DirIndex +
1
70 On Local Error Goto FILESYSTEMPROBLEM:
71 If Ubound(DirContent())
<> -
1 Then
72 FileCountinDir = Ubound(DirContent())+
1
73 For i =
0 to FilecountinDir -
1
74 If bInterruptSearch = True Then
78 Filename = DirContent(i)
79 If oUcbObject.IsFolder(FileName) Then
81 AddFoldertoList(FileName, DirIndex)
84 If bcheckFileType Then
85 RealFileContent = GetRealFileContent(FileName)
87 RealFileContent = GetFileNameExtension(FileName)
89 If RealFileContent
<> "" Then
90 ' Retrieve the Index in the Array, where a Filename is positioned
91 If Not IsMissing(sFileContent()) Then
92 If (FieldInArray(sFileContent(), Ubound(sFileContent), RealFileContent)) Then
93 ' The extension of the current file passes the filter and is therefore admitted to the
95 If Not IsMissing(sExtension) Then
96 If sExtension
<> "" Then
97 ' Consider that some Formats like old StarOffice Templates with the extension
".vor
" can only be
98 ' precisely identified by their mimetype and their extension
99 FileExtension = GetFileNameExtension(FileName)
100 If FileExtension = sExtension Then
101 AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
104 AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
107 AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
111 AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
113 If CurIndex = MaxIndex Then
114 MaxIndex = MaxIndex + StartUbound
115 ReDim Preserve sFileArray(MaxIndex,
1) as String
121 Loop Until DirIndex
>= iDirCount
122 If CurIndex
> -
1 Then
123 ReDim Preserve sFileArray(CurIndex,
1) as String
125 ReDim sFileArray() as String
128 Msgbox(
"Directory
'" & ConvertFromUrl(AnchorDir)
& "' does not exist!
",
16, GetProductName())
130 ReadDirectories() = sFileArray()
134 Msgbox(
"Sorry, Filesystem Problem
")
135 ReadDirectories() = sFileArray()
141 Sub AddFoldertoList(sDirURL as String, iDirIndex)
142 iDirCount = iDirCount +
1
143 If iDirCount = CurDirMaxCount Then
144 CurDirMaxCount = CurDirMaxCount + SBMAXDIRCOUNT
145 ReDim Preserve sDirArray(CurDirMaxCount) as String
147 sDirArray(iDirCount-
1) = sDirURL
151 Sub AddFileNameToList(sFileArray(), FileName as String, FileContent as String, bGetByTitle as Boolean, CurIndex)
152 Dim FileCount As Integer
153 CurIndex = CurIndex +
1
154 sFileArray(CurIndex,
0) = FileName
156 sFileArray(CurIndex,
1) = RetrieveDocTitle(oDocInfo, FileName)
157 ' Add the documenttitles to the Filearray
159 sFileArray(CurIndex,
1) = FileContent
164 Function RetrieveDocTitle(oDocProps as Object, sFileName as String) As String
165 Dim sDocTitle as String
166 On Local Error Goto NOFILE
167 oDocProps.loadFromMedium(sFileName, NoArgs())
168 sDocTitle = oDocProps.Title
170 If Err
<> 0 Then
171 RetrieveDocTitle =
""
175 If sDocTitle =
"" Then
176 sDocTitle = GetFileNameWithoutExtension(sFilename,
"/
")
178 RetrieveDocTitle = sDocTitle
182 ' Retrieves The Filecontent of a Document by extracting the content
183 ' from the Header of the document
184 Function GetRealFileContent(FileName as String) As String
185 On Local Error Goto NOFILE
186 oTypeDetect = createUnoService(
"com.sun.star.document.TypeDetection
")
187 GetRealFileContent = oTypeDetect.queryTypeByURL(FileName)
189 If Err
<> 0 Then
190 GetRealFileContent =
""
197 Function CopyRecursively(SourceFilePath as String, SourceStemDir as String, TargetStemDir as String)
198 Dim TargetDir as String
199 Dim TargetFile as String
201 TargetFile= ReplaceString(SourceFilePath, TargetStemDir, SourceStemDir)
202 TargetFileName = FileNameoutofPath(TargetFile,
"/
")
203 TargetDir = DeleteStr(TargetFile, TargetFileName)
204 CreateFolder(TargetDir)
205 CopyRecursively() = TargetFile
209 ' Opens a help url referenced by a Help ID that is retrieved from the calling button tag
210 Sub ShowHelperDialog(aEvent)
211 Dim oSystemNode as Object
212 Dim sSystem as String
213 Dim oLanguageNode as Object
214 Dim sLocale as String
215 Dim sLocaleList() as String
216 Dim sLanguage as String
217 Dim sHelpUrl as String
218 Dim sDocType as String
219 HelpID = aEvent.Source.Model.Tag
220 oLocDocument = StarDesktop.ActiveFrame.Controller.Model
221 sDocType = GetDocumentType(oLocDocument)
222 oSystemNode = GetRegistryKeyContent(
"org.openoffice.Office.Common/Help
")
223 sSystem = oSystemNode.GetByName(
"System
")
224 oLanguageNode = GetRegistryKeyContent(
"org.openoffice.Setup/L10N/
")
225 sLocale = oLanguageNode.getByName(
"ooLocale
")
226 sLocaleList() = ArrayoutofString(sLocale,
"-
")
227 sLanguage = sLocaleList(
0)
228 sHelpUrl =
"vnd.sun.star.help://
" & sDocType
& "/
" & HelpID
& "?Language=
" & sLanguage
& "&System=
" & sSystem
229 StarDesktop.LoadComponentfromUrl(sHelpUrl,
"OFFICE_HELP
",
63, NoArgs())
233 Sub SaveDataToFile(FilePath as String, DataList())
234 Dim FileChannel as Integer
237 Dim oOutputStream as Object
238 Dim oStreamString as Object
242 sCRLF = CHR(
13)
& CHR(
10)
243 oUcb = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
244 oOutputStream = createUnoService(
"com.sun.star.io.TextOutputStream
")
245 If oUcb.Exists(FilePath) Then
248 oFile = oUcb.OpenFileReadWrite(FilePath)
249 oOutputStream.SetOutputStream(oFile.GetOutputStream)
250 For i =
0 To Ubound(DataList())
251 oOutputStream.WriteString(DataList(i)
& sCRLF)
253 oOutputStream.CloseOutput()
257 Function LoadDataFromFile(FilePath as String, DataList()) as Boolean
258 Dim oInputStream as Object
262 Dim MaxIndex as Integer
263 oUcb = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
264 If oUcb.Exists(FilePath) Then
266 oInputStream = createUnoService(
"com.sun.star.io.TextInputStream
")
267 oFile = oUcb.OpenFileReadWrite(FilePath)
268 oInputStream.SetInputStream(oFile.GetInputStream)
270 Redim Preserve DataList(MaxIndex)
271 While Not oInputStream.IsEOF
273 If i
> MaxIndex Then
274 MaxIndex = MaxIndex +
10
275 Redim Preserve DataList(MaxIndex)
277 DataList(i) = oInputStream.ReadLine
279 If i
> -
1 And i
<> MaxIndex Then
280 Redim Preserve DataList(i)
282 LoadDataFromFile() = True
283 oInputStream.CloseInput()
285 LoadDataFromFile() = False
290 Function CreateFolder(sNewFolder) as Boolean
292 oUcb = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
293 On Local Error Goto NOSPACEONDRIVE
294 If Not oUcb.Exists(sNewFolder) Then
295 oUcb.CreateFolder(sNewFolder)
299 If Err
<> 0 Then
300 If InitResources(
"") Then
301 ErrMsg = GetResText(
"RID_COMMON_0
")
302 ErrMsg = ReplaceString(ErrMsg, chr(
13),
"<BR
>")
303 ErrMsg = ReplaceString(ErrMsg, sNewFolder,
"%
1")
304 Msgbox(ErrMsg,
48, GetProductName())