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
.RenameRemoteFileOperation
;
28 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
29 import com
.owncloud
.android
.utils
.FileStorageUtils
;
30 import com
.owncloud
.android
.utils
.Log_OC
;
32 import android
.accounts
.Account
;
36 * Operation mmoving an {@link OCFile} to a different folder.
38 * @author David A. Velasco
40 public class MoveFileOperation
extends SyncOperation
{
42 private static final String TAG
= MoveFileOperation
.class.getSimpleName();
45 private Account mAccount
;
46 private String mNewParentPath
;
54 * @param path Remote path of the {@link OCFile} to move.
55 * @param newParentPath Path to the folder where the file will be moved into.
56 * @param account OwnCloud account containing both the file and the target folder
58 public MoveFileOperation(String path
, String newParentPath
, Account account
) {
60 mNewParentPath
= newParentPath
;
65 public OCFile
getFile() {
71 * Performs the operation.
73 * @param client Client object to communicate with the remote ownCloud server.
76 protected RemoteOperationResult
run(OwnCloudClient client
) {
77 RemoteOperationResult result
= null
;
79 mFile
= getStorageManager().getFileByPath(mPath
);
81 // check if the new name is valid in the local file system
83 String parentPath
= (new File(mFile
.getRemotePath())).getParent();
84 parentPath
= (parentPath
.endsWith(OCFile
.PATH_SEPARATOR
))
86 : parentPath
+ OCFile
.PATH_SEPARATOR
;
88 String mNewPath
= mNewParentPath
+ mFile
.getFileName();
89 if (mFile
.isFolder() && !mNewPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
90 mNewPath
+= OCFile
.PATH_SEPARATOR
;
93 // check local overwrite
94 if (getStorageManager().getFileByPath(mPath
) != null
) {
95 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
98 MoveRemoteFileOperation operation
= new MoveRemoteFileOperation(
99 mFile
.getRemotePath(),
103 result
= operation
.execute(client
);
105 if (result
.isSuccess()) {
106 if (mFile
.isFolder()) {
107 saveLocalDirectory();
114 } catch (IOException e
) {
115 Log_OC
.e(TAG
, "Move " + mFile
.getRemotePath() + " to " +
116 (mNewParentPath
==null
) + ": " +
117 ((result
!= null
) ? result
.getLogMessage() : ""), e
);
124 private void saveLocalDirectory() {
125 getStorageManager().moveFolder(mFile
, mNewParentPath
);
126 String localPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
127 File localDir
= new File(localPath
);
128 if (localDir
.exists()) {
129 localDir
.renameTo(new File(FileStorageUtils
.getSavePath(mAccount
.name
) + mNewParentPath
));
130 // TODO - if renameTo fails, children files that are already down will result unlinked
134 private void saveLocalFile() {
135 mFile
.setFileName(mNewName
);
137 // try to rename the local copy of the file
138 if (mFile
.isDown()) {
139 File f
= new File(mFile
.getStoragePath());
140 String parentStoragePath
= f
.getParent();
141 if (!parentStoragePath
.endsWith(File
.separator
))
142 parentStoragePath
+= File
.separator
;
143 if (f
.renameTo(new File(parentStoragePath
+ mNewName
))) {
144 mFile
.setStoragePath(parentStoragePath
+ mNewName
);
146 // else - NOTHING: the link to the local file is kept although the local name can't be updated
147 // TODO - study conditions when this could be a problem
150 getStorageManager().saveFile(mFile
);
154 * Checks if the new name to set is valid in the file system
156 * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
157 * for downloads, out of any account, and then removed.
159 * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
160 * could be formatted with a different file system.
162 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities specific for the interactions with the file system
164 * @return 'True' if a temporal file named with the name to set could be created in the file system where
165 * local files are stored.
166 * @throws IOException When the temporal folder can not be created.
168 private boolean isValidNewName() throws IOException
{
169 // check tricky names
170 if (mNewName
== null
|| mNewName
.length() <= 0 || mNewName
.contains(File
.separator
) || mNewName
.contains("%")) {
173 // create a test file
174 String tmpFolderName
= FileStorageUtils
.getTemporalPath("");
175 File testFile
= new File(tmpFolderName
+ mNewName
);
176 File tmpFolder
= testFile
.getParentFile();
178 if (!tmpFolder
.isDirectory()) {
179 throw new IOException("Unexpected error: temporal directory could not be created");
182 testFile
.createNewFile(); // return value is ignored; it could be 'false' because the file already existed, that doesn't invalidate the name
183 } catch (IOException e
) {
184 Log_OC
.i(TAG
, "Test for validity of name " + mNewName
+ " in the file system failed");
187 boolean result
= (testFile
.exists() && testFile
.isFile());
189 // cleaning ; result is ignored, since there is not much we could do in case of failure, but repeat and repeat...