home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2002 November / VPR0211A.ISO / OPENOFFICE / f_0272 / AutoPilotRun.xba next >
Extensible Markup Language  |  2002-02-19  |  14KB  |  395 lines

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