RemoveFileOperation.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 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 org.apache.commons.httpclient.HttpStatus;
  19. import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
  20. import com.owncloud.android.Log_OC;
  21. import com.owncloud.android.datamodel.FileDataStorageManager;
  22. import com.owncloud.android.datamodel.OCFile;
  23. import eu.alefzero.webdav.WebdavClient;
  24. import eu.alefzero.webdav.WebdavUtils;
  25. /**
  26. * Remote operation performing the removal of a remote file or folder in the ownCloud server.
  27. *
  28. * @author David A. Velasco
  29. */
  30. public class RemoveFileOperation extends RemoteOperation {
  31. private static final String TAG = RemoveFileOperation.class.getSimpleName();
  32. private static final int REMOVE_READ_TIMEOUT = 10000;
  33. private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
  34. OCFile mFileToRemove;
  35. boolean mDeleteLocalCopy;
  36. FileDataStorageManager mDataStorageManager;
  37. /**
  38. * Constructor
  39. *
  40. * @param fileToRemove OCFile instance describing the remote file or folder to remove from the server
  41. * @param deleteLocalCopy When 'true', and a local copy of the file exists, it is also removed.
  42. * @param storageManager Reference to the local database corresponding to the account where the file is contained.
  43. */
  44. public RemoveFileOperation(OCFile fileToRemove, boolean deleteLocalCopy, FileDataStorageManager storageManager) {
  45. mFileToRemove = fileToRemove;
  46. mDeleteLocalCopy = deleteLocalCopy;
  47. mDataStorageManager = storageManager;
  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(WebdavClient client) {
  64. RemoteOperationResult result = null;
  65. DeleteMethod delete = null;
  66. try {
  67. delete = new DeleteMethod(client.getBaseUri() + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
  68. int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
  69. if (delete.succeeded() || status == HttpStatus.SC_NOT_FOUND) {
  70. mDataStorageManager.removeFile(mFileToRemove, true, mDeleteLocalCopy);
  71. }
  72. delete.getResponseBodyAsString(); // exhaust the response, although not interesting
  73. result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), status, delete.getResponseHeaders());
  74. Log_OC.i(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage());
  75. } catch (Exception e) {
  76. result = new RemoteOperationResult(e);
  77. Log_OC.e(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage(), e);
  78. } finally {
  79. if (delete != null)
  80. delete.releaseConnection();
  81. }
  82. return result;
  83. }
  84. }