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