AutoPilotRun.xba 15 KB

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