RemoveFileOperation.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-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 com.owncloud.android.datamodel.OCFile;
  19. import com.owncloud.android.lib.common.OwnCloudClient;
  20. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  21. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  22. import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
  23. import com.owncloud.android.operations.common.SyncOperation;
  24. /**
  25. * Remote operation performing the removal of a remote file or folder in the ownCloud server.
  26. *
  27. * @author David A. Velasco
  28. * @author masensio
  29. */
  30. public class RemoveFileOperation extends SyncOperation {
  31. // private static final String TAG = RemoveFileOperation.class.getSimpleName();
  32. OCFile mFileToRemove;
  33. String mRemotePath;
  34. boolean mOnlyLocalCopy;
  35. /**
  36. * Constructor
  37. *
  38. * @param remotePath RemotePath of the OCFile instance describing the remote file or
  39. * folder to remove from the server
  40. * @param onlyLocalCopy When 'true', and a local copy of the file exists, only this is
  41. * removed.
  42. */
  43. public RemoveFileOperation(String remotePath, boolean onlyLocalCopy) {
  44. mRemotePath = remotePath;
  45. mOnlyLocalCopy = onlyLocalCopy;
  46. }
  47. /**
  48. * Getter for the file to remove (or removed, if the operation was successfully performed).
  49. *
  50. * @return File to remove or already removed.
  51. */
  52. public OCFile getFile() {
  53. return mFileToRemove;
  54. }
  55. /**
  56. * Performs the remove operation
  57. *
  58. * @param client Client object to communicate with the remote ownCloud server.
  59. */
  60. @Override
  61. protected RemoteOperationResult run(OwnCloudClient client) {
  62. RemoteOperationResult result = null;
  63. mFileToRemove = getStorageManager().getFileByPath(mRemotePath);
  64. boolean localRemovalFailed = false;
  65. if (!mOnlyLocalCopy) {
  66. RemoveRemoteFileOperation operation = new RemoveRemoteFileOperation(mRemotePath);
  67. result = operation.execute(client);
  68. if (result.isSuccess() || result.getCode() == ResultCode.FILE_NOT_FOUND) {
  69. localRemovalFailed = !(getStorageManager().removeFile(mFileToRemove, true, true));
  70. }
  71. } else {
  72. localRemovalFailed = !(getStorageManager().removeFile(mFileToRemove, false, true));
  73. if (!localRemovalFailed) {
  74. result = new RemoteOperationResult(ResultCode.OK);
  75. }
  76. }
  77. if (localRemovalFailed) {
  78. result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_REMOVED);
  79. }
  80. return result;
  81. }
  82. }