RemoveFileOperation.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author masensio
  6. * @author Tobias Kaminsky
  7. * Copyright (C) 2015 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.owncloud.android.operations;
  23. import android.accounts.Account;
  24. import android.content.Context;
  25. import com.owncloud.android.datamodel.OCFile;
  26. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  27. import com.owncloud.android.lib.common.OwnCloudClient;
  28. import com.owncloud.android.lib.common.operations.RemoteOperation;
  29. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  30. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  31. import com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation;
  32. import com.owncloud.android.operations.common.SyncOperation;
  33. import com.owncloud.android.utils.MimeTypeUtil;
  34. /**
  35. * Remote operation performing the removal of a remote file or folder in the ownCloud server.
  36. */
  37. public class RemoveFileOperation extends SyncOperation {
  38. private OCFile fileToRemove;
  39. private boolean onlyLocalCopy;
  40. private Account account;
  41. private boolean inBackground;
  42. private Context context;
  43. /**
  44. * Constructor
  45. *
  46. * @param fileToRemove OCFile instance describing the remote file or folder to remove from the server
  47. * @param onlyLocalCopy When 'true', and a local copy of the file exists, only this is removed.
  48. */
  49. public RemoveFileOperation(OCFile fileToRemove,
  50. boolean onlyLocalCopy,
  51. Account account,
  52. boolean inBackground,
  53. Context context) {
  54. this.fileToRemove = fileToRemove;
  55. this.onlyLocalCopy = onlyLocalCopy;
  56. this.account = account;
  57. this.inBackground = inBackground;
  58. this.context = context;
  59. }
  60. /**
  61. * Getter for the file to remove (or removed, if the operation was successfully performed).
  62. *
  63. * @return File to remove or already removed.
  64. */
  65. public OCFile getFile() {
  66. return fileToRemove;
  67. }
  68. public boolean isInBackground() {
  69. return inBackground;
  70. }
  71. /**
  72. * Performs the remove operation
  73. *
  74. * @param client Client object to communicate with the remote ownCloud server.
  75. */
  76. @Override
  77. protected RemoteOperationResult run(OwnCloudClient client) {
  78. RemoteOperationResult result = null;
  79. RemoteOperation operation;
  80. if (MimeTypeUtil.isImage(fileToRemove.getMimeType())) {
  81. // store resized image
  82. ThumbnailsCacheManager.generateResizedImage(fileToRemove);
  83. }
  84. boolean localRemovalFailed = false;
  85. if (!onlyLocalCopy) {
  86. if (fileToRemove.isEncrypted() &&
  87. android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
  88. OCFile parent = getStorageManager().getFileByPath(fileToRemove.getParentRemotePath());
  89. operation = new RemoveRemoteEncryptedFileOperation(fileToRemove.getRemotePath(),
  90. parent.getLocalId(),
  91. account,
  92. context,
  93. fileToRemove.getEncryptedFileName());
  94. } else {
  95. operation = new RemoveFileRemoteOperation(fileToRemove.getRemotePath());
  96. }
  97. result = operation.execute(client);
  98. if (result.isSuccess() || result.getCode() == ResultCode.FILE_NOT_FOUND) {
  99. localRemovalFailed = !(getStorageManager().removeFile(fileToRemove, true, true));
  100. }
  101. } else {
  102. localRemovalFailed = !(getStorageManager().removeFile(fileToRemove, false, true));
  103. if (!localRemovalFailed) {
  104. result = new RemoteOperationResult(ResultCode.OK);
  105. }
  106. }
  107. if (localRemovalFailed) {
  108. result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_REMOVED);
  109. }
  110. return result;
  111. }
  112. }