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