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 public class CreateShareViaLinkOperation
extends SyncOperation
{
47 private String mPassword
;
48 private Intent mSendIntent
;
49 private String mFileName
;
53 * @param path Full path of the file/folder being shared. Mandatory argument
54 * @param password Password to protect a public link share.
55 * Only available for public link shares
56 * @param sendIntent Optional Intent with the information of an app where the link to the new share (if public)
57 * should be posted later.
59 public CreateShareViaLinkOperation(
67 mSendIntent
= sendIntent
;
72 protected RemoteOperationResult
run(OwnCloudClient client
) {
73 // Check if the share link already exists
74 RemoteOperation operation
= new GetRemoteSharesForFileOperation(mPath
, false
, false
);
75 RemoteOperationResult result
= operation
.execute(client
);
76 // TODO - fix this check; if the user already shared the file with users or group, a share via link will not be created
78 if (!result
.isSuccess() || result
.getData().size() <= 0) {
79 operation
= new CreateRemoteShareOperation(
81 ShareType
.PUBLIC_LINK
,
85 OCShare
.DEFAULT_PERMISSION
87 result
= operation
.execute(client
);
90 if (result
.isSuccess()) {
91 if (result
.getData().size() > 0) {
92 OCShare share
= (OCShare
) result
.getData().get(0);
100 public String
getPath() {
104 public String
getPassword() {
108 public Intent
getSendIntent() {
112 public Intent
getSendIntentWithSubject(Context context
) {
113 if (context
!= null
&& mSendIntent
!= null
&& mSendIntent
.getStringExtra(Intent
.EXTRA_SUBJECT
) != null
) {
114 if (getClient() == null
|| getClient().getCredentials() == null
||
115 getClient().getCredentials().getUsername() == null
) {
116 mSendIntent
.putExtra(
117 Intent
.EXTRA_SUBJECT
,
118 context
.getString(R
.string
.subject_shared_with_you
, mFileName
)
121 mSendIntent
.putExtra(
122 Intent
.EXTRA_SUBJECT
,
124 R
.string
.subject_user_shared_with_you
,
125 getClient().getCredentials().getUsername(),
134 private void updateData(OCShare share
) {
135 // Update DB with the response
136 share
.setPath(mPath
);
137 if (mPath
.endsWith(FileUtils
.PATH_SEPARATOR
)) {
138 share
.setIsFolder(true
);
140 share
.setIsFolder(false
);
143 getStorageManager().saveShare(share
);
145 // Update OCFile with data from share: ShareByLink and publicLink
146 OCFile file
= getStorageManager().getFileByPath(mPath
);
148 file
.setPublicLink(share
.getShareLink());
149 file
.setShareViaLink(true
);
150 getStorageManager().saveFile(file
);
151 if (mSendIntent
!= null
) {
152 mSendIntent
.putExtra(Intent
.EXTRA_TEXT
, share
.getShareLink());