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