Open, download and cancel operations linked to contextual menu for files
[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.jackrabbit.webdav.client.methods.DeleteMethod;
22
23 import android.util.Log;
24
25 import com.owncloud.android.datamodel.DataStorageManager;
26 import com.owncloud.android.datamodel.OCFile;
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 DataStorageManager 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, DataStorageManager storageManager) {
56 mFileToRemove = fileToRemove;
57 mDeleteLocalCopy = deleteLocalCopy;
58 mDataStorageManager = storageManager;
59 }
60
61
62 /**
63 * Performs the remove operation
64 *
65 * @param client Client object to communicate with the remote ownCloud server.
66 */
67 @Override
68 protected RemoteOperationResult run(WebdavClient client) {
69 RemoteOperationResult result = null;
70 DeleteMethod delete = null;
71 try {
72 delete = new DeleteMethod(client.getBaseUri() + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
73 int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
74 if (delete.succeeded()) {
75 if (mFileToRemove.isDirectory()) {
76 mDataStorageManager.removeDirectory(mFileToRemove, true, mDeleteLocalCopy);
77 } else {
78 mDataStorageManager.removeFile(mFileToRemove, mDeleteLocalCopy);
79 }
80 }
81 delete.getResponseBodyAsString(); // exhaust the response, although not interesting
82 result = new RemoteOperationResult(delete.succeeded(), status);
83 Log.i(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage());
84
85 } catch (Exception e) {
86 result = new RemoteOperationResult(e);
87 Log.e(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage(), e);
88
89 } finally {
90 if (delete != null)
91 delete.releaseConnection();
92 }
93 return result;
94 }
95
96 }