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