+++ /dev/null
-/* 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.datamodel.FileDataStorageManager;\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
- this.setStorageManager(new FileDataStorageManager(account, getContentProvider()));\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
- // q}\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);\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