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
5 Public 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 Public bInterruptSearch as Boolean
12 Public NoArgs()as New com.sun.star.beans.PropertyValue
15 Dim LocsfileContent(
0) as String
16 LocsfileContent(
0) =
"*
"
17 ReadDirectories(
"file:///space
", LocsfileContent(), True, False, false)
20 ' ReadDirectories( sSourceDir, bRecursive, bCheckRealType, False, sFileContent(), sLocExtension)
22 Function ReadDirectories(ByVal AnchorDir As String, bRecursive as Boolean, bcheckFileType as Boolean, bGetByTitle as Boolean, Optional sFileContent(), Optional sExtension as String)
25 Dim FileCountinDir as Integer
26 Dim RealFileContent as String
27 Dim FileName as string
28 Dim oUcbObject as Object
30 Dim CurIndex as Integer
31 Dim MaxIndex as Integer
32 Dim StartUbound as Integer
33 Dim FileExtension as String
35 MaxIndex = StartUBound
36 CurDirMaxCount = SBMAXDIRCOUNT
37 Dim sFileArray(StartUbound,
1) as String
38 On Local Error Goto FILESYSTEMPROBLEM:
40 ' Todo: Is the last separator valid?
42 sDirArray(iDirIndex) = AnchorDir
44 oDocInfo = CreateUnoService(
"com.sun.star.document.DocumentProperties
")
45 oUcbObject = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
46 If oUcbObject.Exists(AnchorDir) Then
48 AnchorDir = sDirArray(DirIndex)
49 On Local Error Resume Next
50 DirContent() = oUcbObject.GetFolderContents(AnchorDir,True)
51 DirIndex = DirIndex +
1
53 On Local Error Goto FILESYSTEMPROBLEM:
54 If Ubound(DirContent())
<> -
1 Then
55 FileCountinDir = Ubound(DirContent())+
1
56 For i =
0 to FilecountinDir -
1
57 If bInterruptSearch = True Then
61 Filename = DirContent(i)
62 If oUcbObject.IsFolder(FileName) Then
64 AddFoldertoList(FileName, DirIndex)
67 If bcheckFileType Then
68 RealFileContent = GetRealFileContent(FileName)
70 RealFileContent = GetFileNameExtension(FileName)
72 If RealFileContent
<> "" Then
73 ' Retrieve the Index in the Array, where a Filename is positioned
74 If Not IsMissing(sFileContent()) Then
75 If (FieldinArray(sFileContent(), Ubound(sFileContent), RealFileContent)) Then
76 ' The extension of the current file passes the filter and is therefor admitted to the
78 If Not IsMissing(sExtension) Then
79 If sExtension
<> "" Then
80 ' Consider that some Formats like old StarOffice Templates with the extension
".vor
" can only be
81 ' precisely identified by their mimetype and their extension
82 FileExtension = GetFileNameExtension(FileName)
83 If FileExtension = sExtension Then
84 AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
87 AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
90 AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
94 AddFileNameToList(sFileArray(), FileName, RealFileContent, bGetByTitle, CurIndex)
96 If CurIndex = MaxIndex Then
97 MaxIndex = MaxIndex + StartUbound
98 ReDim Preserve sFileArray(MaxIndex,
1) as String
104 Loop Until DirIndex
>= iDirCount
105 If CurIndex
> -
1 Then
106 ReDim Preserve sFileArray(CurIndex,
1) as String
108 ReDim sFileArray() as String
111 Msgbox(
"Directory
'" & ConvertFromUrl(AnchorDir)
& "' does not exist!
",
16, GetProductName())
113 ReadDirectories() = sFileArray()
117 Msgbox(
"Sorry, Filesystem Problem
")
118 ReadDirectories() = sFileArray()
124 Sub AddFoldertoList(sDirURL as String, iDirIndex)
125 iDirCount = iDirCount +
1
126 If iDirCount = CurDirMaxCount Then
127 CurDirMaxCount = CurDirMaxCount + SBMAXDIRCOUNT
128 ReDim Preserve sDirArray(CurDirMaxCount) as String
130 sDirArray(iDirCount-
1) = sDirURL
134 Sub AddFileNameToList(sFileArray(), FileName as String, FileContent as String, bGetByTitle as Boolean, CurIndex)
135 Dim FileCount As Integer
136 CurIndex = CurIndex +
1
137 sFileArray(CurIndex,
0) = FileName
139 sFileArray(CurIndex,
1) = RetrieveDocTitle(oDocInfo, FileName)
140 ' Add the documenttitles to the Filearray
142 sFileArray(CurIndex,
1) = FileContent
147 Function RetrieveDocTitle(oDocProps as Object, sFileName as String) As String
148 Dim sDocTitle as String
149 On Local Error Goto NOFILE
150 oDocProps.loadFromMedium(sFileName, NoArgs())
151 sDocTitle = oDocProps.Title
153 If Err
<> 0 Then
154 RetrieveDocTitle =
""
158 If sDocTitle =
"" Then
159 sDocTitle = GetFileNameWithoutExtension(sFilename,
"/
")
161 RetrieveDocTitle = sDocTitle
165 ' Retrieves The Filecontent of a Document by extracting the content
166 ' from the Header of the document
167 Function GetRealFileContent(FileName as String) As String
168 On Local Error Goto NOFILE
169 oTypeDetect = createUnoService(
"com.sun.star.document.TypeDetection
")
170 GetRealFileContent = oTypeDetect.queryTypeByURL(FileName)
172 If Err
<> 0 Then
173 GetRealFileContent =
""
180 Function CopyRecursively(SourceFilePath as String, SourceStemDir as String, TargetStemDir as String)
181 Dim TargetDir as String
182 Dim TargetFile as String
184 TargetFile= ReplaceString(SourceFilePath, TargetStemDir, SourceStemDir)
185 TargetFileName = FileNameoutofPath(TargetFile,
"/
")
186 TargetDir = DeleteStr(TargetFile, TargetFileName)
187 CreateFolder(TargetDir)
188 CopyRecursively() = TargetFile
192 ' Opens a help url referenced by a Help ID that is retrieved from the calling button tag
193 Sub ShowHelperDialog(aEvent)
194 Dim oSystemNode as Object
195 Dim sSystem as String
196 Dim oLanguageNode as Object
197 Dim sLocale as String
198 Dim sLocaleList() as String
199 Dim sLanguage as String
200 Dim sHelpUrl as String
201 Dim sDocType as String
202 HelpID = aEvent.Source.Model.Tag
203 oLocDocument = StarDesktop.ActiveFrame.Controller.Model
204 sDocType = GetDocumentType(oLocDocument)
205 oSystemNode = GetRegistryKeyContent(
"org.openoffice.Office.Common/Help
")
206 sSystem = oSystemNode.GetByName(
"System
")
207 oLanguageNode = GetRegistryKeyContent(
"org.openoffice.Setup/L10N/
")
208 sLocale = oLanguageNode.getByName(
"ooLocale
")
209 sLocaleList() = ArrayoutofString(sLocale,
"-
")
210 sLanguage = sLocaleList(
0)
211 sHelpUrl =
"vnd.sun.star.help://
" & sDocType
& "/
" & HelpID
& "?Language=
" & sLanguage
& "&System=
" & sSystem
212 StarDesktop.LoadComponentfromUrl(sHelpUrl,
"OFFICE_HELP
",
63, NoArgs())
216 Sub SaveDataToFile(FilePath as String, DataList())
217 Dim FileChannel as Integer
220 Dim oOutputStream as Object
221 Dim oStreamString as Object
225 sCRLF = CHR(
13)
& CHR(
10)
226 oUcb = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
227 oOutputStream = createUnoService(
"com.sun.star.io.TextOutputStream
")
228 If oUcb.Exists(FilePath) Then
231 oFile = oUcb.OpenFileReadWrite(FilePath)
232 oOutputStream.SetOutputStream(oFile.GetOutputStream)
233 For i =
0 To Ubound(DataList())
234 oOutputStream.WriteString(DataList(i)
& sCRLF)
236 oOutputStream.CloseOutput()
240 Function LoadDataFromFile(FilePath as String, DataList()) as Boolean
241 Dim oInputStream as Object
245 Dim MaxIndex as Integer
246 oUcb = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
247 If oUcb.Exists(FilePath) Then
249 oInputStream = createUnoService(
"com.sun.star.io.TextInputStream
")
250 oFile = oUcb.OpenFileReadWrite(FilePath)
251 oInputStream.SetInputStream(oFile.GetInputStream)
253 Redim Preserve DataList(MaxIndex)
254 While Not oInputStream.IsEOF
256 If i
> MaxIndex Then
257 MaxIndex = MaxIndex +
10
258 Redim Preserve DataList(MaxIndex)
260 DataList(i) = oInputStream.ReadLine
262 If i
> -
1 And i
<> MaxIndex Then
263 Redim Preserve DataList(i)
265 LoadDataFromFile() = True
266 oInputStream.CloseInput()
268 LoadDataFromFile() = False
273 Function CreateFolder(sNewFolder) as Boolean
275 oUcb = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
276 On Local Error Goto NOSPACEONDRIVE
277 If Not oUcb.Exists(sNewFolder) Then
278 oUcb.CreateFolder(sNewFolder)
282 If Err
<> 0 Then
283 If InitResources(
"",
"dbw
") Then
284 ErrMsg = GetResText(
500)
285 ErrMsg = ReplaceString(ErrMsg, chr(
13),
"<BR
>")
286 ErrMsg = ReplaceString(ErrMsg, sNewFolder,
"%
1")
287 Msgbox(ErrMsg,
48, GetProductName())