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 com
.owncloud
.android
.Log_OC
;
24 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
25 import com
.owncloud
.android
.datamodel
.OCFile
;
27 import eu
.alefzero
.webdav
.WebdavClient
;
28 import eu
.alefzero
.webdav
.WebdavUtils
;
31 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
33 * @author David A. Velasco
35 public class RemoveFileOperation
extends RemoteOperation
{
37 private static final String TAG
= RemoveFileOperation
.class.getSimpleName();
39 private static final int REMOVE_READ_TIMEOUT
= 10000;
40 private static final int REMOVE_CONNECTION_TIMEOUT
= 5000;
43 boolean mDeleteLocalCopy
;
44 DataStorageManager mDataStorageManager
;
50 * @param fileToRemove OCFile instance describing the remote file or folder to remove from the server
51 * @param deleteLocalCopy When 'true', and a local copy of the file exists, it is also removed.
52 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
54 public RemoveFileOperation(OCFile fileToRemove
, boolean deleteLocalCopy
, DataStorageManager storageManager
) {
55 mFileToRemove
= fileToRemove
;
56 mDeleteLocalCopy
= deleteLocalCopy
;
57 mDataStorageManager
= storageManager
;
62 * Getter for the file to remove (or removed, if the operation was successfully performed).
64 * @return File to remove or already removed.
66 public OCFile
getFile() {
72 * Performs the remove operation
74 * @param client Client object to communicate with the remote ownCloud server.
77 protected RemoteOperationResult
run(WebdavClient client
) {
78 RemoteOperationResult result
= null
;
79 DeleteMethod delete
= null
;
81 delete
= new DeleteMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mFileToRemove
.getRemotePath()));
82 int status
= client
.executeMethod(delete
, REMOVE_READ_TIMEOUT
, REMOVE_CONNECTION_TIMEOUT
);
83 if (delete
.succeeded() || status
== HttpStatus
.SC_NOT_FOUND
) {
84 if (mFileToRemove
.isDirectory()) {
85 mDataStorageManager
.removeDirectory(mFileToRemove
, true
, mDeleteLocalCopy
);
87 mDataStorageManager
.removeFile(mFileToRemove
, mDeleteLocalCopy
);
90 delete
.getResponseBodyAsString(); // exhaust the response, although not interesting
91 result
= new RemoteOperationResult((delete
.succeeded() || status
== HttpStatus
.SC_NOT_FOUND
), status
, delete
.getResponseHeaders());
92 Log_OC
.i(TAG
, "Remove " + mFileToRemove
.getRemotePath() + ": " + result
.getLogMessage());
94 } catch (Exception e
) {
95 result
= new RemoteOperationResult(e
);
96 Log_OC
.e(TAG
, "Remove " + mFileToRemove
.getRemotePath() + ": " + result
.getLogMessage(), e
);
100 delete
.releaseConnection();