1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.oc_framework
.operations
.remote
;
22 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DavMethodBase
;
24 import android
.util
.Log
;
26 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
;
27 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavUtils
;
28 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
;
29 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
;
30 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
.ResultCode
;
31 import com
.owncloud
.android
.oc_framework
.utils
.FileUtils
;
35 * Remote operation performing the rename of a remote file or folder in the ownCloud server.
37 * @author David A. Velasco
40 public class RenameRemoteFileOperation
extends RemoteOperation
{
42 private static final String TAG
= RenameRemoteFileOperation
.class.getSimpleName();
44 private static final int RENAME_READ_TIMEOUT
= 10000;
45 private static final int RENAME_CONNECTION_TIMEOUT
= 5000;
47 private String mOldName
;
48 private String mOldRemotePath
;
49 private String mNewName
;
50 private String mNewRemotePath
;
56 * @param oldName Old name of the file.
57 * @param oldRemotePath Old remote path of the file.
58 * @param newName New name to set as the name of file.
59 * @param isFolder 'true' for folder and 'false' for files
61 public RenameRemoteFileOperation(String oldName
, String oldRemotePath
, String newName
, boolean isFolder
) {
63 mOldRemotePath
= oldRemotePath
;
66 String parent
= (new File(mOldRemotePath
)).getParent();
67 parent
= (parent
.endsWith(FileUtils
.PATH_SEPARATOR
)) ? parent
: parent
+ FileUtils
.PATH_SEPARATOR
;
68 mNewRemotePath
= parent
+ mNewName
;
70 mNewRemotePath
+= FileUtils
.PATH_SEPARATOR
;
75 * Performs the rename operation.
77 * @param client Client object to communicate with the remote ownCloud server.
80 protected RemoteOperationResult
run(WebdavClient client
) {
81 RemoteOperationResult result
= null
;
83 LocalMoveMethod move
= null
;
85 boolean noInvalidChars
= FileUtils
.isValidPath(mNewRemotePath
);
90 if (mNewName
.equals(mOldName
)) {
91 return new RemoteOperationResult(ResultCode
.OK
);
95 // check if a file with the new name already exists
96 if (client
.existsFile(mNewRemotePath
)) {
97 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
100 move
= new LocalMoveMethod( client
.getBaseUri() + WebdavUtils
.encodePath(mOldRemotePath
),
101 client
.getBaseUri() + WebdavUtils
.encodePath(mNewRemotePath
));
102 int status
= client
.executeMethod(move
, RENAME_READ_TIMEOUT
, RENAME_CONNECTION_TIMEOUT
);
104 move
.getResponseBodyAsString(); // exhaust response, although not interesting
105 result
= new RemoteOperationResult(move
.succeeded(), status
, move
.getResponseHeaders());
106 Log
.i(TAG
, "Rename " + mOldRemotePath
+ " to " + mNewRemotePath
+ ": " + result
.getLogMessage());
108 } catch (Exception e
) {
109 result
= new RemoteOperationResult(e
);
110 Log
.e(TAG
, "Rename " + mOldRemotePath
+ " to " + ((mNewRemotePath
==null
) ? mNewName
: mNewRemotePath
) + ": " + result
.getLogMessage(), e
);
114 move
.releaseConnection();
117 result
= new RemoteOperationResult(ResultCode
.INVALID_CHARACTER_IN_NAME
);
127 private class LocalMoveMethod
extends DavMethodBase
{
129 public LocalMoveMethod(String uri
, String dest
) {
131 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
135 public String
getName() {
140 protected boolean isSuccess(int status
) {
141 return status
== 201 || status
== 204;