Merge branch 'develop' into share_password_support
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RemoveFileOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * @author masensio
6 * Copyright (C) 2015 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 package com.owncloud.android.operations;
23
24 import com.owncloud.android.datamodel.OCFile;
25 import com.owncloud.android.lib.common.OwnCloudClient;
26 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
27 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
28 import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
29 import com.owncloud.android.operations.common.SyncOperation;
30
31
32 /**
33 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
34 */
35 public class RemoveFileOperation extends SyncOperation {
36
37 // private static final String TAG = RemoveFileOperation.class.getSimpleName();
38
39 OCFile mFileToRemove;
40 String mRemotePath;
41 boolean mOnlyLocalCopy;
42
43
44 /**
45 * Constructor
46 *
47 * @param remotePath RemotePath of the OCFile instance describing the remote file or
48 * folder to remove from the server
49 * @param onlyLocalCopy When 'true', and a local copy of the file exists, only this is
50 * removed.
51 */
52 public RemoveFileOperation(String remotePath, boolean onlyLocalCopy) {
53 mRemotePath = remotePath;
54 mOnlyLocalCopy = onlyLocalCopy;
55 }
56
57
58 /**
59 * Getter for the file to remove (or removed, if the operation was successfully performed).
60 *
61 * @return File to remove or already removed.
62 */
63 public OCFile getFile() {
64 return mFileToRemove;
65 }
66
67 /**
68 * Performs the remove operation
69 *
70 * @param client Client object to communicate with the remote ownCloud server.
71 */
72 @Override
73 protected RemoteOperationResult run(OwnCloudClient client) {
74 RemoteOperationResult result = null;
75
76 mFileToRemove = getStorageManager().getFileByPath(mRemotePath);
77
78 boolean localRemovalFailed = false;
79 if (!mOnlyLocalCopy) {
80 RemoveRemoteFileOperation operation = new RemoveRemoteFileOperation(mRemotePath);
81 result = operation.execute(client);
82 if (result.isSuccess() || result.getCode() == ResultCode.FILE_NOT_FOUND) {
83 localRemovalFailed = !(getStorageManager().removeFile(mFileToRemove, true, true));
84 }
85
86 } else {
87 localRemovalFailed = !(getStorageManager().removeFile(mFileToRemove, false, true));
88 if (!localRemovalFailed) {
89 result = new RemoteOperationResult(ResultCode.OK);
90 }
91 }
92
93 if (localRemovalFailed) {
94 result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_REMOVED);
95 }
96
97 return result;
98 }
99
100 }