9b71695ecc94cfa577254cab904d32e92efed954
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateShareWithShareeOperation.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 private share for a given file
26 */
27
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.common.operations.RemoteOperationResult;
33 import com.owncloud.android.lib.resources.files.FileUtils;
34 import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
35 import com.owncloud.android.lib.resources.shares.OCShare;
36 import com.owncloud.android.lib.resources.shares.ShareType;
37 import com.owncloud.android.operations.common.SyncOperation;
38
39 public class CreateShareWithShareeOperation extends SyncOperation {
40
41 private static final int READ_ONLY = 1;
42 private static final int ALL_PRIVILEGES = 31;
43
44 protected FileDataStorageManager mStorageManager;
45
46 private String mPath;
47 private String mShareeName;
48 private ShareType mShareType;
49
50 /**
51 * Constructor.
52 *
53 * @param path Full path of the file/folder being shared.
54 * @param shareeName User or group name of the target sharee.
55 * @param shareType Type of share determines type of sharee; {@link ShareType#USER} and {@link ShareType#GROUP}
56 * are the only valid values for the moment.
57 */
58 public CreateShareWithShareeOperation(String path, String shareeName, ShareType shareType) {
59 if (!ShareType.USER.equals(shareType) && !ShareType.GROUP.equals(shareType)) {
60 throw new IllegalArgumentException("Illegal share type " + shareType);
61 }
62 mPath = path;
63 mShareeName = shareeName;
64 mShareType = shareType;
65 }
66
67 @Override
68 protected RemoteOperationResult run(OwnCloudClient client) {
69 // Check if the share link already exists
70 // TODO or not
71 /*
72 RemoteOperation operation = new GetRemoteSharesForFileOperation(mPath, false, false);
73 RemoteOperationResult result = operation.execute(client);
74 if (!result.isSuccess() || result.getData().size() <= 0) {
75 */
76
77 CreateRemoteShareOperation operation = new CreateRemoteShareOperation(
78 mPath,
79 mShareType,
80 mShareeName,
81 false,
82 "",
83 ALL_PRIVILEGES
84 );
85 operation.setGetShareDetails(true);
86 RemoteOperationResult result = operation.execute(client);
87
88
89 if (result.isSuccess()) {
90 if (result.getData().size() > 0) {
91 OCShare share = (OCShare) result.getData().get(0);
92 updateData(share);
93 }
94 }
95
96 return result;
97 }
98
99 public String getPath() {
100 return mPath;
101 }
102
103 private void updateData(OCShare share) {
104 // Update DB with the response
105 share.setPath(mPath);
106 share.setIsFolder(mPath.endsWith(FileUtils.PATH_SEPARATOR));
107 share.setPermissions(READ_ONLY);
108
109 getStorageManager().saveShare(share);
110
111 // Update OCFile with data from share: ShareByLink and publicLink
112 OCFile file = getStorageManager().getFileByPath(mPath);
113 if (file!=null) {
114 file.setShareWithSharee(true); // TODO - this should be done by the FileContentProvider, as part of getStorageManager().saveShare(share)
115 getStorageManager().saveFile(file);
116 }
117 }
118
119 }