UCB.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #
  2. # This file is part of the LibreOffice project.
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. #
  8. # This file incorporates work covered by the following license notice:
  9. #
  10. # Licensed to the Apache Software Foundation (ASF) under one or more
  11. # contributor license agreements. See the NOTICE file distributed
  12. # with this work for additional information regarding copyright
  13. # ownership. The ASF licenses this file to you under the Apache
  14. # License, Version 2.0 (the "License"); you may not use this file
  15. # except in compliance with the License. You may obtain a copy of
  16. # the License at http://www.apache.org/licenses/LICENSE-2.0 .
  17. import uno
  18. import traceback
  19. from abc import abstractmethod
  20. from ..common.FileAccess import FileAccess
  21. from com.sun.star.beans import Property
  22. from com.sun.star.ucb import Command
  23. from com.sun.star.ucb import GlobalTransferCommandArgument
  24. from com.sun.star.ucb.NameClash import OVERWRITE
  25. from com.sun.star.ucb import OpenCommandArgument2
  26. from com.sun.star.ucb.OpenMode import ALL
  27. from com.sun.star.ucb.TransferCommandOperation import COPY
  28. # This class is used to copy the content of a folder to
  29. # another folder.
  30. # There is an inconsistency with argument order.
  31. # It should be always: dir,filename.
  32. class UCB(object):
  33. ucb = None
  34. fa = None
  35. xmsf = None
  36. def __init__(self, xmsf):
  37. self.ucb = xmsf.createInstanceWithArguments("com.sun.star.ucb.UniversalContentBroker", ())
  38. self.fa = FileAccess(xmsf)
  39. self.xmsf = xmsf
  40. def delete(self, filename):
  41. # System.out.println("UCB.delete(" + filename)
  42. self.executeCommand(self.getContent(filename),"delete", True)
  43. def copy(self, sourceDir, targetDir):
  44. self.copy1(sourceDir,targetDir, None)
  45. def copy1(self, sourceDir, targetDir, verifier):
  46. files = self.listFiles(sourceDir, verifier)
  47. for i in range(len(files)):
  48. self.copy2(sourceDir, files[i], targetDir, "")
  49. def copy2(self, sourceDir, filename, targetDir, targetName):
  50. if (not self.fa.exists(targetDir, True)):
  51. self.fa.xInterface.createFolder(targetDir)
  52. self.executeCommand(self.ucb, "globalTransfer", self.copyArg(sourceDir, filename, targetDir, targetName))
  53. # target name can be PropertyNames.EMPTY_STRING, in which case the name stays lige the source name
  54. # @param sourceDir
  55. # @param sourceFilename
  56. # @param targetDir
  57. # @param targetFilename
  58. # @return
  59. def copyArg(self, sourceDir, sourceFilename, targetDir, targetFilename):
  60. aArg = GlobalTransferCommandArgument()
  61. aArg.Operation = COPY
  62. aArg.SourceURL = self.fa.getURL(sourceDir, sourceFilename)
  63. aArg.TargetURL = targetDir
  64. aArg.NewTitle = targetFilename
  65. # fail, if object with same name exists in target folder
  66. aArg.NameClash = OVERWRITE
  67. return aArg
  68. def executeCommand(self, xContent, aCommandName, aArgument):
  69. aCommand = Command()
  70. aCommand.Name = aCommandName
  71. aCommand.Handle = -1 # not available
  72. aCommand.Argument = aArgument
  73. return xContent.execute(aCommand, 0, None)
  74. def listFiles(self, path, verifier):
  75. xContent = self.getContent(path)
  76. aArg = OpenCommandArgument2()
  77. aArg.Mode = ALL
  78. aArg.Priority = 32768
  79. # Fill info for the properties wanted.
  80. aArg.Properties = (Property(),)
  81. aArg.Properties[0].Name = "Title"
  82. aArg.Properties[0].Handle = -1
  83. xSet = self.executeCommand(xContent, "open", aArg)
  84. xResultSet = xSet.getStaticResultSet()
  85. files = []
  86. if (xResultSet.first()):
  87. # obtain XContentAccess interface for child content access and XRow for properties
  88. while (True):
  89. # Obtain URL of child.
  90. if (hasattr(xResultSet, "queryContentIdentifierString")):
  91. aId = xResultSet.queryContentIdentifierString()
  92. aTitle = FileAccess.getFilename(aId)
  93. elif (hasattr(xResultSet, "getString")):
  94. # First column: Title (column numbers are 1-based!)
  95. aTitle = xResultSet.getString(1)
  96. else:
  97. aTitle = ""
  98. #if (len(aTitle) == 0 and xResultSet.wasNull()):
  99. if (len(aTitle) == 0):
  100. # ignore
  101. pass
  102. else:
  103. files.append(aTitle)
  104. if (not xResultSet.next()):
  105. break
  106. # next child
  107. if (verifier is not None):
  108. for i in range(len(files)):
  109. if (not verifier.verify(files[i])):
  110. files.pop(i) # FIXME !!! dangerous
  111. return files
  112. def getContent(self, path):
  113. try:
  114. ident = self.ucb.createContentIdentifier(path)
  115. return self.ucb.queryContent(ident)
  116. except Exception:
  117. traceback.print_exc()
  118. return None
  119. class Verifier:
  120. @abstractmethod
  121. def verify(object):
  122. pass