Add new string for share_groups_indicator
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / UnshareLinkOperation.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 /**
39 * Unshare file/folder
40 * Save the data in Database
41 */
42 public class UnshareLinkOperation extends SyncOperation {
43
44 private static final String TAG = UnshareLinkOperation.class.getSimpleName();
45
46 private String mRemotePath;
47 private Context mContext;
48
49
50 public UnshareLinkOperation(String remotePath, Context context) {
51 mRemotePath = remotePath;
52 mContext = context;
53 }
54
55 @Override
56 protected RemoteOperationResult run(OwnCloudClient client) {
57 RemoteOperationResult result = null;
58
59 // Get Share for a file
60 OCShare share = getStorageManager().getFirstShareByPathAndType(mRemotePath,
61 ShareType.PUBLIC_LINK);
62
63 if (share != null) {
64 RemoveRemoteShareOperation operation =
65 new RemoveRemoteShareOperation((int) share.getIdRemoteShared());
66 result = operation.execute(client);
67
68 if (result.isSuccess() || result.getCode() == ResultCode.SHARE_NOT_FOUND) {
69 Log_OC.d(TAG, "Share id = " + share.getIdRemoteShared() + " deleted");
70
71 OCFile file = getStorageManager().getFileByPath(mRemotePath);
72 file.setShareViaLink(false);
73 file.setPublicLink("");
74 getStorageManager().saveFile(file);
75 getStorageManager().removeShare(share);
76
77 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
78 if (existsFile(client, file.getRemotePath())) {
79 result = new RemoteOperationResult(ResultCode.OK);
80 } else {
81 getStorageManager().removeFile(file, true, true);
82 }
83 }
84 }
85
86 } else {
87 result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND);
88 }
89
90 return result;
91 }
92
93 private boolean existsFile(OwnCloudClient client, String remotePath){
94 ExistenceCheckRemoteOperation existsOperation =
95 new ExistenceCheckRemoteOperation(remotePath, mContext, false);
96 RemoteOperationResult result = existsOperation.execute(client);
97 return result.isSuccess();
98 }
99
100 }