home *** CD-ROM | disk | FTP | other *** search
/ com!online 2001 December / COMCD1201.iso / openoffice / f_0243 / FilesModul.xba < prev    next >
Encoding:
Extensible Markup Language  |  2001-08-27  |  13.2 KB  |  374 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="FilesModul" script:language="StarBasic">Option Explicit
  4.  
  5. Public AbsTemplateFound as Integer
  6. Public AbsDocuFound as Integer
  7. Public oLogDocument as Object
  8. Public oLogTable as Object
  9. Public bInsertRow as Boolean
  10.  
  11. Function ReadApplicationDirectories(ApplIndex as Integer, FilesList(),bIsDocument as Boolean, sFiltername()) as Integer
  12. Dim bCheckDocuType as Boolean
  13. Dim FilterIndex as Integer
  14. Dim bRecursive as Boolean
  15. Dim sSourceDir as String
  16. Dim bCheckRealType as Boolean
  17. Dim a as Integer
  18. Dim sFileContent() as String
  19. Dim NewList() as String
  20. Dim Index as Integer
  21. Dim sLocExtension as String
  22.     Index = Applications(ApplIndex,SBAPPLKEY)
  23.     sLocExtension = ""
  24.     If bIsDocument Then
  25.         bCheckDocuType = Applications(ApplIndex,SBDOCCONVERT)
  26.         bCheckRealType = False
  27.         bRecursive = Applications(ApplIndex,SBDOCRECURSIVE)
  28.         FilterIndex = Index
  29.         sSourceDir = Applications(ApplIndex,SBDOCSOURCE)
  30.     Else
  31.         ' Templates
  32.         bCheckDocuType = Applications(ApplIndex,SBTEMPLCONVERT)
  33.         ' In SO the documenttype cannot be derived from the extension name
  34.         bCheckRealType = WizardMode = SBXMLMODE
  35.         If bCheckRealType Then
  36.             ' Note: StarOffice-Math-Documents cannot be treated like templates
  37.             bCheckRealType = Index <> 3
  38.             If bCheckRealType Then
  39.                 sLocExtension = "vor"
  40.             End If
  41.             bIsDocument = Not bCheckRealType
  42.         End If
  43.         bRecursive = Applications(ApplIndex,SBTEMPLRECURSIVE)
  44.         FilterIndex = Index + MaxApplCount
  45.         sSourceDir = Applications(ApplIndex,SBTEMPLSOURCE)
  46.     End If
  47.     If bCheckDocuType Then
  48.         sFileContent() = GetMimeTypeList(sFilterName(FilterIndex))
  49.         NewList() = ReadDirectories(sSourceDir, bRecursive, bCheckRealType, False, sFileContent(), sLocExtension)
  50.         If Ubound(NewList()) > -1 Then
  51.             AddListtoFilesList(FilesList(), NewList(), ApplIndex)
  52.             ImportDialog.LabelRetrieval.Label = sProgressPage_2 &  "  " & ReplaceString(sProgressPage_5, Str(Ubound(FilesList()) + 1) & " ", "%1")
  53.         End If
  54.     End If
  55.     ReadApplicationDirectories() = Ubound(NewList(),1) + 1
  56. End Function
  57.  
  58.  
  59. Sub    ShowCurrentProgress(bIsDocument as Boolean, CurFound as Integer)
  60.     If bIsDocument Then
  61.         AbsDocuFound = AbsDocuFound + CurFound
  62.         ImportDialog.LabelCurDocumentRetrieval.Label =  sProgressFound & " " & CStr(AbsDocuFound) &  " " & sProgressMoreDocs
  63.     Else
  64.         AbsTemplateFound = AbsTemplateFound + CurFound
  65.         ImportDialog.LabelCurTemplateRetrieval.Label = sProgressFound & " " & CStr(AbsTemplateFound) & " " & sProgressMoreTemplates
  66.   End If
  67. End Sub
  68.  
  69.  
  70. Sub ConvertAllDocuments(sFilterName())
  71. Dim FileProperties(0) as new com.sun.star.beans.PropertyValue
  72. Dim WriterWebProperties(0) as new com.sun.star.beans.PropertyValue
  73. Dim OpenProperties(1) as new com.sun.star.beans.PropertyValue
  74. Dim FilesList(0,2) as String
  75. Dim sViewPath as String
  76. Dim i as Integer
  77. Dim FilterIndex as Integer
  78. Dim sFullName as String
  79. Dim sFileName as String
  80. Dim oDocument as Object
  81. Dim sExtension as String
  82. Dim OldExtension as String
  83. Dim CurFound as Integer
  84. Dim TotFound as Integer
  85. Dim TargetStemDir as String
  86. Dim SourceStemDir as String
  87. Dim TargetDir as String
  88. Dim TargetFile as String
  89. Dim CurFilterName as String
  90. Dim ApplIndex as Integer
  91. Dim Index as Integer
  92. Dim bIsDocument as Boolean
  93. Dim iOverWrite as Integer
  94. Dim bDoSave as Boolean
  95. Dim sCurFileExists as String
  96. Dim oTaskEnum as Object
  97. Dim oTask as Object
  98. Dim oModel as Object
  99. Dim oTaskController as Object
  100. Dim MaxFileIndex as Integer
  101. Dim sOldExtension as String
  102.     bConversionIsRunnig = True
  103.     AbsTemplateFound = 0
  104.     AbsDocuFound = 0
  105.     For i = 0 To ApplCount-1
  106.         'templates
  107.         bIsDocument = False
  108.         CurFound = ReadApplicationDirectories(i, FilesList(), bIsDocument, sFilterName())
  109.         ShowCurrentProgress(bIsDocument, CurFound)
  110.     Next i
  111.     For i = 0 To ApplCount-1
  112.         'documents
  113.         bIsDocument = True
  114.         CurFound = ReadApplicationDirectories(i, FilesList(), bIsDocument, sFilterName())
  115.         ShowCurrentProgress(bIsDocument, CurFound)
  116.     Next i
  117.     TotFound = AbsTemplateFound + AbsDocuFound
  118.     CreateLogDocument(OpenProperties())
  119.     If TotFound > 0 Then
  120.         InitializeProgressPage(ImportDialog)
  121.         OpenProperties(0).Name = "Hidden"
  122.         OpenProperties(0).Value = True
  123.         OpenProperties(1).Name = "AsTemplate"
  124.  
  125.         MaxFileIndex = Ubound(FilesList(),1)
  126.         For i = 0 To MaxFileIndex
  127.             If bCancelTask Then
  128.                 bConversionIsRunnig = False
  129.                 Exit Sub
  130.             End if
  131.             bDoSave = True
  132.             sFullName = FilesList(i,0)
  133.             CurFiltername =    GetFilterName(FilesList(i,1), sFilterName(), sExtension, FilterIndex)
  134.             ApplIndex = FilesList(i,2)
  135.             sViewPath = CutPathView(sFullName, 60)
  136.             ImportDialog.LabelCurDocument.Label = Str(i+1) & "/" & MaxFileIndex + 1 & "  (" & sViewPath & ")"
  137.             
  138.             sOldExtension = GetFileNameExtension(sFullName, "/")
  139.             Select Case sOldExtension
  140.                 Case "vor", "dot", "xlt", "pot"
  141.                     OpenProperties(1).Value = False
  142.                 Case Else
  143.                     OpenProperties(1).Value = True
  144.             End Select
  145.             oDocument = StarDesktop.LoadComponentFromURL(sFullName, "_blank", 0, OpenProperties())
  146.             If bSetFonts Then
  147.                 CheckScripts(oDocument, 1)
  148.             End If
  149.  
  150.             If Not IsNull(oDocument) Then
  151.                 Select Case sExtension
  152.                     Case "sxw", "sxc", "sxi", "sxd", "sxs", "sxm"
  153.                         SourceStemDir = RTrimStr(Applications(ApplIndex,SBDOCSOURCE), "/")
  154.                         TargetStemDir = RTrimStr(Applications(ApplIndex,SBDOCTARGET), "/")
  155.                     Case Else                                 ' Templates and Helper-Applications remain
  156.                         SourceStemDir = RTrimStr(Applications(ApplIndex,SBTEMPLSOURCE), "/")
  157.                         TargetStemDir = RTrimStr(Applications(ApplIndex,SBTEMPLTARGET), "/")
  158.                 End Select
  159.  
  160.                 TargetFile = ReplaceString(sFullname, TargetStemDir, SourceStemDir)
  161.                 sFileName = GetFileNameWithoutExtension(TargetFile, "/")
  162.                 OldExtension = GetFileNameExtension(TargetFile)
  163.  
  164.                 TargetFile = RTrimStr(TargetFile, OldExtension)
  165.                 TargetFile = TargetFile & sExtension
  166.                 TargetDir = RTrimStr(TargetFile, sFileName & "." & sExtension)
  167.                 If Not oUcb.Exists(TargetDir) Then
  168.                     oUcb.CreateFolder(TargetDir)
  169.                 End If
  170.                 If oUcb.Exists(TargetFile) Then
  171.                     sCurFileExists = ReplaceString(sFileExists, ConvertFromUrl(TargetFile), "<1>")
  172.                     sCurFileExists = ReplaceString(sCurFileExists, chr(13), "<CR>")
  173.                     iOverWrite = Msgbox (sCurFileExists, 32 + 3, sTitle)
  174.                     Select Case iOverWrite
  175.                         Case 1    ' OK
  176.                             ' In the FileProperty-Bean this is already default
  177.                             bDoSave = True
  178.                         Case 2     ' Abort
  179.                             CancelTask(False)
  180.                             bDoSave = False
  181.                         Case 7     ' No
  182.                             bDoSave = False
  183.                     End Select
  184.                 End If
  185.                 If bDoSave Then
  186.                     InsertDocNamesToLogDocument(sFullName, TargetFile)
  187. '                    On Local Error Resume Next
  188.                     FileProperties(0).Name = "FilterName"
  189.                     FileProperties(0).Value = CurFilterName
  190.                     oDocument.StoreAsUrl(TargetFile,FileProperties())
  191.                     oDocument.Dispose()
  192. '                    On Local Error Goto 0
  193.                 End If
  194. '                oTaskenum = StarDesktop.Tasks.CreateEnumeration
  195. '                While oTaskEnum.HasmoreElements
  196. '                    oTask = oTaskenum.NextElement
  197. '                    If oTask.Name <> "" Then
  198. '                        oTaskController = oTask.Controller
  199. '                        PrintdbgInfo oTaskController
  200. '                        If hasUnoInterfaces(oTaskController,"com.sun.star.frame.XModel") then
  201. '                            oModel = oTaskController.Model
  202. '                            If Ucase(oModel.Url) = Ucase(sFullName) Then
  203. '                                oTask.Close
  204. '                            End If
  205. '                        End If
  206. '                    End If
  207. '                Wend
  208.             End If
  209.         Next i
  210.     End If
  211.     ImportDialog.cbCancel.Label = sCloseButton
  212.     ImportDialog.cbGoOn.Label = sReady
  213.     ImportDialog.cbGoOn.Enabled = True
  214.     bConversionIsRunnig = False
  215.     Exit Sub
  216. RTError:
  217.     Msgbox sRTErrorDesc, 16, sRTErrorHeader
  218. End Sub
  219.  
  220.  
  221. Sub AddListtoFilesList(FirstList(), SecList(), ApplIndex as Integer)
  222. Dim FirstStart as Integer, FirstEnd as Integer, i as Integer, s as Integer
  223.     If FirstList(0,0) = "" Then
  224.         FirstStart = Ubound(FirstList(),1)
  225.     Else
  226.         FirstStart = Ubound(FirstList(),1) + 1
  227.     End If
  228.     FirstEnd = FirstStart + Ubound(SecList(),1)
  229.     ReDim Preserve FirstList(FirstEnd,2)
  230.     s = 0
  231.     For i = FirstStart To FirstEnd
  232.         FirstList(i,0) = SecList(s,0)
  233.         FirstList(i,1) = SecList(s,1)
  234.         FirstList(i,2) = CStr(ApplIndex)
  235.         s = s + 1
  236.     Next i
  237. End Sub
  238.  
  239.  
  240. Function GetTargetTemplatePath(Index as Integer)
  241.     Select Case WizardMode
  242.         Case SBMICROSOFTMODE
  243.             GetTargetTemplatePath() = SOTemplatePath & "/" & sTemplateGroupName
  244.         Case SBXMLMODE
  245.             If Index = 3 Then
  246.                 ' Helper Application
  247.                 GetTargetTemplatePath = SOWorkPath
  248.             Else
  249.                 GetTargetTemplatePath = SOTemplatePath
  250.             End If
  251.     End Select
  252. End Function
  253.  
  254.  
  255. ' Retrieves the second value for a next to 'SearchString' in
  256. ' a two-dimensional string-Array
  257. Function GetFilterName(sMimetypeorExtension as String, sFilterName(), sExtension as string, FilterIndex as Integer) as String
  258. Dim i as Integer
  259. Dim MaxIndex as Integer
  260. Dim sLocFilterlist() as String
  261.     For i = 0 To Ubound(sFiltername(),1)
  262.         If Instr(1,sFilterName(i,0),sMimeTypeOrExtension) <> 0 Then
  263.             sLocFilterList() = ArrayoutofString(sFiltername(i,0),"|", MaxIndex)
  264.             If MaxIndex = 0 Then
  265.                 sExtension = sFiltername(i,2)
  266.                 GetFilterName = sFilterName(i,1)
  267.             Else
  268.                 Dim a as Integer
  269.                 Dim sLocExtensionList() as String
  270.                 a =    SearchArrayForPartString(sMimetypeOrExtension, sLocFilterList())
  271.                 sLocFilterList() = ArrayoutofString(sFiltername(i,1),"|", MaxIndex)
  272.                 GetFilterName = sLocFilterList(a)
  273.                 sLocExtensionList() = ArrayoutofString(sFilterName(i,2), "|", MaxIndex)
  274.                 sExtension = sLocExtensionList(a)
  275.             End If
  276.             Exit For
  277.         End If
  278.     Next
  279.     FilterIndex = i
  280. End Function
  281.  
  282.  
  283. Function SearchArrayforPartString(SearchString as String, LocList()) as Integer
  284. Dim i as integer
  285.     For i = Lbound(LocList(),1) to Ubound(LocList(),1)
  286.         If Instr(1,LocList(i), SearchString) <> 0 Then
  287.             SearchArrayForPartString() = i
  288.             Exit Function
  289.         End if
  290.     Next
  291.     IndexinArray = -1
  292. End Function
  293.  
  294.  
  295. Function GetMimeTypeList(BigFiltername as STring)
  296. Dim sBigList() as String
  297. Dim sSmallList() as String
  298. Dim sMimeTypeList()
  299. Dim BigMaxIndex as Integer
  300. Dim n as Integer
  301.     sBigList() = ArrayoutofString(BigFilterName,"|", BigMaxIndex)
  302.     For n = 0 To BigMaxIndex
  303.         sSmallList() = ArrayoutofString(sBigList(n),";")
  304.         sMimeTypeList() = AddListToList(sMimeTypeList(), sSmallList())
  305.     Next n
  306.     GetMimetypeList() = sMimeTypeList()
  307. End Function
  308.  
  309.  
  310. Sub CreateLogDocument(HiddenProperties())
  311. Dim oTableCursor as Object
  312. Dim oLogCursor as Object
  313. Dim oLogRows as Object
  314. Dim sLogUrl as String
  315. Dim NoArgs()
  316. Dim i as Integer
  317. Dim bLogExists as Boolean
  318.     If ImportDialog.chkLogfile.State = 1 Then
  319.         i = 2
  320.         oLogDocument = StarDesktop.LoadComponentFromURL("private:factory/swriter", "_blank", 4, NoArgs())' HiddenProperties()) ' HiddenProperties())
  321.         oLogCursor = oLogDocument.Text.CreateTextCursor
  322.         oLogTable =  oLogDocument.CreateInstance("com.sun.star.text.TextTable")
  323.         oLogCursor.Text.InsertTextContent(oLogCursor, oLogTable, True)
  324.         oLogCursor = oLogTable.GetCellbyPosition(0,0).createTextCursor
  325.         oLogCursor.SetString(sSourceDocuments)
  326.         oLogCursor = oLogTable.GetCellbyPosition(1,0).createTextCursor
  327.         oLogCursor.SetString(sTargetDocuments)
  328.         bInsertRow = False
  329. ' Todo: Strings in Resourcen
  330.         sLogUrl = SOWorkPath & "/Logfile.sxw"
  331.         Do
  332.             bLogExists = oUcb.Exists(sLogUrl)
  333.             If bLogExists Then
  334.                 If i = 2 Then
  335.                     sLogUrl = ReplaceString(sLogUrl, "/Logfile_2.sxw", "/Logfile.sxw")
  336.                 Else
  337.                     sLogUrl = ReplaceString(sLogUrl, "/Logfile_" & cStr(i) & ".sxw", "/Logfile_" & cStr(i-1) & ".sxw")
  338.                 End If
  339.                 i = i + 1
  340.             End If
  341.         Loop Until Not bLogExists
  342.         oLogDocument.StoreAsUrl(sLogUrl, NoArgs())
  343.     EndIf
  344. End Sub
  345.  
  346.  
  347. Sub InsertDocNamesToLogDocument(SourceUrl as String, TargetUrl as String)
  348. Dim oCell as Object
  349. Dim oLogCursor as Object
  350. Dim UrlList(1) as String
  351. Dim LocFileName as String
  352. Dim LocUrl as String
  353. Dim i as Integer
  354.     If ImportDialog.chkLogfile.State = 1 Then
  355.         If bInsertRow Then
  356.             oLogTable.Rows.InsertByIndex(oLogTable.Rows.Count,1)
  357.         Else
  358.             bInsertRow = True
  359.         End If
  360.         UrlList(0) = SourceUrl
  361.         UrlList(1) = TargetUrl
  362.         For i = 0 To 1
  363.             oCell = oLogTable.GetCellbyPosition(i,oLogTable.Rows.Count-1)
  364.             oLogCursor = oCell.createTextCursor()
  365.             LocUrl = UrlList(i)
  366.             oLogCursor.HyperLinkURL = LocUrl
  367.             oLogCursor.HyperLinkName = LocUrl
  368.             oLogCursor.HyperLinkTarget = LocUrl
  369.             LocFileName = FileNameOutOfPath(LocUrl, "/")
  370.             oCell.InsertString(oLogCursor, LocFileName,False)
  371.         Next i 
  372.         oLogDocument.Store()
  373.     End If
  374. End Sub</script:module>