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