Removal of folders completed
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / FileDataStorageManager.java
index 7bb29d4..1f7ee4c 100644 (file)
@@ -38,7 +38,6 @@ import android.content.ContentValues;
 import android.content.OperationApplicationException;
 import android.database.Cursor;
 import android.net.Uri;
 import android.content.OperationApplicationException;
 import android.database.Cursor;
 import android.net.Uri;
-import android.os.Environment;
 import android.os.RemoteException;
 import android.util.Log;
 
 import android.os.RemoteException;
 import android.util.Log;
 
@@ -72,7 +71,7 @@ public class FileDataStorageManager implements DataStorageManager {
         c.close();
         return file;
     }
         c.close();
         return file;
     }
-
+    
     @Override
     public OCFile getFileById(long id) {
         Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
     @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;
     }
         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) {
 
     @Override
     public boolean fileExists(long id) {
@@ -269,6 +278,7 @@ public class FileDataStorageManager implements DataStorageManager {
         return mContentProvider;
     }
 
         return mContentProvider;
     }
 
+    @Override
     public Vector<OCFile> getDirectoryContent(OCFile f) {
         if (f != null && f.isDirectory() && f.getFileId() != -1) {
             Vector<OCFile> ret = new Vector<OCFile>();
     public Vector<OCFile> getDirectoryContent(OCFile f) {
         if (f != null && f.isDirectory() && f.getFileId() != -1) {
             Vector<OCFile> ret = new Vector<OCFile>();
@@ -399,6 +409,7 @@ public class FileDataStorageManager implements DataStorageManager {
         return file;
     }
     
         return file;
     }
     
+    @Override
     public void removeFile(OCFile file, boolean removeLocalCopy) {
         Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
         if (getContentProvider() != null) {
     public void removeFile(OCFile file, boolean removeLocalCopy) {
         Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
         if (getContentProvider() != null) {
@@ -417,6 +428,40 @@ public class FileDataStorageManager implements DataStorageManager {
         if (file.isDown() && removeLocalCopy) {
             new File(file.getStoragePath()).delete();
         }
         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<OCFile> children = getDirectoryContent(dir);
+            if (children != null) {
+                OCFile child = null;
+                for (int i=0; i<children.size(); i++) {
+                    child = children.get(i);
+                    if (child.isDirectory()) {
+                        removeDirectory(child, removeDBData, removeLocalContent);
+                    } else {
+                        if (removeDBData) {
+                            removeFile(child, removeLocalContent);
+                        } else if (removeLocalContent) {
+                            if (child.isDown()) {
+                                new File(child.getStoragePath()).delete();
+                            }
+                        }
+                    }
+                }
+                if (removeDBData) {
+                    removeFile(dir, true);
+                }
+            }
+        }
     }
 
 }
     }
 
 }