Added folder name to 'file not found' error message
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFolderOperation.java
index 534e16a..088c578 100644 (file)
@@ -88,7 +88,6 @@ public class SynchronizeFolderOperation extends SyncOperation {
 
     /** 'True' means that the remote folder changed and should be fetched */
     private boolean mRemoteFolderChanged;
-    private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
 
     private List<OCFile> mFilesForDirectDownload;
         // to avoid extra PROPFINDs when there was no change in the folder
@@ -101,6 +100,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
 
     private List<SyncOperation> mFoldersToWalkDown;
 
+    private final AtomicBoolean mCancellationRequested;
 
     /**
      * Creates a new instance of {@link SynchronizeFolderOperation}.
@@ -120,7 +120,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
         mFilesToSyncContentsWithoutUpload = new Vector<SyncOperation>();
         mFavouriteFilesToSyncContents = new Vector<SyncOperation>();
         mFoldersToWalkDown = new Vector<SyncOperation>();
-        
+        mCancellationRequested = new AtomicBoolean(false);
     }
 
 
@@ -142,28 +142,35 @@ public class SynchronizeFolderOperation extends SyncOperation {
         RemoteOperationResult result = null;
         mFailsInFileSyncsFound = 0;
         mConflictsFound = 0;
-
-        synchronized(mCancellationRequested) {
-            if (mCancellationRequested.get()) {
-                return new RemoteOperationResult(new OperationCancelledException());
-            }
-        }
-
-        // get locally cached information about folder 
-        mLocalFolder = getStorageManager().getFileByPath(mRemotePath);   
         
-        result = checkForChanges(client);
-
-        if (result.isSuccess()) {
-            if (mRemoteFolderChanged) {
-                result = fetchAndSyncRemoteFolder(client);
+        try {
+            // get locally cached information about folder 
+            mLocalFolder = getStorageManager().getFileByPath(mRemotePath);   
+            
+            result = checkForChanges(client);
+    
+            if (result.isSuccess()) {
+                if (mRemoteFolderChanged) {
+                    result = fetchAndSyncRemoteFolder(client);
+                    
+                } else {
+                    prepareOpsFromLocalKnowledge();
+                }
                 
-            } else {
-                prepareOpsFromLocalKnowledge();
+                if (result.isSuccess()) {
+                    syncContents(client);
+                }
+
+                if (mFilesForDirectDownload.isEmpty()) {
+                    sendBroadcastForNotifyingUIUpdate(result.isSuccess());
+                }
             }
+        } catch (OperationCancelledException e) {
+            result = new RemoteOperationResult(e);
             
-            if (result.isSuccess()) {
-                syncContents(client);
+            // cancel 'child' synchronizations
+            for (SyncOperation  synchOp: mFoldersToWalkDown) {
+                ((SynchronizeFolderOperation) synchOp).cancel();
             }
         }
 
@@ -171,12 +178,16 @@ public class SynchronizeFolderOperation extends SyncOperation {
 
     }
 
-    private RemoteOperationResult checkForChanges(OwnCloudClient client) {
+    private RemoteOperationResult checkForChanges(OwnCloudClient client) throws OperationCancelledException {
         Log_OC.d(TAG, "Checking changes in " + mAccount.name + mRemotePath);
 
         mRemoteFolderChanged = true;
         RemoteOperationResult result = null;
         
+        if (mCancellationRequested.get()) {
+            throw new OperationCancelledException();
+        }
+        
         // remote request
         ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mRemotePath);
         result = operation.execute(client);
@@ -204,13 +215,19 @@ public class SynchronizeFolderOperation extends SyncOperation {
                 Log_OC.e(TAG, "Checked " + mAccount.name + mRemotePath + " : " +
                         result.getLogMessage());
             }
+
+            sendBroadcastForNotifyingUIUpdate(result.isSuccess());
         }
 
         return result;
     }
 
 
-    private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) {
+    private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) throws OperationCancelledException {
+        if (mCancellationRequested.get()) {
+            throw new OperationCancelledException();
+        }
+        
         ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(mRemotePath);
         RemoteOperationResult result = operation.execute(client);
         Log_OC.d(TAG, "Synchronizing " + mAccount.name + mRemotePath);
@@ -394,7 +411,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
 
 
-    private void syncContents(OwnCloudClient client) {
+    private void syncContents(OwnCloudClient client) throws OperationCancelledException {
         startDirectDownloads();
         startContentSynchronizations(mFilesToSyncContentsWithoutUpload, client);
         startContentSynchronizations(mFavouriteFilesToSyncContents, client);
@@ -402,8 +419,11 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
 
     
-    private void startDirectDownloads() {
+    private void startDirectDownloads() throws OperationCancelledException {
         for (OCFile file : mFilesForDirectDownload) {
+            if (mCancellationRequested.get()) {
+                throw new OperationCancelledException();
+            }
             Intent i = new Intent(mContext, FileDownloader.class);
             i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
             i.putExtra(FileDownloader.EXTRA_FILE, file);
@@ -421,9 +441,14 @@ public class SynchronizeFolderOperation extends SyncOperation {
      * @param filesToSyncContents       Synchronization operations to execute.
      * @param client                    Interface to the remote ownCloud server.
      */
-    private void startContentSynchronizations(List<SyncOperation> filesToSyncContents, OwnCloudClient client) {
+    private void startContentSynchronizations(List<SyncOperation> filesToSyncContents, OwnCloudClient client) 
+            throws OperationCancelledException {
+        
         RemoteOperationResult contentsResult = null;
         for (SyncOperation op: filesToSyncContents) {
+            if (mCancellationRequested.get()) {
+                throw new OperationCancelledException();
+            }
             contentsResult = op.execute(getStorageManager(), mContext);
             if (!contentsResult.isSuccess()) {
                 if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
@@ -444,9 +469,12 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
 
 
-    private void walkSubfolders(OwnCloudClient client) {
+    private void walkSubfolders(OwnCloudClient client) throws OperationCancelledException {
         RemoteOperationResult contentsResult = null;
         for (SyncOperation op: mFoldersToWalkDown) {
+            if (mCancellationRequested.get()) {
+                throw new OperationCancelledException();
+            }
             contentsResult = op.execute(client, getStorageManager());   // to watch out: possibly deep recursion
             if (!contentsResult.isSuccess()) {
                 // TODO - some kind of error count, and use it with notifications
@@ -460,7 +488,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
         }
     }
 
-
+    
     /**
      * Creates and populates a new {@link com.owncloud.android.datamodel.OCFile} object with the data read from the server.
      *
@@ -497,13 +525,29 @@ public class SynchronizeFolderOperation extends SyncOperation {
         }
     }
 
+    private void sendBroadcastForNotifyingUIUpdate(boolean result) {
+        // Send a broadcast message for notifying UI update
+        Intent uiUpdate = new Intent(FileDownloader.getDownloadFinishMessage());
+        uiUpdate.putExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, result);
+        uiUpdate.putExtra(FileDownloader.ACCOUNT_NAME, mAccount.name);
+        uiUpdate.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mRemotePath);
+        uiUpdate.putExtra(FileDownloader.EXTRA_FILE_PATH, mLocalFolder.getRemotePath());
+        mContext.sendStickyBroadcast(uiUpdate);
+    }
+
     
     /**
      * Cancel operation
      */
-    public void cancel(){
-        // WIP Cancel the sync operation
+    public void cancel() {
         mCancellationRequested.set(true);
     }
 
+    public String getFolderPath() {
+        String path = mLocalFolder.getStoragePath();
+        if (path != null && path.length() > 0) {
+            return path;
+        }
+        return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mLocalFolder);
+    }
 }