Show current expiration date selected in date picker while modifying it
[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 long mExpirationDateInMillis;
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 mExpirationDateInMillis = 0;
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 expirationDateInMillis Expiration date to set to the public link.
76 * A negative value clears the current expiration date.
77 * Zero value (start-of-epoch) results in no update done on
78 * the expiration date.
79 */
80 public void setExpirationDate(long expirationDateInMillis) {
81 mExpirationDateInMillis = expirationDateInMillis;
82 }
83
84
85 @Override
86 protected RemoteOperationResult run(OwnCloudClient client) {
87
88 OCShare publicShare = getStorageManager().getFirstShareByPathAndType(
89 mPath,
90 ShareType.PUBLIC_LINK,
91 ""
92 );
93
94 if (publicShare == null) {
95 // TODO try to get remote share before failing?
96 return new RemoteOperationResult(
97 RemoteOperationResult.ResultCode.SHARE_NOT_FOUND
98 );
99 }
100
101 // Update remote share with password
102 UpdateRemoteShareOperation udpateOp = new UpdateRemoteShareOperation(
103 publicShare.getRemoteId()
104 );
105 udpateOp.setPassword(mPassword);
106 udpateOp.setExpirationDate(mExpirationDateInMillis);
107 RemoteOperationResult result = udpateOp.execute(client);
108
109 if (result.isSuccess()) {
110 // Retrieve updated share / save directly with password? -> no; the password is not be saved
111 RemoteOperation getShareOp = new GetRemoteShareOperation(publicShare.getRemoteId());
112 result = getShareOp.execute(client);
113 if (result.isSuccess()) {
114 OCShare share = (OCShare) result.getData().get(0);
115 updateData(share);
116 }
117 }
118
119 return result;
120 }
121
122 public String getPath() {
123 return mPath;
124 }
125
126 public String getPassword() {
127 return mPassword;
128 }
129
130 private void updateData(OCShare share) {
131 // Update DB with the response
132 share.setPath(mPath);
133 if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
134 share.setIsFolder(true);
135 } else {
136 share.setIsFolder(false);
137 }
138
139 getStorageManager().saveShare(share); // TODO info about having a password? ask to Gonzalo
140
141 // Update OCFile with data from share: ShareByLink and publicLink
142 // TODO check & remove if not needed
143 OCFile file = getStorageManager().getFileByPath(mPath);
144 if (file != null) {
145 file.setPublicLink(share.getShareLink());
146 file.setShareViaLink(true);
147 getStorageManager().saveFile(file);
148 }
149 }
150
151 }
152