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