AutoPilotRun.xba 14 KB

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