X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/a4ba6170ea7696e085b07adfef73eeb8b77cb8e2..ac6b3d25ac71cf9bf01bcdb00eb43745af96ffb9:/src/com/owncloud/android/datamodel/FileDataStorageManager.java diff --git a/src/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/com/owncloud/android/datamodel/FileDataStorageManager.java index 49a88c36..a8186cd5 100644 --- a/src/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -38,7 +38,6 @@ import android.content.ContentValues; import android.content.OperationApplicationException; import android.database.Cursor; import android.net.Uri; -import android.os.Environment; import android.os.RemoteException; import android.util.Log; @@ -72,7 +71,7 @@ public class FileDataStorageManager implements DataStorageManager { c.close(); return file; } - + @Override public OCFile getFileById(long id) { Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id)); @@ -83,6 +82,16 @@ public class FileDataStorageManager implements DataStorageManager { c.close(); return file; } + + public OCFile getFileByLocalPath(String path) { + Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path); + OCFile file = null; + if (c.moveToFirst()) { + file = createFileInstance(c); + } + c.close(); + return file; + } @Override public boolean fileExists(long id) { @@ -136,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) { @@ -192,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). @@ -204,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()); } @@ -269,6 +315,7 @@ public class FileDataStorageManager implements DataStorageManager { return mContentProvider; } + @Override public Vector getDirectoryContent(OCFile f) { if (f != null && f.isDirectory() && f.getFileId() != -1) { Vector ret = new Vector(); @@ -399,7 +446,8 @@ public class FileDataStorageManager implements DataStorageManager { return file; } - public void removeFile(OCFile file) { + @Override + public void removeFile(OCFile file, boolean removeLocalCopy) { Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId()); if (getContentProvider() != null) { try { @@ -414,9 +462,43 @@ public class FileDataStorageManager implements DataStorageManager { ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?", new String[]{mAccount.name}); } - if (file.isDown()) { + if (file.isDown() && removeLocalCopy) { new File(file.getStoragePath()).delete(); } + if (file.isDirectory() && removeLocalCopy) { + File f = new File(FileDownloader.getSavePath(mAccount.name) + file.getRemotePath()); + if (f.exists() && f.isDirectory() && (f.list() == null || f.list().length == 0)) { + f.delete(); + } + } + } + + @Override + public void removeDirectory(OCFile dir, boolean removeDBData, boolean removeLocalContent) { + // TODO consider possible failures + if (dir != null && dir.isDirectory() && dir.getFileId() != -1) { + Vector children = getDirectoryContent(dir); + if (children != null) { + OCFile child = null; + for (int i=0; i