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