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