Adding cancellation to uploads (WIP)
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RemoteOperationResult.java
index 080e0b6..0618b86 100644 (file)
@@ -30,15 +30,13 @@ import org.apache.commons.httpclient.ConnectTimeoutException;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpStatus;
 
-import android.util.Log;
-
 import com.owncloud.android.network.CertificateCombinedException;
 
 
 /**
  * The result of a remote operation required to an ownCloud server.
  * 
- * Provides a common classification of resulst for all the application. 
+ * Provides a common classification of remote operation results for all the application. 
  * 
  * @author David A. Velasco
  */
@@ -58,11 +56,12 @@ public class RemoteOperationResult {
         HOST_NOT_AVAILABLE, 
         NO_NETWORK_CONNECTION, 
         SSL_ERROR,
-        BAD_OC_VERSION, 
+        SSL_RECOVERABLE_PEER_UNVERIFIED,
+        BAD_OC_VERSION,
+        STORAGE_ERROR_MOVING_FROM_TMP,
+        CANCELLED
     }
 
-    private static final String TAG = null;
-    
     private boolean mSuccess = false;
     private int mHttpCode = -1;
     private Exception mException = null;
@@ -97,7 +96,10 @@ public class RemoteOperationResult {
     public RemoteOperationResult(Exception e) {
         mException = e; 
         
-        if (e instanceof SocketException) {  
+        if (e instanceof OperationCancelledException) {
+            mCode = ResultCode.CANCELLED;
+            
+        } else if (e instanceof SocketException) {  
             mCode = ResultCode.WRONG_CONNECTION;
         
         } else if (e instanceof SocketTimeoutException) {
@@ -112,8 +114,17 @@ public class RemoteOperationResult {
         } else if (e instanceof UnknownHostException) {
             mCode = ResultCode.HOST_NOT_AVAILABLE;
         
-        } else if (e instanceof SSLException) {
-            mCode = ResultCode.SSL_ERROR;
+        } else if (e instanceof SSLException || e instanceof RuntimeException) {
+            CertificateCombinedException se = getCertificateCombinedException(e);
+            if (se != null) {
+                mException = se;
+                if (se.isRecoverable()) { 
+                    mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
+                }
+                
+            } else { 
+                mCode = ResultCode.SSL_ERROR;
+            }
             
         } else {
             mCode = ResultCode.UNKNOWN_ERROR;
@@ -126,6 +137,10 @@ public class RemoteOperationResult {
         return mSuccess;
     }
     
+    public boolean isCancelled() {
+        return mCode == ResultCode.CANCELLED;
+    }
+    
     public int getHttpCode() {
         return mHttpCode;
     }
@@ -139,34 +154,34 @@ public class RemoteOperationResult {
     }
 
     public boolean isSslRecoverableException() {
-        return (getSslRecoverableException() != null);
+        return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
     }
     
-    public CertificateCombinedException getSslRecoverableException() {
+    private CertificateCombinedException getCertificateCombinedException(Exception e) {
         CertificateCombinedException result = null;
-        if (mCode == ResultCode.SSL_ERROR) {
-            if (mException instanceof CertificateCombinedException)
-                result = (CertificateCombinedException)mException;
-            Throwable cause = mException.getCause();
-            Throwable previousCause = null;
-            while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
-                previousCause = cause;
-                cause = cause.getCause();
-            }
-            if (cause != null && cause instanceof CertificateCombinedException)
-                result = (CertificateCombinedException)cause; 
+        if (e instanceof CertificateCombinedException) {
+            return (CertificateCombinedException)e;
         }
-        if (result != null && result.isRecoverable())
-            return result;
-        else
-            return null;
+        Throwable cause = mException.getCause();
+        Throwable previousCause = null;
+        while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
+            previousCause = cause;
+            cause = cause.getCause();
+        }
+        if (cause != null && cause instanceof CertificateCombinedException) {
+            result = (CertificateCombinedException)cause; 
+        }
+        return result;
     }
     
     
     public String getLogMessage() {
         
         if (mException != null) {
-            if (mException instanceof SocketException) {  
+            if (mException instanceof OperationCancelledException) {
+                return "Operation cancelled by the caller";
+                
+            } else if (mException instanceof SocketException) {  
                 return "Socket exception";
         
             } else if (mException instanceof SocketTimeoutException) {
@@ -182,7 +197,10 @@ public class RemoteOperationResult {
                 return "Unknown host exception";
         
             } else if (mException instanceof SSLException) {
-                return "SSL exception";
+                if (mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)
+                    return "SSL recoverable exception";
+                else
+                    return "SSL exception";
 
             } else if (mException instanceof HttpException) {
                 return "HTTP violation";
@@ -195,6 +213,19 @@ public class RemoteOperationResult {
             }
         }
         
+        if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
+            return "The ownCloud server is not configured!";
+            
+        } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
+            return "No network connection";
+            
+        } else if (mCode == ResultCode.BAD_OC_VERSION) {
+            return "No valid ownCloud version was found at the server";
+            
+        } else if (mCode == ResultCode.STORAGE_ERROR_MOVING_FROM_TMP) {
+            return "Error while moving file from temporal to final directory";
+        }
+        
         return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess()?"success":"fail") + ")";
 
     }