UnshareOperation.java 4.4 KB

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