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