List view is shown completelly on the ShareFileFragment
[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 operation = new CreateRemoteShareOperation(
93 mPath,
94 ShareType.PUBLIC_LINK,
95 "",
96 false,
97 mPassword,
98 OCShare.DEFAULT_PERMISSION
99 );
100 result = operation.execute(client);
101 }
102
103 if (result.isSuccess()) {
104 if (result.getData().size() > 0) {
105 OCShare share = (OCShare) result.getData().get(0);
106 updateData(share);
107 }
108 }
109
110 return result;
111 }
112
113 public String getPath() {
114 return mPath;
115 }
116
117 public String getPassword() {
118 return mPassword;
119 }
120
121 public Intent getSendIntent() {
122 return mSendIntent;
123 }
124
125 public Intent getSendIntentWithSubject(Context context) {
126 if (context != null && mSendIntent != null && mSendIntent.getStringExtra(Intent.EXTRA_SUBJECT) != null) {
127 if (getClient() == null || getClient().getCredentials() == null ||
128 getClient().getCredentials().getUsername() == null) {
129 mSendIntent.putExtra(
130 Intent.EXTRA_SUBJECT,
131 context.getString(R.string.subject_shared_with_you, mFileName)
132 );
133 } else {
134 mSendIntent.putExtra(
135 Intent.EXTRA_SUBJECT,
136 context.getString(
137 R.string.subject_user_shared_with_you,
138 getClient().getCredentials().getUsername(),
139 mFileName
140 )
141 );
142 }
143 }
144 return mSendIntent;
145 }
146
147 private void updateData(OCShare share) {
148 // Update DB with the response
149 share.setPath(mPath);
150 if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
151 share.setIsFolder(true);
152 } else {
153 share.setIsFolder(false);
154 }
155
156 getStorageManager().saveShare(share);
157
158 // Update OCFile with data from share: ShareByLink and publicLink
159 OCFile file = getStorageManager().getFileByPath(mPath);
160 if (file!=null) {
161 file.setPublicLink(share.getShareLink());
162 file.setShareViaLink(true);
163 getStorageManager().saveFile(file);
164 if (mSendIntent != null) {
165 mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
166 }
167 }
168 }
169
170 }