-/* ownCloud Android client application
- * Copyright (C) 2011 Bartek Przybylski
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-package eu.alefzero.owncloud.syncadapter;
-
-import java.io.IOException;
-
-import org.apache.http.entity.StringEntity;
-
-import android.accounts.Account;
-import android.accounts.AuthenticatorException;
-import android.accounts.OperationCanceledException;
-import android.content.ContentProviderClient;
-import android.content.ContentValues;
-import android.content.Context;
-import android.content.SyncResult;
-import android.database.Cursor;
-import android.net.Uri;
-import android.os.Bundle;
-import android.os.RemoteException;
-import android.util.Log;
-import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
-import eu.alefzero.webdav.HttpPropFind;
-import eu.alefzero.webdav.TreeNode;
-import eu.alefzero.webdav.TreeNode.NodeProperty;
-import eu.alefzero.webdav.WebdavUtils;
-
-/**
- * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
- * platform ContactOperations provider.
- */
-public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
- private static final String TAG = "FileSyncAdapter";
-
- public FileSyncAdapter(Context context, boolean autoInitialize) {
- super(context, autoInitialize);
- }
-
- @Override
- public synchronized void onPerformSync(
- Account account,
- Bundle extras,
- String authority,
- ContentProviderClient provider,
- SyncResult syncResult) {
-
- try {
- this.setAccount(account);
- this.setContentProvider(provider);
-
- HttpPropFind query = this.getBasicQuery();
- query.setEntity(new StringEntity(WebdavUtils.prepareXmlForPropFind()));
- TreeNode root = this.fireQuery(query);
-
- commitToDatabase(root, null);
- } catch (OperationCanceledException e) {
- e.printStackTrace();
- } catch (AuthenticatorException e) {
- syncResult.stats.numAuthExceptions++;
- e.printStackTrace();
- } catch (IOException e) {
- syncResult.stats.numIoExceptions++;
- e.printStackTrace();
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
-
- private void commitToDatabase(TreeNode root, String parentId) throws RemoteException {
- for (TreeNode n : root.getChildList()) {
- Log.d(TAG, n.toString());
- ContentValues cv = new ContentValues();
- cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, n.getProperty(NodeProperty.CONTENT_LENGTH));
- cv.put(ProviderTableMeta.FILE_MODIFIED, n.getProperty(NodeProperty.LAST_MODIFIED_DATE));
- cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, n.getProperty(NodeProperty.RESOURCE_TYPE));
- cv.put(ProviderTableMeta.FILE_PARENT, parentId);
-
- String name = n.getProperty(NodeProperty.NAME),
- path = n.getProperty(NodeProperty.PATH);
- Cursor c = this.getContentProvider().query(ProviderTableMeta.CONTENT_URI_FILE,
- null,
- ProviderTableMeta.FILE_NAME+"=? AND " + ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
- new String[]{name, path, this.getAccount().name},
- null);
- if (c.moveToFirst()) {
- this.getContentProvider().update(ProviderTableMeta.CONTENT_URI,
- cv,
- ProviderTableMeta._ID+"=?",
- new String[]{c.getString(c.getColumnIndex(ProviderTableMeta._ID))});
- Log.d(TAG, "ID of: "+name+":"+c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
- } else {
- cv.put(ProviderTableMeta.FILE_NAME, n.getProperty(NodeProperty.NAME));
- cv.put(ProviderTableMeta.FILE_PATH, n.getProperty(NodeProperty.PATH));
- cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, this.getAccount().name);
- Uri entry = this.getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
- Log.d(TAG, "Inserting new entry " + path + name);
- c = this.getContentProvider().query(entry, null, null, null, null);
- c.moveToFirst();
- }
- if (n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
- commitToDatabase(n, c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
- }
- }
- // clean removed files
- String[] selection = new String[root.getChildList().size()+2];
- selection[0] = this.getAccount().name;
- selection[1] = parentId;
- String qm = "";
- for (int i = 2; i < selection.length-1; ++i) {
- qm += "?,";
- selection[i] = root.getChildList().get(i-2).getProperty(NodeProperty.NAME);
- }
- if (selection.length >= 3) {
- selection[selection.length-1] = root.getChildrenNames()[selection.length-3];
- qm += "?";
- }
- for (int i = 0; i < selection.length; ++i) {
- Log.d(TAG,selection[i]+"");
- }
- Log.d(TAG,"Removing files "+ parentId);
- this.getContentProvider().delete(ProviderTableMeta.CONTENT_URI,
- ProviderTableMeta.FILE_ACCOUNT_OWNER+"=? AND " + ProviderTableMeta.FILE_PARENT + (parentId==null?" IS ":"=")+"? AND " + ProviderTableMeta.FILE_NAME + " NOT IN ("+qm+")",
- selection);
- }
-}
+/* 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
+\r
+package eu.alefzero.owncloud.syncadapter;\r
+\r
+import java.io.IOException;\r
+\r
+import org.apache.http.entity.StringEntity;\r
+\r
+import android.accounts.Account;\r
+import android.accounts.AuthenticatorException;\r
+import android.accounts.OperationCanceledException;\r
+import android.content.ContentProviderClient;\r
+import android.content.ContentValues;\r
+import android.content.Context;\r
+import android.content.SyncResult;\r
+import android.database.Cursor;\r
+import android.net.Uri;\r
+import android.os.Bundle;\r
+import android.os.RemoteException;\r
+import android.util.Log;\r
+import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;\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
+ * SyncAdapter implementation for syncing sample SyncAdapter contacts to the\r
+ * platform ContactOperations provider.\r
+ * \r
+ * @author Bartek Przybylski\r
+ */\r
+public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {\r
+ private static final String TAG = "FileSyncAdapter";\r
+\r
+ public FileSyncAdapter(Context context, boolean autoInitialize) {\r
+ super(context, autoInitialize);\r
+ }\r
+\r
+ @Override\r
+ public synchronized void onPerformSync(\r
+ Account account, \r
+ Bundle extras, \r
+ String authority, \r
+ ContentProviderClient provider, \r
+ SyncResult syncResult) {\r
+\r
+ try {\r
+ this.setAccount(account);\r
+ this.setContentProvider(provider);\r
+\r
+ HttpPropFind query = this.getPropFindQuery();\r
+ query.setEntity(new StringEntity(WebdavUtils.prepareXmlForPropFind()));\r
+ TreeNode root = this.fireRequest(query);\r
+\r
+ commitToDatabase(root, null);\r
+ } catch (OperationCanceledException e) {\r
+ e.printStackTrace();\r
+ } catch (AuthenticatorException e) {\r
+ syncResult.stats.numAuthExceptions++;\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ syncResult.stats.numIoExceptions++;\r
+ e.printStackTrace();\r
+ } catch (RemoteException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+ private void commitToDatabase(TreeNode root, String parentId) throws RemoteException {\r
+ for (TreeNode n : root.getChildList()) {\r
+ Log.d(TAG, n.toString());\r
+ ContentValues cv = new ContentValues();\r
+ cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, n.getProperty(NodeProperty.CONTENT_LENGTH));\r
+ cv.put(ProviderTableMeta.FILE_MODIFIED, n.getProperty(NodeProperty.LAST_MODIFIED_DATE));\r
+ cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, n.getProperty(NodeProperty.RESOURCE_TYPE));\r
+ cv.put(ProviderTableMeta.FILE_PARENT, parentId);\r
+\r
+ String name = n.getProperty(NodeProperty.NAME),\r
+ path = n.getProperty(NodeProperty.PATH);\r
+ Cursor c = this.getContentProvider().query(ProviderTableMeta.CONTENT_URI_FILE,\r
+ null,\r
+ ProviderTableMeta.FILE_NAME+"=? AND " + ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",\r
+ new String[]{name, path, this.getAccount().name},\r
+ null);\r
+ if (c.moveToFirst()) {\r
+ this.getContentProvider().update(ProviderTableMeta.CONTENT_URI,\r
+ cv,\r
+ ProviderTableMeta._ID+"=?",\r
+ new String[]{c.getString(c.getColumnIndex(ProviderTableMeta._ID))});\r
+ Log.d(TAG, "ID of: "+name+":"+c.getString(c.getColumnIndex(ProviderTableMeta._ID)));\r
+ } else {\r
+ cv.put(ProviderTableMeta.FILE_NAME, n.getProperty(NodeProperty.NAME));\r
+ cv.put(ProviderTableMeta.FILE_PATH, n.getProperty(NodeProperty.PATH));\r
+ cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, this.getAccount().name);\r
+ Uri entry = this.getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);\r
+ Log.d(TAG, "Inserting new entry " + path + name);\r
+ c = this.getContentProvider().query(entry, null, null, null, null);\r
+ c.moveToFirst();\r
+ }\r
+ if (n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {\r
+ commitToDatabase(n, c.getString(c.getColumnIndex(ProviderTableMeta._ID)));\r
+ }\r
+ }\r
+ // clean removed files\r
+ String[] selection = new String[root.getChildList().size()+2];\r
+ selection[0] = this.getAccount().name;\r
+ selection[1] = parentId;\r
+ String qm = "";\r
+ for (int i = 2; i < selection.length-1; ++i) {\r
+ qm += "?,";\r
+ selection[i] = root.getChildList().get(i-2).getProperty(NodeProperty.NAME);\r
+ }\r
+ if (selection.length >= 3) {\r
+ selection[selection.length-1] = root.getChildrenNames()[selection.length-3];\r
+ qm += "?";\r
+ }\r
+ for (int i = 0; i < selection.length; ++i) {\r
+ Log.d(TAG,selection[i]+"");\r
+ }\r
+ Log.d(TAG,"Removing files "+ parentId);\r
+ this.getContentProvider().delete(ProviderTableMeta.CONTENT_URI,\r
+ ProviderTableMeta.FILE_ACCOUNT_OWNER+"=? AND " + ProviderTableMeta.FILE_PARENT + (parentId==null?" IS ":"=")+"? AND " + ProviderTableMeta.FILE_NAME + " NOT IN ("+qm+")",\r
+ selection);\r
+ }\r
+}\r