UnshareLinkOperation.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* ownCloud Android client application
  2. *
  3. * @author masensio
  4. * Copyright (C) 2014 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.operations;
  20. import android.content.Context;
  21. import com.owncloud.android.datamodel.OCFile;
  22. import com.owncloud.android.lib.common.OwnCloudClient;
  23. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  24. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  25. import com.owncloud.android.lib.common.utils.Log_OC;
  26. import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
  27. import com.owncloud.android.lib.resources.shares.OCShare;
  28. import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation;
  29. import com.owncloud.android.lib.resources.shares.ShareType;
  30. import com.owncloud.android.operations.common.SyncOperation;
  31. /**
  32. * Unshare file/folder
  33. * Save the data in Database
  34. */
  35. public class UnshareLinkOperation extends SyncOperation {
  36. private static final String TAG = UnshareLinkOperation.class.getSimpleName();
  37. private String mRemotePath;
  38. private Context mContext;
  39. public UnshareLinkOperation(String remotePath, Context context) {
  40. mRemotePath = remotePath;
  41. mContext = context;
  42. }
  43. @Override
  44. protected RemoteOperationResult run(OwnCloudClient client) {
  45. RemoteOperationResult result = null;
  46. // Get Share for a file
  47. OCShare share = getStorageManager().getFirstShareByPathAndType(mRemotePath, ShareType.PUBLIC_LINK);
  48. if (share != null) {
  49. RemoveRemoteShareOperation operation = new RemoveRemoteShareOperation((int) share.getIdRemoteShared());
  50. result = operation.execute(client);
  51. if (result.isSuccess() || result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  52. Log_OC.d(TAG, "Share id = " + share.getIdRemoteShared() + " deleted");
  53. OCFile file = getStorageManager().getFileByPath(mRemotePath);
  54. file.setShareByLink(false);
  55. file.setPublicLink("");
  56. getStorageManager().saveFile(file);
  57. getStorageManager().removeShare(share);
  58. if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  59. if (existsFile(client, file.getRemotePath())) {
  60. result = new RemoteOperationResult(ResultCode.OK);
  61. } else {
  62. getStorageManager().removeFile(file, true, true);
  63. }
  64. }
  65. }
  66. } else {
  67. result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND);
  68. }
  69. return result;
  70. }
  71. private boolean existsFile(OwnCloudClient client, String remotePath){
  72. ExistenceCheckRemoteOperation existsOperation = new ExistenceCheckRemoteOperation(remotePath, mContext, false);
  73. RemoteOperationResult result = existsOperation.execute(client);
  74. return result.isSuccess();
  75. }
  76. }