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