Merge remote-tracking branch 'remotes/upstream/cancelUploadOnWlanExit' into beta
[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.MainApp;
25 import com.owncloud.android.datamodel.OCFile;
26 import com.owncloud.android.lib.common.OwnCloudClient;
27 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
28 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
29 import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
30 import com.owncloud.android.operations.common.SyncOperation;
31
32
33 /**
34 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
35 */
36 public class RemoveFileOperation extends SyncOperation {
37
38 // private static final String TAG = RemoveFileOperation.class.getSimpleName();
39
40 OCFile mFileToRemove;
41 String mRemotePath;
42 boolean mOnlyLocalCopy;
43
44
45 /**
46 * Constructor
47 *
48 * @param remotePath RemotePath of the OCFile instance describing the remote file or
49 * folder to remove from the server
50 * @param onlyLocalCopy When 'true', and a local copy of the file exists, only this is
51 * removed.
52 */
53 public RemoveFileOperation(String remotePath, boolean onlyLocalCopy) {
54 mRemotePath = remotePath;
55 mOnlyLocalCopy = onlyLocalCopy;
56 }
57
58
59 /**
60 * Getter for the file to remove (or removed, if the operation was successfully performed).
61 *
62 * @return File to remove or already removed.
63 */
64 public OCFile getFile() {
65 return mFileToRemove;
66 }
67
68 /**
69 * Performs the remove operation
70 *
71 * @param client Client object to communicate with the remote ownCloud server.
72 */
73 @Override
74 protected RemoteOperationResult run(OwnCloudClient client) {
75 RemoteOperationResult result = null;
76
77 mFileToRemove = getStorageManager().getFileByPath(mRemotePath);
78
79 boolean localRemovalFailed = false;
80 if (!mOnlyLocalCopy) {
81 RemoveRemoteFileOperation operation = new RemoveRemoteFileOperation(mRemotePath);
82 result = operation.execute(client);
83 if (result.isSuccess() || result.getCode() == ResultCode.FILE_NOT_FOUND) {
84 localRemovalFailed = !(getStorageManager().removeFile(mFileToRemove, true, true));
85 }
86
87 } else {
88 localRemovalFailed = !(getStorageManager().removeFile(mFileToRemove, false, true));
89 if (!localRemovalFailed) {
90 result = new RemoteOperationResult(ResultCode.OK);
91 }
92 }
93
94 if (localRemovalFailed) {
95 result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_REMOVED);
96 }
97
98 return result;
99 }
100
101 }