UpdateShareInfoOperation.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author TSI-mc
  5. * Copyright (C) 2021 TSI-mc
  6. * Copyright (C) 2021 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.operations;
  22. import android.text.TextUtils;
  23. import com.owncloud.android.datamodel.FileDataStorageManager;
  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.shares.GetShareRemoteOperation;
  28. import com.owncloud.android.lib.resources.shares.OCShare;
  29. import com.owncloud.android.lib.resources.shares.UpdateShareRemoteOperation;
  30. import com.owncloud.android.operations.common.SyncOperation;
  31. /**
  32. * Updates an existing private share for a given file.
  33. */
  34. public class UpdateShareInfoOperation extends SyncOperation {
  35. private OCShare share;
  36. private long shareId;
  37. private long expirationDateInMillis;
  38. private String note;
  39. private boolean hideFileDownload;
  40. private int permissions = -1;
  41. private String password;
  42. private String label;
  43. /**
  44. * Constructor
  45. *
  46. * @param share {@link OCShare} to update. Mandatory argument
  47. * <p>
  48. * this will be triggered while creating new share
  49. */
  50. public UpdateShareInfoOperation(OCShare share, FileDataStorageManager storageManager) {
  51. super(storageManager);
  52. this.share = share;
  53. expirationDateInMillis = 0L;
  54. note = null;
  55. }
  56. /**
  57. * Constructor
  58. *
  59. * @param shareId {@link OCShare} to update. Mandatory argument
  60. * <p>
  61. * this will be triggered while modifying existing share
  62. */
  63. public UpdateShareInfoOperation(long shareId, FileDataStorageManager storageManager) {
  64. super(storageManager);
  65. this.shareId = shareId;
  66. expirationDateInMillis = 0L;
  67. note = null;
  68. }
  69. @Override
  70. protected RemoteOperationResult run(OwnCloudClient client) {
  71. OCShare share;
  72. if (shareId > 0) {
  73. share = getStorageManager().getShareById(shareId);
  74. } else {
  75. share = this.share;
  76. }
  77. if (share == null) {
  78. // TODO try to get remote share before failing?
  79. return new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
  80. }
  81. // Update remote share
  82. UpdateShareRemoteOperation updateOp = new UpdateShareRemoteOperation(share.getRemoteId());
  83. updateOp.setExpirationDate(expirationDateInMillis);
  84. updateOp.setHideFileDownload(hideFileDownload);
  85. if (!TextUtils.isEmpty(note)) {
  86. updateOp.setNote(note);
  87. }
  88. if (permissions > -1) {
  89. updateOp.setPermissions(permissions);
  90. }
  91. updateOp.setPassword(password);
  92. updateOp.setLabel(label);
  93. RemoteOperationResult result = updateOp.execute(client);
  94. if (result.isSuccess()) {
  95. RemoteOperation getShareOp = new GetShareRemoteOperation(share.getRemoteId());
  96. result = getShareOp.execute(client);
  97. //only update the share in storage if shareId is available
  98. //this will be triggered by editing existing share
  99. if (result.isSuccess() && shareId > 0) {
  100. OCShare ocShare = (OCShare) result.getData().get(0);
  101. ocShare.setPasswordProtected(!TextUtils.isEmpty(password));
  102. getStorageManager().saveShare(ocShare);
  103. }
  104. }
  105. return result;
  106. }
  107. public void setExpirationDateInMillis(long expirationDateInMillis) {
  108. this.expirationDateInMillis = expirationDateInMillis;
  109. }
  110. public void setNote(String note) {
  111. this.note = note;
  112. }
  113. public void setHideFileDownload(boolean hideFileDownload) {
  114. this.hideFileDownload = hideFileDownload;
  115. }
  116. public void setPermissions(int permissions) {
  117. this.permissions = permissions;
  118. }
  119. public void setPassword(String password) {
  120. this.password = password;
  121. }
  122. public void setLabel(String label) {
  123. this.label = label;
  124. }
  125. }