Get rid of dependency on Context in CreateShareOperation
[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
28 import android.content.Context;
29 import android.content.Intent;
30
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;
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 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 private String mFileName;
59
60 /**
61 * Constructor
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
72 * 2 - Update
73 * 4 - Create
74 * 8 - Delete
75 * 16- Re-share
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.
82 */
83 public CreateShareOperation(String path, ShareType shareType, String shareWith,
84 boolean publicUpload, String password, int permissions, Intent sendIntent) {
85
86 mPath = path;
87 mShareType = shareType;
88 mShareWith = shareWith != null ? shareWith : "";
89 mPublicUpload = publicUpload;
90 mPassword = password;
91 mPermissions = permissions;
92 mSendIntent = sendIntent;
93 mFileName = null;
94 }
95
96 @Override
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);
101
102 if (!result.isSuccess() || result.getData().size() <= 0) {
103 operation = new CreateRemoteShareOperation(
104 mPath, mShareType, mShareWith,
105 mPublicUpload, mPassword, mPermissions
106 );
107 result = operation.execute(client);
108 }
109
110 if (result.isSuccess()) {
111 if (result.getData().size() > 0) {
112 OCShare share = (OCShare) result.getData().get(0);
113 updateData(share);
114 }
115 }
116
117 return result;
118 }
119
120 public String getPath() {
121 return mPath;
122 }
123
124 public ShareType getShareType() {
125 return mShareType;
126 }
127
128 public String getShareWith() {
129 return mShareWith;
130 }
131
132 public boolean getPublicUpload() {
133 return mPublicUpload;
134 }
135
136 public String getPassword() {
137 return mPassword;
138 }
139
140 public int getPermissions() {
141 return mPermissions;
142 }
143
144 public Intent getSendIntent() {
145 return mSendIntent;
146 }
147
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)
154 );
155 } else {
156 mSendIntent.putExtra(
157 Intent.EXTRA_SUBJECT,
158 context.getString(
159 R.string.subject_user_shared_with_you,
160 getClient().getCredentials().getUsername(),
161 mFileName
162 )
163 );
164 }
165 }
166 return mSendIntent;
167 }
168
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);
174 } else {
175 share.setIsFolder(false);
176 }
177 share.setPermissions(mPermissions);
178
179 getStorageManager().saveShare(share);
180
181 // Update OCFile with data from share: ShareByLink and publicLink
182 OCFile file = getStorageManager().getFileByPath(mPath);
183 if (file!=null) {
184 mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
185 file.setPublicLink(share.getShareLink());
186 file.setShareViaLink(true);
187 getStorageManager().saveFile(file);
188 }
189 }
190
191 }