1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.operations
;
21 import java
.io
.IOException
;
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
.common
.utils
.Log_OC
;
28 import com
.owncloud
.android
.lib
.resources
.files
.RenameRemoteFileOperation
;
29 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
30 import com
.owncloud
.android
.utils
.FileStorageUtils
;
34 * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
36 * @author David A. Velasco
39 public class RenameFileOperation
extends SyncOperation
{
41 private static final String TAG
= RenameFileOperation
.class.getSimpleName();
44 private String mRemotePath
;
45 private String mNewName
;
46 private String mNewRemotePath
;
53 * @param remotePath RemotePath of the OCFile instance describing the remote file or folder to rename
54 * @param account OwnCloud account containing the remote file
55 * @param newName New name to set as the name of file.
57 public RenameFileOperation(String remotePath
, String newName
) {
58 mRemotePath
= remotePath
;
60 mNewRemotePath
= null
;
63 public OCFile
getFile() {
69 * Performs the rename operation.
71 * @param client Client object to communicate with the remote ownCloud server.
74 protected RemoteOperationResult
run(OwnCloudClient client
) {
75 RemoteOperationResult result
= null
;
77 mFile
= getStorageManager().getFileByPath(mRemotePath
);
79 // check if the new name is valid in the local file system
81 if (!isValidNewName()) {
82 return new RemoteOperationResult(ResultCode
.INVALID_LOCAL_FILE_NAME
);
84 String parent
= (new File(mFile
.getRemotePath())).getParent();
85 parent
= (parent
.endsWith(OCFile
.PATH_SEPARATOR
)) ? parent
: parent
+ OCFile
.PATH_SEPARATOR
;
86 mNewRemotePath
= parent
+ mNewName
;
87 if (mFile
.isFolder()) {
88 mNewRemotePath
+= OCFile
.PATH_SEPARATOR
;
91 // check local overwrite
92 if (getStorageManager().getFileByPath(mNewRemotePath
) != null
) {
93 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
96 RenameRemoteFileOperation operation
= new RenameRemoteFileOperation(mFile
.getFileName(), mFile
.getRemotePath(),
97 mNewName
, mFile
.isFolder());
98 result
= operation
.execute(client
);
100 if (result
.isSuccess()) {
101 if (mFile
.isFolder()) {
102 getStorageManager().moveLocalFile(mFile
, mNewRemotePath
, parent
);
103 //saveLocalDirectory();
110 } catch (IOException e
) {
111 Log_OC
.e(TAG
, "Rename " + mFile
.getRemotePath() + " to " + ((mNewRemotePath
==null
) ? mNewName
: mNewRemotePath
) + ": " +
112 ((result
!= null
) ? result
.getLogMessage() : ""), e
);
118 private void saveLocalFile() {
119 mFile
.setFileName(mNewName
);
121 // try to rename the local copy of the file
122 if (mFile
.isDown()) {
123 String oldPath
= mFile
.getStoragePath();
124 File f
= new File(oldPath
);
125 String parentStoragePath
= f
.getParent();
126 if (!parentStoragePath
.endsWith(File
.separator
))
127 parentStoragePath
+= File
.separator
;
128 if (f
.renameTo(new File(parentStoragePath
+ mNewName
))) {
129 String newPath
= parentStoragePath
+ mNewName
;
130 mFile
.setStoragePath(newPath
);
132 // notify MediaScanner about removed file - TODO really works?
133 getStorageManager().triggerMediaScan(oldPath
);
134 // notify to scan about new file
135 getStorageManager().triggerMediaScan(newPath
);
137 // else - NOTHING: the link to the local file is kept although the local name can't be updated
138 // TODO - study conditions when this could be a problem
141 getStorageManager().saveFile(mFile
);
145 * Checks if the new name to set is valid in the file system
147 * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
148 * for downloads, out of any account, and then removed.
150 * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
151 * could be formatted with a different file system.
153 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities specific for the interactions with the file system
155 * @return 'True' if a temporal file named with the name to set could be created in the file system where
156 * local files are stored.
157 * @throws IOException When the temporal folder can not be created.
159 private boolean isValidNewName() throws IOException
{
160 // check tricky names
161 if (mNewName
== null
|| mNewName
.length() <= 0 || mNewName
.contains(File
.separator
)) {
164 // create a test file
165 String tmpFolderName
= FileStorageUtils
.getTemporalPath("");
166 File testFile
= new File(tmpFolderName
+ mNewName
);
167 File tmpFolder
= testFile
.getParentFile();
169 if (!tmpFolder
.isDirectory()) {
170 throw new IOException("Unexpected error: temporal directory could not be created");
173 testFile
.createNewFile(); // return value is ignored; it could be 'false' because the file already existed, that doesn't invalidate the name
174 } catch (IOException e
) {
175 Log_OC
.i(TAG
, "Test for validity of name " + mNewName
+ " in the file system failed");
178 boolean result
= (testFile
.exists() && testFile
.isFile());
180 // cleaning ; result is ignored, since there is not much we could do in case of failure, but repeat and repeat...