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