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