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