Refactoring: removed old FileDataStorageManager#moveFolder method, redirected to...
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RenameFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 import java.io.File;
21 import java.io.IOException;
22
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.common.utils.Log_OC;
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
32 import android.accounts.Account;
33
34
35 /**
36 * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
37 *
38 * @author David A. Velasco
39 * @author masensio
40 */
41 public class RenameFileOperation extends SyncOperation {
42
43 private static final String TAG = RenameFileOperation.class.getSimpleName();
44
45 private OCFile mFile;
46 private String mRemotePath;
47 private String mNewName;
48 private String mNewRemotePath;
49
50
51
52 /**
53 * Constructor
54 *
55 * @param remotePath RemotePath of the OCFile instance describing the remote file or folder to rename
56 * @param account OwnCloud account containing the remote file
57 * @param newName New name to set as the name of file.
58 */
59 public RenameFileOperation(String remotePath, String newName) {
60 mRemotePath = remotePath;
61 mNewName = newName;
62 mNewRemotePath = null;
63 }
64
65 public OCFile getFile() {
66 return mFile;
67 }
68
69
70 /**
71 * Performs the rename operation.
72 *
73 * @param client Client object to communicate with the remote ownCloud server.
74 */
75 @Override
76 protected RemoteOperationResult run(OwnCloudClient client) {
77 RemoteOperationResult result = null;
78
79 mFile = getStorageManager().getFileByPath(mRemotePath);
80
81 // check if the new name is valid in the local file system
82 try {
83 if (!isValidNewName()) {
84 return new RemoteOperationResult(ResultCode.INVALID_LOCAL_FILE_NAME);
85 }
86 String parent = (new File(mFile.getRemotePath())).getParent();
87 parent = (parent.endsWith(OCFile.PATH_SEPARATOR)) ? parent : parent + OCFile.PATH_SEPARATOR;
88 mNewRemotePath = parent + mNewName;
89 if (mFile.isFolder()) {
90 mNewRemotePath += OCFile.PATH_SEPARATOR;
91 }
92
93 // check local overwrite
94 if (getStorageManager().getFileByPath(mNewRemotePath) != null) {
95 return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
96 }
97
98 RenameRemoteFileOperation operation = new RenameRemoteFileOperation(mFile.getFileName(), mFile.getRemotePath(),
99 mNewName, mFile.isFolder());
100 result = operation.execute(client);
101
102 if (result.isSuccess()) {
103 if (mFile.isFolder()) {
104 getStorageManager().moveLocalFile(mFile, mNewRemotePath, parent);
105 //saveLocalDirectory();
106
107 } else {
108 saveLocalFile();
109 }
110 }
111
112 } catch (IOException e) {
113 Log_OC.e(TAG, "Rename " + mFile.getRemotePath() + " to " + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " +
114 ((result!= null) ? result.getLogMessage() : ""), e);
115 }
116
117 return result;
118 }
119
120 private void saveLocalFile() {
121 mFile.setFileName(mNewName);
122
123 // try to rename the local copy of the file
124 if (mFile.isDown()) {
125 String oldPath = mFile.getStoragePath();
126 File f = new File(oldPath);
127 String parentStoragePath = f.getParent();
128 if (!parentStoragePath.endsWith(File.separator))
129 parentStoragePath += File.separator;
130 if (f.renameTo(new File(parentStoragePath + mNewName))) {
131 String newPath = parentStoragePath + mNewName;
132 mFile.setStoragePath(newPath);
133
134 // notify MediaScanner about removed file - TODO really works?
135 getStorageManager().triggerMediaScan(oldPath);
136 // notify to scan about new file
137 getStorageManager().triggerMediaScan(newPath);
138 }
139 // else - NOTHING: the link to the local file is kept although the local name can't be updated
140 // TODO - study conditions when this could be a problem
141 }
142
143 getStorageManager().saveFile(mFile);
144 }
145
146 /**
147 * Checks if the new name to set is valid in the file system
148 *
149 * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
150 * for downloads, out of any account, and then removed.
151 *
152 * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
153 * could be formatted with a different file system.
154 *
155 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities specific for the interactions with the file system
156 *
157 * @return 'True' if a temporal file named with the name to set could be created in the file system where
158 * local files are stored.
159 * @throws IOException When the temporal folder can not be created.
160 */
161 private boolean isValidNewName() throws IOException {
162 // check tricky names
163 if (mNewName == null || mNewName.length() <= 0 || mNewName.contains(File.separator) || mNewName.contains("%")) {
164 return false;
165 }
166 // create a test file
167 String tmpFolderName = FileStorageUtils.getTemporalPath("");
168 File testFile = new File(tmpFolderName + mNewName);
169 File tmpFolder = testFile.getParentFile();
170 tmpFolder.mkdirs();
171 if (!tmpFolder.isDirectory()) {
172 throw new IOException("Unexpected error: temporal directory could not be created");
173 }
174 try {
175 testFile.createNewFile(); // return value is ignored; it could be 'false' because the file already existed, that doesn't invalidate the name
176 } catch (IOException e) {
177 Log_OC.i(TAG, "Test for validity of name " + mNewName + " in the file system failed");
178 return false;
179 }
180 boolean result = (testFile.exists() && testFile.isFile());
181
182 // cleaning ; result is ignored, since there is not much we could do in case of failure, but repeat and repeat...
183 testFile.delete();
184
185 return result;
186 }
187
188 }