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 as published by
6 * the Free Software Foundation, either version 2 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
;
21 import org
.apache
.commons
.httpclient
.HttpStatus
;
22 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DeleteMethod
;
24 import android
.util
.Log
;
26 import com
.owncloud
.android
.Log_OC
;
27 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
28 import com
.owncloud
.android
.datamodel
.OCFile
;
30 import eu
.alefzero
.webdav
.WebdavClient
;
31 import eu
.alefzero
.webdav
.WebdavUtils
;
34 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
36 * @author David A. Velasco
38 public class RemoveFileOperation
extends RemoteOperation
{
40 private static final String TAG
= RemoveFileOperation
.class.getSimpleName();
42 private static final int REMOVE_READ_TIMEOUT
= 10000;
43 private static final int REMOVE_CONNECTION_TIMEOUT
= 5000;
46 boolean mDeleteLocalCopy
;
47 DataStorageManager mDataStorageManager
;
53 * @param fileToRemove OCFile instance describing the remote file or folder to remove from the server
54 * @param deleteLocalCopy When 'true', and a local copy of the file exists, it is also removed.
55 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
57 public RemoveFileOperation(OCFile fileToRemove
, boolean deleteLocalCopy
, DataStorageManager storageManager
) {
58 mFileToRemove
= fileToRemove
;
59 mDeleteLocalCopy
= deleteLocalCopy
;
60 mDataStorageManager
= storageManager
;
65 * Getter for the file to remove (or removed, if the operation was successfully performed).
67 * @return File to remove or already removed.
69 public OCFile
getFile() {
75 * Performs the remove operation
77 * @param client Client object to communicate with the remote ownCloud server.
80 protected RemoteOperationResult
run(WebdavClient client
) {
81 RemoteOperationResult result
= null
;
82 DeleteMethod delete
= null
;
84 delete
= new DeleteMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mFileToRemove
.getRemotePath()));
85 int status
= client
.executeMethod(delete
, REMOVE_READ_TIMEOUT
, REMOVE_CONNECTION_TIMEOUT
);
86 if (delete
.succeeded() || status
== HttpStatus
.SC_NOT_FOUND
) {
87 if (mFileToRemove
.isDirectory()) {
88 mDataStorageManager
.removeDirectory(mFileToRemove
, true
, mDeleteLocalCopy
);
90 mDataStorageManager
.removeFile(mFileToRemove
, mDeleteLocalCopy
);
93 delete
.getResponseBodyAsString(); // exhaust the response, although not interesting
94 result
= new RemoteOperationResult((delete
.succeeded() || status
== HttpStatus
.SC_NOT_FOUND
), status
);
95 Log_OC
.i(TAG
, "Remove " + mFileToRemove
.getRemotePath() + ": " + result
.getLogMessage());
97 } catch (Exception e
) {
98 result
= new RemoteOperationResult(e
);
99 Log_OC
.e(TAG
, "Remove " + mFileToRemove
.getRemotePath() + ": " + result
.getLogMessage(), e
);
103 delete
.releaseConnection();