moving from eu.alefzero.eu to com.owncloud.android
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / syncadapter / AbstractOwnCloudSyncAdapter.java
diff --git a/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java b/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java
deleted file mode 100644 (file)
index ff1f818..0000000
+++ /dev/null
@@ -1,201 +0,0 @@
-/* ownCloud Android client application\r
- *   Copyright (C) 2011  Bartek Przybylski\r
- *\r
- *   This program is free software: you can redistribute it and/or modify\r
- *   it under the terms of the GNU General Public License as published by\r
- *   the Free Software Foundation, either version 3 of the License, or\r
- *   (at your option) any later version.\r
- *\r
- *   This program is distributed in the hope that it will be useful,\r
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- *   GNU General Public License for more details.\r
- *\r
- *   You should have received a copy of the GNU General Public License\r
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
- *\r
- */\r
-
-package eu.alefzero.owncloud.syncadapter;
-
-import java.io.IOException;\r
-import java.net.UnknownHostException;\r
-import java.util.Date;\r
-import java.util.LinkedList;\r
-\r
-import org.apache.http.HttpHost;\r
-import org.apache.http.HttpRequest;\r
-import org.apache.http.HttpResponse;\r
-import org.apache.http.client.ClientProtocolException;\r
-import org.apache.http.conn.ConnectionKeepAliveStrategy;\r
-import org.apache.http.impl.auth.BasicScheme;\r
-import org.apache.http.impl.client.DefaultHttpClient;\r
-import org.apache.http.protocol.BasicHttpContext;\r
-import org.apache.http.protocol.HttpContext;\r
-\r
-import android.accounts.Account;\r
-import android.accounts.AccountManager;\r
-import android.accounts.AuthenticatorException;\r
-import android.accounts.OperationCanceledException;\r
-import android.content.AbstractThreadedSyncAdapter;\r
-import android.content.ContentProviderClient;\r
-import android.content.ContentResolver;\r
-import android.content.Context;\r
-import android.net.Uri;\r
-import android.text.TextUtils;\r
-import android.util.Log;\r
-import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
-import eu.alefzero.owncloud.datamodel.OCFile;\r
-import eu.alefzero.webdav.HttpPropFind;\r
-import eu.alefzero.webdav.TreeNode;\r
-import eu.alefzero.webdav.WebdavClient;\r
-import eu.alefzero.webdav.WebdavUtils;\r
-import eu.alefzero.webdav.TreeNode.NodeProperty;\r
-
-/**
- * Base SyncAdapter for OwnCloud
- * Designed to be subclassed for the concreete 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;
-
-       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) {
-                               // TODO: change keep alive straategy basing on response: ie forbidden/not found/etc
-                               // should have keep alive 0
-                               // default return: 5s
-                               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<TreeNode> insertList, boolean sf, long parent_id) throws IOException, OperationCanceledException, AuthenticatorException {
-               boolean skipFirst = sf, override_parent = true;
-               for (TreeNode n :WebdavUtils.parseResponseToNodes(resp.getEntity().getContent())) {\r
-                 if (skipFirst) {\r
-        skipFirst = false;\r
-        continue;\r
-      }
-                       String path = n.stripPathFromFilename(uri.getPath());\r
-                       OCFile new_file = OCFile.createNewFile(getContentProvider(),\r
-          getAccount(),\r
-          n.getProperty(NodeProperty.PATH),\r
-          0,//Long.parseLong(n.getProperty(NodeProperty.CONTENT_LENGTH)),\r
-          0,//Long.parseLong(n.getProperty(NodeProperty.CREATE_DATE)),\r
-          0,//Long.parseLong(n.getProperty(NodeProperty.LAST_MODIFIED_DATE)),\r
-          n.getProperty(NodeProperty.RESOURCE_TYPE),\r
-          parent_id);\r
-                       new_file.save();\r
-                       Log.e("ASD", new_file.getFileId()+"");\r
-                       Log.e("ASD", new_file.getFileName()+"");\r
-                       Log.e("ASD", new_file.getPath()+"");\r
-                       if (override_parent) {\r
-                         parent_id = new_file.getFileId();\r
-                         override_parent = false;\r
-                       }
-
-                       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, new_file.getFileId());
-                       }
-               }
-       }
-}
\ No newline at end of file