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 18import uno 19import traceback 20 21from abc import abstractmethod 22 23from ..common.FileAccess import FileAccess 24 25from com.sun.star.beans import Property 26 27from com.sun.star.ucb import Command 28from com.sun.star.ucb import GlobalTransferCommandArgument 29from com.sun.star.ucb.NameClash import OVERWRITE 30from com.sun.star.ucb import OpenCommandArgument2 31from com.sun.star.ucb.OpenMode import ALL 32from com.sun.star.ucb.TransferCommandOperation import COPY 33 34 35# This class is used to copy the content of a folder to 36# another folder. 37# There is an inconsistency with argument order. 38# It should be always: dir,filename. 39class UCB(object): 40 41 ucb = None 42 fa = None 43 xmsf = None 44 45 def __init__(self, xmsf): 46 self.ucb = xmsf.createInstanceWithArguments("com.sun.star.ucb.UniversalContentBroker", ()) 47 self.fa = FileAccess(xmsf) 48 self.xmsf = xmsf 49 50 def delete(self, filename): 51 # System.out.println("UCB.delete(" + filename) 52 self.executeCommand(self.getContent(filename),"delete", True) 53 54 def copy(self, sourceDir, targetDir): 55 self.copy1(sourceDir,targetDir, None) 56 57 def copy1(self, sourceDir, targetDir, verifier): 58 files = self.listFiles(sourceDir, verifier) 59 for i in range(len(files)): 60 self.copy2(sourceDir, files[i], targetDir, "") 61 62 def copy2(self, sourceDir, filename, targetDir, targetName): 63 if (not self.fa.exists(targetDir, True)): 64 self.fa.xInterface.createFolder(targetDir) 65 self.executeCommand(self.ucb, "globalTransfer", self.copyArg(sourceDir, filename, targetDir, targetName)) 66 67 # target name can be PropertyNames.EMPTY_STRING, in which case the name stays lige the source name 68 # @param sourceDir 69 # @param sourceFilename 70 # @param targetDir 71 # @param targetFilename 72 # @return 73 def copyArg(self, sourceDir, sourceFilename, targetDir, targetFilename): 74 aArg = GlobalTransferCommandArgument() 75 aArg.Operation = COPY 76 aArg.SourceURL = self.fa.getURL(sourceDir, sourceFilename) 77 aArg.TargetURL = targetDir 78 aArg.NewTitle = targetFilename 79 # fail, if object with same name exists in target folder 80 aArg.NameClash = OVERWRITE 81 return aArg 82 83 def executeCommand(self, xContent, aCommandName, aArgument): 84 aCommand = Command() 85 aCommand.Name = aCommandName 86 aCommand.Handle = -1 # not available 87 aCommand.Argument = aArgument 88 return xContent.execute(aCommand, 0, None) 89 90 def listFiles(self, path, verifier): 91 xContent = self.getContent(path) 92 93 aArg = OpenCommandArgument2() 94 aArg.Mode = ALL 95 aArg.Priority = 32768 96 97 # Fill info for the properties wanted. 98 aArg.Properties = (Property(),) 99 100 aArg.Properties[0].Name = "Title" 101 aArg.Properties[0].Handle = -1 102 103 xSet = self.executeCommand(xContent, "open", aArg) 104 105 xResultSet = xSet.getStaticResultSet() 106 107 files = [] 108 109 if (xResultSet.first()): 110 # obtain XContentAccess interface for child content access and XRow for properties 111 while (True): 112 # Obtain URL of child. 113 if (hasattr(xResultSet, "queryContentIdentifierString")): 114 aId = xResultSet.queryContentIdentifierString() 115 aTitle = FileAccess.getFilename(aId) 116 elif (hasattr(xResultSet, "getString")): 117 # First column: Title (column numbers are 1-based!) 118 aTitle = xResultSet.getString(1) 119 else: 120 aTitle = "" 121 #if (len(aTitle) == 0 and xResultSet.wasNull()): 122 if (len(aTitle) == 0): 123 # ignore 124 pass 125 else: 126 files.append(aTitle) 127 if (not xResultSet.next()): 128 break 129 # next child 130 if (verifier is not None): 131 for i in range(len(files)): 132 if (not verifier.verify(files[i])): 133 files.pop(i) # FIXME !!! dangerous 134 return files 135 136 def getContent(self, path): 137 try: 138 ident = self.ucb.createContentIdentifier(path) 139 return self.ucb.queryContent(ident) 140 except Exception: 141 traceback.print_exc() 142 return None 143 144 class Verifier: 145 @abstractmethod 146 def verify(object): 147 pass 148 149