2 * ownCloud Android client application
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.operations
;
24 * Creates a new share from a given file
28 import android
.content
.Context
;
29 import android
.content
.Intent
;
31 import com
.owncloud
.android
.R
;
32 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
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
.common
.utils
.Log_OC
;
38 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
39 import com
.owncloud
.android
.lib
.resources
.shares
.CreateRemoteShareOperation
;
40 import com
.owncloud
.android
.lib
.resources
.shares
.GetRemoteSharesForFileOperation
;
41 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
42 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
43 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
45 public class CreateShareOperation
extends SyncOperation
{
47 private static final String TAG
= CreateShareOperation
.class.getSimpleName();
49 protected FileDataStorageManager mStorageManager
;
52 private ShareType mShareType
;
53 private String mShareWith
;
54 private boolean mPublicUpload
;
55 private String mPassword
;
56 private int mPermissions
;
57 private Intent mSendIntent
;
58 private String mFileName
;
62 * @param path Full path of the file/folder being shared. Mandatory argument
63 * @param shareType 0 = user, 1 = group, 3 = Public link. Mandatory argument
64 * @param shareWith User/group ID with who the file should be shared.
65 * This is mandatory for shareType of 0 or 1
66 * @param publicUpload If false (default) public cannot upload to a public shared folder.
67 * If true public can upload to a shared folder.
68 * Only available for public link shares
69 * @param password Password to protect a public link share.
70 * Only available for public link shares
71 * @param permissions 1 - Read only - Default for public shares
76 * 31- All above - Default for private shares
77 * For user or group shares.
78 * To obtain combinations, add the desired values together.
79 * For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27.
80 * @param sendIntent Optional Intent with the information of an app where the link to the new share (if public)
81 * should be posted later.
83 public CreateShareOperation(String path
, ShareType shareType
, String shareWith
,
84 boolean publicUpload
, String password
, int permissions
, Intent sendIntent
) {
87 mShareType
= shareType
;
88 mShareWith
= shareWith
!= null ? shareWith
: "";
89 mPublicUpload
= publicUpload
;
91 mPermissions
= permissions
;
92 mSendIntent
= sendIntent
;
97 protected RemoteOperationResult
run(OwnCloudClient client
) {
98 // Check if the share link already exists
99 RemoteOperation operation
= new GetRemoteSharesForFileOperation(mPath
, false
, false
);
100 RemoteOperationResult result
= operation
.execute(client
);
102 if (!result
.isSuccess() || result
.getData().size() <= 0) {
103 operation
= new CreateRemoteShareOperation(
104 mPath
, mShareType
, mShareWith
,
105 mPublicUpload
, mPassword
, mPermissions
107 result
= operation
.execute(client
);
110 if (result
.isSuccess()) {
111 if (result
.getData().size() > 0) {
112 OCShare share
= (OCShare
) result
.getData().get(0);
120 public String
getPath() {
124 public ShareType
getShareType() {
128 public String
getShareWith() {
132 public boolean getPublicUpload() {
133 return mPublicUpload
;
136 public String
getPassword() {
140 public int getPermissions() {
144 public Intent
getSendIntent() {
148 public Intent
getSendIntentWithSubject(Context context
) {
149 if (context
!= null
&& mSendIntent
!= null
&& mSendIntent
.getStringExtra(Intent
.EXTRA_SUBJECT
) != null
) {
150 if (getClient() == null
|| getClient().getCredentials().getUsername() == null
) {
151 mSendIntent
.putExtra(
152 Intent
.EXTRA_SUBJECT
,
153 context
.getString(R
.string
.subject_shared_with_you
, mFileName
)
156 mSendIntent
.putExtra(
157 Intent
.EXTRA_SUBJECT
,
159 R
.string
.subject_user_shared_with_you
,
160 getClient().getCredentials().getUsername(),
169 private void updateData(OCShare share
) {
170 // Update DB with the response
171 share
.setPath(mPath
);
172 if (mPath
.endsWith(FileUtils
.PATH_SEPARATOR
)) {
173 share
.setIsFolder(true
);
175 share
.setIsFolder(false
);
177 share
.setPermissions(mPermissions
);
179 getStorageManager().saveShare(share
);
181 // Update OCFile with data from share: ShareByLink and publicLink
182 OCFile file
= getStorageManager().getFileByPath(mPath
);
184 mSendIntent
.putExtra(Intent
.EXTRA_TEXT
, share
.getShareLink());
185 file
.setPublicLink(share
.getShareLink());
186 file
.setShareViaLink(true
);
187 getStorageManager().saveFile(file
);