1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.operations
;
22 import java
.io
.IOException
;
24 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DavMethodBase
;
25 //import org.apache.jackrabbit.webdav.client.methods.MoveMethod;
27 import android
.util
.Log
;
29 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
30 import com
.owncloud
.android
.datamodel
.OCFile
;
31 import com
.owncloud
.android
.files
.services
.FileDownloader
;
32 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
34 import eu
.alefzero
.webdav
.WebdavClient
;
35 import eu
.alefzero
.webdav
.WebdavUtils
;
38 * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
40 * @author David A. Velasco
42 public class RenameFileOperation
extends RemoteOperation
{
44 private static final String TAG
= RemoveFileOperation
.class.getSimpleName();
46 private static final int RENAME_READ_TIMEOUT
= 10000;
47 private static final int RENAME_CONNECTION_TIMEOUT
= 5000;
51 private String mNewName
;
52 private DataStorageManager mStorageManager
;
58 * @param file OCFile instance describing the remote file or folder to rename
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.
62 public RenameFileOperation(OCFile file
, String newName
, DataStorageManager storageManager
) {
65 mStorageManager
= storageManager
;
68 public OCFile
getFile() {
74 * Performs the rename operation.
76 * @param client Client object to communicate with the remote ownCloud server.
79 protected RemoteOperationResult
run(WebdavClient client
) {
80 RemoteOperationResult result
= null
;
82 LocalMoveMethod move
= null
;
83 String newRemotePath
= null
;
85 if (mNewName
.equals(mFile
.getFileName())) {
86 return new RemoteOperationResult(ResultCode
.OK
);
89 newRemotePath
= (new File(mFile
.getRemotePath())).getParent() + mNewName
;
91 // check if the new name is valid in the local file system
92 if (!isValidNewName()) {
93 return new RemoteOperationResult(ResultCode
.INVALID_LOCAL_FILE_NAME
);
96 // check if a file with the new name already exists
97 if (client
.existsFile(newRemotePath
) || // remote check could fail by network failure, or by indeterminate behavior of HEAD for folders ...
98 mStorageManager
.getFileByPath(newRemotePath
) != null
) { // ... so local check is convenient
99 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
101 move
= new LocalMoveMethod( client
.getBaseUri() + WebdavUtils
.encodePath(mFile
.getRemotePath()),
102 client
.getBaseUri() + WebdavUtils
.encodePath(newRemotePath
));
103 int status
= client
.executeMethod(move
, RENAME_READ_TIMEOUT
, RENAME_CONNECTION_TIMEOUT
);
104 if (move
.succeeded()) {
106 // create new OCFile instance for the renamed file
107 /*OCFile newFile = new OCFile(mStorageManager.getFileById(mFile.getParentId()).getRemotePath() + mNewName; // TODO - NOT CREATE NEW OCFILE; ADD setFileName METHOD
108 OCFile oldFile = mFile;
110 mFile
.setFileName(mNewName
);
112 // try to rename the local copy of the file
113 if (mFile
.isDown()) {
114 File f
= new File(mFile
.getStoragePath());
115 String newStoragePath
= f
.getParent() + mNewName
;
116 if (f
.renameTo(new File(newStoragePath
))) {
117 mFile
.setStoragePath(newStoragePath
);
119 // else - NOTHING: the link to the local file is kept although the local name can't be updated
120 // TODO - study conditions when this could be a problem
123 //mStorageManager.removeFile(oldFile, false);
124 mStorageManager
.saveFile(mFile
);
127 *} else if (mFile.isDirectory() && (status == 207 || status >= 500)) {
129 * // if server fails in the rename of a folder, some children files could have been moved to a folder with the new name while some others
130 * // stayed in the old folder;
132 * // easiest and heaviest solution is synchronizing the parent folder (or the full account);
134 * // a better solution is synchronizing the folders with the old and new names;
140 move
.getResponseBodyAsString(); // exhaust response, although not interesting
141 result
= new RemoteOperationResult(move
.succeeded(), status
);
142 Log
.i(TAG
, "Rename " + mFile
.getRemotePath() + " to " + newRemotePath
+ ": " + result
.getLogMessage());
144 } catch (Exception e
) {
145 result
= new RemoteOperationResult(e
);
146 Log
.e(TAG
, "Rename " + mFile
.getRemotePath() + " to " + ((newRemotePath
==null
) ? mNewName
: newRemotePath
) + ": " + result
.getLogMessage(), e
);
150 move
.releaseConnection();
157 * Checks if the new name to set is valid in the file system
159 * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
160 * for downloads, out of any account, and then removed.
162 * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
163 * could be formatted with a different file system.
165 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities specific for the interactions with the file system
167 * @return 'True' if a temporal file named with the name to set could be created in the file system where
168 * local files are stored.
170 private boolean isValidNewName() {
171 // check tricky names
172 if (mNewName
== null
|| mNewName
.length() <= 0 || mNewName
.contains(File
.separator
) || mNewName
.contains("%")) {
175 // create a test file
176 String tmpFolder
= FileDownloader
.getTemporalPath("");
177 File testFile
= new File(tmpFolder
+ mNewName
);
179 testFile
.createNewFile(); // return value is ignored; it could be 'false' because the file already existed, that doesn't invalidate the name
180 } catch (IOException e
) {
181 Log
.i(TAG
, "Test for validity of name " + mNewName
+ " in the file system failed");
184 boolean result
= (testFile
.exists() && testFile
.isFile());
186 // cleaning ; result is ignored, since there is not much we could do in case of failure, but repeat and repeat...
194 // move operation - TODO: find out why org.apache.jackrabbit.webdav.client.methods.MoveMethod is not used instead ¿?
195 private class LocalMoveMethod
extends DavMethodBase
{
197 public LocalMoveMethod(String uri
, String dest
) {
199 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
203 public String
getName() {
208 protected boolean isSuccess(int status
) {
209 return status
== 201 || status
== 204;