Remove toast with create_share info
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / UnshareOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * Copyright (C) 2015 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 android.content.Context;
24
25 import com.owncloud.android.datamodel.OCFile;
26
27 import com.owncloud.android.lib.common.OwnCloudClient;
28 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
29 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
30 import com.owncloud.android.lib.common.utils.Log_OC;
31 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
32 import com.owncloud.android.lib.resources.shares.OCShare;
33 import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation;
34 import com.owncloud.android.lib.resources.shares.ShareType;
35
36 import com.owncloud.android.operations.common.SyncOperation;
37
38 import java.util.ArrayList;
39
40 /**
41 * Unshare file/folder
42 * Save the data in Database
43 */
44 public class UnshareOperation extends SyncOperation {
45
46 private static final String TAG = UnshareOperation.class.getSimpleName();
47
48 private String mRemotePath;
49 private ShareType mShareType;
50 private String mShareWith;
51 private Context mContext;
52
53 public UnshareOperation(String remotePath, ShareType shareType, String shareWith,
54 Context context) {
55 mRemotePath = remotePath;
56 mShareType = shareType;
57 mShareWith = shareWith;
58 mContext = context;
59 }
60
61 @Override
62 protected RemoteOperationResult run(OwnCloudClient client) {
63 RemoteOperationResult result = null;
64
65 // Get Share for a file
66 OCShare share = getStorageManager().getFirstShareByPathAndType(mRemotePath,
67 mShareType, mShareWith);
68
69 if (share != null) {
70 RemoveRemoteShareOperation operation =
71 new RemoveRemoteShareOperation((int) share.getIdRemoteShared());
72 result = operation.execute(client);
73
74 if (result.isSuccess() || result.getCode() == ResultCode.SHARE_NOT_FOUND) {
75 Log_OC.d(TAG, "Share id = " + share.getIdRemoteShared() + " deleted");
76
77 OCFile file = getStorageManager().getFileByPath(mRemotePath);
78 if (mShareType == ShareType.PUBLIC_LINK) {
79 file.setShareViaLink(false);
80 file.setPublicLink("");
81 } else if (mShareType == ShareType.USER || mShareType == ShareType.GROUP){
82 // Check if it is the last share
83 ArrayList <OCShare> sharesWith = getStorageManager().
84 getSharesWithForAFile(mRemotePath,
85 getStorageManager().getAccount().name);
86 if (sharesWith.size() == 1) {
87 file.setShareWithSharee(false);
88 }
89 }
90
91 getStorageManager().saveFile(file);
92 getStorageManager().removeShare(share);
93
94 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
95 if (existsFile(client, file.getRemotePath())) {
96 result = new RemoteOperationResult(ResultCode.OK);
97 } else {
98 getStorageManager().removeFile(file, true, true);
99 }
100 }
101 }
102
103 } else {
104 result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND);
105 }
106
107 return result;
108 }
109
110 private boolean existsFile(OwnCloudClient client, String remotePath){
111 ExistenceCheckRemoteOperation existsOperation =
112 new ExistenceCheckRemoteOperation(remotePath, mContext, false);
113 RemoteOperationResult result = existsOperation.execute(client);
114 return result.isSuccess();
115 }
116
117 }