bf00240e11affdd87606ff2f792004f004b6ed44
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / syncadapter / FileSyncAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.syncadapter;
20
21 import java.io.IOException;
22
23 import org.apache.http.entity.StringEntity;
24
25 import android.accounts.Account;
26 import android.accounts.AuthenticatorException;
27 import android.accounts.OperationCanceledException;
28 import android.content.ContentProviderClient;
29 import android.content.ContentValues;
30 import android.content.Context;
31 import android.content.SyncResult;
32 import android.database.Cursor;
33 import android.net.Uri;
34 import android.os.Bundle;
35 import android.os.RemoteException;
36 import android.util.Log;
37 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
38 import eu.alefzero.webdav.HttpPropFind;
39 import eu.alefzero.webdav.TreeNode;
40 import eu.alefzero.webdav.TreeNode.NodeProperty;
41 import eu.alefzero.webdav.WebdavUtils;
42
43 /**
44 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
45 * platform ContactOperations provider.
46 */
47 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
48 private static final String TAG = "FileSyncAdapter";
49
50 public FileSyncAdapter(Context context, boolean autoInitialize) {
51 super(context, autoInitialize);
52 }
53
54 @Override
55 public synchronized void onPerformSync(
56 Account account,
57 Bundle extras,
58 String authority,
59 ContentProviderClient provider,
60 SyncResult syncResult) {
61
62 try {
63 this.setAccount(account);
64 this.setContentProvider(provider);
65
66 HttpPropFind query = this.getPropFindQuery();
67 query.setEntity(new StringEntity(WebdavUtils.prepareXmlForPropFind()));
68 TreeNode root = this.fireRequest(query);
69
70 commitToDatabase(root, null);
71 } catch (OperationCanceledException e) {
72 e.printStackTrace();
73 } catch (AuthenticatorException e) {
74 syncResult.stats.numAuthExceptions++;
75 e.printStackTrace();
76 } catch (IOException e) {
77 syncResult.stats.numIoExceptions++;
78 e.printStackTrace();
79 } catch (RemoteException e) {
80 e.printStackTrace();
81 }
82 }
83
84 private void commitToDatabase(TreeNode root, String parentId) throws RemoteException {
85 for (TreeNode n : root.getChildList()) {
86 Log.d(TAG, n.toString());
87 ContentValues cv = new ContentValues();
88 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, n.getProperty(NodeProperty.CONTENT_LENGTH));
89 cv.put(ProviderTableMeta.FILE_MODIFIED, n.getProperty(NodeProperty.LAST_MODIFIED_DATE));
90 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, n.getProperty(NodeProperty.RESOURCE_TYPE));
91 cv.put(ProviderTableMeta.FILE_PARENT, parentId);
92
93 String name = n.getProperty(NodeProperty.NAME),
94 path = n.getProperty(NodeProperty.PATH);
95 Cursor c = this.getContentProvider().query(ProviderTableMeta.CONTENT_URI_FILE,
96 null,
97 ProviderTableMeta.FILE_NAME+"=? AND " + ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
98 new String[]{name, path, this.getAccount().name},
99 null);
100 if (c.moveToFirst()) {
101 this.getContentProvider().update(ProviderTableMeta.CONTENT_URI,
102 cv,
103 ProviderTableMeta._ID+"=?",
104 new String[]{c.getString(c.getColumnIndex(ProviderTableMeta._ID))});
105 Log.d(TAG, "ID of: "+name+":"+c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
106 } else {
107 cv.put(ProviderTableMeta.FILE_NAME, n.getProperty(NodeProperty.NAME));
108 cv.put(ProviderTableMeta.FILE_PATH, n.getProperty(NodeProperty.PATH));
109 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, this.getAccount().name);
110 Uri entry = this.getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
111 Log.d(TAG, "Inserting new entry " + path + name);
112 c = this.getContentProvider().query(entry, null, null, null, null);
113 c.moveToFirst();
114 }
115 if (n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
116 commitToDatabase(n, c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
117 }
118 }
119 // clean removed files
120 String[] selection = new String[root.getChildList().size()+2];
121 selection[0] = this.getAccount().name;
122 selection[1] = parentId;
123 String qm = "";
124 for (int i = 2; i < selection.length-1; ++i) {
125 qm += "?,";
126 selection[i] = root.getChildList().get(i-2).getProperty(NodeProperty.NAME);
127 }
128 if (selection.length >= 3) {
129 selection[selection.length-1] = root.getChildrenNames()[selection.length-3];
130 qm += "?";
131 }
132 for (int i = 0; i < selection.length; ++i) {
133 Log.d(TAG,selection[i]+"");
134 }
135 Log.d(TAG,"Removing files "+ parentId);
136 this.getContentProvider().delete(ProviderTableMeta.CONTENT_URI,
137 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=? AND " + ProviderTableMeta.FILE_PARENT + (parentId==null?" IS ":"=")+"? AND " + ProviderTableMeta.FILE_NAME + " NOT IN ("+qm+")",
138 selection);
139 }
140 }