CopyFileOperation.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. package com.owncloud.android.operations;
  17. import com.owncloud.android.datamodel.OCFile;
  18. import com.owncloud.android.lib.common.OwnCloudClient;
  19. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  20. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  21. import com.owncloud.android.lib.resources.files.CopyFileRemoteOperation;
  22. import com.owncloud.android.operations.common.SyncOperation;
  23. /**
  24. * Operation copying an {@link OCFile} to a different folder.
  25. *
  26. * @author David A. Velasco
  27. */
  28. public class CopyFileOperation extends SyncOperation {
  29. private String srcPath;
  30. private String targetParentPath;
  31. /**
  32. * Constructor
  33. *
  34. * @param srcPath Remote path of the {@link OCFile} to move.
  35. * @param targetParentPath Path to the folder where the file will be copied into.
  36. */
  37. public CopyFileOperation(String srcPath, String targetParentPath) {
  38. this.srcPath = srcPath;
  39. this.targetParentPath = targetParentPath;
  40. if (!this.targetParentPath.endsWith(OCFile.PATH_SEPARATOR)) {
  41. this.targetParentPath += OCFile.PATH_SEPARATOR;
  42. }
  43. }
  44. /**
  45. * Performs the operation.
  46. *
  47. * @param client Client object to communicate with the remote ownCloud server.
  48. */
  49. @Override
  50. protected RemoteOperationResult run(OwnCloudClient client) {
  51. /// 1. check copy validity
  52. if (targetParentPath.startsWith(srcPath)) {
  53. return new RemoteOperationResult(ResultCode.INVALID_COPY_INTO_DESCENDANT);
  54. }
  55. OCFile file = getStorageManager().getFileByPath(srcPath);
  56. if (file == null) {
  57. return new RemoteOperationResult(ResultCode.FILE_NOT_FOUND);
  58. }
  59. /// 2. remote copy
  60. String targetPath = targetParentPath + file.getFileName();
  61. if (file.isFolder()) {
  62. targetPath += OCFile.PATH_SEPARATOR;
  63. }
  64. RemoteOperationResult result = new CopyFileRemoteOperation(srcPath, targetPath, false).execute(client);
  65. /// 3. local copy
  66. if (result.isSuccess()) {
  67. getStorageManager().copyLocalFile(file, targetPath);
  68. }
  69. // TODO handle ResultCode.PARTIAL_COPY_DONE in client Activity, for the moment
  70. return result;
  71. }
  72. }