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