MoveFileOperation.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.owncloud.android.operations;
  20. import com.owncloud.android.datamodel.OCFile;
  21. import com.owncloud.android.lib.common.OwnCloudClient;
  22. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  23. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  24. import com.owncloud.android.lib.resources.files.MoveFileRemoteOperation;
  25. import com.owncloud.android.operations.common.SyncOperation;
  26. /**
  27. * Operation mmoving an {@link OCFile} to a different folder.
  28. */
  29. public class MoveFileOperation extends SyncOperation {
  30. //private static final String TAG = MoveFileOperation.class.getSimpleName();
  31. private String mSrcPath;
  32. private String mTargetParentPath;
  33. private OCFile mFile;
  34. /**
  35. * Constructor
  36. *
  37. * @param srcPath Remote path of the {@link OCFile} to move.
  38. * @param targetParentPath Path to the folder where the file will be moved into.
  39. */
  40. public MoveFileOperation(String srcPath, String targetParentPath) {
  41. mSrcPath = srcPath;
  42. mTargetParentPath = targetParentPath;
  43. if (!mTargetParentPath.endsWith(OCFile.PATH_SEPARATOR)) {
  44. mTargetParentPath += OCFile.PATH_SEPARATOR;
  45. }
  46. mFile = null;
  47. }
  48. /**
  49. * Performs the operation.
  50. *
  51. * @param client Client object to communicate with the remote ownCloud server.
  52. */
  53. @Override
  54. protected RemoteOperationResult run(OwnCloudClient client) {
  55. /// 1. check move validity
  56. if (mTargetParentPath.startsWith(mSrcPath)) {
  57. return new RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT);
  58. }
  59. mFile = getStorageManager().getFileByPath(mSrcPath);
  60. if (mFile == null) {
  61. return new RemoteOperationResult(ResultCode.FILE_NOT_FOUND);
  62. }
  63. /// 2. remote move
  64. String targetPath = mTargetParentPath + mFile.getFileName();
  65. if (mFile.isFolder()) {
  66. targetPath += OCFile.PATH_SEPARATOR;
  67. }
  68. RemoteOperationResult result = new MoveFileRemoteOperation(mSrcPath, targetPath, false).execute(client);
  69. /// 3. local move
  70. if (result.isSuccess()) {
  71. getStorageManager().moveLocalFile(mFile, targetPath, mTargetParentPath);
  72. }
  73. // TODO handle ResultCode.PARTIAL_MOVE_DONE in client Activity, for the moment
  74. return result;
  75. }
  76. }