17151abf06befb3fac4b4d881bec4f196eed8e86
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateShareOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 /**
21 * Creates a new share from a given file
22 *
23 * @author masensio
24 *
25 */
26
27 import android.content.Intent;
28
29 import com.owncloud.android.datamodel.FileDataStorageManager;
30 import com.owncloud.android.datamodel.OCFile;
31 import com.owncloud.android.lib.common.OwnCloudClient;
32 import com.owncloud.android.lib.resources.shares.OCShare;
33 import com.owncloud.android.lib.common.operations.RemoteOperation;
34 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
35 import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
36 import com.owncloud.android.lib.resources.shares.ShareType;
37 import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
38 import com.owncloud.android.lib.resources.files.FileUtils;
39 import com.owncloud.android.operations.common.SyncOperation;
40 import com.owncloud.android.utils.Log_OC;
41
42 public class CreateShareOperation extends SyncOperation {
43
44 private static final String TAG = CreateShareOperation.class.getSimpleName();
45
46 // String to build the link with the token of a share: server address + "/public.php?service=files&t=" + token
47 private final String SHARING_LINK_TOKEN = "/public.php?service=files&t=";
48
49 protected FileDataStorageManager mStorageManager;
50
51 private String mPath;
52 private ShareType mShareType;
53 private String mShareWith;
54 private boolean mPublicUpload;
55 private String mPassword;
56 private int mPermissions;
57 private Intent mSendIntent;
58
59 /**
60 * Constructor
61 * @param path Full path of the file/folder being shared. Mandatory argument
62 * @param shareType \910\92 = user, \911\92 = group, \913\92 = Public link. Mandatory argument
63 * @param shareWith User/group ID with who the file should be shared. This is mandatory for shareType of 0 or 1
64 * @param publicUpload If \91false\92 (default) public cannot upload to a public shared folder.
65 * If \91true\92 public can upload to a shared folder. Only available for public link shares
66 * @param password Password to protect a public link share. Only available for public link shares
67 * @param permissions 1 - Read only \96 Default for \93public\94 shares
68 * 2 - Update
69 * 4 - Create
70 * 8 - Delete
71 * 16- Re-share
72 * 31- All above \96 Default for \93private\94 shares
73 * For user or group shares.
74 * To obtain combinations, add the desired values together.
75 * For instance, for \93Re-Share\94, \93delete\94, \93read\94, \93update\94, add 16+8+2+1 = 27.
76 */
77 public CreateShareOperation(String path, ShareType shareType, String shareWith, boolean publicUpload,
78 String password, int permissions, Intent sendIntent) {
79
80 mPath = path;
81 mShareType = shareType;
82 mShareWith = shareWith;
83 mPublicUpload = publicUpload;
84 mPassword = password;
85 mPermissions = permissions;
86 mSendIntent = sendIntent;
87 }
88
89 @Override
90 protected RemoteOperationResult run(OwnCloudClient client) {
91 RemoteOperation operation = null;
92
93 // Check if the share link already exists
94 operation = new GetRemoteSharesForFileOperation(mPath, false, false);
95 RemoteOperationResult result = ((GetRemoteSharesForFileOperation)operation).execute(client);
96
97 if (result.isSuccess()) {
98 if (result.getData().size() > 0) {
99 OCShare share = (OCShare) result.getData().get(0);
100 // Update the link, build it with the token: server address + "/public.php?service=files&t=" + token
101 share.setShareLink(client.getBaseUri() + SHARING_LINK_TOKEN + share.getToken());
102 Log_OC.d(TAG, "Build Share link= " + share.getShareLink());
103 updateData(share);
104 } else {
105 operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload, mPassword, mPermissions);
106 result = ((CreateRemoteShareOperation)operation).execute(client);
107
108 if (result.isSuccess()) {
109 if (result.getData().size() > 0) {
110 OCShare share = (OCShare) result.getData().get(0);
111 updateData(share);
112 }
113 }
114 }
115 }
116 return result;
117 }
118
119
120 public Intent getSendIntent() {
121 return mSendIntent;
122 }
123
124 private void updateData(OCShare share) {
125 // Update DB with the response
126 share.setPath(mPath);
127 if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
128 share.setIsFolder(true);
129 } else {
130 share.setIsFolder(false);
131 }
132 share.setPermissions(mPermissions);
133
134 getStorageManager().saveShare(share);
135
136 // Update OCFile with data from share: ShareByLink and publicLink
137 OCFile file = getStorageManager().getFileByPath(mPath);
138 if (file!=null) {
139 mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
140 file.setPublicLink(share.getShareLink());
141 file.setShareByLink(true);
142 getStorageManager().saveFile(file);
143 Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
144
145 }
146 }
147
148 }