2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.operations
;
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
;
34 import java
.util
.Calendar
;
38 * Updates an existing public share for a given file
41 public class UpdateShareViaLinkOperation
extends SyncOperation
{
44 private String mPassword
;
45 private long mExpirationDateInMillis
;
50 * @param path Full path of the file/folder being shared. Mandatory argument
52 public UpdateShareViaLinkOperation(String path
) {
56 mExpirationDateInMillis
= 0;
61 * Set password to update in public link.
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.
67 public void setPassword(String password
) {
73 * Set expiration date to update in Share resource.
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.
80 public void setExpirationDate(long expirationDateInMillis
) {
81 mExpirationDateInMillis
= expirationDateInMillis
;
86 protected RemoteOperationResult
run(OwnCloudClient client
) {
88 OCShare publicShare
= getStorageManager().getFirstShareByPathAndType(
90 ShareType
.PUBLIC_LINK
,
94 if (publicShare
== null
) {
95 // TODO try to get remote share before failing?
96 return new RemoteOperationResult(
97 RemoteOperationResult
.ResultCode
.SHARE_NOT_FOUND
101 // Update remote share with password
102 UpdateRemoteShareOperation udpateOp
= new UpdateRemoteShareOperation(
103 publicShare
.getRemoteId()
105 udpateOp
.setPassword(mPassword
);
106 udpateOp
.setExpirationDate(mExpirationDateInMillis
);
107 RemoteOperationResult result
= udpateOp
.execute(client
);
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);
122 public String
getPath() {
126 public String
getPassword() {
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
);
136 share
.setIsFolder(false
);
139 getStorageManager().saveShare(share
); // TODO info about having a password? ask to Gonzalo
141 // Update OCFile with data from share: ShareByLink and publicLink
142 // TODO check & remove if not needed
143 OCFile file
= getStorageManager().getFileByPath(mPath
);
145 file
.setPublicLink(share
.getShareLink());
146 file
.setShareViaLink(true
);
147 getStorageManager().saveFile(file
);