diff --git a/src/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/com/owncloud/android/datamodel/FileDataStorageManager.java index 1f7ee4c372..a8186cd581 100644 --- a/src/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -145,6 +145,29 @@ public class FileDataStorageManager implements DataStorageManager { + e.getMessage()); } } + } else if (fileExists(file.getFileId())) { // for renamed files; no more delete and create + OCFile oldFile = getFileById(file.getFileId()); + if (file.getStoragePath() == null && oldFile.getStoragePath() != null) + file.setStoragePath(oldFile.getStoragePath()); + if (!file.isDirectory()); + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); + + overriden = true; + if (getContentResolver() != null) { + getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, + ProviderTableMeta._ID + "=?", + new String[] { String.valueOf(file.getFileId()) }); + } else { + try { + getContentProvider().update(ProviderTableMeta.CONTENT_URI, + cv, ProviderTableMeta._ID + "=?", + new String[] { String.valueOf(file.getFileId()) }); + } catch (RemoteException e) { + Log.e(TAG, + "Fail to insert insert file to database " + + e.getMessage()); + } + } } else { Uri result_uri = null; if (getContentResolver() != null) { @@ -201,11 +224,12 @@ public class FileDataStorageManager implements DataStorageManager { cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0); if (fileExists(file.getRemotePath())) { - OCFile tmpfile = getFileByPath(file.getRemotePath()); - file.setStoragePath(tmpfile.getStoragePath()); + OCFile oldFile = getFileByPath(file.getRemotePath()); + if (file.getStoragePath() == null && oldFile.getStoragePath() != null) + file.setStoragePath(oldFile.getStoragePath()); if (!file.isDirectory()); cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); - file.setFileId(tmpfile.getFileId()); + file.setFileId(oldFile.getFileId()); operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). withValues(cv). @@ -213,6 +237,19 @@ public class FileDataStorageManager implements DataStorageManager { new String[] { String.valueOf(file.getFileId()) }) .build()); + } else if (fileExists(file.getFileId())) { + OCFile oldFile = getFileById(file.getFileId()); + if (file.getStoragePath() == null && oldFile.getStoragePath() != null) + file.setStoragePath(oldFile.getStoragePath()); + if (!file.isDirectory()); + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); + + operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). + withValues(cv). + withSelection( ProviderTableMeta._ID + "=?", + new String[] { String.valueOf(file.getFileId()) }) + .build()); + } else { operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build()); } diff --git a/src/com/owncloud/android/datamodel/OCFile.java b/src/com/owncloud/android/datamodel/OCFile.java index ec5a67a988..d2d6478437 100644 --- a/src/com/owncloud/android/datamodel/OCFile.java +++ b/src/com/owncloud/android/datamodel/OCFile.java @@ -212,7 +212,21 @@ public class OCFile implements Parcelable, Comparable { */ public String getFileName() { File f = new File(getRemotePath()); - return f.getName().length() == 0 ? "/" : f.getName(); + return f.getName().length() == 0 ? PATH_SEPARATOR : f.getName(); + } + + /** + * Sets the name of the file + * + * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory + */ + public void setFileName(String name) { + if (name != null && name.length() > 0 && !name.contains(PATH_SEPARATOR) && !mRemotePath.equals(PATH_SEPARATOR)) { + mRemotePath = (new File(getRemotePath())).getParent() + name; + if (isDirectory()) { + mRemotePath += PATH_SEPARATOR; + } + } } /** @@ -370,7 +384,7 @@ public class OCFile implements Parcelable, Comparable { @Override public String toString() { String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]"; - asString = String.format(asString, new Long(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, new Long(mParentId), new Boolean(mKeepInSync)); + asString = String.format(asString, Long.valueOf(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, Long.valueOf(mParentId), Boolean.valueOf(mKeepInSync)); return asString; } diff --git a/src/com/owncloud/android/operations/RenameFileOperation.java b/src/com/owncloud/android/operations/RenameFileOperation.java index 120a880e8a..fe7ad796e9 100644 --- a/src/com/owncloud/android/operations/RenameFileOperation.java +++ b/src/com/owncloud/android/operations/RenameFileOperation.java @@ -41,7 +41,7 @@ import eu.alefzero.webdav.WebdavUtils; */ public class RenameFileOperation extends RemoteOperation { - private static final String TAG = RenameFileOperation.class.getSimpleName(); + private static final String TAG = RemoveFileOperation.class.getSimpleName(); private static final int RENAME_READ_TIMEOUT = 10000; private static final int RENAME_CONNECTION_TIMEOUT = 5000; @@ -80,7 +80,6 @@ public class RenameFileOperation extends RemoteOperation { RemoteOperationResult result = null; LocalMoveMethod move = null; - //MoveMethod move = null; // TODO find out why not use this String newRemotePath = null; try { if (mNewName.equals(mFile.getFileName())) { @@ -94,26 +93,25 @@ public class RenameFileOperation extends RemoteOperation { return new RemoteOperationResult(ResultCode.INVALID_LOCAL_FILE_NAME); } - // check if a remote file with the new name already exists - if (client.existsFile(newRemotePath)) { + // check if a file with the new name already exists + if (client.existsFile(newRemotePath) || // remote check could fail by network failure, or by indeterminate behavior of HEAD for folders ... + mStorageManager.getFileByPath(newRemotePath) != null) { // ... so local check is convenient return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE); } - /*move = new MoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mFile.getRemotePath()), - client.getBaseUri() + WebdavUtils.encodePath(newRemotePath), - false);*/ move = new LocalMoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mFile.getRemotePath()), client.getBaseUri() + WebdavUtils.encodePath(newRemotePath)); int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT); if (move.succeeded()) { // create new OCFile instance for the renamed file - OCFile newFile = obtainUpdatedFile(); + /*OCFile newFile = new OCFile(mStorageManager.getFileById(mFile.getParentId()).getRemotePath() + mNewName; // TODO - NOT CREATE NEW OCFILE; ADD setFileName METHOD OCFile oldFile = mFile; - mFile = newFile; + mFile = newFile; */ + mFile.setFileName(mNewName); // try to rename the local copy of the file - if (oldFile.isDown()) { - File f = new File(oldFile.getStoragePath()); + if (mFile.isDown()) { + File f = new File(mFile.getStoragePath()); String newStoragePath = f.getParent() + mNewName; if (f.renameTo(new File(newStoragePath))) { mFile.setStoragePath(newStoragePath); @@ -122,8 +120,20 @@ public class RenameFileOperation extends RemoteOperation { // TODO - study conditions when this could be a problem } - mStorageManager.removeFile(oldFile, false); + //mStorageManager.removeFile(oldFile, false); mStorageManager.saveFile(mFile); + + /* + *} else if (mFile.isDirectory() && (status == 207 || status >= 500)) { + * // TODO + * // if server fails in the rename of a folder, some children files could have been moved to a folder with the new name while some others + * // stayed in the old folder; + * // + * // easiest and heaviest solution is synchronizing the parent folder (or the full account); + * // + * // a better solution is synchronizing the folders with the old and new names; + *} + */ } @@ -180,24 +190,6 @@ public class RenameFileOperation extends RemoteOperation { } - /** - * Creates a new OCFile for the new remote name of the renamed file. - * - * @return OCFile object with the same information than mFile, but the renamed remoteFile and the storagePath (empty) - */ - private OCFile obtainUpdatedFile() { - OCFile file = new OCFile(mStorageManager.getFileById(mFile.getParentId()).getRemotePath() + mNewName); - file.setCreationTimestamp(mFile.getCreationTimestamp()); - file.setFileId(mFile.getFileId()); - file.setFileLength(mFile.getFileLength()); - file.setKeepInSync(mFile.keepInSync()); - file.setLastSyncDate(mFile.getLastSyncDate()); - file.setMimetype(mFile.getMimetype()); - file.setModificationTimestamp(mFile.getModificationTimestamp()); - file.setParentId(mFile.getParentId()); - return file; - } - // move operation - TODO: find out why org.apache.jackrabbit.webdav.client.methods.MoveMethod is not used instead ¿? private class LocalMoveMethod extends DavMethodBase {