2 * ownCloud Android client application
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.operations
;
25 * Creates a new public share for a given file
29 import android
.content
.Context
;
30 import android
.content
.Intent
;
32 import com
.owncloud
.android
.R
;
33 import com
.owncloud
.android
.datamodel
.OCFile
;
34 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
35 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
36 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
37 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
38 import com
.owncloud
.android
.lib
.resources
.shares
.CreateRemoteShareOperation
;
39 import com
.owncloud
.android
.lib
.resources
.shares
.GetRemoteSharesForFileOperation
;
40 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
41 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
42 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
44 import java
.util
.ArrayList
;
46 public class CreateShareViaLinkOperation
extends SyncOperation
{
49 private String mPassword
;
50 private Intent mSendIntent
;
51 private String mFileName
;
55 * @param path Full path of the file/folder being shared. Mandatory argument
56 * @param password Password to protect a public link share.
57 * Only available for public link shares
58 * @param sendIntent Optional Intent with the information of an app where the link to the new share (if public)
59 * should be posted later.
61 public CreateShareViaLinkOperation(
69 mSendIntent
= sendIntent
;
74 protected RemoteOperationResult
run(OwnCloudClient client
) {
75 // Check if the share link already exists
76 RemoteOperation operation
= new GetRemoteSharesForFileOperation(mPath
, false
, false
);
77 RemoteOperationResult result
= operation
.execute(client
);
79 boolean shareByLink
= false
;
80 // Check if the file is shared by link
81 if (result
.isSuccess() && result
.getData().size() > 0){
82 ArrayList
<Object
> shares
= result
.getData();
83 for(Object object
: shares
){
84 if (((OCShare
) object
).getShareType() == ShareType
.PUBLIC_LINK
){
91 if (!result
.isSuccess() || !shareByLink
) {
92 operation
= new CreateRemoteShareOperation(
94 ShareType
.PUBLIC_LINK
,
98 OCShare
.DEFAULT_PERMISSION
100 result
= operation
.execute(client
);
103 if (result
.isSuccess()) {
104 if (result
.getData().size() > 0) {
105 OCShare share
= (OCShare
) result
.getData().get(0);
113 public String
getPath() {
117 public String
getPassword() {
121 public Intent
getSendIntent() {
125 public Intent
getSendIntentWithSubject(Context context
) {
126 if (context
!= null
&& mSendIntent
!= null
&& mSendIntent
.getStringExtra(Intent
.EXTRA_SUBJECT
) != null
) {
127 if (getClient() == null
|| getClient().getCredentials() == null
||
128 getClient().getCredentials().getUsername() == null
) {
129 mSendIntent
.putExtra(
130 Intent
.EXTRA_SUBJECT
,
131 context
.getString(R
.string
.subject_shared_with_you
, mFileName
)
134 mSendIntent
.putExtra(
135 Intent
.EXTRA_SUBJECT
,
137 R
.string
.subject_user_shared_with_you
,
138 getClient().getCredentials().getUsername(),
147 private void updateData(OCShare share
) {
148 // Update DB with the response
149 share
.setPath(mPath
);
150 if (mPath
.endsWith(FileUtils
.PATH_SEPARATOR
)) {
151 share
.setIsFolder(true
);
153 share
.setIsFolder(false
);
156 getStorageManager().saveShare(share
);
158 // Update OCFile with data from share: ShareByLink and publicLink
159 OCFile file
= getStorageManager().getFileByPath(mPath
);
161 file
.setPublicLink(share
.getShareLink());
162 file
.setShareViaLink(true
);
163 getStorageManager().saveFile(file
);
164 if (mSendIntent
!= null
) {
165 mSendIntent
.putExtra(Intent
.EXTRA_TEXT
, share
.getShareLink());