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 3c1caf9..0000000
+++ /dev/null
@@ -1,188 +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;\r
-\r
-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.auth.AuthScope;\r
-import org.apache.http.auth.UsernamePasswordCredentials;\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.Context;\r
-import android.net.Uri;\r
-import android.text.TextUtils;\r
-import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
-import eu.alefzero.webdav.HttpPropFind;\r
-import eu.alefzero.webdav.TreeNode;\r
-import eu.alefzero.webdav.TreeNode.NodeProperty;\r
-import eu.alefzero.webdav.WebdavUtils;\r
-\r
-/**\r
- * Base SyncAdapter for OwnCloud\r
- * Designed to be subclassed for the concreete SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..\r
- * \r
- * @author sassman\r
- *\r
- */\r
-public abstract class AbstractOwnCloudSyncAdapter extends AbstractThreadedSyncAdapter {\r
-\r
-       private AccountManager accountManager;\r
-       private Account account;\r
-       private ContentProviderClient contentProvider;\r
-       private Date lastUpdated;\r
-       \r
-       private DefaultHttpClient client = null;\r
-       private HttpHost host;\r
-\r
-       public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize) {\r
-               super(context, autoInitialize);\r
-               this.setAccountManager(AccountManager.get(context));\r
-       }\r
-\r
-       public AccountManager getAccountManager() {\r
-               return accountManager;\r
-       }\r
-\r
-       public void setAccountManager(AccountManager accountManager) {\r
-               this.accountManager = accountManager;\r
-       }\r
-\r
-       public Account getAccount() {\r
-               return account;\r
-       }\r
-\r
-       public void setAccount(Account account) {\r
-               this.account = account;\r
-       }\r
-\r
-       public ContentProviderClient getContentProvider() {\r
-               return contentProvider;\r
-       }\r
-\r
-       public void setContentProvider(ContentProviderClient contentProvider) {\r
-               this.contentProvider = contentProvider;\r
-       }\r
-\r
-       public Date getLastUpdated() {\r
-               return lastUpdated;\r
-       }\r
-\r
-       public void setLastUpdated(Date lastUpdated) {\r
-               this.lastUpdated = lastUpdated;\r
-       }\r
-       \r
-       protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {\r
-               return new ConnectionKeepAliveStrategy() {\r
-                       public long getKeepAliveDuration(HttpResponse response, HttpContext context) {\r
-                               // TODO: change keep alive straategy basing on response: ie forbidden/not found/etc\r
-                               // should have keep alive 0\r
-                               // default return: 5s\r
-                               return 5 * 1000;\r
-                       }\r
-               };\r
-       }\r
-       \r
-       protected HttpPropFind getPropFindQuery() throws OperationCanceledException, AuthenticatorException, IOException {\r
-               HttpPropFind query = new HttpPropFind(getUri().toString());\r
-               query.setHeader("Content-type", "text/xml");\r
-               query.setHeader("User-Agent", "Android-ownCloud");\r
-               return query;\r
-       }\r
-       \r
-       protected HttpResponse fireRawRequest(HttpRequest query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {\r
-           BasicHttpContext httpContext = new BasicHttpContext();\r
-        BasicScheme basicAuth = new BasicScheme();\r
-        httpContext.setAttribute("preemptive-auth", basicAuth);\r
-        \r
-        HttpResponse response = getClient().execute(this.host, query, httpContext);\r
-        return response;\r
-       }\r
-       \r
-       protected TreeNode fireRequest(HttpRequest query) throws ClientProtocolException, OperationCanceledException, AuthenticatorException, IOException {\r
-               HttpResponse response = fireRawRequest(query);\r
-               \r
-               TreeNode root = new TreeNode();\r
-               root.setProperty(TreeNode.NodeProperty.NAME, "/");\r
-               this.parseResponse(response, getUri(), getClient(), this.host, root.getChildList());\r
-               return root;\r
-       }\r
-       \r
-       protected Uri getUri() {\r
-               return Uri.parse(this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL));\r
-       }\r
-       \r
-       private DefaultHttpClient getClient() throws OperationCanceledException, AuthenticatorException, IOException {\r
-               if(this.client == null) {\r
-                       String username = getAccount().name.split("@")[0];\r
-                       String password = this.getAccountManager().blockingGetAuthToken(getAccount(), AccountAuthenticator.AUTH_TOKEN_TYPE, true);\r
-                       if (this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL) == null) {\r
-                               throw new UnknownHostException();\r
-                       }\r
-                       Uri uri = getUri();\r
-       \r
-                       int port = (uri.getPort() == -1) ? 80 : uri.getPort();\r
-                       this.client = new DefaultHttpClient();\r
-                       this.client.getCredentialsProvider().setCredentials(\r
-                               new AuthScope(uri.getHost(), port),\r
-                               new UsernamePasswordCredentials(username, password)\r
-                       );\r
-                       this.client.setKeepAliveStrategy(this.getKeepAliveStrategy());\r
-                       this.host = new HttpHost(uri.getHost(), port, (uri.getScheme() == "https") ? "https" : "http");\r
-               }\r
-               \r
-               return this.client;\r
-       }\r
-       \r
-       private void parseResponse(HttpResponse resp, Uri uri, DefaultHttpClient client, HttpHost targetHost, LinkedList<TreeNode> insertList) throws IOException, OperationCanceledException, AuthenticatorException {\r
-               boolean skipFirst = true;\r
-               for (TreeNode n :WebdavUtils.parseResponseToNodes(resp.getEntity().getContent())) {\r
-                       String path = n.stripPathFromFilename(uri.getPath());\r
-                       if (skipFirst) {\r
-                               skipFirst = false;\r
-                               continue;\r
-                       }\r
-                       insertList.add(n);\r
-\r
-                       if (!TextUtils.isEmpty(n.getProperty(NodeProperty.NAME)) &&\r
-                                       n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {\r
-                           \r
-                           HttpPropFind method = new HttpPropFind(uri.getPath() + path + n.getProperty(NodeProperty.NAME).replace(" ", "%20") + "/");\r
-                               HttpResponse response = fireRawRequest(method);\r
-                               parseResponse(response, uri, client, targetHost, n.getChildList());\r
-                       }\r
-               }\r
-       }\r
-       \r
-}
\ No newline at end of file