OC-2868: Fix bug: Unshare a file that does not exist any more, no message is shown
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / UnshareLinkOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2014 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 version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 import android.content.Context;
21
22 import com.owncloud.android.datamodel.OCFile;
23 import com.owncloud.android.lib.network.OwnCloudClient;
24 import com.owncloud.android.lib.operations.common.OCShare;
25 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
26 import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode;
27 import com.owncloud.android.lib.operations.remote.ExistenceCheckRemoteOperation;
28 import com.owncloud.android.lib.operations.remote.RemoveRemoteShareOperation;
29 import com.owncloud.android.operations.common.SyncOperation;
30 import com.owncloud.android.utils.Log_OC;
31
32 /**
33 * Unshare file/folder
34 * Save the data in Database
35 *
36 * @author masensio
37 */
38 public class UnshareLinkOperation extends SyncOperation {
39
40 private static final String TAG = UnshareLinkOperation.class.getSimpleName();
41
42 private OCFile mFile;
43 private Context mContext;
44
45
46 public UnshareLinkOperation(OCFile file, Context context) {
47 mFile = file;
48 mContext = context;
49 }
50
51 @Override
52 protected RemoteOperationResult run(OwnCloudClient client) {
53 RemoteOperationResult result = null;
54
55 // Get Share for a file
56 String path = mFile.getRemotePath();
57 if (mFile.isFolder()) {
58 path = path.substring(0, path.length()-1); // Remove last /
59 }
60 OCShare share = getStorageManager().getShareByPath(path);
61
62 if (share != null) {
63 RemoveRemoteShareOperation operation = new RemoveRemoteShareOperation((int) share.getIdRemoteShared());
64 result = operation.execute(client);
65
66 if (result.isSuccess() || result.getCode() == ResultCode.SHARE_NOT_FOUND) {
67 Log_OC.d(TAG, "Share id = " + share.getIdRemoteShared() + " deleted");
68
69 mFile.setShareByLink(false);
70 mFile.setPublicLink("");
71 getStorageManager().saveFile(mFile);
72 getStorageManager().removeShare(share);
73
74 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
75 if (existsFile(client, mFile.getRemotePath())) {
76 result = new RemoteOperationResult(ResultCode.OK);
77 } else {
78 getStorageManager().removeFile(mFile, true, true);
79 }
80 }
81 }
82
83 } else {
84 result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND);
85 }
86
87 return result;
88 }
89
90 private boolean existsFile(OwnCloudClient client, String remotePath){
91 ExistenceCheckRemoteOperation existsOperation = new ExistenceCheckRemoteOperation(remotePath, mContext, false);
92 RemoteOperationResult result = existsOperation.execute(client);
93 return result.isSuccess();
94 }
95
96 }