bfc063f9e1c16d96234b201981dd2aae7d2f9bcd
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RemoveFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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 as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.operations;
20
21 import org.apache.commons.httpclient.HttpStatus;
22 import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
23
24 import android.util.Log;
25
26 import com.owncloud.android.datamodel.DataStorageManager;
27 import com.owncloud.android.datamodel.OCFile;
28
29 import eu.alefzero.webdav.WebdavClient;
30 import eu.alefzero.webdav.WebdavUtils;
31
32 /**
33 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
34 *
35 * @author David A. Velasco
36 */
37 public class RemoveFileOperation extends RemoteOperation {
38
39 private static final String TAG = RemoveFileOperation.class.getSimpleName();
40
41 private static final int REMOVE_READ_TIMEOUT = 10000;
42 private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
43
44 OCFile mFileToRemove;
45 boolean mDeleteLocalCopy;
46 DataStorageManager mDataStorageManager;
47
48
49 /**
50 * Constructor
51 *
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.
55 */
56 public RemoveFileOperation(OCFile fileToRemove, boolean deleteLocalCopy, DataStorageManager storageManager) {
57 mFileToRemove = fileToRemove;
58 mDeleteLocalCopy = deleteLocalCopy;
59 mDataStorageManager = storageManager;
60 }
61
62
63 /**
64 * Getter for the file to remove (or removed, if the operation was successfully performed).
65 *
66 * @return File to remove or already removed.
67 */
68 public OCFile getFile() {
69 return mFileToRemove;
70 }
71
72
73 /**
74 * Performs the remove operation
75 *
76 * @param client Client object to communicate with the remote ownCloud server.
77 */
78 @Override
79 protected RemoteOperationResult run(WebdavClient client) {
80 RemoteOperationResult result = null;
81 DeleteMethod delete = null;
82 try {
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);
88 } else {
89 mDataStorageManager.removeFile(mFileToRemove, mDeleteLocalCopy);
90 }
91 }
92 delete.getResponseBodyAsString(); // exhaust the response, although not interesting
93 result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), status);
94 Log.i(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage());
95
96 } catch (Exception e) {
97 result = new RemoteOperationResult(e);
98 Log.e(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage(), e);
99
100 } finally {
101 if (delete != null)
102 delete.releaseConnection();
103 }
104 return result;
105 }
106
107 }