Fixed now - FileDisplayActivity is not recreated anymore when the user selects anothe...
[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.MainApp;
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.resources.shares.OCShare;
36 import com.owncloud.android.lib.common.operations.RemoteOperation;
37 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
38 import com.owncloud.android.lib.common.utils.Log_OC;
39 import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
40 import com.owncloud.android.lib.resources.shares.ShareType;
41 import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
42 import com.owncloud.android.lib.resources.files.FileUtils;
43 import com.owncloud.android.operations.common.SyncOperation;
44
45 public class CreateShareOperation extends SyncOperation {
46
47 private static final String TAG = CreateShareOperation.class.getSimpleName();
48
49 protected FileDataStorageManager mStorageManager;
50
51 private Context mContext;
52 private String mPath;
53 private ShareType mShareType;
54 private String mShareWith;
55 private boolean mPublicUpload;
56 private String mPassword;
57 private int mPermissions;
58 private Intent mSendIntent;
59
60 /**
61 * Constructor
62 * @param context The context that the share is coming from.
63 * @param path Full path of the file/folder being shared. Mandatory argument
64 * @param shareType 0 = user, 1 = group, 3 = Public link. Mandatory argument
65 * @param shareWith User/group ID with who the file should be shared.
66 * This is mandatory for shareType of 0 or 1
67 * @param publicUpload If false (default) public cannot upload to a public shared folder.
68 * If true public can upload to a shared folder.
69 * Only available for public link shares
70 * @param password Password to protect a public link share.
71 * Only available for public link shares
72 * @param permissions 1 - Read only - Default for public shares
73 * 2 - Update
74 * 4 - Create
75 * 8 - Delete
76 * 16- Re-share
77 * 31- All above - Default for private shares
78 * For user or group shares.
79 * To obtain combinations, add the desired values together.
80 * For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27.
81 */
82 public CreateShareOperation(Context context, String path, ShareType shareType, String shareWith,
83 boolean publicUpload, String password, int permissions,
84 Intent sendIntent) {
85
86 mContext = context;
87 mPath = path;
88 mShareType = shareType;
89 mShareWith = shareWith;
90 mPublicUpload = publicUpload;
91 mPassword = password;
92 mPermissions = permissions;
93 mSendIntent = sendIntent;
94 }
95
96 @Override
97 protected RemoteOperationResult run(OwnCloudClient client) {
98 RemoteOperation operation = null;
99
100 // Check if the share link already exists
101 operation = new GetRemoteSharesForFileOperation(mPath, false, false);
102 RemoteOperationResult result =
103 ((GetRemoteSharesForFileOperation)operation).execute(client);
104
105 if (!result.isSuccess() || result.getData().size() <= 0) {
106 operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload,
107 mPassword, mPermissions);
108 result = ((CreateRemoteShareOperation)operation).execute(client);
109 }
110
111 if (result.isSuccess()) {
112 if (result.getData().size() > 0) {
113 OCShare share = (OCShare) result.getData().get(0);
114 updateData(share);
115 }
116 }
117
118 return result;
119 }
120
121 public String getPath() {
122 return mPath;
123 }
124
125 public ShareType getShareType() {
126 return mShareType;
127 }
128
129 public String getShareWith() {
130 return mShareWith;
131 }
132
133 public boolean getPublicUpload() {
134 return mPublicUpload;
135 }
136
137 public String getPassword() {
138 return mPassword;
139 }
140
141 public int getPermissions() {
142 return mPermissions;
143 }
144
145 public Intent getSendIntent() {
146 return mSendIntent;
147 }
148
149 private void updateData(OCShare share) {
150 // Update DB with the response
151 share.setPath(mPath);
152 if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
153 share.setIsFolder(true);
154 } else {
155 share.setIsFolder(false);
156 }
157 share.setPermissions(mPermissions);
158
159 getStorageManager().saveShare(share);
160
161 // Update OCFile with data from share: ShareByLink and publicLink
162 OCFile file = getStorageManager().getFileByPath(mPath);
163 if (file!=null) {
164 mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
165 mSendIntent.putExtra(Intent.EXTRA_SUBJECT, String.format(mContext.getString(R.string.subject_token),
166 getClient().getCredentials().getUsername(), file.getFileName()));
167 file.setPublicLink(share.getShareLink());
168 file.setShareByLink(true);
169 getStorageManager().saveFile(file);
170 Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
171
172 }
173 }
174
175 }