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