Set or clear expiration date 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 import java.util.Calendar;
35
36
37 /**
38 * Updates an existing public share for a given file
39 */
40
41 public class UpdateShareViaLinkOperation extends SyncOperation {
42
43 private String mPath;
44 private String mPassword;
45 private Calendar mExpirationDate;
46
47 /**
48 * Constructor
49 *
50 * @param path Full path of the file/folder being shared. Mandatory argument
51 */
52 public UpdateShareViaLinkOperation(String path) {
53
54 mPath = path;
55 mPassword = null;
56 mExpirationDate = null;
57 }
58
59
60 /**
61 * Set password to update in public link.
62 *
63 * @param password Password to set to the public link.
64 * Empty string clears the current password.
65 * Null results in no update applied to the password.
66 */
67 public void setPassword(String password) {
68 mPassword = password;
69 }
70
71
72 /**
73 * Set expiration date to update in Share resource.
74 *
75 * @param expirationDate Expiration date to set to the public link.
76 * Start-of-epoch clears the current expiration date.
77 * Null results in no update applied to the expiration date.
78 */
79 public void setExpirationDate(Calendar expirationDate) {
80 mExpirationDate = expirationDate;
81 }
82
83
84 @Override
85 protected RemoteOperationResult run(OwnCloudClient client) {
86
87 OCShare publicShare = getStorageManager().getFirstShareByPathAndType(
88 mPath,
89 ShareType.PUBLIC_LINK,
90 ""
91 );
92
93 if (publicShare == null) {
94 // TODO try to get remote?
95
96 }
97
98 // Update remote share with password
99 UpdateRemoteShareOperation udpateOp = new UpdateRemoteShareOperation(
100 publicShare.getRemoteId()
101 );
102 udpateOp.setPassword(mPassword);
103 udpateOp.setExpirationDate(mExpirationDate);
104 RemoteOperationResult result = udpateOp.execute(client);
105
106 /*
107 if (!result.isSuccess() || result.getData().size() <= 0) {
108 operation = new CreateRemoteShareOperation(
109 mPath,
110 ShareType.PUBLIC_LINK,
111 "",
112 false,
113 mPassword,
114 OCShare.DEFAULT_PERMISSION
115 );
116 result = operation.execute(client);
117 }
118 */
119
120 if (result.isSuccess()) {
121 // Retrieve updated share / save directly with password? -> no; the password is not be saved
122 RemoteOperation getShareOp = new GetRemoteShareOperation(publicShare.getRemoteId());
123 result = getShareOp.execute(client);
124 if (result.isSuccess()) {
125 OCShare share = (OCShare) result.getData().get(0);
126 updateData(share);
127 }
128 }
129
130 return result;
131 }
132
133 public String getPath() {
134 return mPath;
135 }
136
137 public String getPassword() {
138 return mPassword;
139 }
140
141 private void updateData(OCShare share) {
142 // Update DB with the response
143 share.setPath(mPath);
144 if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
145 share.setIsFolder(true);
146 } else {
147 share.setIsFolder(false);
148 }
149
150 getStorageManager().saveShare(share); // TODO info about having a password? ask to Gonzalo
151
152 // Update OCFile with data from share: ShareByLink and publicLink
153 // TODO check & remove if not needed
154 OCFile file = getStorageManager().getFileByPath(mPath);
155 if (file != null) {
156 file.setPublicLink(share.getShareLink());
157 file.setShareViaLink(true);
158 getStorageManager().saveFile(file);
159 }
160 }
161
162 }
163