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(14) 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			TypeList(2) = &quot;calc_StarCalc_30&quot;
81			TypeList(3) = &quot;calc_StarCalc_40&quot;
82			TypeList(4) = &quot;calc_StarCalc_50&quot;
83			If DialogModel.chkTextDocuments.State = 1 Then
84				ReDim Preserve TypeList(13) as String
85
86				TypeList(5) = &quot;writer8&quot;
87				TypeList(6) = &quot;writerglobal8&quot;
88				TypeList(7) = &quot;writer_StarOffice_XML_Writer&quot;
89				TypeList(8) = &quot;writer_globaldocument_StarOffice_XML_Writer_GlobalDocument&quot;
90				TypeList(9) = &quot;writer_StarWriter_30&quot;
91				TypeList(10) = &quot;writer_StarWriter_40&quot;
92				TypeList(11) = &quot;writer_globaldocument_StarWriter_40GlobalDocument&quot;
93				TypeList(12) = &quot;writer_StarWriter_50&quot;
94				TypeList(13) = &quot;writer_globaldocument_StarWriter_50GlobalDocument&quot;
95			End If
96			FilesList() = ReadDirectories(SourceDir, bRecursive, True, False, TypeList())
97			TotDocCount = Ubound(FilesList(),1) + 1
98		End If
99		InitializeProgressPage(DialogModel)
100&apos;		ChangeToNextProgressStep()
101		sTotDocCount = CStr(TotDocCount)
102		OpenProperties(0).Name = &quot;Hidden&quot;
103		OpenProperties(0).Value = True
104		OpenProperties(1).Name = &quot;AsTemplate&quot;
105		OpenProperties(1).Value = False
106		For DocIndex = 0 To TotDocCount - 1
107			If InitializeDocument(FilesList(), bDisposable) Then
108				If StoreDocument() Then
109					ConvertDocument()
110					oDocument.Store
111				End If
112				If bDisposable Then
113					oDocument.Dispose()
114				End If
115			End If
116		Next DocIndex
117		DialogModel.cmdBack.Enabled = True
118		DialogModel.cmdGoOn.Enabled = True
119		DialogModel.cmdGoOn.Label = sReady
120		DialogModel.cmdCancel.Label = sEnd
121	End If
122End Sub
123
124
125Function InitializeDocument(FilesList(), bDisposable as Boolean) as Boolean
126&apos; The Autopilot is started from step No. 2
127Dim sViewPath as String
128Dim bIsReadOnly as Boolean
129Dim sExtension as String
130	On Local Error Goto NEXTFILE
131	If Not bCancelTask Then
132		If DialogModel.optWholeDir.State = 1 Then
133			SourceFile = FilesList(DocIndex,0)
134			TargetFile = ReplaceString(SourceFile,TargetStemDir,SourceDir)
135			TargetDir = DirectorynameoutofPath(TargetFile, &quot;/&quot;)
136		Else
137			SourceFile = Source
138			TargetFile = TargetDir &amp; &quot;/&quot; &amp; FileNameoutofPath(SourceFile, &quot;/&quot;)
139		End If
140		If CreateFolder(TargetDir) Then
141			sExtension = GetFileNameExtension(SourceFile, &quot;/&quot;)
142 			oDocument = OpenDocument(SourceFile, OpenProperties(), bDisposable)
143			If (oDocument.IsReadOnly) AND (UCase(SourceFile) = UCase(TargetFile)) Then
144				bIsReadOnly = True
145				Msgbox(sMsgDOCISREADONLY, 16, GetProductName())
146			Else
147				bIsReadOnly = False
148				RetrieveDocumentObjects()
149				sViewPath = CutPathView(SourceFile, 60)
150				DialogModel.lblCurDocument.Label = Str(DocIndex+1) &amp; &quot;/&quot; &amp; sTotDocCount &amp; &quot; (&quot; &amp; sViewPath &amp; &quot;)&quot;
151			End If
152			InitializeDocument() = Not bIsReadOnly
153		Else
154			InitializeDocument() = False
155		End If
156	Else
157		InitializeDocument() = False
158	End If
159NEXTFILE:
160	If Err &lt;&gt; 0 Then
161		InitializeDocument() = False
162		Resume LETSGO
163LETSGO:
164	End If
165End Function
166
167
168Sub ChangeToNextProgressStep()
169	DialogModel.lblCurProgress.FontWeight = com.sun.star.awt.FontWeight.NORMAL
170	DialogConvert.GetControl(&quot;lblCurProgress&quot;).Visible = True
171End Sub
172
173
174Function StoreDocument() as Boolean
175Dim sCurFileExists as String
176Dim iOverWrite as Integer
177	If (TargetFile &lt;&gt; &quot;&quot;) And (Not bCancelTask) Then
178		On Local Error Goto NOSAVING
179		If oUcb.Exists(TargetFile) Then
180			sCurFileExists = ReplaceString(sMsgFileExists, ConvertFromUrl(TargetFile), &quot;&lt;1&gt;&quot;)
181			sCurFileExists = ReplaceString(sCurFileExists, chr(13), &quot;&lt;CR&gt;&quot;)
182			iOverWrite = Msgbox (sCurFileExists, 32 + 3, sMsgDLGTITLE)
183			Select Case iOverWrite
184				Case 1	&apos; OK
185				Case 2 	&apos; Abort
186					bCancelTask = True
187					StoreDocument() = False
188					Exit Function
189				Case 7 	&apos; No
190					StoreDocument() = False
191					Exit Function
192			End Select
193		End If
194		If TargetFile &lt;&gt; SourceFile Then
195			oDocument.StoreAsUrl(TargetFile,NoArgs)
196		Else
197			oDocument.Store
198		End If
199		StoreDocument() = True
200		NOSAVING:
201		If Err &lt;&gt; 0 Then
202			StoreDocument() = False
203			Resume CLERROR
204		End If
205		CLERROR:
206	End If
207End Function
208
209
210Sub SwapExtent()
211	DialogModel.chkRecursive.Enabled = DialogModel.optWholeDir.State = 1
212	If DialogModel.optWholeDir.State = 1 Then
213		DialogModel.lblSource.Label = sSOURCEDIR
214		If Not IsNull(SubstFile) Then
215			SubstFile = DialogModel.txtSource.Text
216			DialogModel.txtSource.Text = SubstDir
217		End If
218	Else
219		DialogModel.LblSource.Label = sSOURCEFILE
220		If Not IsNull(SubstDir) Then
221			SubstDir = DialogModel.txtSource.Text
222			DialogModel.txtSource.Text = SubstFile
223		End If
224	End If
225	ToggleGoOnButton()
226End Sub
227
228
229Function InitializeThirdStep() as Boolean
230Dim TextBoxText as String
231	Source = AssignFileName(DialogModel.txtSource.Text, DialogModel.lblSource.Label, True)
232	If CheckTextBoxPath(DialogModel.txtTarget, True, True, sMsgDLGTITLE, True) Then
233		TargetDir = AssignFileName(DialogModel.txtTarget.Text, DialogModel.lblTarget.Label, False)
234	Else
235		TargetDir = &quot;&quot;
236	End If
237	If Source &lt;&gt; &quot;&quot; And TargetDir &lt;&gt; &quot;&quot; Then
238		bRecursive = DialogModel.chkRecursive.State = 1
239		bDoUnprotect = DialogModel.chkProtect.State = 1
240		DialogModel.lblRetrieval.FontWeight = com.sun.star.awt.FontWeight.BOLD
241		DialogModel.lblRetrieval.Label = sPrgsRETRIEVAL
242		DialogModel.lblCurProgress.Label = sPrgsCONVERTING
243		If DialogModel.optWholeDir.State = 1 Then
244			TextBoxText = sSOURCEDIR &amp; &quot; &quot; &amp; ConvertFromUrl(Source) &amp; chr(13)
245			If DialogModel.chkRecursive.State = 1 Then
246				TextBoxText = TextBoxText &amp; DeleteStr(sInclusiveSubDir,&quot;~&quot;) &amp; chr(13)
247			End If
248		Else
249			TextBoxText = sSOURCEFILE &amp; &quot; &quot; &amp; ConvertFromUrl(Source) &amp; chr(13)
250		End If
251		TextBoxText = TextBoxText &amp; sTARGETDIR &amp; &quot; &quot; &amp; ConvertFromUrl(TargetDir) &amp; chr(13)
252		If DialogModel.chkProtect.State = 1 Then
253			TextBoxText = TextboxText &amp; sPrgsUNPROTECT
254		End If
255		DialogModel.txtConfig.Text = TextBoxText
256	 	ToggleProgressStep()
257		DialogModel.cmdGoOn.Enabled = False
258		InitializeThirdStep() = True
259	Else
260		InitializeThirdStep() = False
261	End If
262End Function
263
264
265Sub ToggleProgressStep(Optional aEvent as Object)
266Dim bMakeVisible as Boolean
267Dim LocStep as Integer
268	&apos; If the Sub is call by the &apos;cmdBack&apos; Button then set the &apos;bMakeVisible&apos; variable accordingly
269	bMakeVisible = IsMissing(aEvent)
270	If bMakeVisible Then
271		DialogModel.Step = 3
272	Else
273		DialogModel.Step = 2
274	End If
275 	DialogConvert.GetControl(&quot;lblCurrencies&quot;).Visible = Not bMakeVisible
276 	DialogConvert.GetControl(&quot;lstCurrencies&quot;).Visible = Not bMakeVisible
277	DialogConvert.GetControl(&quot;cmdBack&quot;).Visible = bMakeVisible
278 	DialogConvert.GetControl(&quot;cmdGoOn&quot;).Visible = bMakeVisible
279	DialogModel.imgPreview.ImageUrl = BitmapDir &amp; &quot;euro_&quot; &amp; DialogModel.Step &amp; &quot;.png&quot;
280End Sub
281
282
283Sub EnableStep2DialogControls(OnValue as Boolean)
284	With DialogModel
285		.hlnExtent.Enabled = OnValue
286		.optWholeDir.Enabled = OnValue
287		.optSingleFile.Enabled = OnValue
288		.chkProtect.Enabled = OnValue
289		.cmdCallSourceDialog.Enabled = OnValue
290		.cmdCallTargetDialog.Enabled = OnValue
291		.lblSource.Enabled = OnValue
292		.lblTarget.Enabled = OnValue
293		.txtSource.Enabled = OnValue
294		.txtTarget.Enabled = OnValue
295		.imgPreview.Enabled = OnValue
296		.lstCurrencies.Enabled = OnValue
297		.lblCurrencies.Enabled = OnValue
298		If OnValue Then
299			ToggleGoOnButton()
300			.chkRecursive.Enabled = .optWholeDir.State = 1
301		Else
302			.cmdGoOn.Enabled = False
303			.chkRecursive.Enabled = False
304		End If
305	End With
306End Sub
307
308
309Sub InitializeProgressPage()
310	DialogConvert.GetControl(&quot;lblRetrieval&quot;).Visible = False
311	DialogConvert.GetControl(&quot;lblCurProgress&quot;).Visible = False
312	DialogModel.lblRetrieval.FontWeight = com.sun.star.awt.FontWeight.NORMAL
313	DialogModel.lblCurProgress.FontWeight = com.sun.star.awt.FontWeight.BOLD
314	DialogConvert.GetControl(&quot;lblRetrieval&quot;).Visible = True
315	DialogConvert.GetControl(&quot;lblCurProgress&quot;).Visible = True
316End Sub
317
318
319Function AssignFileName(sPath as String, ByVal HeaderString, bCheckFileType as Boolean) as String
320Dim bIsValid as Boolean
321Dim sLocMimeType as String
322Dim sNoDirMessage as String
323	HeaderString = DeleteStr(HeaderString, &quot;:&quot;)
324	sPath = ConvertToUrl(Trim(sPath))
325	bIsValid = oUcb.Exists(sPath)
326	If bIsValid Then
327		If DialogModel.optSingleFile.State = 1 Then
328			If bCheckFileType Then
329				sLocMimeType = GetRealFileContent(sPath)
330				If DialogModel.chkTextDocuments.State = 1 Then
331					If (Instr(1, sLocMimeType, &quot;text&quot;) = 0) And (Instr(1, sLocMimeType, &quot;calc&quot;) = 0) Then
332						Msgbox(sMsgFileInvalid, 48, sMsgDLGTITLE)
333						bIsValid = False
334					End If
335				Else
336					If (Instr(1, sLocMimeType, &quot;spreadsheet&quot;) = 0) And (Instr(1, sLocMimeType, &quot;calc&quot;)) = 0 Then
337						Msgbox(sMsgFileInvalid, 48, sMsgDLGTITLE)
338						bIsValid = False
339					End If
340				End If
341			End If
342		Else
343			If Not oUcb.IsFolder(sPath) Then
344				sNoDirMessage = ReplaceString(sMsgNODIRECTORY,sPath,&quot;&lt;1&gt;&quot;)
345				Msgbox(sNoDirMessage,48, sMsgDLGTITLE)
346				bIsValid = False
347			Else
348				sPath = RTrimStr(sPath,&quot;/&quot;)
349				sPath = sPath &amp; &quot;/&quot;
350			End If
351		End if
352	Else
353		Msgbox(HeaderString &amp; &quot; &apos;&quot; &amp; ConvertFromUrl(sPath) &amp; &quot;&apos; &quot; &amp; sMsgNOTTHERE,48, sMsgDLGTITLE)
354	End If
355	If bIsValid Then
356		AssignFileName() = sPath
357	Else
358		AssignFilename() = &quot;&quot;
359	End If
360End Function
361
362
363Sub ToggleGoOnButton()
364Dim bDoEnable as Boolean
365Dim sLocMimeType as String
366Dim sPath as String
367	bDoEnable = Ubound(DialogModel.lstCurrencies.SelectedItems()) &gt; -1
368	If bDoEnable Then
369		&apos; Check if Source is set correctly
370		sPath = ConvertToUrl(Trim(DialogModel.txtSource.Text))
371		bDoEnable = oUcb.Exists(sPath)
372	End If
373	DialogModel.cmdGoOn.Enabled = bDoEnable
374End Sub
375
376
377Sub CallFolderPicker()
378	GetFolderName(DialogModel.txtTarget)
379	ToggleGoOnButton()
380End Sub
381
382
383Sub CallFilePicker()
384	If DialogModel.optSingleFile.State = 1 Then
385		Dim oMasterKey as Object
386		Dim oTypes() as Object
387		Dim oUIKey() as Object
388
389		oMasterKey = GetRegistryKeyContent(&quot;org.openoffice.TypeDetection.Types&quot;)
390		oTypes() = oMasterKey.Types
391		oUIKey = GetRegistryKeyContent(&quot;org.openoffice.Office.UI/FilterClassification/LocalFilters&quot;)
392		If DialogModel.chkTextDocuments.State = 1 Then
393			Dim FilterNames(11,1) as String
394			FilterNames(6,0) = oTypes.GetByName(&quot;writer_StarOffice_XML_Writer&quot;).UIName
395			FilterNames(6,1) = &quot;*.sxw&quot;
396			FilterNames(7,0) = oTypes.GetByName(&quot;writer_StarOffice_XML_Writer_Template&quot;).UIName
397			FilterNames(7,1) = &quot;*.stw&quot;
398			FilterNames(8,0) = oUIKey.Classes.GetByName(&quot;sw3to5&quot;).DisplayName
399			FilterNames(8,1) = &quot;*.sdw&quot;
400			FilterNames(9,0) = oUIKey.Classes.GetByName(&quot;sw3to5templ&quot;).DisplayName
401			Filternames(9,1) = &quot;*.vor&quot;
402			FilterNames(10,0) = oTypes.GetByName(&quot;writer8&quot;).UIName
403			FilterNames(10,1) = &quot;*.odt&quot;
404			FilterNames(11,0) = oTypes.GetByName(&quot;writer8_template&quot;).UIName
405			FilterNames(11,1) = &quot;*.ott&quot;
406		Else
407			ReDim FilterNames(5,1) as String
408		End If
409		FilterNames(0,0) = oTypes.GetByName(&quot;calc_StarOffice_XML_Calc&quot;).UIName
410		Filternames(0,1) = &quot;*.sxc&quot;
411		FilterNames(1,0) = oTypes.GetByName(&quot;calc_StarOffice_XML_Calc_Template&quot;).UIName
412		Filternames(1,1) = &quot;*.stc&quot;
413		FilterNames(2,0) = oUIKey.Classes.GetByName(&quot;sc345&quot;).DisplayName
414		FilterNames(2,1) = &quot;*.sdc&quot;
415		FilterNames(3,0) = oUIKey.Classes.GetByName(&quot;sc345templ&quot;).DisplayName
416		Filternames(3,1) = &quot;*.vor&quot;
417		FilterNames(4,0) = oTypes.GetByName(&quot;calc8&quot;).UIName
418		Filternames(4,1) = &quot;*.ods&quot;
419		FilterNames(5,0) = oTypes.GetByName(&quot;calc8_template&quot;).UIName
420		Filternames(5,1) = &quot;*.ots&quot;
421		GetFileName(DialogModel.txtSource, Filternames())
422	Else
423		GetFolderName(DialogModel.txtSource)
424	End If
425	ToggleGoOnButton()
426End Sub
427
428
429Sub PreviousStep()
430	DialogModel.Step = 2
431	DialogModel.cmdGoOn.Label = sGOON
432	DialogModel.cmdCancel.Label = sCANCEL
433End Sub
434</script:module>
435