Merge branch 'develop' into share_password_support
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / MoveFileOperation.java
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 */
20
21 package com.owncloud.android.operations;
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.MoveRemoteFileOperation;
28 import com.owncloud.android.operations.common.SyncOperation;
29
30 import android.accounts.Account;
31
32
33 /**
34 * Operation mmoving an {@link OCFile} to a different folder.
35 */
36 public class MoveFileOperation 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 /**
48 * Constructor
49 *
50 * @param path Remote path of the {@link OCFile} to move.
51 * @param newParentPath Path to the folder where the file will be moved into.
52 * @param account OwnCloud account containing both the file and the target folder
53 */
54 public MoveFileOperation(String srcPath, String targetParentPath, Account account) {
55 mSrcPath = srcPath;
56 mTargetParentPath = targetParentPath;
57 if (!mTargetParentPath.endsWith(OCFile.PATH_SEPARATOR)) {
58 mTargetParentPath += OCFile.PATH_SEPARATOR;
59 }
60
61 mFile = null;
62 }
63
64 /**
65 * Performs the operation.
66 *
67 * @param client Client object to communicate with the remote ownCloud server.
68 */
69 @Override
70 protected RemoteOperationResult run(OwnCloudClient client) {
71 RemoteOperationResult result = null;
72
73 /// 1. check move validity
74 if (mTargetParentPath.startsWith(mSrcPath)) {
75 return new RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT);
76 }
77 mFile = getStorageManager().getFileByPath(mSrcPath);
78 if (mFile == null) {
79 return new RemoteOperationResult(ResultCode.FILE_NOT_FOUND);
80 }
81
82 /// 2. remote move
83 String targetPath = mTargetParentPath + mFile.getFileName();
84 if (mFile.isFolder()) {
85 targetPath += OCFile.PATH_SEPARATOR;
86 }
87 MoveRemoteFileOperation operation = new MoveRemoteFileOperation(
88 mSrcPath,
89 targetPath,
90 false
91 );
92 result = operation.execute(client);
93
94 /// 3. local move
95 if (result.isSuccess()) {
96 getStorageManager().moveLocalFile(mFile, targetPath, mTargetParentPath);
97 }
98 // TODO handle ResultCode.PARTIAL_MOVE_DONE in client Activity, for the moment
99
100 return result;
101 }
102
103
104 }