Merge branch 'develop' into setup_buttons
[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.FileDataStorageManager;
25 import com.owncloud.android.datamodel.OCFile;
26
27
28 import eu.alefzero.webdav.WebdavClient;
29 import eu.alefzero.webdav.WebdavUtils;
30
31 /**
32 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
33 *
34 * @author David A. Velasco
35 */
36 public class RemoveFileOperation extends RemoteOperation {
37
38 private static final String TAG = RemoveFileOperation.class.getSimpleName();
39
40 private static final int REMOVE_READ_TIMEOUT = 10000;
41 private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
42
43 OCFile mFileToRemove;
44 boolean mDeleteLocalCopy;
45 FileDataStorageManager mDataStorageManager;
46
47
48 /**
49 * Constructor
50 *
51 * @param fileToRemove OCFile instance describing the remote file or folder to remove from the server
52 * @param deleteLocalCopy When 'true', and a local copy of the file exists, it is also removed.
53 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
54 */
55 public RemoveFileOperation(OCFile fileToRemove, boolean deleteLocalCopy, FileDataStorageManager storageManager) {
56 mFileToRemove = fileToRemove;
57 mDeleteLocalCopy = deleteLocalCopy;
58 mDataStorageManager = storageManager;
59 }
60
61
62 /**
63 * Getter for the file to remove (or removed, if the operation was successfully performed).
64 *
65 * @return File to remove or already removed.
66 */
67 public OCFile getFile() {
68 return mFileToRemove;
69 }
70
71
72 /**
73 * Performs the remove operation
74 *
75 * @param client Client object to communicate with the remote ownCloud server.
76 */
77 @Override
78 protected RemoteOperationResult run(WebdavClient client) {
79 RemoteOperationResult result = null;
80 DeleteMethod delete = null;
81 try {
82 delete = new DeleteMethod(client.getBaseUri() + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
83 int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
84 if (delete.succeeded() || status == HttpStatus.SC_NOT_FOUND) {
85 mDataStorageManager.removeFile(mFileToRemove, true, mDeleteLocalCopy);
86 }
87 delete.getResponseBodyAsString(); // exhaust the response, although not interesting
88 result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), status, delete.getResponseHeaders());
89 Log_OC.i(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage());
90
91 } catch (Exception e) {
92 result = new RemoteOperationResult(e);
93 Log_OC.e(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage(), e);
94
95 } finally {
96 if (delete != null)
97 delete.releaseConnection();
98 }
99 return result;
100 }
101
102 }