UnshareOperation.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. *
  19. */
  20. package com.owncloud.android.operations;
  21. import android.content.Context;
  22. import com.owncloud.android.datamodel.OCFile;
  23. import com.owncloud.android.lib.common.OwnCloudClient;
  24. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  25. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  26. import com.owncloud.android.lib.common.utils.Log_OC;
  27. import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
  28. import com.owncloud.android.lib.resources.shares.OCShare;
  29. import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation;
  30. import com.owncloud.android.lib.resources.shares.ShareType;
  31. import com.owncloud.android.operations.common.SyncOperation;
  32. import java.util.ArrayList;
  33. /**
  34. * Unshare file/folder
  35. * Save the data in Database
  36. */
  37. public class UnshareOperation extends SyncOperation {
  38. private static final String TAG = UnshareOperation.class.getSimpleName();
  39. private String mRemotePath;
  40. private ShareType mShareType;
  41. private String mShareWith;
  42. private Context mContext;
  43. public UnshareOperation(String remotePath, ShareType shareType, String shareWith,
  44. Context context) {
  45. mRemotePath = remotePath;
  46. mShareType = shareType;
  47. mShareWith = shareWith;
  48. mContext = context;
  49. }
  50. @Override
  51. protected RemoteOperationResult run(OwnCloudClient client) {
  52. RemoteOperationResult result = null;
  53. // Get Share for a file
  54. OCShare share = getStorageManager().getFirstShareByPathAndType(mRemotePath,
  55. mShareType, mShareWith);
  56. if (share != null) {
  57. OCFile file = getStorageManager().getFileByPath(mRemotePath);
  58. RemoveRemoteShareOperation operation =
  59. new RemoveRemoteShareOperation((int) share.getRemoteId());
  60. result = operation.execute(client);
  61. if (result.isSuccess()) {
  62. Log_OC.d(TAG, "Share id = " + share.getRemoteId() + " deleted");
  63. if (ShareType.PUBLIC_LINK.equals(mShareType)) {
  64. file.setShareViaLink(false);
  65. file.setPublicLink("");
  66. } else if (ShareType.USER.equals(mShareType) || ShareType.GROUP.equals(mShareType)
  67. || ShareType.FEDERATED.equals(mShareType)){
  68. // Check if it is the last share
  69. ArrayList <OCShare> sharesWith = getStorageManager().
  70. getSharesWithForAFile(mRemotePath,
  71. getStorageManager().getAccount().name);
  72. if (sharesWith.size() == 1) {
  73. file.setShareWithSharee(false);
  74. }
  75. }
  76. getStorageManager().saveFile(file);
  77. getStorageManager().removeShare(share);
  78. } else if (result.getCode() != ResultCode.MAINTENANCE_MODE && !existsFile(client, file.getRemotePath())) {
  79. // unshare failed because file was deleted before
  80. getStorageManager().removeFile(file, true, true);
  81. }
  82. } else {
  83. result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND);
  84. }
  85. return result;
  86. }
  87. private boolean existsFile(OwnCloudClient client, String remotePath){
  88. ExistenceCheckRemoteOperation existsOperation =
  89. new ExistenceCheckRemoteOperation(remotePath, mContext, false);
  90. RemoteOperationResult result = existsOperation.execute(client);
  91. return result.isSuccess();
  92. }
  93. }