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