RemoveFileOperation.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  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 as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.operations;
  19. import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
  20. import android.util.Log;
  21. import com.owncloud.android.datamodel.DataStorageManager;
  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. DataStorageManager 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, DataStorageManager storageManager) {
  45. mFileToRemove = fileToRemove;
  46. mDeleteLocalCopy = deleteLocalCopy;
  47. mDataStorageManager = storageManager;
  48. }
  49. /**
  50. * Performs the remove operation
  51. *
  52. * @param client Client object to communicate with the remote ownCloud server.
  53. */
  54. @Override
  55. protected RemoteOperationResult run(WebdavClient client) {
  56. RemoteOperationResult result = null;
  57. DeleteMethod delete = null;
  58. try {
  59. delete = new DeleteMethod(client.getBaseUri() + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
  60. int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
  61. if (delete.succeeded()) {
  62. if (mFileToRemove.isDirectory()) {
  63. mDataStorageManager.removeDirectory(mFileToRemove, true, mDeleteLocalCopy);
  64. } else {
  65. mDataStorageManager.removeFile(mFileToRemove, mDeleteLocalCopy);
  66. }
  67. }
  68. delete.getResponseBodyAsString(); // exhaust the response, although not interesting
  69. result = new RemoteOperationResult(delete.succeeded(), status);
  70. Log.i(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage());
  71. } catch (Exception e) {
  72. result = new RemoteOperationResult(e);
  73. Log.e(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage(), e);
  74. } finally {
  75. if (delete != null)
  76. delete.releaseConnection();
  77. }
  78. return result;
  79. }
  80. }