UnshareOperation.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 android.content.Context;
  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.RemoveRemoteShareOperation;
  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
  36. * Save the data in Database
  37. */
  38. public class UnshareOperation extends SyncOperation {
  39. private static final String TAG = UnshareOperation.class.getSimpleName();
  40. private static final int SINGLY_SHARED = 1;
  41. private String mRemotePath;
  42. private ShareType mShareType;
  43. private String mShareWith;
  44. private Context mContext;
  45. public UnshareOperation(String remotePath, ShareType shareType, String shareWith,
  46. Context context) {
  47. mRemotePath = remotePath;
  48. mShareType = shareType;
  49. mShareWith = shareWith;
  50. mContext = context;
  51. }
  52. @Override
  53. protected RemoteOperationResult run(OwnCloudClient client) {
  54. RemoteOperationResult result = null;
  55. // Get Share for a file
  56. OCShare share = getStorageManager().getFirstShareByPathAndType(mRemotePath,
  57. mShareType, mShareWith);
  58. if (share != null) {
  59. OCFile file = getStorageManager().getFileByPath(mRemotePath);
  60. RemoveRemoteShareOperation operation =
  61. new RemoveRemoteShareOperation((int) share.getRemoteId());
  62. result = operation.execute(client);
  63. if (result.isSuccess()) {
  64. Log_OC.d(TAG, "Share id = " + share.getRemoteId() + " deleted");
  65. if (ShareType.PUBLIC_LINK.equals(mShareType)) {
  66. file.setShareViaLink(false);
  67. file.setPublicLink("");
  68. } else if (ShareType.USER.equals(mShareType) || ShareType.GROUP.equals(mShareType)
  69. || ShareType.FEDERATED.equals(mShareType)){
  70. // Check if it is the last share
  71. List <OCShare> sharesWith = getStorageManager().
  72. getSharesWithForAFile(mRemotePath,
  73. getStorageManager().getAccount().name);
  74. if (sharesWith.size() == SINGLY_SHARED) {
  75. file.setShareWithSharee(false);
  76. }
  77. }
  78. getStorageManager().saveFile(file);
  79. getStorageManager().removeShare(share);
  80. } else if (result.getCode() != ResultCode.MAINTENANCE_MODE && !existsFile(client, file.getRemotePath())) {
  81. // unshare failed because file was deleted before
  82. getStorageManager().removeFile(file, true, true);
  83. }
  84. } else {
  85. result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND);
  86. }
  87. return result;
  88. }
  89. private boolean existsFile(OwnCloudClient client, String remotePath){
  90. ExistenceCheckRemoteOperation existsOperation =
  91. new ExistenceCheckRemoteOperation(remotePath, mContext, false);
  92. RemoteOperationResult result = existsOperation.execute(client);
  93. return result.isSuccess();
  94. }
  95. public ShareType getShareType() {
  96. return mShareType;
  97. }
  98. }