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