Completing previous commit
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / datamodel / FileDataStorageManager.java
index e0870b9..0ad0d1b 100644 (file)
@@ -18,6 +18,8 @@
 
 package eu.alefzero.owncloud.datamodel;
 
+import java.io.File;
+import java.util.Collections;
 import java.util.Vector;
 
 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
@@ -27,6 +29,7 @@ import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.database.Cursor;
 import android.net.Uri;
+import android.os.Environment;
 import android.os.RemoteException;
 import android.util.Log;
 
@@ -56,8 +59,8 @@ public class FileDataStorageManager implements DataStorageManager {
         OCFile file = null;
         if (c.moveToFirst()) {
             file = createFileInstance(c);
-            c.close();
         }
+        c.close();
         return file;
     }
 
@@ -67,8 +70,8 @@ public class FileDataStorageManager implements DataStorageManager {
         OCFile file = null;
         if (c.moveToFirst()) {
             file = createFileInstance(c);
-            c.close();
         }
+        c.close();
         return file;
     }
 
@@ -94,14 +97,16 @@ public class FileDataStorageManager implements DataStorageManager {
         if (file.getParentId() != 0)
             cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
         cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
-        cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
+        if (!file.isDirectory())
+            cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
         cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
         cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDate());
 
         if (fileExists(file.getRemotePath())) {
             OCFile tmpfile = getFileByPath(file.getRemotePath());
             file.setStoragePath(tmpfile.getStoragePath());
-            cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
+            if (!file.isDirectory());
+                cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
             file.setFileId(tmpfile.getFileId());
 
             overriden = true;
@@ -205,6 +210,9 @@ public class FileDataStorageManager implements DataStorageManager {
             }
 
             c.close();
+            
+            Collections.sort(ret);
+            
             return ret;
         }
         return null;
@@ -274,10 +282,19 @@ public class FileDataStorageManager implements DataStorageManager {
             file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
             file.setParentId(c.getLong(c
                     .getColumnIndex(ProviderTableMeta.FILE_PARENT)));
-            file.setStoragePath(c.getString(c
-                    .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
             file.setMimetype(c.getString(c
                     .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
+            if (!file.isDirectory()) {
+                file.setStoragePath(c.getString(c
+                        .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
+                if (file.getStoragePath() == null) {
+                    // try to find existing file and bind it with current account
+                    File sdCard = Environment.getExternalStorageDirectory();
+                    File f = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + file.getURLDecodedRemotePath());
+                    if (f.exists())
+                        file.setStoragePath(f.getAbsolutePath());
+                }
+            }
             file.setFileLength(c.getLong(c
                     .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
             file.setCreationTimestamp(c.getLong(c
@@ -305,6 +322,9 @@ public class FileDataStorageManager implements DataStorageManager {
                                         ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
                                         new String[]{mAccount.name});
         }
+        if (file.getStoragePath() != null) {
+            new File(file.getStoragePath()).delete();
+        }
     }
 
 }