1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3<!--***********************************************************
4 *
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements.  See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership.  The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License.  You may obtain a copy of the License at
12 *
13 *   http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied.  See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 *
22 ***********************************************************-->
23<script:module xmlns:script="http://openoffice.org/2000/script" script:name="AutoPilotRun" script:language="StarBasic">Option Explicit
24
25Public SourceDir as String
26Public TargetDir as String
27Public TargetStemDir as String
28Public SourceFile as String
29Public TargetFile as String
30Public Source as String
31Public SubstFile as String
32Public SubstDir as String
33Public NoArgs()
34Public TypeList(6) as String
35Public GoOn as Boolean
36Public DoUnprotect as Integer
37Public Password as String
38Public DocIndex as Integer
39Public oPathSettings as Object
40Public oUcb as Object
41Public TotDocCount as Integer
42Public sTotDocCount as String
43Public OpenProperties(1) as New com.sun.star.beans.PropertyValue
44
45
46Sub StartAutoPilot()
47Dim i As Integer
48Dim oFactoryKey as Object
49	BasicLibraries.LoadLibrary(&quot;Tools&quot;)
50	BasicLibraries.LoadLibrary(&quot;ImportWizard&quot;)
51	If InitResources(&quot;Euro Converter&quot;, &quot;eur&quot;) Then
52		oUcb = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
53		oLocale = GetStarOfficeLocale()
54		InitializeConverter(oLocale, 2)
55		ToggleGoOnButton()
56		oFactoryKey = GetRegistryKeyContent(&quot;org.openoffice.Setup/Office/Factories&quot;)
57		DialogModel.chkTextDocuments.Enabled = oFactoryKey.hasbyName(&quot;com.sun.star.text.TextDocument&quot;)
58		DialogModel.cmdGoOn.DefaultButton = True
59		DialogModel.lstCurrencies.TabIndex = 12
60		DialogConvert.GetControl(&quot;optWholeDir&quot;).SetFocus()
61		DialogConvert.Execute()
62		DialogConvert.Dispose()
63	End If
64End Sub
65
66
67Sub ConvertDocuments()
68Dim FilesList()
69Dim bDisposable as Boolean
70
71	If Source &lt;&gt; &quot;&quot; And TargetDir &lt;&gt; &quot;&quot; Then
72		If DialogModel.optSingleFile.State = 1 Then
73			SourceFile = Source
74			TotDocCount = 1
75		Else
76			SourceDir = Source
77			TargetStemDir = TargetDir
78			TypeList(0) = &quot;calc8&quot;
79			TypeList(1) = &quot;calc_StarOffice_XML_Calc&quot;
80			If DialogModel.chkTextDocuments.State = 1 Then
81				ReDim Preserve TypeList(5) as String
82
83				TypeList(2) = &quot;writer8&quot;
84				TypeList(3) = &quot;writerglobal8&quot;
85				TypeList(4) = &quot;writer_StarOffice_XML_Writer&quot;
86				TypeList(5) = &quot;writer_globaldocument_StarOffice_XML_Writer_GlobalDocument&quot;
87			End If
88			FilesList() = ReadDirectories(SourceDir, bRecursive, True, False, TypeList())
89			TotDocCount = Ubound(FilesList(),1) + 1
90		End If
91		InitializeProgressPage(DialogModel)
92&apos;		ChangeToNextProgressStep()
93		sTotDocCount = CStr(TotDocCount)
94		OpenProperties(0).Name = &quot;Hidden&quot;
95		OpenProperties(0).Value = True
96		OpenProperties(1).Name = &quot;AsTemplate&quot;
97		OpenProperties(1).Value = False
98		For DocIndex = 0 To TotDocCount - 1
99			If InitializeDocument(FilesList(), bDisposable) Then
100				If StoreDocument() Then
101					ConvertDocument()
102					oDocument.Store
103				End If
104				If bDisposable Then
105					oDocument.Dispose()
106				End If
107			End If
108		Next DocIndex
109		DialogModel.cmdBack.Enabled = True
110		DialogModel.cmdGoOn.Enabled = True
111		DialogModel.cmdGoOn.Label = sReady
112		DialogModel.cmdCancel.Label = sEnd
113	End If
114End Sub
115
116
117Function InitializeDocument(FilesList(), bDisposable as Boolean) as Boolean
118&apos; The Autopilot is started from step No. 2
119Dim sViewPath as String
120Dim bIsReadOnly as Boolean
121Dim sExtension as String
122	On Local Error Goto NEXTFILE
123	If Not bCancelTask Then
124		If DialogModel.optWholeDir.State = 1 Then
125			SourceFile = FilesList(DocIndex,0)
126			TargetFile = ReplaceString(SourceFile,TargetStemDir,SourceDir)
127			TargetDir = DirectorynameoutofPath(TargetFile, &quot;/&quot;)
128		Else
129			SourceFile = Source
130			TargetFile = TargetDir &amp; &quot;/&quot; &amp; FileNameoutofPath(SourceFile, &quot;/&quot;)
131		End If
132		If CreateFolder(TargetDir) Then
133			sExtension = GetFileNameExtension(SourceFile, &quot;/&quot;)
134			oDocument = OpenDocument(SourceFile, OpenProperties(), bDisposable)
135			If (oDocument.IsReadOnly) AND (UCase(SourceFile) = UCase(TargetFile)) Then
136				bIsReadOnly = True
137				Msgbox(sMsgDOCISREADONLY, 16, GetProductName())
138			Else
139				bIsReadOnly = False
140				RetrieveDocumentObjects()
141				sViewPath = CutPathView(SourceFile, 60)
142				DialogModel.lblCurDocument.Label = Str(DocIndex+1) &amp; &quot;/&quot; &amp; sTotDocCount &amp; &quot; (&quot; &amp; sViewPath &amp; &quot;)&quot;
143			End If
144			InitializeDocument() = Not bIsReadOnly
145		Else
146			InitializeDocument() = False
147		End If
148	Else
149		InitializeDocument() = False
150	End If
151NEXTFILE:
152	If Err &lt;&gt; 0 Then
153		InitializeDocument() = False
154		Resume LETSGO
155LETSGO:
156	End If
157End Function
158
159
160Sub ChangeToNextProgressStep()
161	DialogModel.lblCurProgress.FontWeight = com.sun.star.awt.FontWeight.NORMAL
162	DialogConvert.GetControl(&quot;lblCurProgress&quot;).Visible = True
163End Sub
164
165
166Function StoreDocument() as Boolean
167Dim sCurFileExists as String
168Dim iOverWrite as Integer
169	If (TargetFile &lt;&gt; &quot;&quot;) And (Not bCancelTask) Then
170		On Local Error Goto NOSAVING
171		If oUcb.Exists(TargetFile) Then
172			sCurFileExists = ReplaceString(sMsgFileExists, ConvertFromUrl(TargetFile), &quot;&lt;1&gt;&quot;)
173			sCurFileExists = ReplaceString(sCurFileExists, chr(13), &quot;&lt;CR&gt;&quot;)
174			iOverWrite = Msgbox (sCurFileExists, 32 + 3, sMsgDLGTITLE)
175			Select Case iOverWrite
176				Case 1 &apos; OK
177				Case 2 &apos; Abort
178					bCancelTask = True
179					StoreDocument() = False
180					Exit Function
181				Case 7 &apos; No
182					StoreDocument() = False
183					Exit Function
184			End Select
185		End If
186		If TargetFile &lt;&gt; SourceFile Then
187			oDocument.StoreAsUrl(TargetFile,NoArgs)
188		Else
189			oDocument.Store
190		End If
191		StoreDocument() = True
192		NOSAVING:
193		If Err &lt;&gt; 0 Then
194			StoreDocument() = False
195			Resume CLERROR
196		End If
197		CLERROR:
198	End If
199End Function
200
201
202Sub SwapExtent()
203	DialogModel.chkRecursive.Enabled = DialogModel.optWholeDir.State = 1
204	If DialogModel.optWholeDir.State = 1 Then
205		DialogModel.lblSource.Label = sSOURCEDIR
206		If Not IsNull(SubstFile) Then
207			SubstFile = DialogModel.txtSource.Text
208			DialogModel.txtSource.Text = SubstDir
209		End If
210	Else
211		DialogModel.LblSource.Label = sSOURCEFILE
212		If Not IsNull(SubstDir) Then
213			SubstDir = DialogModel.txtSource.Text
214			DialogModel.txtSource.Text = SubstFile
215		End If
216	End If
217	ToggleGoOnButton()
218End Sub
219
220
221Function InitializeThirdStep() as Boolean
222Dim TextBoxText as String
223	Source = AssignFileName(DialogModel.txtSource.Text, DialogModel.lblSource.Label, True)
224	If CheckTextBoxPath(DialogModel.txtTarget, True, True, sMsgDLGTITLE, True) Then
225		TargetDir = AssignFileName(DialogModel.txtTarget.Text, DialogModel.lblTarget.Label, False)
226	Else
227		TargetDir = &quot;&quot;
228	End If
229	If Source &lt;&gt; &quot;&quot; And TargetDir &lt;&gt; &quot;&quot; Then
230		bRecursive = DialogModel.chkRecursive.State = 1
231		bDoUnprotect = DialogModel.chkProtect.State = 1
232		DialogModel.lblRetrieval.FontWeight = com.sun.star.awt.FontWeight.BOLD
233		DialogModel.lblRetrieval.Label = sPrgsRETRIEVAL
234		DialogModel.lblCurProgress.Label = sPrgsCONVERTING
235		If DialogModel.optWholeDir.State = 1 Then
236			TextBoxText = sSOURCEDIR &amp; &quot; &quot; &amp; ConvertFromUrl(Source) &amp; chr(13)
237			If DialogModel.chkRecursive.State = 1 Then
238				TextBoxText = TextBoxText &amp; DeleteStr(sInclusiveSubDir,&quot;~&quot;) &amp; chr(13)
239			End If
240		Else
241			TextBoxText = sSOURCEFILE &amp; &quot; &quot; &amp; ConvertFromUrl(Source) &amp; chr(13)
242		End If
243		TextBoxText = TextBoxText &amp; sTARGETDIR &amp; &quot; &quot; &amp; ConvertFromUrl(TargetDir) &amp; chr(13)
244		If DialogModel.chkProtect.State = 1 Then
245			TextBoxText = TextboxText &amp; sPrgsUNPROTECT
246		End If
247		DialogModel.txtConfig.Text = TextBoxText
248		ToggleProgressStep()
249		DialogModel.cmdGoOn.Enabled = False
250		InitializeThirdStep() = True
251	Else
252		InitializeThirdStep() = False
253	End If
254End Function
255
256
257Sub ToggleProgressStep(Optional aEvent as Object)
258Dim bMakeVisible as Boolean
259Dim LocStep as Integer
260	&apos; If the Sub is call by the &apos;cmdBack&apos; Button then set the &apos;bMakeVisible&apos; variable accordingly
261	bMakeVisible = IsMissing(aEvent)
262	If bMakeVisible Then
263		DialogModel.Step = 3
264	Else
265		DialogModel.Step = 2
266	End If
267	DialogConvert.GetControl(&quot;lblCurrencies&quot;).Visible = Not bMakeVisible
268	DialogConvert.GetControl(&quot;lstCurrencies&quot;).Visible = Not bMakeVisible
269	DialogConvert.GetControl(&quot;cmdBack&quot;).Visible = bMakeVisible
270	DialogConvert.GetControl(&quot;cmdGoOn&quot;).Visible = bMakeVisible
271	DialogModel.imgPreview.ImageUrl = BitmapDir &amp; &quot;euro_&quot; &amp; DialogModel.Step &amp; &quot;.png&quot;
272End Sub
273
274
275Sub EnableStep2DialogControls(OnValue as Boolean)
276	With DialogModel
277		.hlnExtent.Enabled = OnValue
278		.optWholeDir.Enabled = OnValue
279		.optSingleFile.Enabled = OnValue
280		.chkProtect.Enabled = OnValue
281		.cmdCallSourceDialog.Enabled = OnValue
282		.cmdCallTargetDialog.Enabled = OnValue
283		.lblSource.Enabled = OnValue
284		.lblTarget.Enabled = OnValue
285		.txtSource.Enabled = OnValue
286		.txtTarget.Enabled = OnValue
287		.imgPreview.Enabled = OnValue
288		.lstCurrencies.Enabled = OnValue
289		.lblCurrencies.Enabled = OnValue
290		If OnValue Then
291			ToggleGoOnButton()
292			.chkRecursive.Enabled = .optWholeDir.State = 1
293		Else
294			.cmdGoOn.Enabled = False
295			.chkRecursive.Enabled = False
296		End If
297	End With
298End Sub
299
300
301Sub InitializeProgressPage()
302	DialogConvert.GetControl(&quot;lblRetrieval&quot;).Visible = False
303	DialogConvert.GetControl(&quot;lblCurProgress&quot;).Visible = False
304	DialogModel.lblRetrieval.FontWeight = com.sun.star.awt.FontWeight.NORMAL
305	DialogModel.lblCurProgress.FontWeight = com.sun.star.awt.FontWeight.BOLD
306	DialogConvert.GetControl(&quot;lblRetrieval&quot;).Visible = True
307	DialogConvert.GetControl(&quot;lblCurProgress&quot;).Visible = True
308End Sub
309
310
311Function AssignFileName(sPath as String, ByVal HeaderString, bCheckFileType as Boolean) as String
312Dim bIsValid as Boolean
313Dim sLocMimeType as String
314Dim sNoDirMessage as String
315	HeaderString = DeleteStr(HeaderString, &quot;:&quot;)
316	sPath = ConvertToUrl(Trim(sPath))
317	bIsValid = oUcb.Exists(sPath)
318	If bIsValid Then
319		If DialogModel.optSingleFile.State = 1 Then
320			If bCheckFileType Then
321				sLocMimeType = GetRealFileContent(sPath)
322				If DialogModel.chkTextDocuments.State = 1 Then
323					If (Instr(1, sLocMimeType, &quot;text&quot;) = 0) And (Instr(1, sLocMimeType, &quot;calc&quot;) = 0) Then
324						Msgbox(sMsgFileInvalid, 48, sMsgDLGTITLE)
325						bIsValid = False
326					End If
327				Else
328					If (Instr(1, sLocMimeType, &quot;spreadsheet&quot;) = 0) And (Instr(1, sLocMimeType, &quot;calc&quot;)) = 0 Then
329						Msgbox(sMsgFileInvalid, 48, sMsgDLGTITLE)
330						bIsValid = False
331					End If
332				End If
333			End If
334		Else
335			If Not oUcb.IsFolder(sPath) Then
336				sNoDirMessage = ReplaceString(sMsgNODIRECTORY,sPath,&quot;&lt;1&gt;&quot;)
337				Msgbox(sNoDirMessage,48, sMsgDLGTITLE)
338				bIsValid = False
339			Else
340				sPath = RTrimStr(sPath,&quot;/&quot;)
341				sPath = sPath &amp; &quot;/&quot;
342			End If
343		End if
344	Else
345		Msgbox(HeaderString &amp; &quot; &apos;&quot; &amp; ConvertFromUrl(sPath) &amp; &quot;&apos; &quot; &amp; sMsgNOTTHERE,48, sMsgDLGTITLE)
346	End If
347	If bIsValid Then
348		AssignFileName() = sPath
349	Else
350		AssignFilename() = &quot;&quot;
351	End If
352End Function
353
354
355Sub ToggleGoOnButton()
356Dim bDoEnable as Boolean
357Dim sLocMimeType as String
358Dim sPath as String
359	bDoEnable = Ubound(DialogModel.lstCurrencies.SelectedItems()) &gt; -1
360	If bDoEnable Then
361		&apos; Check if Source is set correctly
362		sPath = ConvertToUrl(Trim(DialogModel.txtSource.Text))
363		bDoEnable = oUcb.Exists(sPath)
364	End If
365	DialogModel.cmdGoOn.Enabled = bDoEnable
366End Sub
367
368
369Sub CallFolderPicker()
370	GetFolderName(DialogModel.txtTarget)
371	ToggleGoOnButton()
372End Sub
373
374
375Sub CallFilePicker()
376	If DialogModel.optSingleFile.State = 1 Then
377		Dim oMasterKey as Object
378		Dim oTypes() as Object
379		Dim oUIKey() as Object
380
381		oMasterKey = GetRegistryKeyContent(&quot;org.openoffice.TypeDetection.Types&quot;)
382		oTypes() = oMasterKey.Types
383		oUIKey = GetRegistryKeyContent(&quot;org.openoffice.Office.UI/FilterClassification/LocalFilters&quot;)
384		If DialogModel.chkTextDocuments.State = 1 Then
385			Dim FilterNames(7,1) as String
386			FilterNames(4,0) = oTypes.GetByName(&quot;writer8&quot;).UIName
387			FilterNames(4,1) = &quot;*.odt&quot;
388			FilterNames(5,0) = oTypes.GetByName(&quot;writer8_template&quot;).UIName
389			FilterNames(5,1) = &quot;*.ott&quot;
390			FilterNames(6,0) = oTypes.GetByName(&quot;writer_StarOffice_XML_Writer&quot;).UIName
391			FilterNames(6,1) = &quot;*.sxw&quot;
392			FilterNames(7,0) = oTypes.GetByName(&quot;writer_StarOffice_XML_Writer_Template&quot;).UIName
393			FilterNames(7,1) = &quot;*.stw&quot;
394		Else
395			ReDim FilterNames(3,1) as String
396		End If
397		FilterNames(0,0) = oTypes.GetByName(&quot;calc8&quot;).UIName
398		Filternames(0,1) = &quot;*.ods&quot;
399		FilterNames(1,0) = oTypes.GetByName(&quot;calc8_template&quot;).UIName
400		Filternames(1,1) = &quot;*.ots&quot;
401		FilterNames(2,0) = oTypes.GetByName(&quot;calc_StarOffice_XML_Calc&quot;).UIName
402		Filternames(2,1) = &quot;*.sxc&quot;
403		FilterNames(3,0) = oTypes.GetByName(&quot;calc_StarOffice_XML_Calc_Template&quot;).UIName
404		Filternames(3,1) = &quot;*.stc&quot;
405		GetFileName(DialogModel.txtSource, Filternames())
406	Else
407		GetFolderName(DialogModel.txtSource)
408	End If
409	ToggleGoOnButton()
410End Sub
411
412
413Sub PreviousStep()
414	DialogModel.Step = 2
415	DialogModel.cmdGoOn.Label = sGOON
416	DialogModel.cmdCancel.Label = sCANCEL
417End Sub
418</script:module>
419