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