70d6bf7b26a7ce55a9bffcf0c7fd0b056e16112f
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateShareOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * Copyright (C) 2015 ownCloud Inc.
6 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 package com.owncloud.android.operations;
22
23 /**
24 * Creates a new share from a given file
25 */
26
27 import android.content.Context;
28 import android.content.Intent;
29
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;
43
44 public class CreateShareOperation extends SyncOperation {
45
46 private static final String TAG = CreateShareOperation.class.getSimpleName();
47
48 protected FileDataStorageManager mStorageManager;
49
50 private Context mContext;
51 private String mPath;
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
59 /**
60 * Constructor
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. This is mandatory for shareType of 0 or 1
65 * @param publicUpload If false (default) public cannot upload to a public shared folder.
66 * If true public can upload to a shared folder. Only available for public link shares
67 * @param password Password to protect a public link share. Only available for public link shares
68 * @param permissions 1 - Read only - Default for public shares
69 * 2 - Update
70 * 4 - Create
71 * 8 - Delete
72 * 16- Re-share
73 * 31- All above - Default for private shares
74 * For user or group shares.
75 * To obtain combinations, add the desired values together.
76 * For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27.
77 */
78 public CreateShareOperation(Context context, String path, ShareType shareType, String shareWith,
79 boolean publicUpload, String password, int permissions,
80 Intent sendIntent) {
81
82 mContext = context;
83 mPath = path;
84 mShareType = shareType;
85 mShareWith = shareWith;
86 mPublicUpload = publicUpload;
87 mPassword = password;
88 mPermissions = permissions;
89 mSendIntent = sendIntent;
90 }
91
92 @Override
93 protected RemoteOperationResult run(OwnCloudClient client) {
94 RemoteOperation operation = null;
95
96 // Check if the share link already exists
97 operation = new GetRemoteSharesForFileOperation(mPath, false, false);
98 RemoteOperationResult result = ((GetRemoteSharesForFileOperation)operation).execute(client);
99
100 if (!result.isSuccess() || result.getData().size() <= 0) {
101 operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload,
102 mPassword, mPermissions);
103 result = ((CreateRemoteShareOperation)operation).execute(client);
104 }
105
106 if (result.isSuccess()) {
107 if (result.getData().size() > 0) {
108 OCShare share = (OCShare) result.getData().get(0);
109 updateData(share);
110 }
111 }
112
113 return result;
114 }
115
116 public String getPath() {
117 return mPath;
118 }
119
120 public ShareType getShareType() {
121 return mShareType;
122 }
123
124 public String getShareWith() {
125 return mShareWith;
126 }
127
128 public boolean getPublicUpload() {
129 return mPublicUpload;
130 }
131
132 public String getPassword() {
133 return mPassword;
134 }
135
136 public int getPermissions() {
137 return mPermissions;
138 }
139
140 public Intent getSendIntent() {
141 return mSendIntent;
142 }
143
144 private void updateData(OCShare share) {
145 // Update DB with the response
146 share.setPath(mPath);
147 if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
148 share.setIsFolder(true);
149 } else {
150 share.setIsFolder(false);
151 }
152 share.setPermissions(mPermissions);
153
154 getStorageManager().saveShare(share);
155
156 // Update OCFile with data from share: ShareByLink and publicLink
157 OCFile file = getStorageManager().getFileByPath(mPath);
158 if (file!=null) {
159 mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
160 mSendIntent.putExtra(Intent.EXTRA_SUBJECT, String.format(mContext.getString(R.string.subject_token),
161 getClient().getCredentials().getUsername(), file.getFileName()));
162 file.setPublicLink(share.getShareLink());
163 file.setShareByLink(true);
164 getStorageManager().saveFile(file);
165 Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
166
167 }
168 }
169
170 }