UnshareOperation.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2015 ownCloud Inc.
  7. * Copyright (C) 2018 Andy Scherzinger
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  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 General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.operations;
  22. import com.owncloud.android.datamodel.FileDataStorageManager;
  23. import com.owncloud.android.datamodel.OCFile;
  24. import com.owncloud.android.lib.common.OwnCloudClient;
  25. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  26. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  27. import com.owncloud.android.lib.common.utils.Log_OC;
  28. import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
  29. import com.owncloud.android.lib.resources.shares.OCShare;
  30. import com.owncloud.android.lib.resources.shares.RemoveShareRemoteOperation;
  31. import com.owncloud.android.lib.resources.shares.ShareType;
  32. import com.owncloud.android.operations.common.SyncOperation;
  33. import java.util.List;
  34. /**
  35. * Unshare file/folder Save the data in Database
  36. */
  37. public class UnshareOperation extends SyncOperation {
  38. private static final String TAG = UnshareOperation.class.getSimpleName();
  39. private static final int SINGLY_SHARED = 1;
  40. private final String remotePath;
  41. private final long shareId;
  42. public UnshareOperation(String remotePath, long shareId, FileDataStorageManager storageManager) {
  43. super(storageManager);
  44. this.remotePath = remotePath;
  45. this.shareId = shareId;
  46. }
  47. @Override
  48. protected RemoteOperationResult run(OwnCloudClient client) {
  49. RemoteOperationResult result;
  50. // Get Share for a file
  51. OCShare share = getStorageManager().getShareById(shareId);
  52. if (share != null) {
  53. OCFile file = getStorageManager().getFileByEncryptedRemotePath(remotePath);
  54. RemoveShareRemoteOperation operation = new RemoveShareRemoteOperation(share.getRemoteId());
  55. result = operation.execute(client);
  56. if (result.isSuccess()) {
  57. Log_OC.d(TAG, "Share id = " + share.getRemoteId() + " deleted");
  58. if (ShareType.PUBLIC_LINK.equals(share.getShareType())) {
  59. file.setSharedViaLink(false);
  60. } else if (ShareType.USER.equals(share.getShareType()) || ShareType.GROUP.equals(share.getShareType())
  61. || ShareType.FEDERATED.equals(share.getShareType())) {
  62. // Check if it is the last share
  63. List<OCShare> sharesWith = getStorageManager().
  64. getSharesWithForAFile(remotePath,
  65. getStorageManager().getAccount().name);
  66. if (sharesWith.size() == SINGLY_SHARED) {
  67. file.setSharedWithSharee(false);
  68. }
  69. }
  70. getStorageManager().saveFile(file);
  71. getStorageManager().removeShare(share);
  72. } else if (result.getCode() != ResultCode.MAINTENANCE_MODE && !existsFile(client, file.getRemotePath())) {
  73. // unshare failed because file was deleted before
  74. getStorageManager().removeFile(file, true, true);
  75. }
  76. } else {
  77. result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND);
  78. }
  79. return result;
  80. }
  81. private boolean existsFile(OwnCloudClient client, String remotePath) {
  82. return new ExistenceCheckRemoteOperation(remotePath, false).execute(client).isSuccess();
  83. }
  84. }