RemoveFileOperation.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author masensio
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.operations;
  22. import com.owncloud.android.MainApp;
  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.resources.files.RemoveRemoteFileOperation;
  28. import com.owncloud.android.operations.common.SyncOperation;
  29. /**
  30. * Remote operation performing the removal of a remote file or folder in the ownCloud server.
  31. */
  32. public class RemoveFileOperation extends SyncOperation {
  33. // private static final String TAG = RemoveFileOperation.class.getSimpleName();
  34. OCFile mFileToRemove;
  35. String mRemotePath;
  36. boolean mOnlyLocalCopy;
  37. /**
  38. * Constructor
  39. *
  40. * @param remotePath RemotePath of the OCFile instance describing the remote file or
  41. * folder to remove from the server
  42. * @param onlyLocalCopy When 'true', and a local copy of the file exists, only this is
  43. * removed.
  44. */
  45. public RemoveFileOperation(String remotePath, boolean onlyLocalCopy) {
  46. mRemotePath = remotePath;
  47. mOnlyLocalCopy = onlyLocalCopy;
  48. }
  49. /**
  50. * Getter for the file to remove (or removed, if the operation was successfully performed).
  51. *
  52. * @return File to remove or already removed.
  53. */
  54. public OCFile getFile() {
  55. return mFileToRemove;
  56. }
  57. /**
  58. * Performs the remove operation
  59. *
  60. * @param client Client object to communicate with the remote ownCloud server.
  61. */
  62. @Override
  63. protected RemoteOperationResult run(OwnCloudClient client) {
  64. RemoteOperationResult result = null;
  65. mFileToRemove = getStorageManager().getFileByPath(mRemotePath);
  66. boolean localRemovalFailed = false;
  67. if (!mOnlyLocalCopy) {
  68. RemoveRemoteFileOperation operation = new RemoveRemoteFileOperation(mRemotePath);
  69. result = operation.execute(client);
  70. if (result.isSuccess() || result.getCode() == ResultCode.FILE_NOT_FOUND) {
  71. localRemovalFailed = !(getStorageManager().removeFile(mFileToRemove, true, true));
  72. }
  73. } else {
  74. localRemovalFailed = !(getStorageManager().removeFile(mFileToRemove, false, true));
  75. if (!localRemovalFailed) {
  76. result = new RemoteOperationResult(ResultCode.OK);
  77. }
  78. }
  79. if (localRemovalFailed) {
  80. result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_REMOVED);
  81. }
  82. return result;
  83. }
  84. }