620a058cbfc3ae25b5d45fdbb86f012e22d5e131
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / MoveFileOperation.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 java.io.File;
21 import java.io.IOException;
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.lib.resources.files.RenameRemoteFileOperation;
29 import com.owncloud.android.operations.common.SyncOperation;
30 import com.owncloud.android.utils.FileStorageUtils;
31 import com.owncloud.android.utils.Log_OC;
32
33 import android.accounts.Account;
34
35
36 /**
37 * Operation mmoving an {@link OCFile} to a different folder.
38 *
39 * @author David A. Velasco
40 */
41 public class MoveFileOperation extends SyncOperation {
42
43 private static final String TAG = MoveFileOperation.class.getSimpleName();
44
45 private String mPath;
46 private Account mAccount;
47 private String mNewParentPath;
48 private OCFile mFile;
49 private String mNewPath;
50
51
52
53 /**
54 * Constructor
55 *
56 * @param path Remote path of the {@link OCFile} to move.
57 * @param newParentPath Path to the folder where the file will be moved into.
58 * @param account OwnCloud account containing both the file and the target folder
59 */
60 public MoveFileOperation(String path, String newParentPath, Account account) {
61 mPath = path;
62 mNewParentPath = newParentPath;
63 mAccount = account;
64
65 mFile = null;
66 mNewPath = "";
67 }
68
69 public OCFile getFile() {
70 return mFile;
71 }
72
73
74 /**
75 * Performs the operation.
76 *
77 * @param client Client object to communicate with the remote ownCloud server.
78 */
79 @Override
80 protected RemoteOperationResult run(OwnCloudClient client) {
81 RemoteOperationResult result = null;
82
83
84 mFile = getStorageManager().getFileByPath(mPath);
85
86 try {
87 /// 1. check move validity
88
89
90 /// 2. remove move
91 MoveRemoteFileOperation operation = new MoveRemoteFileOperation(
92 mFile.getRemotePath(),
93 mNewPath,
94 false
95 );
96 result = operation.execute(client);
97
98
99 /// 3. local move
100 if (result.isSuccess()) {
101 //moveLocaly();
102 /*
103 if (mFile.isFolder()) {
104 saveLocalDirectory();
105
106 } else {
107 saveLocalFile();
108 }
109 */
110 }
111
112
113 String parentPath = (new File(mFile.getRemotePath())).getParent();
114 parentPath = (parentPath.endsWith(OCFile.PATH_SEPARATOR))
115 ? parentPath
116 : parentPath + OCFile.PATH_SEPARATOR;
117
118 mNewPath = mNewParentPath + mFile.getFileName();
119 if (mFile.isFolder() && !mNewPath.endsWith(OCFile.PATH_SEPARATOR)) {
120 mNewPath += OCFile.PATH_SEPARATOR;
121 }
122
123 // check local overwrite
124 if (getStorageManager().getFileByPath(mPath) != null) {
125 return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
126 }
127
128 /*
129 } catch (IOException e) {
130 Log_OC.e(TAG, "Move " + mFile.getRemotePath() + " to " +
131 (mNewParentPath==null) + ": " +
132 ((result!= null) ? result.getLogMessage() : ""), e);*/
133 } finally {
134
135 }
136
137 return result;
138 }
139
140
141 private void saveLocalDirectory() {
142 getStorageManager().moveFolder(mFile, mNewPath);
143 String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
144 File localDir = new File(localPath);
145 if (localDir.exists()) {
146 localDir.renameTo(new File(FileStorageUtils.getSavePath(mAccount.name) + mNewPath));
147 // TODO - if renameTo fails, children files that are already down will result unlinked
148 }
149 }
150
151 private void saveLocalFile() {
152 /*
153 mFile.setRFileName(mNewName); <<< NO >
154
155 // try to move the local copy of the file
156 if (mFile.isDown()) {
157 File f = new File(mFile.getStoragePath());
158 String parentStoragePath = f.getParent();
159 if (!parentStoragePath.endsWith(File.separator))
160 parentStoragePath += File.separator;
161 if (f.renameTo(new File())) {
162 mFile.setStoragePath(parentStoragePath + mNewName);
163 }
164 // else - NOTHING: the link to the local file is kept although the local name can't be updated
165 // TODO - study conditions when this could be a problem
166 }
167
168 getStorageManager().saveFile(mFile);
169 */
170 }
171
172 /**
173 * Checks if the new name to set is valid in the file system
174 *
175 * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
176 * for downloads, out of any account, and then removed.
177 *
178 * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
179 * could be formatted with a different file system.
180 *
181 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities specific for the interactions with the file system
182 *
183 * @return 'True' if a temporal file named with the name to set could be created in the file system where
184 * local files are stored.
185 * @throws IOException When the temporal folder can not be created.
186 */
187 private boolean isValidNewName() throws IOException {
188
189 return true;
190 /*
191 // check tricky names
192 if (mNewName == null || mNewName.length() <= 0 || mNewName.contains(File.separator) || mNewName.contains("%")) {
193 return false;
194 }
195 // create a test file
196 String tmpFolderName = FileStorageUtils.getTemporalPath("");
197 File testFile = new File(tmpFolderName + mNewName);
198 File tmpFolder = testFile.getParentFile();
199 tmpFolder.mkdirs();
200 if (!tmpFolder.isDirectory()) {
201 throw new IOException("Unexpected error: temporal directory could not be created");
202 }
203 try {
204 testFile.createNewFile(); // return value is ignored; it could be 'false' because the file already existed, that doesn't invalidate the name
205 } catch (IOException e) {
206 Log_OC.i(TAG, "Test for validity of name " + mNewName + " in the file system failed");
207 return false;
208 }
209 boolean result = (testFile.exists() && testFile.isFile());
210
211 // cleaning ; result is ignored, since there is not much we could do in case of failure, but repeat and repeat...
212 testFile.delete();
213
214 return result;
215 */
216 }
217
218 }