674730e309395531e6bfe909cc31e200dfbc4101
[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.RemoteOperationResult.ResultCode;
27 import com.owncloud.android.lib.operations.remote.ExistenceCheckRemoteOperation;
28 import com.owncloud.android.lib.operations.remote.RemoveRemoteShareOperation;
29 import com.owncloud.android.lib.utils.FileUtils;
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 String path = mRemotePath;
58 if (path.endsWith(FileUtils.PATH_SEPARATOR)) {
59 path = path.substring(0, path.length()-1); // Remove last /
60 }
61 OCShare share = getStorageManager().getShareByPath(path);
62
63 if (share != null) {
64 RemoveRemoteShareOperation operation = new RemoveRemoteShareOperation((int) share.getIdRemoteShared());
65 result = operation.execute(client);
66
67 if (result.isSuccess() || result.getCode() == ResultCode.SHARE_NOT_FOUND) {
68 Log_OC.d(TAG, "Share id = " + share.getIdRemoteShared() + " deleted");
69
70 OCFile file = getStorageManager().getFileByPath(mRemotePath);
71 file.setShareByLink(false);
72 file.setPublicLink("");
73 getStorageManager().saveFile(file);
74 getStorageManager().removeShare(share);
75
76 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
77 if (existsFile(client, file.getRemotePath())) {
78 result = new RemoteOperationResult(ResultCode.OK);
79 } else {
80 getStorageManager().removeFile(file, true, true);
81 }
82 }
83 }
84
85 } else {
86 result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND);
87 }
88
89 return result;
90 }
91
92 private boolean existsFile(OwnCloudClient client, String remotePath){
93 ExistenceCheckRemoteOperation existsOperation = new ExistenceCheckRemoteOperation(remotePath, mContext, false);
94 RemoteOperationResult result = existsOperation.execute(client);
95 return result.isSuccess();
96 }
97
98 }