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