UnshareLinkOperation.java 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* ownCloud Android client application
  2. * Copyright (C) 2014 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.operations;
  18. import android.content.Context;
  19. import com.owncloud.android.datamodel.OCFile;
  20. import com.owncloud.android.lib.network.OwnCloudClient;
  21. import com.owncloud.android.lib.operations.common.OCShare;
  22. import com.owncloud.android.lib.operations.common.RemoteOperationResult;
  23. import com.owncloud.android.lib.operations.common.ShareType;
  24. import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode;
  25. import com.owncloud.android.lib.operations.remote.ExistenceCheckRemoteOperation;
  26. import com.owncloud.android.lib.operations.remote.RemoveRemoteShareOperation;
  27. import com.owncloud.android.operations.common.SyncOperation;
  28. import com.owncloud.android.utils.Log_OC;
  29. /**
  30. * Unshare file/folder
  31. * Save the data in Database
  32. *
  33. * @author masensio
  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. }