02920a14560af70c78c12ce95f8c4d1dab88b0e1
[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.datamodel.FileDataStorageManager;
38 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
39 import eu.alefzero.webdav.HttpPropFind;
40 import eu.alefzero.webdav.TreeNode;
41 import eu.alefzero.webdav.TreeNode.NodeProperty;
42 import eu.alefzero.webdav.WebdavUtils;
43
44 /**
45 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
46 * platform ContactOperations provider.
47 *
48 * @author Bartek Przybylski
49 */
50 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
51 private static final String TAG = "FileSyncAdapter";
52
53 public FileSyncAdapter(Context context, boolean autoInitialize) {
54 super(context, autoInitialize);
55 }
56
57 @Override
58 public synchronized void onPerformSync(
59 Account account,
60 Bundle extras,
61 String authority,
62 ContentProviderClient provider,
63 SyncResult syncResult) {
64
65 try {
66 this.setAccount(account);
67 this.setContentProvider(provider);
68 this.setStorageManager(new FileDataStorageManager(account, getContentProvider()));
69
70 HttpPropFind query = this.getPropFindQuery();
71 query.setEntity(new StringEntity(WebdavUtils.prepareXmlForPropFind()));
72 TreeNode root = this.fireRequest(query);
73
74 // commitToDatabase(root, null);
75 } catch (OperationCanceledException e) {
76 e.printStackTrace();
77 } catch (AuthenticatorException e) {
78 syncResult.stats.numAuthExceptions++;
79 e.printStackTrace();
80 } catch (IOException e) {
81 syncResult.stats.numIoExceptions++;
82 e.printStackTrace();
83 }// catch (RemoteException e) {
84 // e.printStackTrace();
85 // q}
86 }
87
88 private void commitToDatabase(TreeNode root, String parentId) throws RemoteException {
89 for (TreeNode n : root.getChildList()) {
90 Log.d(TAG, n.toString());
91 ContentValues cv = new ContentValues();
92 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, n.getProperty(NodeProperty.CONTENT_LENGTH));
93 cv.put(ProviderTableMeta.FILE_MODIFIED, n.getProperty(NodeProperty.LAST_MODIFIED_DATE));
94 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, n.getProperty(NodeProperty.RESOURCE_TYPE));
95 cv.put(ProviderTableMeta.FILE_PARENT, parentId);
96
97 String name = n.getProperty(NodeProperty.NAME),
98 path = n.getProperty(NodeProperty.PATH);
99 Cursor c = this.getContentProvider().query(ProviderTableMeta.CONTENT_URI_FILE,
100 null,
101 ProviderTableMeta.FILE_NAME+"=? AND " + ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
102 new String[]{name, path, this.getAccount().name},
103 null);
104 if (c.moveToFirst()) {
105 this.getContentProvider().update(ProviderTableMeta.CONTENT_URI,
106 cv,
107 ProviderTableMeta._ID+"=?",
108 new String[]{c.getString(c.getColumnIndex(ProviderTableMeta._ID))});
109 Log.d(TAG, "ID of: "+name+":"+c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
110 } else {
111 cv.put(ProviderTableMeta.FILE_NAME, n.getProperty(NodeProperty.NAME));
112 cv.put(ProviderTableMeta.FILE_PATH, n.getProperty(NodeProperty.PATH));
113 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, this.getAccount().name);
114 Uri entry = this.getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
115 Log.d(TAG, "Inserting new entry " + path);
116 c = this.getContentProvider().query(entry, null, null, null, null);
117 c.moveToFirst();
118 }
119 if (n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
120 commitToDatabase(n, c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
121 }
122 }
123 // clean removed files
124 String[] selection = new String[root.getChildList().size()+2];
125 selection[0] = this.getAccount().name;
126 selection[1] = parentId;
127 String qm = "";
128 for (int i = 2; i < selection.length-1; ++i) {
129 qm += "?,";
130 selection[i] = root.getChildList().get(i-2).getProperty(NodeProperty.NAME);
131 }
132 if (selection.length >= 3) {
133 selection[selection.length-1] = root.getChildrenNames()[selection.length-3];
134 qm += "?";
135 }
136 for (int i = 0; i < selection.length; ++i) {
137 Log.d(TAG,selection[i]+"");
138 }
139 Log.d(TAG,"Removing files "+ parentId);
140 this.getContentProvider().delete(ProviderTableMeta.CONTENT_URI,
141 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=? AND " + ProviderTableMeta.FILE_PARENT + (parentId==null?" IS ":"=")+"? AND " + ProviderTableMeta.FILE_NAME + " NOT IN ("+qm+")",
142 selection);
143 }
144 }