Set or clear password to protect public link in ShareFileFragment
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / UpdateShareViaLinkOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.operations;
22
23 import com.owncloud.android.datamodel.OCFile;
24 import com.owncloud.android.lib.common.OwnCloudClient;
25 import com.owncloud.android.lib.common.operations.RemoteOperation;
26 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
27 import com.owncloud.android.lib.resources.files.FileUtils;
28 import com.owncloud.android.lib.resources.shares.GetRemoteShareOperation;
29 import com.owncloud.android.lib.resources.shares.OCShare;
30 import com.owncloud.android.lib.resources.shares.ShareType;
31 import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation;
32 import com.owncloud.android.operations.common.SyncOperation;
33
34
35 /**
36 * Updates an existing public share for a given file
37 */
38
39 public class UpdateShareViaLinkOperation extends SyncOperation {
40
41 private String mPath;
42 private String mPassword;
43
44 /**
45 * Constructor
46 * @param path Full path of the file/folder being shared. Mandatory argument
47 * @param password Password to protect a public link share.
48 */
49 public UpdateShareViaLinkOperation(
50 String path,
51 String password
52 ) {
53
54 mPath = path;
55 mPassword = password;
56 }
57
58 @Override
59 protected RemoteOperationResult run(OwnCloudClient client) {
60
61 OCShare publicShare = getStorageManager().getFirstShareByPathAndType(
62 mPath,
63 ShareType.PUBLIC_LINK,
64 ""
65 );
66
67 if (publicShare == null) {
68 // TODO try to get remote?
69
70 }
71
72 // Update remote share with password
73 RemoteOperation operation = new UpdateRemoteShareOperation(
74 publicShare.getRemoteId()
75 );
76 ((UpdateRemoteShareOperation)operation).setPassword(mPassword);
77 RemoteOperationResult result = operation.execute(client);
78
79 /*
80 if (!result.isSuccess() || result.getData().size() <= 0) {
81 operation = new CreateRemoteShareOperation(
82 mPath,
83 ShareType.PUBLIC_LINK,
84 "",
85 false,
86 mPassword,
87 OCShare.DEFAULT_PERMISSION
88 );
89 result = operation.execute(client);
90 }
91 */
92
93 if (result.isSuccess()) {
94 // Retrieve updated share / save directly with password? -> no; the password is not be saved
95 operation = new GetRemoteShareOperation(publicShare.getRemoteId());
96 result = operation.execute(client);
97 if (result.isSuccess()) {
98 OCShare share = (OCShare) result.getData().get(0);
99 updateData(share);
100 }
101 }
102
103 return result;
104 }
105
106 public String getPath() {
107 return mPath;
108 }
109
110 public String getPassword() {
111 return mPassword;
112 }
113
114 private void updateData(OCShare share) {
115 // Update DB with the response
116 share.setPath(mPath);
117 if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
118 share.setIsFolder(true);
119 } else {
120 share.setIsFolder(false);
121 }
122
123 getStorageManager().saveShare(share); // TODO info about having a password? ask to Gonzalo
124
125 // Update OCFile with data from share: ShareByLink and publicLink
126 // TODO check & remove if not needed
127 OCFile file = getStorageManager().getFileByPath(mPath);
128 if (file != null) {
129 file.setPublicLink(share.getShareLink());
130 file.setShareViaLink(true);
131 getStorageManager().saveFile(file);
132 }
133 }
134
135 }
136