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