Merge pull request #118 from owncloud/loggingtool
[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 as published by
6 * the Free Software Foundation, either version 2 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.Log_OC;
27 import com.owncloud.android.datamodel.DataStorageManager;
28 import com.owncloud.android.datamodel.OCFile;
29
30 import eu.alefzero.webdav.WebdavClient;
31 import eu.alefzero.webdav.WebdavUtils;
32
33 /**
34 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
35 *
36 * @author David A. Velasco
37 */
38 public class RemoveFileOperation extends RemoteOperation {
39
40 private static final String TAG = RemoveFileOperation.class.getSimpleName();
41
42 private static final int REMOVE_READ_TIMEOUT = 10000;
43 private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
44
45 OCFile mFileToRemove;
46 boolean mDeleteLocalCopy;
47 DataStorageManager mDataStorageManager;
48
49
50 /**
51 * Constructor
52 *
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.
56 */
57 public RemoveFileOperation(OCFile fileToRemove, boolean deleteLocalCopy, DataStorageManager storageManager) {
58 mFileToRemove = fileToRemove;
59 mDeleteLocalCopy = deleteLocalCopy;
60 mDataStorageManager = storageManager;
61 }
62
63
64 /**
65 * Getter for the file to remove (or removed, if the operation was successfully performed).
66 *
67 * @return File to remove or already removed.
68 */
69 public OCFile getFile() {
70 return mFileToRemove;
71 }
72
73
74 /**
75 * Performs the remove operation
76 *
77 * @param client Client object to communicate with the remote ownCloud server.
78 */
79 @Override
80 protected RemoteOperationResult run(WebdavClient client) {
81 RemoteOperationResult result = null;
82 DeleteMethod delete = null;
83 try {
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);
89 } else {
90 mDataStorageManager.removeFile(mFileToRemove, mDeleteLocalCopy);
91 }
92 }
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());
96
97 } catch (Exception e) {
98 result = new RemoteOperationResult(e);
99 Log_OC.e(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage(), e);
100
101 } finally {
102 if (delete != null)
103 delete.releaseConnection();
104 }
105 return result;
106 }
107
108 }