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