update
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / FileDataStorageManager.java
index 71bc3ee..fef4802 100644 (file)
@@ -23,16 +23,9 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Vector;
 
 import java.util.Vector;
 
-import com.owncloud.android.MainApp;
-import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
-import com.owncloud.android.lib.common.utils.Log_OC;
-import com.owncloud.android.lib.resources.shares.OCShare;
-import com.owncloud.android.lib.resources.shares.ShareType;
-import com.owncloud.android.lib.resources.files.FileUtils;
-import com.owncloud.android.utils.FileStorageUtils;
-
 import android.accounts.Account;
 import android.content.ContentProviderClient;
 import android.content.ContentProviderOperation;
 import android.accounts.Account;
 import android.content.ContentProviderClient;
 import android.content.ContentProviderOperation;
@@ -46,6 +39,14 @@ import android.database.Cursor;
 import android.net.Uri;
 import android.os.RemoteException;
 
 import android.net.Uri;
 import android.os.RemoteException;
 
+import com.owncloud.android.MainApp;
+import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
+import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.lib.resources.files.FileUtils;
+import com.owncloud.android.lib.resources.shares.OCShare;
+import com.owncloud.android.lib.resources.shares.ShareType;
+import com.owncloud.android.utils.FileStorageUtils;
+
 public class FileDataStorageManager {
 
     public static final int ROOT_PARENT_ID = 0;
 public class FileDataStorageManager {
 
     public static final int ROOT_PARENT_ID = 0;
@@ -138,9 +139,9 @@ public class FileDataStorageManager {
     }
 
     
     }
 
     
-    public Vector<OCFile> getFolderContent(OCFile f) {
+    public Vector<OCFile> getFolderContent(OCFile f, boolean onlyOnDevice) {
         if (f != null && f.isFolder() && f.getFileId() != -1) {
         if (f != null && f.isFolder() && f.getFileId() != -1) {
-            return getFolderContent(f.getFileId());
+            return getFolderContent(f.getFileId(), onlyOnDevice);
 
         } else {
             return new Vector<OCFile>();
 
         } else {
             return new Vector<OCFile>();
@@ -148,11 +149,11 @@ public class FileDataStorageManager {
     }
     
     
     }
     
     
-    public Vector<OCFile> getFolderImages(OCFile folder) {
+    public Vector<OCFile> getFolderImages(OCFile folder, boolean onlyOnDevice) {
         Vector<OCFile> ret = new Vector<OCFile>(); 
         if (folder != null) {
             // TODO better implementation, filtering in the access to database instead of here 
         Vector<OCFile> ret = new Vector<OCFile>(); 
         if (folder != null) {
             // TODO better implementation, filtering in the access to database instead of here 
-            Vector<OCFile> tmp = getFolderContent(folder);
+            Vector<OCFile> tmp = getFolderContent(folder, onlyOnDevice);
             OCFile current = null; 
             for (int i=0; i<tmp.size(); i++) {
                 current = tmp.get(i);
             OCFile current = null; 
             for (int i=0; i<tmp.size(); i++) {
                 current = tmp.get(i);
@@ -541,7 +542,7 @@ public class FileDataStorageManager {
         File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, folder));
         if (localFolder.exists()) {
             // stage 1: remove the local files already registered in the files database
         File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, folder));
         if (localFolder.exists()) {
             // stage 1: remove the local files already registered in the files database
-            Vector<OCFile> files = getFolderContent(folder.getFileId());
+            Vector<OCFile> files = getFolderContent(folder.getFileId(), false);
             if (files != null) {
                 for (OCFile file : files) {
                     if (file.isFolder()) {
             if (files != null) {
                 for (OCFile file : files) {
                     if (file.isFolder()) {
@@ -586,11 +587,6 @@ public class FileDataStorageManager {
         return success;
     }
 
         return success;
     }
 
-    public void moveFolder(OCFile folder, String newPath) {
-        // TODO check newPath
-
-    }
-
     
     /**
      * Updates database and file system for a file or folder that was moved to a different location.
     
     /**
      * Updates database and file system for a file or folder that was moved to a different location.
@@ -644,6 +640,8 @@ public class FileDataStorageManager {
             ArrayList<ContentProviderOperation> operations = 
                     new ArrayList<ContentProviderOperation>(c.getCount());
             String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
             ArrayList<ContentProviderOperation> operations = 
                     new ArrayList<ContentProviderOperation>(c.getCount());
             String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
+            List<String> originalPathsToTriggerMediaScan = new ArrayList<String>();
+            List<String> newPathsToTriggerMediaScan = new ArrayList<String>();
             if (c.moveToFirst()) {
                 int lengthOfOldPath = file.getRemotePath().length();
                 int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
             if (c.moveToFirst()) {
                 int lengthOfOldPath = file.getRemotePath().length();
                 int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
@@ -657,11 +655,14 @@ public class FileDataStorageManager {
                     if (child.getStoragePath() != null && 
                             child.getStoragePath().startsWith(defaultSavePath)) {
                         // update link to downloaded content - but local move is not done here!
                     if (child.getStoragePath() != null && 
                             child.getStoragePath().startsWith(defaultSavePath)) {
                         // update link to downloaded content - but local move is not done here!
-                        cv.put(
-                            ProviderTableMeta.FILE_STORAGE_PATH, 
-                            defaultSavePath + targetPath + 
-                                child.getStoragePath().substring(lengthOfOldStoragePath)
-                        );
+                        String targetLocalPath = defaultSavePath + targetPath + 
+                                child.getStoragePath().substring(lengthOfOldStoragePath);
+                        
+                        cv.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath);
+                        
+                        originalPathsToTriggerMediaScan.add(child.getStoragePath());
+                        newPathsToTriggerMediaScan.add(targetLocalPath);
+                        
                     }
                     if (child.getRemotePath().equals(file.getRemotePath())) {
                         cv.put(
                     }
                     if (child.getRemotePath().equals(file.getRemotePath())) {
                         cv.put(
@@ -696,11 +697,12 @@ public class FileDataStorageManager {
             }
 
             /// 4. move in local file system 
             }
 
             /// 4. move in local file system 
-            String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
-            File localFile = new File(localPath);
+            String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
+            String targetLocalPath = defaultSavePath + targetPath;
+            File localFile = new File(originalLocalPath);
             boolean renamed = false;
             if (localFile.exists()) {
             boolean renamed = false;
             if (localFile.exists()) {
-                File targetFile = new File(defaultSavePath + targetPath);
+                File targetFile = new File(targetLocalPath);
                 File targetFolder = targetFile.getParentFile();
                 if (!targetFolder.exists()) {
                     targetFolder.mkdirs();
                 File targetFolder = targetFile.getParentFile();
                 if (!targetFolder.exists()) {
                     targetFolder.mkdirs();
@@ -709,25 +711,23 @@ public class FileDataStorageManager {
             }
 
             if (renamed) {
             }
 
             if (renamed) {
-                if (file.isFolder()) {
-                    
-                } else {
+                Iterator<String> it = originalPathsToTriggerMediaScan.iterator();
+                while (it.hasNext()) {
                     // Notify MediaScanner about removed file
                     // Notify MediaScanner about removed file
-                    triggerMediaScan(file.getStoragePath());
-                    
+                    triggerMediaScan(it.next());
+                }
+                it = newPathsToTriggerMediaScan.iterator();
+                while (it.hasNext()) {
                     // Notify MediaScanner about new file/folder
                     // Notify MediaScanner about new file/folder
-                    triggerMediaScan(defaultSavePath + targetPath);
+                    triggerMediaScan(it.next());
                 }
             }
                 }
             }
-            
-            Log_OC.d(TAG, "uri old: " + file.getStoragePath());
-            Log_OC.d(TAG, "uri new: " + defaultSavePath + targetPath);
         }
         
     }
     
     
         }
         
     }
     
     
-    private Vector<OCFile> getFolderContent(long parentId) {
+    private Vector<OCFile> getFolderContent(long parentId, boolean onlyOnDevice) {
 
         Vector<OCFile> ret = new Vector<OCFile>();
 
 
         Vector<OCFile> ret = new Vector<OCFile>();
 
@@ -754,7 +754,9 @@ public class FileDataStorageManager {
         if (c.moveToFirst()) {
             do {
                 OCFile child = createFileInstance(c);
         if (c.moveToFirst()) {
             do {
                 OCFile child = createFileInstance(c);
-                ret.add(child);
+                if (child.isFolder() || !onlyOnDevice || onlyOnDevice && child.isDown()){
+                    ret.add(child);
+                }
             } while (c.moveToNext());
         }
 
             } while (c.moveToNext());
         }
 
@@ -1425,14 +1427,13 @@ public class FileDataStorageManager {
     }
 
     private ArrayList<ContentProviderOperation> prepareRemoveSharesInFolder(
     }
 
     private ArrayList<ContentProviderOperation> prepareRemoveSharesInFolder(
-            OCFile folder, ArrayList<ContentProviderOperation> preparedOperations
-            ) {
+            OCFile folder, ArrayList<ContentProviderOperation> preparedOperations) {
         if (folder != null) {
             String where = ProviderTableMeta.OCSHARES_PATH + "=?" + " AND " 
                     + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
             String [] whereArgs = new String[]{ "", mAccount.name };
             
         if (folder != null) {
             String where = ProviderTableMeta.OCSHARES_PATH + "=?" + " AND " 
                     + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
             String [] whereArgs = new String[]{ "", mAccount.name };
             
-            Vector<OCFile> files = getFolderContent(folder);
+            Vector<OCFile> files = getFolderContent(folder, false);
             
             for (OCFile file : files) {
                 whereArgs[0] = file.getRemotePath();
             
             for (OCFile file : files) {
                 whereArgs[0] = file.getRemotePath();