Add author's line in license
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateShareOperation.java
1 /* ownCloud Android client application
2 *
3 * @author masensio
4 * Copyright (C) 2014 ownCloud Inc.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.operations;
21
22 /**
23 * Creates a new share from a given file
24 */
25
26 import android.content.Context;
27 import android.content.Intent;
28
29 import com.owncloud.android.R;
30 import com.owncloud.android.datamodel.FileDataStorageManager;
31 import com.owncloud.android.datamodel.OCFile;
32 import com.owncloud.android.lib.common.OwnCloudClient;
33 import com.owncloud.android.lib.resources.shares.OCShare;
34 import com.owncloud.android.lib.common.operations.RemoteOperation;
35 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
36 import com.owncloud.android.lib.common.utils.Log_OC;
37 import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
38 import com.owncloud.android.lib.resources.shares.ShareType;
39 import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
40 import com.owncloud.android.lib.resources.files.FileUtils;
41 import com.owncloud.android.operations.common.SyncOperation;
42
43 public class CreateShareOperation extends SyncOperation {
44
45 private static final String TAG = CreateShareOperation.class.getSimpleName();
46
47
48 protected FileDataStorageManager mStorageManager;
49
50 private Context mContext;
51 private String mPath;
52 private ShareType mShareType;
53 private String mShareWith;
54 private boolean mPublicUpload;
55 private String mPassword;
56 private int mPermissions;
57 private Intent mSendIntent;
58
59 /**
60 * Constructor
61 * @param context The context that the share is coming from.
62 * @param path Full path of the file/folder being shared. Mandatory argument
63 * @param shareType 0 = user, 1 = group, 3 = Public link. Mandatory argument
64 * @param shareWith User/group ID with who the file should be shared. This is mandatory for shareType of 0 or 1
65 * @param publicUpload If false (default) public cannot upload to a public shared folder.
66 * If true public can upload to a shared folder. Only available for public link shares
67 * @param password Password to protect a public link share. Only available for public link shares
68 * @param permissions 1 - Read only - Default for public shares
69 * 2 - Update
70 * 4 - Create
71 * 8 - Delete
72 * 16- Re-share
73 * 31- All above - Default for private shares
74 * For user or group shares.
75 * To obtain combinations, add the desired values together.
76 * For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27.
77 */
78 public CreateShareOperation(Context context, String path, ShareType shareType, String shareWith, boolean publicUpload,
79 String password, int permissions, Intent sendIntent) {
80
81 mContext = context;
82 mPath = path;
83 mShareType = shareType;
84 mShareWith = shareWith;
85 mPublicUpload = publicUpload;
86 mPassword = password;
87 mPermissions = permissions;
88 mSendIntent = sendIntent;
89 }
90
91 @Override
92 protected RemoteOperationResult run(OwnCloudClient client) {
93 RemoteOperation operation = null;
94
95 // Check if the share link already exists
96 operation = new GetRemoteSharesForFileOperation(mPath, false, false);
97 RemoteOperationResult result = ((GetRemoteSharesForFileOperation)operation).execute(client);
98
99 if (!result.isSuccess() || result.getData().size() <= 0) {
100 operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload, mPassword, mPermissions);
101 result = ((CreateRemoteShareOperation)operation).execute(client);
102 }
103
104 if (result.isSuccess()) {
105 if (result.getData().size() > 0) {
106 OCShare share = (OCShare) result.getData().get(0);
107 updateData(share);
108 }
109 }
110
111 return result;
112 }
113
114
115 public Intent getSendIntent() {
116 return mSendIntent;
117 }
118
119 private void updateData(OCShare share) {
120 // Update DB with the response
121 share.setPath(mPath);
122 if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
123 share.setIsFolder(true);
124 } else {
125 share.setIsFolder(false);
126 }
127 share.setPermissions(mPermissions);
128
129 getStorageManager().saveShare(share);
130
131 // Update OCFile with data from share: ShareByLink and publicLink
132 OCFile file = getStorageManager().getFileByPath(mPath);
133 if (file!=null) {
134 mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
135 mSendIntent.putExtra(Intent.EXTRA_SUBJECT, String.format(mContext.getString(R.string.subject_token),
136 getClient().getCredentials().getUsername(), file.getFileName()));
137 file.setPublicLink(share.getShareLink());
138 file.setShareByLink(true);
139 getStorageManager().saveFile(file);
140 Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
141
142 }
143 }
144
145 }