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