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