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
.operations
;
20 import org
.apache
.commons
.httpclient
.HttpStatus
;
21 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DeleteMethod
;
23 import android
.util
.Log
;
25 import com
.owncloud
.android
.Log_OC
;
26 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
27 import com
.owncloud
.android
.datamodel
.OCFile
;
29 import eu
.alefzero
.webdav
.WebdavClient
;
30 import eu
.alefzero
.webdav
.WebdavUtils
;
33 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
35 * @author David A. Velasco
37 public class RemoveFileOperation
extends RemoteOperation
{
39 private static final String TAG
= RemoveFileOperation
.class.getSimpleName();
41 private static final int REMOVE_READ_TIMEOUT
= 10000;
42 private static final int REMOVE_CONNECTION_TIMEOUT
= 5000;
45 boolean mDeleteLocalCopy
;
46 DataStorageManager mDataStorageManager
;
52 * @param fileToRemove OCFile instance describing the remote file or folder to remove from the server
53 * @param deleteLocalCopy When 'true', and a local copy of the file exists, it is also removed.
54 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
56 public RemoveFileOperation(OCFile fileToRemove
, boolean deleteLocalCopy
, DataStorageManager storageManager
) {
57 mFileToRemove
= fileToRemove
;
58 mDeleteLocalCopy
= deleteLocalCopy
;
59 mDataStorageManager
= storageManager
;
64 * Getter for the file to remove (or removed, if the operation was successfully performed).
66 * @return File to remove or already removed.
68 public OCFile
getFile() {
74 * Performs the remove operation
76 * @param client Client object to communicate with the remote ownCloud server.
79 protected RemoteOperationResult
run(WebdavClient client
) {
80 RemoteOperationResult result
= null
;
81 DeleteMethod delete
= null
;
83 delete
= new DeleteMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mFileToRemove
.getRemotePath()));
84 int status
= client
.executeMethod(delete
, REMOVE_READ_TIMEOUT
, REMOVE_CONNECTION_TIMEOUT
);
85 if (delete
.succeeded() || status
== HttpStatus
.SC_NOT_FOUND
) {
86 if (mFileToRemove
.isDirectory()) {
87 mDataStorageManager
.removeDirectory(mFileToRemove
, true
, mDeleteLocalCopy
);
89 mDataStorageManager
.removeFile(mFileToRemove
, mDeleteLocalCopy
);
92 delete
.getResponseBodyAsString(); // exhaust the response, although not interesting
93 result
= new RemoteOperationResult((delete
.succeeded() || status
== HttpStatus
.SC_NOT_FOUND
), status
);
94 Log_OC
.i(TAG
, "Remove " + mFileToRemove
.getRemotePath() + ": " + result
.getLogMessage());
96 } catch (Exception e
) {
97 result
= new RemoteOperationResult(e
);
98 Log_OC
.e(TAG
, "Remove " + mFileToRemove
.getRemotePath() + ": " + result
.getLogMessage(), e
);
102 delete
.releaseConnection();