Merge branch 'saml_based_federated_single_sign_on' into saml_based_federated_single_s...
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RemoveFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 import org.apache.commons.httpclient.HttpStatus;
21 import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
22
23 import com.owncloud.android.Log_OC;
24 import com.owncloud.android.datamodel.DataStorageManager;
25 import com.owncloud.android.datamodel.OCFile;
26
27 import eu.alefzero.webdav.WebdavClient;
28 import eu.alefzero.webdav.WebdavUtils;
29
30 /**
31 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
32 *
33 * @author David A. Velasco
34 */
35 public class RemoveFileOperation extends RemoteOperation {
36
37 private static final String TAG = RemoveFileOperation.class.getSimpleName();
38
39 private static final int REMOVE_READ_TIMEOUT = 10000;
40 private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
41
42 OCFile mFileToRemove;
43 boolean mDeleteLocalCopy;
44 DataStorageManager mDataStorageManager;
45
46
47 /**
48 * Constructor
49 *
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.
53 */
54 public RemoveFileOperation(OCFile fileToRemove, boolean deleteLocalCopy, DataStorageManager storageManager) {
55 mFileToRemove = fileToRemove;
56 mDeleteLocalCopy = deleteLocalCopy;
57 mDataStorageManager = storageManager;
58 }
59
60
61 /**
62 * Getter for the file to remove (or removed, if the operation was successfully performed).
63 *
64 * @return File to remove or already removed.
65 */
66 public OCFile getFile() {
67 return mFileToRemove;
68 }
69
70
71 /**
72 * Performs the remove operation
73 *
74 * @param client Client object to communicate with the remote ownCloud server.
75 */
76 @Override
77 protected RemoteOperationResult run(WebdavClient client) {
78 RemoteOperationResult result = null;
79 DeleteMethod delete = null;
80 try {
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);
86 } else {
87 mDataStorageManager.removeFile(mFileToRemove, mDeleteLocalCopy);
88 }
89 }
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());
93
94 } catch (Exception e) {
95 result = new RemoteOperationResult(e);
96 Log_OC.e(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage(), e);
97
98 } finally {
99 if (delete != null)
100 delete.releaseConnection();
101 }
102 return result;
103 }
104
105 }