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
27 import android
.content
.Context
;
28 import android
.content
.Intent
;
30 import com
.owncloud
.android
.R
;
31 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
32 import com
.owncloud
.android
.datamodel
.OCFile
;
33 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
34 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
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
.shares
.GetRemoteSharesForFileOperation
;
39 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
40 import com
.owncloud
.android
.lib
.resources
.shares
.CreateRemoteShareOperation
;
41 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
42 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
44 public class CreateShareOperation
extends SyncOperation
{
46 private static final String TAG
= CreateShareOperation
.class.getSimpleName();
48 protected FileDataStorageManager mStorageManager
;
50 private Context mContext
;
52 private ShareType mShareType
;
53 private String mShareWith
;
54 private boolean mPublicUpload
;
55 private String mPassword
;
56 private int mPermissions
;
57 private Intent mSendIntent
;
61 * @param context The context that the share is coming from.
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.
81 public CreateShareOperation(Context context
, String path
, ShareType shareType
, String shareWith
,
82 boolean publicUpload
, String password
, int permissions
,
87 mShareType
= shareType
;
88 mShareWith
= shareWith
;
89 mPublicUpload
= publicUpload
;
91 mPermissions
= permissions
;
92 mSendIntent
= sendIntent
;
96 protected RemoteOperationResult
run(OwnCloudClient client
) {
97 RemoteOperation operation
= null
;
99 // Check if the share link already exists
100 operation
= new GetRemoteSharesForFileOperation(mPath
, false
, false
);
101 RemoteOperationResult result
=
102 ((GetRemoteSharesForFileOperation
)operation
).execute(client
);
104 if (!result
.isSuccess() || result
.getData().size() <= 0) {
105 operation
= new CreateRemoteShareOperation(mPath
, mShareType
, mShareWith
, mPublicUpload
,
106 mPassword
, mPermissions
);
107 result
= ((CreateRemoteShareOperation
)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 private void updateData(OCShare share
) {
149 // Update DB with the response
150 share
.setPath(mPath
);
151 if (mPath
.endsWith(FileUtils
.PATH_SEPARATOR
)) {
152 share
.setIsFolder(true
);
154 share
.setIsFolder(false
);
156 share
.setPermissions(mPermissions
);
158 getStorageManager().saveShare(share
);
160 // Update OCFile with data from share: ShareByLink and publicLink
161 OCFile file
= getStorageManager().getFileByPath(mPath
);
163 mSendIntent
.putExtra(Intent
.EXTRA_TEXT
, share
.getShareLink());
164 if (getClient().getCredentials().getUsername() == null
) {
166 mSendIntent
.putExtra(Intent
.EXTRA_SUBJECT
,
167 String
.format(mContext
.getString(R
.string
.saml_subject_token
),
168 file
.getFileName()));
170 mSendIntent
.putExtra(Intent
.EXTRA_SUBJECT
,
171 String
.format(mContext
.getString(R
.string
.subject_token
),
172 getClient().getCredentials().getUsername(), file
.getFileName()));
174 file
.setPublicLink(share
.getShareLink());
175 file
.setShareByLink(true
);
176 getStorageManager().saveFile(file
);
177 Log_OC
.d(TAG
, "Public Link = " + file
.getPublicLink());