CopyFileOperation.java 3.3 KB

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