Merge branch 'share_link__new_share' into share_link__new_share_menu
[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 com.owncloud.android.datamodel.FileDataStorageManager;
28 import com.owncloud.android.datamodel.OCFile;
29 import com.owncloud.android.lib.network.OwnCloudClient;
30 import com.owncloud.android.lib.operations.common.OCShare;
31 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
32 import com.owncloud.android.lib.operations.common.ShareType;
33 import com.owncloud.android.lib.operations.remote.CreateShareRemoteOperation;
34 import com.owncloud.android.lib.utils.FileUtils;
35 import com.owncloud.android.operations.common.SyncOperation;
36 import com.owncloud.android.utils.Log_OC;
37
38 public class CreateShareOperation extends SyncOperation {
39
40 private static final String TAG = CreateShareOperation.class.getSimpleName();
41
42 protected FileDataStorageManager mStorageManager;
43
44 private String mPath;
45 private ShareType mShareType;
46 private String mShareWith;
47 private boolean mPublicUpload;
48 private String mPassword;
49 private int mPermissions;
50
51 /**
52 * Constructor
53 * @param path Full path of the file/folder being shared. Mandatory argument
54 * @param shareType \910\92 = user, \911\92 = group, \913\92 = Public link. Mandatory argument
55 * @param shareWith User/group ID with who the file should be shared. This is mandatory for shareType of 0 or 1
56 * @param publicUpload If \91false\92 (default) public cannot upload to a public shared folder.
57 * If \91true\92 public can upload to a shared folder. Only available for public link shares
58 * @param password Password to protect a public link share. Only available for public link shares
59 * @param permissions 1 - Read only \96 Default for \93public\94 shares
60 * 2 - Update
61 * 4 - Create
62 * 8 - Delete
63 * 16- Re-share
64 * 31- All above \96 Default for \93private\94 shares
65 * For user or group shares.
66 * To obtain combinations, add the desired values together.
67 * For instance, for \93Re-Share\94, \93delete\94, \93read\94, \93update\94, add 16+8+2+1 = 27.
68 */
69 public CreateShareOperation(String path, ShareType shareType, String shareWith, boolean publicUpload,
70 String password, int permissions) {
71
72 mPath = path;
73 mShareType = shareType;
74 mShareWith = shareWith;
75 mPublicUpload = publicUpload;
76 mPassword = password;
77 mPermissions = permissions;
78 }
79
80 @Override
81 protected RemoteOperationResult run(OwnCloudClient client) {
82 CreateShareRemoteOperation operation = new CreateShareRemoteOperation(mPath, mShareType, mShareWith, mPublicUpload, mPassword, mPermissions);
83 RemoteOperationResult result = operation.execute(client);
84
85 if (result.isSuccess()) {
86
87 if (result.getData().size() > 0) {
88 OCShare share = (OCShare) result.getData().get(0);
89
90 // Update DB with the response
91 if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
92 share.setPath(mPath.substring(0, mPath.length()-1));
93 share.setIsDirectory(true);
94
95 } else {
96 share.setPath(mPath);
97 share.setIsDirectory(false);
98 }
99 share.setPermissions(mPermissions);
100
101 getStorageManager().saveShare(share);
102
103 // Update OCFile with data from share: ShareByLink and publicLink
104 OCFile file = getStorageManager().getFileByPath(mPath);
105 if (file!=null) {
106 file.setPublicLink(share.getShareLink());
107 getStorageManager().saveFile(file);
108 Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
109
110 }
111 }
112 }
113
114
115 return result;
116 }
117
118 }