1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.operations
;
20 import android
.content
.Context
;
22 import com
.owncloud
.android
.datamodel
.OCFile
;
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
;
33 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
37 * Save the data in Database
41 public class UnshareLinkOperation
extends SyncOperation
{
43 private static final String TAG
= UnshareLinkOperation
.class.getSimpleName();
45 private String mRemotePath
;
46 private Context mContext
;
49 public UnshareLinkOperation(String remotePath
, Context context
) {
50 mRemotePath
= remotePath
;
55 protected RemoteOperationResult
run(OwnCloudClient client
) {
56 RemoteOperationResult result
= null
;
58 // Get Share for a file
59 OCShare share
= getStorageManager().getFirstShareByPathAndType(mRemotePath
, ShareType
.PUBLIC_LINK
);
62 RemoveRemoteShareOperation operation
= new RemoveRemoteShareOperation((int) share
.getIdRemoteShared());
63 result
= operation
.execute(client
);
65 if (result
.isSuccess() || result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
66 Log_OC
.d(TAG
, "Share id = " + share
.getIdRemoteShared() + " deleted");
68 OCFile file
= getStorageManager().getFileByPath(mRemotePath
);
69 file
.setShareByLink(false
);
70 file
.setPublicLink("");
71 getStorageManager().saveFile(file
);
72 getStorageManager().removeShare(share
);
74 if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
75 if (existsFile(client
, file
.getRemotePath())) {
76 result
= new RemoteOperationResult(ResultCode
.OK
);
78 getStorageManager().removeFile(file
, true
, true
);
84 result
= new RemoteOperationResult(ResultCode
.SHARE_NOT_FOUND
);
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();