Fixed bug: a file already shared with users couldn't be shared via link
[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 public class CreateShareViaLinkOperation extends SyncOperation {
45
46 private String mPath;
47 private String mPassword;
48 private Intent mSendIntent;
49 private String mFileName;
50
51 /**
52 * Constructor
53 * @param path Full path of the file/folder being shared. Mandatory argument
54 * @param password Password to protect a public link share.
55 * Only available for public link shares
56 * @param sendIntent Optional Intent with the information of an app where the link to the new share (if public)
57 * should be posted later.
58 */
59 public CreateShareViaLinkOperation(
60 String path,
61 String password,
62 Intent sendIntent
63 ) {
64
65 mPath = path;
66 mPassword = password;
67 mSendIntent = sendIntent;
68 mFileName = null;
69 }
70
71 @Override
72 protected RemoteOperationResult run(OwnCloudClient client) {
73 // Check if the share link already exists
74 RemoteOperation operation = new GetRemoteSharesForFileOperation(mPath, false, false);
75 RemoteOperationResult result = operation.execute(client);
76
77 // Create public link if doesn't exist yet
78 boolean publicShareExists = false;
79 if (result.isSuccess()) {
80 OCShare share = null;
81 for (int i=0 ; i<result.getData().size(); i++) {
82 share = (OCShare) result.getData().get(i);
83 if (ShareType.PUBLIC_LINK.equals(share.getShareType())) {
84 publicShareExists = true;
85 break;
86 }
87 }
88 }
89 if (!publicShareExists) {
90 CreateRemoteShareOperation createOp = new CreateRemoteShareOperation(
91 mPath,
92 ShareType.PUBLIC_LINK,
93 "",
94 false,
95 mPassword,
96 OCShare.DEFAULT_PERMISSION
97 );
98 createOp.setGetShareDetails(true);
99 result = createOp.execute(client);
100 }
101
102 if (result.isSuccess()) {
103 if (result.getData().size() > 0) {
104 OCShare share = (OCShare) result.getData().get(0);
105 updateData(share);
106 }
107 }
108
109 return result;
110 }
111
112 public String getPath() {
113 return mPath;
114 }
115
116 public String getPassword() {
117 return mPassword;
118 }
119
120 public Intent getSendIntent() {
121 return mSendIntent;
122 }
123
124 public Intent getSendIntentWithSubject(Context context) {
125 if (context != null && mSendIntent != null && mSendIntent.getStringExtra(Intent.EXTRA_SUBJECT) != null) {
126 if (getClient() == null || getClient().getCredentials() == null ||
127 getClient().getCredentials().getUsername() == null) {
128 mSendIntent.putExtra(
129 Intent.EXTRA_SUBJECT,
130 context.getString(R.string.subject_shared_with_you, mFileName)
131 );
132 } else {
133 mSendIntent.putExtra(
134 Intent.EXTRA_SUBJECT,
135 context.getString(
136 R.string.subject_user_shared_with_you,
137 getClient().getCredentials().getUsername(),
138 mFileName
139 )
140 );
141 }
142 }
143 return mSendIntent;
144 }
145
146 private void updateData(OCShare share) {
147 // Update DB with the response
148 share.setPath(mPath);
149 if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
150 share.setIsFolder(true);
151 } else {
152 share.setIsFolder(false);
153 }
154
155 getStorageManager().saveShare(share);
156
157 // Update OCFile with data from share: ShareByLink and publicLink
158 OCFile file = getStorageManager().getFileByPath(mPath);
159 if (file!=null) {
160 file.setPublicLink(share.getShareLink());
161 file.setShareViaLink(true);
162 getStorageManager().saveFile(file);
163 if (mSendIntent != null) {
164 mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
165 }
166 }
167 }
168
169 }