home *** CD-ROM | disk | FTP | other *** search
/ Kompuutteri Kaikille K-CD 2002 #3 / K-CD_2002-03.iso / OpenOffice / f_0243 / FilesModul.xba < prev    next >
Extensible Markup Language  |  2001-10-17  |  14KB  |  382 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.         MaxFileIndex = Ubound(FilesList(),1)
  125.         For i = 0 To MaxFileIndex
  126.             If bCancelTask Then
  127.                 bConversionIsRunnig = False
  128.                 Exit Sub
  129.             End if
  130.             bDoSave = True
  131.             sFullName = FilesList(i,0)
  132.             CurFiltername =    GetFilterName(FilesList(i,1), sFilterName(), sExtension, FilterIndex)
  133.             ApplIndex = FilesList(i,2)
  134.             sViewPath = CutPathView(sFullName, 60)
  135.             ImportDialog.LabelCurDocument.Label = Str(i+1) & "/" & MaxFileIndex + 1 & "  (" & sViewPath & ")"
  136.             
  137.             sOldExtension = GetFileNameExtension(sFullName, "/")
  138.             Select Case sOldExtension
  139.                 Case "vor", "dot", "xlt", "pot"
  140.                     OpenProperties(1).Value = False
  141.                 Case Else
  142.                     OpenProperties(1).Value = True
  143.             End Select
  144.             oDocument = StarDesktop.LoadComponentFromURL(sFullName, "_blank", 0, OpenProperties())
  145.             If bSetFonts Then
  146.                 CheckScripts(oDocument, 1)
  147.             End If
  148.  
  149.             If Not IsNull(oDocument) Then
  150.                 Select Case sExtension
  151.                     Case "sxw", "sxc", "sxi", "sxd", "sxs", "sxm"
  152.                         SourceStemDir = RTrimStr(Applications(ApplIndex,SBDOCSOURCE), "/")
  153.                         TargetStemDir = RTrimStr(Applications(ApplIndex,SBDOCTARGET), "/")
  154.                     Case Else                                 ' Templates and Helper-Applications remain
  155.                         SourceStemDir = RTrimStr(Applications(ApplIndex,SBTEMPLSOURCE), "/")
  156.                         TargetStemDir = RTrimStr(Applications(ApplIndex,SBTEMPLTARGET), "/")
  157.                 End Select
  158.  
  159.                 TargetFile = ReplaceString(sFullname, TargetStemDir, SourceStemDir)
  160.                 sFileName = GetFileNameWithoutExtension(TargetFile, "/")
  161.                 OldExtension = GetFileNameExtension(TargetFile)
  162.  
  163.                 TargetFile = RTrimStr(TargetFile, OldExtension)
  164.                 TargetFile = TargetFile & sExtension
  165.                 TargetDir = RTrimStr(TargetFile, sFileName & "." & sExtension)
  166.                 If Not oUcb.Exists(TargetDir) Then
  167.                     oUcb.CreateFolder(TargetDir)
  168.                 End If
  169.                 If oUcb.Exists(TargetFile) Then
  170.                     sCurFileExists = ReplaceString(sFileExists, ConvertFromUrl(TargetFile), "<1>")
  171.                     sCurFileExists = ReplaceString(sCurFileExists, chr(13), "<CR>")
  172.                     iOverWrite = Msgbox (sCurFileExists, 32 + 3, sTitle)
  173.                     Select Case iOverWrite
  174.                         Case 1    ' OK
  175.                             ' In the FileProperty-Bean this is already default
  176.                             bDoSave = True
  177.                         Case 2     ' Abort
  178.                             CancelTask(False)
  179.                             bDoSave = False
  180.                         Case 7     ' No
  181.                             bDoSave = False
  182.                     End Select
  183.                 End If
  184.                 If bDoSave Then
  185.                     InsertDocNamesToLogDocument(sFullName, TargetFile)
  186.                     On Local Error Resume Next
  187.                     FileProperties(0).Name = "FilterName"
  188.                     FileProperties(0).Value = CurFilterName
  189.                     oDocument.StoreAsUrl(TargetFile,FileProperties())
  190.                     oDocument.Dispose()
  191.                     On Local Error Goto 0
  192.                 End If
  193. '                oTaskenum = StarDesktop.Tasks.CreateEnumeration
  194. '                While oTaskEnum.HasmoreElements
  195. '                    oTask = oTaskenum.NextElement
  196. '                    If oTask.Name <> "" Then
  197. '                        oTaskController = oTask.Controller
  198. '                        PrintdbgInfo oTaskController
  199. '                        If hasUnoInterfaces(oTaskController,"com.sun.star.frame.XModel") then
  200. '                            oModel = oTaskController.Model
  201. '                            If Ucase(oModel.Url) = Ucase(sFullName) Then
  202. '                                oTask.Close
  203. '                            End If
  204. '                        End If
  205. '                    End If
  206. '                Wend
  207.             End If
  208.         Next i
  209.     End If
  210.     ImportDialog.cbCancel.Label = sCloseButton
  211.     ImportDialog.cbGoOn.Label = sReady
  212.     ImportDialog.cbGoOn.Enabled = True
  213.     bConversionIsRunnig = False
  214.     Exit Sub
  215. RTError:
  216.     Msgbox sRTErrorDesc, 16, sRTErrorHeader
  217. End Sub
  218.  
  219.  
  220. Sub AddListtoFilesList(FirstList(), SecList(), ApplIndex as Integer)
  221. Dim FirstStart as Integer, FirstEnd as Integer, i as Integer, s as Integer
  222.     If FirstList(0,0) = "" Then
  223.         FirstStart = Ubound(FirstList(),1)
  224.     Else
  225.         FirstStart = Ubound(FirstList(),1) + 1
  226.     End If
  227.     FirstEnd = FirstStart + Ubound(SecList(),1)
  228.     ReDim Preserve FirstList(FirstEnd,2)
  229.     s = 0
  230.     For i = FirstStart To FirstEnd
  231.         FirstList(i,0) = SecList(s,0)
  232.         FirstList(i,1) = SecList(s,1)
  233.         FirstList(i,2) = CStr(ApplIndex)
  234.         s = s + 1
  235.     Next i
  236. End Sub
  237.  
  238.  
  239. Function GetTargetTemplatePath(Index as Integer)
  240.     Select Case WizardMode
  241.         Case SBMICROSOFTMODE
  242.             GetTargetTemplatePath() = SOTemplatePath & "/" & sTemplateGroupName
  243.         Case SBXMLMODE
  244.             If Index = 3 Then
  245.                 ' Helper Application
  246.                 GetTargetTemplatePath = SOWorkPath
  247.             Else
  248.                 GetTargetTemplatePath = SOTemplatePath
  249.             End If
  250.     End Select
  251. End Function
  252.  
  253.  
  254. ' Retrieves the second value for a next to 'SearchString' in
  255. ' a two-dimensional string-Array
  256. Function GetFilterName(sMimetypeorExtension as String, sFilterName(), sExtension as string, FilterIndex as Integer) as String
  257. Dim i as Integer
  258. Dim MaxIndex as Integer
  259. Dim sLocFilterlist() as String
  260.     For i = 0 To Ubound(sFiltername(),1)
  261.         If Instr(1,sFilterName(i,0),sMimeTypeOrExtension) <> 0 Then
  262.             sLocFilterList() = ArrayoutofString(sFiltername(i,0),"|", MaxIndex)
  263.             If MaxIndex = 0 Then
  264.                 sExtension = sFiltername(i,2)
  265.                 GetFilterName = sFilterName(i,1)
  266.             Else
  267.                 Dim a as Integer
  268.                 Dim sLocExtensionList() as String
  269.                 a =    SearchArrayForPartString(sMimetypeOrExtension, sLocFilterList())
  270.                 sLocFilterList() = ArrayoutofString(sFiltername(i,1),"|", MaxIndex)
  271.                 GetFilterName = sLocFilterList(a)
  272.                 sLocExtensionList() = ArrayoutofString(sFilterName(i,2), "|", MaxIndex)
  273.                 sExtension = sLocExtensionList(a)
  274.             End If
  275.             Exit For
  276.         End If
  277.     Next
  278.     FilterIndex = i
  279. End Function
  280.  
  281.  
  282. Function SearchArrayforPartString(SearchString as String, LocList()) as Integer
  283. Dim i as integer
  284.     For i = Lbound(LocList(),1) to Ubound(LocList(),1)
  285.         If Instr(1,LocList(i), SearchString) <> 0 Then
  286.             SearchArrayForPartString() = i
  287.             Exit Function
  288.         End if
  289.     Next
  290.     IndexinArray = -1
  291. End Function
  292.  
  293.  
  294. Function GetMimeTypeList(BigFiltername as STring)
  295. Dim sBigList() as String
  296. Dim sSmallList() as String
  297. Dim sMimeTypeList()
  298. Dim BigMaxIndex as Integer
  299. Dim n as Integer
  300.     sBigList() = ArrayoutofString(BigFilterName,"|", BigMaxIndex)
  301.     For n = 0 To BigMaxIndex
  302.         sSmallList() = ArrayoutofString(sBigList(n),";")
  303.         sMimeTypeList() = AddListToList(sMimeTypeList(), sSmallList())
  304.     Next n
  305.     GetMimetypeList() = sMimeTypeList()
  306. End Function
  307.  
  308.  
  309. Sub CreateLogDocument(HiddenProperties())
  310. Dim oTableCursor as Object
  311. Dim oLogCursor as Object
  312. Dim oLogRows as Object
  313. Dim sLogUrl as String
  314. Dim NoArgs()
  315. Dim i as Integer
  316. Dim bLogExists as Boolean
  317.     If ImportDialog.chkLogfile.State = 1 Then
  318.         i = 2
  319. Dim oArg() as new com.sun.star.beans.PropertyValue
  320. Dim oUrl as new com.sun.star.util.URL
  321. Dim oDisp as Object
  322.     oUrl.Complete = "private:factory/swriter"
  323. '    oDisp = StarDesktop.currentFrame.queryDispatch(oUrl, "_self", 0)    
  324.     oDisp = StarDesktop.currentFrame.queryDispatch(oUrl, "_self", 0)    
  325.  
  326.     oDisp.dispatch(oUrl, oArg())
  327.     printdbgInfo oDisp
  328. '    oLogDocument = StarDesktop.LoadComponentFromURL("private:factory/swriter", "_blank", 4, NoArgs())' HiddenProperties()) ' HiddenProperties())
  329.         oLogCursor = oLogDocument.Text.CreateTextCursor
  330.         oLogTable =  oLogDocument.CreateInstance("com.sun.star.text.TextTable")
  331.         oLogCursor.Text.InsertTextContent(oLogCursor, oLogTable, True)
  332.         oLogCursor = oLogTable.GetCellbyPosition(0,0).createTextCursor
  333.         oLogCursor.SetString(sSourceDocuments)
  334.         oLogCursor = oLogTable.GetCellbyPosition(1,0).createTextCursor
  335.         oLogCursor.SetString(sTargetDocuments)
  336.         bInsertRow = False
  337. ' Todo: Strings in Resourcen
  338.         sLogUrl = SOWorkPath & "/Logfile.sxw"
  339.         Do
  340.             bLogExists = oUcb.Exists(sLogUrl)
  341.             If bLogExists Then
  342.                 If i = 2 Then
  343.                     sLogUrl = ReplaceString(sLogUrl, "/Logfile_2.sxw", "/Logfile.sxw")
  344.                 Else
  345.                     sLogUrl = ReplaceString(sLogUrl, "/Logfile_" & cStr(i) & ".sxw", "/Logfile_" & cStr(i-1) & ".sxw")
  346.                 End If
  347.                 i = i + 1
  348.             End If
  349.         Loop Until Not bLogExists
  350.         oLogDocument.StoreAsUrl(sLogUrl, NoArgs())
  351.     EndIf
  352. End Sub
  353.  
  354.  
  355. Sub InsertDocNamesToLogDocument(SourceUrl as String, TargetUrl as String)
  356. Dim oCell as Object
  357. Dim oLogCursor as Object
  358. Dim UrlList(1) as String
  359. Dim LocFileName as String
  360. Dim LocUrl as String
  361. Dim i as Integer
  362.     If ImportDialog.chkLogfile.State = 1 Then
  363.         If bInsertRow Then
  364.             oLogTable.Rows.InsertByIndex(oLogTable.Rows.Count,1)
  365.         Else
  366.             bInsertRow = True
  367.         End If
  368.         UrlList(0) = SourceUrl
  369.         UrlList(1) = TargetUrl
  370.         For i = 0 To 1
  371.             oCell = oLogTable.GetCellbyPosition(i,oLogTable.Rows.Count-1)
  372.             oLogCursor = oCell.createTextCursor()
  373.             LocUrl = UrlList(i)
  374.             oLogCursor.HyperLinkURL = LocUrl
  375.             oLogCursor.HyperLinkName = LocUrl
  376.             oLogCursor.HyperLinkTarget = LocUrl
  377.             LocFileName = FileNameOutOfPath(LocUrl)
  378.             oCell.InsertString(oLogCursor, LocFileName,False)
  379.         Next i 
  380.         oLogDocument.Store()
  381.     End If
  382. End Sub</script:module>