X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/dc648f1ae9b636b6089906db88219924519f6dbf..cdf25ac52c67182bb7e5e44d0091f97b55305584:/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java?ds=inline
diff --git a/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java b/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java
index 60520ed4..5314e636 100644
--- a/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java
+++ b/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java
@@ -15,9 +15,9 @@
  *   along with this program.  If not, see .
  *
  */
-
-package eu.alefzero.owncloud.syncadapter;
-
+
+package eu.alefzero.owncloud.syncadapter;
+
 import java.io.IOException;
 import java.net.UnknownHostException;
 import java.util.Date;
@@ -44,181 +44,217 @@ import android.net.Uri;
 import android.text.TextUtils;
 import android.util.Log;
 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
+import eu.alefzero.owncloud.datamodel.DataStorageManager;
 import eu.alefzero.owncloud.datamodel.OCFile;
 import eu.alefzero.webdav.HttpPropFind;
 import eu.alefzero.webdav.TreeNode;
 import eu.alefzero.webdav.TreeNode.NodeProperty;
 import eu.alefzero.webdav.WebdavClient;
 import eu.alefzero.webdav.WebdavUtils;
-
-/**
- * Base SyncAdapter for OwnCloud
- * Designed to be subclassed for the concrete SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..
- * 
- * @author sassman
- *
- */
-public abstract class AbstractOwnCloudSyncAdapter extends AbstractThreadedSyncAdapter {
-
-	private AccountManager accountManager;
-	private Account account;
-	private ContentProviderClient contentProvider;
-	private Date lastUpdated;
-	
-	private HttpHost mHost;
-	private WebdavClient mClient = null;
+
+/**
+ * Base SyncAdapter for OwnCloud Designed to be subclassed for the concrete
+ * SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..
+ * 
+ * @author sassman
+ * 
+ */
+public abstract class AbstractOwnCloudSyncAdapter extends
+		AbstractThreadedSyncAdapter {
+
+	private AccountManager accountManager;
+	private Account account;
+	private ContentProviderClient contentProvider;
+	private Date lastUpdated;
+	private DataStorageManager mStoreManager;
+
+	private HttpHost mHost;
+	private WebdavClient mClient = null;
 	private static String TAG = "AbstractOwnCloudSyncAdapter";
-	
-	public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
-		super(context, autoInitialize);
-		this.setAccountManager(AccountManager.get(context));
-	}
-
-	public AccountManager getAccountManager() {
-		return accountManager;
-	}
-
-	public void setAccountManager(AccountManager accountManager) {
-		this.accountManager = accountManager;
-	}
-
-	public Account getAccount() {
-		return account;
-	}
-
-	public void setAccount(Account account) {
-		this.account = account;
-	}
-
-	public ContentProviderClient getContentProvider() {
-		return contentProvider;
-	}
-
-	public void setContentProvider(ContentProviderClient contentProvider) {
-		this.contentProvider = contentProvider;
-	}
-
-	public Date getLastUpdated() {
-		return lastUpdated;
-	}
-
-	public void setLastUpdated(Date lastUpdated) {
-		this.lastUpdated = lastUpdated;
-	}
-	
-	protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
-		return new ConnectionKeepAliveStrategy() {
-			public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
-				// Change keep alive straategy basing on response: ie forbidden/not found/etc
-				// should have keep alive 0
+
+	public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {
+		super(context, autoInitialize);
+		this.setAccountManager(AccountManager.get(context));
+	}
+
+	public AccountManager getAccountManager() {
+		return accountManager;
+	}
+
+	public void setAccountManager(AccountManager accountManager) {
+		this.accountManager = accountManager;
+	}
+
+	public Account getAccount() {
+		return account;
+	}
+
+	public void setAccount(Account account) {
+		this.account = account;
+	}
+
+	public ContentProviderClient getContentProvider() {
+		return contentProvider;
+	}
+
+	public void setContentProvider(ContentProviderClient contentProvider) {
+		this.contentProvider = contentProvider;
+	}
+
+	public Date getLastUpdated() {
+		return lastUpdated;
+	}
+
+	public void setLastUpdated(Date lastUpdated) {
+		this.lastUpdated = lastUpdated;
+	}
+
+	public void setStorageManager(DataStorageManager storage_manager) {
+	  mStoreManager = storage_manager;
+	}
+	
+	public DataStorageManager getStorageManager() {
+	  return mStoreManager;
+	}
+	
+	protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
+		return new ConnectionKeepAliveStrategy() {
+			public long getKeepAliveDuration(HttpResponse response,
+					HttpContext context) {
+				// Change keep alive straategy basing on response: ie
+				// forbidden/not found/etc
+				// should have keep alive 0
 				// default return: 5s
 				int statusCode = response.getStatusLine().getStatusCode();
-				
-				// HTTP 400, 500 Errors as well as HTTP 118 - Connection timed out
-				if((statusCode >= 400 && statusCode <= 418) || 
-						(statusCode >= 421 && statusCode <= 426) ||
-						(statusCode >= 500 && statusCode <= 510 ) ||
-						statusCode == 118) {
+
+				// HTTP 400, 500 Errors as well as HTTP 118 - Connection timed
+				// out
+				if ((statusCode >= 400 && statusCode <= 418)
+						|| (statusCode >= 421 && statusCode <= 426)
+						|| (statusCode >= 500 && statusCode <= 510)
+						|| statusCode == 118) {
 					return 0;
 				}
-				
-				return 5 * 1000;
-			}
-		};
-	}
-	
-	protected HttpPropFind getPropFindQuery() throws OperationCanceledException, AuthenticatorException, IOException {
-		HttpPropFind query = new HttpPropFind(getUri().toString());
-		query.setHeader("Content-type", "text/xml");
-		query.setHeader("User-Agent", "Android-ownCloud");
-		return query;
-	}
-	
-	protected HttpResponse fireRawRequest(HttpRequest query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {
-	    BasicHttpContext httpContext = new BasicHttpContext();
-        BasicScheme basicAuth = new BasicScheme();
-        httpContext.setAttribute("preemptive-auth", basicAuth);
-        
-        HttpResponse response = getClient().execute(mHost, query, httpContext);
-        return response;
-	}
-	
-	protected TreeNode fireRequest(HttpRequest query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {
-		HttpResponse response = fireRawRequest(query);
-		
-		TreeNode root = new TreeNode();
-		root.setProperty(TreeNode.NodeProperty.NAME, "");
-		this.parseResponse(response, getUri(), getClient(), mHost, root.getChildList(), false, 0);
-		return root;
-	}
-	
-	protected Uri getUri() {
-		return Uri.parse(this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL));
-	}
-	
-	private DefaultHttpClient getClient() throws OperationCanceledException, AuthenticatorException, IOException {
-		if(mClient == null) {
-			String username = getAccount().name.split("@")[0];
-			String password = this.getAccountManager().blockingGetAuthToken(getAccount(), AccountAuthenticator.AUTH_TOKEN_TYPE, true);
-			if (this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL) == null) {
-				throw new UnknownHostException();
-			}
-			Uri uri = getUri();
-	
-			mClient = new WebdavClient(uri);
-			mClient.setCredentials(username, password);
-			mClient.allowUnsignedCertificates();
-			mHost = mClient.getTargetHost();
-		}
-		
-		return mClient.getHttpClient();
-	}
-	
-	private void parseResponse(HttpResponse resp, Uri uri, DefaultHttpClient client, HttpHost targetHost, LinkedList insertList, boolean sf, long parent_id) throws IOException, OperationCanceledException, AuthenticatorException {
-		boolean skipFirst = sf, override_parent = !sf;
-		for (TreeNode n :WebdavUtils.parseResponseToNodes(resp.getEntity().getContent())) {
-		  if (skipFirst) {
-        skipFirst = false;
-        continue;
-      }
+
+				return 5 * 1000;
+			}
+		};
+	}
+
+	protected HttpPropFind getPropFindQuery()
+			throws OperationCanceledException, AuthenticatorException,
+			IOException {
+		HttpPropFind query = new HttpPropFind(getUri().toString());
+		query.setHeader("Content-type", "text/xml");
+		query.setHeader("User-Agent", "Android-ownCloud");
+		return query;
+	}
+
+	protected HttpResponse fireRawRequest(HttpRequest query)
+			throws ClientProtocolException, OperationCanceledException,
+			AuthenticatorException, IOException {
+		/*BasicHttpContext httpContext = new BasicHttpContext();
+		BasicScheme basicAuth = new BasicScheme();
+		httpContext.setAttribute("preemptive-auth", basicAuth);
+
+		HttpResponse response = getClient().execute(mHost, query, httpContext);*/
+		return null;
+	}
+
+	protected TreeNode fireRequest(HttpRequest query)
+			throws ClientProtocolException, OperationCanceledException,
+			AuthenticatorException, IOException {
+		HttpResponse response = fireRawRequest(query);
+
+		TreeNode root = new TreeNode();
+		root.setProperty(TreeNode.NodeProperty.NAME, "");
+		//this.parseResponse(response, getUri(), getClient(), mHost,
+		//		root.getChildList(), false, 0);
+		return root;
+	}
+
+	protected Uri getUri() {
+		return Uri.parse(this.getAccountManager().getUserData(getAccount(),
+				AccountAuthenticator.KEY_OC_URL));
+	}
+
+	protected WebdavClient getClient() throws OperationCanceledException,
+			AuthenticatorException, IOException {
+		if (mClient == null) {
+			String username = getAccount().name.split("@")[0];
+			String password = this.getAccountManager().blockingGetAuthToken(
+					getAccount(), AccountAuthenticator.AUTH_TOKEN_TYPE, true);
+			if (this.getAccountManager().getUserData(getAccount(),
+					AccountAuthenticator.KEY_OC_URL) == null) {
+				throw new UnknownHostException();
+			}
+			Uri uri = getUri();
+
+			mClient = new WebdavClient(uri);
+			mClient.setCredentials(username, password);
+			mClient.allowUnsignedCertificates();
+			mHost = mClient.getTargetHost();
+		}
+
+		return mClient;
+	}
+
+	private void parseResponse(HttpResponse resp, Uri uri,
+			DefaultHttpClient client, HttpHost targetHost,
+			LinkedList insertList, boolean sf, long parent_id)
+			throws IOException, OperationCanceledException,
+			AuthenticatorException {
+		boolean skipFirst = sf, override_parent = !sf;
+		for (TreeNode n : WebdavUtils.parseResponseToNodes(resp.getEntity()
+				.getContent())) {
+			if (skipFirst) {
+				skipFirst = false;
+				continue;
+			}
 			String path = n.stripPathFromFilename(uri.getPath());
+
+			long mod = n.getProperty(NodeProperty.LAST_MODIFIED_DATE) == null ? 0
+					: Long.parseLong(n
+							.getProperty(NodeProperty.LAST_MODIFIED_DATE));
 			
-			long mod = n.getProperty(NodeProperty.LAST_MODIFIED_DATE) == null ?
-			           0 :
-			           Long.parseLong(n.getProperty(NodeProperty.LAST_MODIFIED_DATE));
-	    OCFile file = new OCFile(getContentProvider(), getAccount(), n.getProperty(NodeProperty.PATH));
-	    if (file.fileExtist() && file.getModificationTimestamp() >= mod) {
-	      Log.d(TAG, "No update for file/dir " + file.getFileName() + " is needed");
-	    } else {
-	      Log.d(TAG, "File " + n.getProperty(NodeProperty.PATH) + " will be " + (file.fileExtist() ? "updated" : "created"));
-  	    long len = n.getProperty(NodeProperty.CONTENT_LENGTH) == null ?
-                   0 :
-                   Long.parseLong(n.getProperty(NodeProperty.CONTENT_LENGTH));
-  	    long create = n.getProperty(NodeProperty.CREATE_DATE) == null ?
-                      0 :
-                      Long.parseLong(n.getProperty(NodeProperty.CREATE_DATE));
-  			file = OCFile.createNewFile(getContentProvider(),
-            getAccount(),
-            n.getProperty(NodeProperty.PATH),
-            len,
-            create,
-            mod,
-            n.getProperty(NodeProperty.RESOURCE_TYPE),
-            parent_id);
-  			file.save();
-  			if (override_parent) {
-  			  parent_id = file.getFileId();
-  			  override_parent = false;
-  			}
-	    }
-
-			if (!TextUtils.isEmpty(n.getProperty(NodeProperty.NAME)) &&
-					n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
-			    
-			    HttpPropFind method = new HttpPropFind(uri.getPath() + path + n.getProperty(NodeProperty.NAME).replace(" ", "%20") + "/");
-				HttpResponse response = fireRawRequest(method);
-				parseResponse(response, uri, client, targetHost, n.getChildList(), true, file.getFileId());
-			}
-		}
-	}
+			OCFile file = getStorageManager().getFileByPath(n.getProperty(NodeProperty.PATH));
+			if (file != null && file.fileExists() && file.getModificationTimestamp() >= mod) {
+				Log.d(TAG, "No update for file/dir " + file.getFileName()
+						+ " is needed");
+			} else {
+			  file = new OCFile(n.getProperty(NodeProperty.PATH));
+				Log.d(TAG, "File " + n.getProperty(NodeProperty.PATH)
+						+ " will be "
+						+ (file.fileExists() ? "updated" : "created"));
+				long len = n.getProperty(NodeProperty.CONTENT_LENGTH) == null ? 0
+						: Long.parseLong(n
+								.getProperty(NodeProperty.CONTENT_LENGTH));
+				long create = n.getProperty(NodeProperty.CREATE_DATE) == null ? 0
+						: Long.parseLong(n
+								.getProperty(NodeProperty.CREATE_DATE));
+
+				file.setFileLength(len);
+				file.setCreationTimestamp(create);
+				file.setModificationTimestamp(mod);
+				file.setMimetype(n.getProperty(NodeProperty.RESOURCE_TYPE));
+				file.setParentId(parent_id);
+				getStorageManager().saveFile(file);
+				if (override_parent) {
+					parent_id = file.getFileId();
+					override_parent = false;
+				}
+			}
+
+			if (!TextUtils.isEmpty(n.getProperty(NodeProperty.NAME))
+					&& n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
+
+				HttpPropFind method = new HttpPropFind(uri.getPath() + path
+						+ n.getProperty(NodeProperty.NAME).replace(" ", "%20")
+						+ "/");
+				HttpResponse response = fireRawRequest(method);
+				parseResponse(response, uri, client, targetHost,
+						n.getChildList(), true, file.getFileId());
+			}
+		}
+	}
 }
\ No newline at end of file