1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.operations
;
21 import java
.io
.IOException
;
23 import com
.owncloud
.android
.datamodel
.OCFile
;
24 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
25 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
26 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
27 import com
.owncloud
.android
.lib
.resources
.files
.MoveRemoteFileOperation
;
28 import com
.owncloud
.android
.lib
.resources
.files
.RenameRemoteFileOperation
;
29 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
30 import com
.owncloud
.android
.utils
.FileStorageUtils
;
31 import com
.owncloud
.android
.utils
.Log_OC
;
33 import android
.accounts
.Account
;
37 * Operation mmoving an {@link OCFile} to a different folder.
39 * @author David A. Velasco
41 public class MoveFileOperation
extends SyncOperation
{
43 private static final String TAG
= MoveFileOperation
.class.getSimpleName();
46 private Account mAccount
;
47 private String mNewParentPath
;
49 private String mNewPath
;
56 * @param path Remote path of the {@link OCFile} to move.
57 * @param newParentPath Path to the folder where the file will be moved into.
58 * @param account OwnCloud account containing both the file and the target folder
60 public MoveFileOperation(String path
, String newParentPath
, Account account
) {
62 mNewParentPath
= newParentPath
;
69 public OCFile
getFile() {
75 * Performs the operation.
77 * @param client Client object to communicate with the remote ownCloud server.
80 protected RemoteOperationResult
run(OwnCloudClient client
) {
81 RemoteOperationResult result
= null
;
84 mFile
= getStorageManager().getFileByPath(mPath
);
87 /// 1. check move validity
91 MoveRemoteFileOperation operation
= new MoveRemoteFileOperation(
92 mFile
.getRemotePath(),
96 result
= operation
.execute(client
);
100 if (result
.isSuccess()) {
103 if (mFile.isFolder()) {
104 saveLocalDirectory();
113 String parentPath
= (new File(mFile
.getRemotePath())).getParent();
114 parentPath
= (parentPath
.endsWith(OCFile
.PATH_SEPARATOR
))
116 : parentPath
+ OCFile
.PATH_SEPARATOR
;
118 mNewPath
= mNewParentPath
+ mFile
.getFileName();
119 if (mFile
.isFolder() && !mNewPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
120 mNewPath
+= OCFile
.PATH_SEPARATOR
;
123 // check local overwrite
124 if (getStorageManager().getFileByPath(mPath
) != null
) {
125 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
129 } catch (IOException e) {
130 Log_OC.e(TAG, "Move " + mFile.getRemotePath() + " to " +
131 (mNewParentPath==null) + ": " +
132 ((result!= null) ? result.getLogMessage() : ""), e);*/
141 private void saveLocalDirectory() {
142 getStorageManager().moveFolder(mFile
, mNewPath
);
143 String localPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
144 File localDir
= new File(localPath
);
145 if (localDir
.exists()) {
146 localDir
.renameTo(new File(FileStorageUtils
.getSavePath(mAccount
.name
) + mNewPath
));
147 // TODO - if renameTo fails, children files that are already down will result unlinked
151 private void saveLocalFile() {
153 mFile.setRFileName(mNewName); <<< NO >
155 // try to move the local copy of the file
156 if (mFile.isDown()) {
157 File f = new File(mFile.getStoragePath());
158 String parentStoragePath = f.getParent();
159 if (!parentStoragePath.endsWith(File.separator))
160 parentStoragePath += File.separator;
161 if (f.renameTo(new File())) {
162 mFile.setStoragePath(parentStoragePath + mNewName);
164 // else - NOTHING: the link to the local file is kept although the local name can't be updated
165 // TODO - study conditions when this could be a problem
168 getStorageManager().saveFile(mFile);
173 * Checks if the new name to set is valid in the file system
175 * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
176 * for downloads, out of any account, and then removed.
178 * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
179 * could be formatted with a different file system.
181 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities specific for the interactions with the file system
183 * @return 'True' if a temporal file named with the name to set could be created in the file system where
184 * local files are stored.
185 * @throws IOException When the temporal folder can not be created.
187 private boolean isValidNewName() throws IOException
{
191 // check tricky names
192 if (mNewName == null || mNewName.length() <= 0 || mNewName.contains(File.separator) || mNewName.contains("%")) {
195 // create a test file
196 String tmpFolderName = FileStorageUtils.getTemporalPath("");
197 File testFile = new File(tmpFolderName + mNewName);
198 File tmpFolder = testFile.getParentFile();
200 if (!tmpFolder.isDirectory()) {
201 throw new IOException("Unexpected error: temporal directory could not be created");
204 testFile.createNewFile(); // return value is ignored; it could be 'false' because the file already existed, that doesn't invalidate the name
205 } catch (IOException e) {
206 Log_OC.i(TAG, "Test for validity of name " + mNewName + " in the file system failed");
209 boolean result = (testFile.exists() && testFile.isFile());
211 // cleaning ; result is ignored, since there is not much we could do in case of failure, but repeat and repeat...