Merge remote-tracking branch 'remotes/upstream/externalSD2' into beta
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateShareViaLinkOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 package com.owncloud.android.operations;
23
24 /**
25 * Creates a new public share for a given file
26 */
27
28
29 import android.content.Context;
30 import android.content.Intent;
31
32 import com.owncloud.android.R;
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.resources.files.FileUtils;
38 import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
39 import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
40 import com.owncloud.android.lib.resources.shares.OCShare;
41 import com.owncloud.android.lib.resources.shares.ShareType;
42 import com.owncloud.android.operations.common.SyncOperation;
43
44 import java.util.ArrayList;
45
46 public class CreateShareViaLinkOperation extends SyncOperation {
47
48 private String mPath;
49 private String mPassword;
50 private Intent mSendIntent;
51 private String mFileName;
52
53 /**
54 * Constructor
55 * @param path Full path of the file/folder being shared. Mandatory argument
56 * @param password Password to protect a public link share.
57 * Only available for public link shares
58 * @param sendIntent Optional Intent with the information of an app where the link to the new share (if public)
59 * should be posted later.
60 */
61 public CreateShareViaLinkOperation(
62 String path,
63 String password,
64 Intent sendIntent
65 ) {
66
67 mPath = path;
68 mPassword = password;
69 mSendIntent = sendIntent;
70 mFileName = null;
71 }
72
73 @Override
74 protected RemoteOperationResult run(OwnCloudClient client) {
75 // Check if the share link already exists
76 RemoteOperation operation = new GetRemoteSharesForFileOperation(mPath, false, false);
77 RemoteOperationResult result = operation.execute(client);
78
79 boolean shareByLink = false;
80 // Check if the file is shared by link
81 if (result.isSuccess() && result.getData().size() > 0){
82 ArrayList<Object> shares = result.getData();
83 for(Object object: shares){
84 if (((OCShare) object).getShareType() == ShareType.PUBLIC_LINK){
85 shareByLink = true;
86 break;
87 }
88 }
89 }
90
91 if (!result.isSuccess() || !shareByLink) {
92 CreateRemoteShareOperation createOp = new CreateRemoteShareOperation(
93 mPath,
94 ShareType.PUBLIC_LINK,
95 "",
96 false,
97 mPassword,
98 OCShare.DEFAULT_PERMISSION
99 );
100 createOp.setGetShareDetails(true);
101 result = createOp.execute(client);
102 }
103
104 if (result.isSuccess()) {
105 if (result.getData().size() > 0) {
106 OCShare share = (OCShare) result.getData().get(0);
107 updateData(share);
108 }
109 }
110
111 return result;
112 }
113
114 public String getPath() {
115 return mPath;
116 }
117
118 public String getPassword() {
119 return mPassword;
120 }
121
122 public Intent getSendIntent() {
123 return mSendIntent;
124 }
125
126 public Intent getSendIntentWithSubject(Context context) {
127 if (context != null && mSendIntent != null && mSendIntent.getStringExtra(Intent.EXTRA_SUBJECT) != null) {
128 if (getClient() == null || getClient().getCredentials() == null ||
129 getClient().getCredentials().getUsername() == null) {
130 mSendIntent.putExtra(
131 Intent.EXTRA_SUBJECT,
132 context.getString(R.string.subject_shared_with_you, mFileName)
133 );
134 } else {
135 mSendIntent.putExtra(
136 Intent.EXTRA_SUBJECT,
137 context.getString(
138 R.string.subject_user_shared_with_you,
139 getClient().getCredentials().getUsername(),
140 mFileName
141 )
142 );
143 }
144 }
145 return mSendIntent;
146 }
147
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);
153 } else {
154 share.setIsFolder(false);
155 }
156
157 getStorageManager().saveShare(share);
158
159 // Update OCFile with data from share: ShareByLink and publicLink
160 OCFile file = getStorageManager().getFileByPath(mPath);
161 if (file!=null) {
162 file.setPublicLink(share.getShareLink());
163 file.setShareViaLink(true);
164 getStorageManager().saveFile(file);
165 if (mSendIntent != null) {
166 mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
167 }
168 }
169 }
170
171 }