bf4801273ed0698ab85405884588467af02ac5cd
[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 import org.apache.jackrabbit.webdav.DavException;
25 import org.apache.jackrabbit.webdav.MultiStatus;
26 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
27 import org.apache.jackrabbit.webdav.property.DavProperty;
28 import org.apache.jackrabbit.webdav.property.DavPropertyName;
29
30 import android.accounts.Account;
31 import android.accounts.AuthenticatorException;
32 import android.accounts.OperationCanceledException;
33 import android.content.ContentProviderClient;
34 import android.content.ContentValues;
35 import android.content.Context;
36 import android.content.SyncResult;
37 import android.database.Cursor;
38 import android.net.Uri;
39 import android.os.Bundle;
40 import android.os.RemoteException;
41 import android.util.Log;
42 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
43 import eu.alefzero.owncloud.datamodel.OCFile;
44 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
45 import eu.alefzero.webdav.HttpPropFind;
46 import eu.alefzero.webdav.TreeNode;
47 import eu.alefzero.webdav.TreeNode.NodeProperty;
48 import eu.alefzero.webdav.WebdavEntry;
49 import eu.alefzero.webdav.WebdavUtils;
50
51 /**
52 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
53 * platform ContactOperations provider.
54 *
55 * @author Bartek Przybylski
56 */
57 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
58 private static final String TAG = "FileSyncAdapter";
59
60 public FileSyncAdapter(Context context, boolean autoInitialize) {
61 super(context, autoInitialize);
62 }
63
64 @Override
65 public synchronized void onPerformSync(
66 Account account,
67 Bundle extras,
68 String authority,
69 ContentProviderClient provider,
70 SyncResult syncResult) {
71
72 this.setAccount(account);
73 this.setContentProvider(provider);
74 this.setStorageManager(new FileDataStorageManager(account, getContentProvider()));
75
76 fetchData(getUri().toString(), syncResult, 0);
77 }
78
79 private void fetchData(String uri, SyncResult syncResult, long parentId) {
80 try {
81 PropFindMethod query = new PropFindMethod(uri);
82 getClient().executeMethod(query);
83 MultiStatus resp = null;
84 resp = query.getResponseBodyAsMultiStatus();
85 for (int i = (parentId==0?0:1); i < resp.getResponses().length; ++i) {
86 WebdavEntry we = new WebdavEntry(resp.getResponses()[i]);
87 OCFile file = new OCFile(we.path());
88 file.setCreationTimestamp(we.createTimestamp());
89 file.setFileLength(we.contentLength());
90 file.setMimetype(we.contentType());
91 file.setModificationTimestamp(we.modifiedTimesamp());
92 file.setParentId(parentId);
93 if (we.contentType().equals("DIR"))
94 fetchData(getUri().toString() + we.path(), syncResult, file.getFileId());
95 }
96
97 } catch (OperationCanceledException e) {
98 e.printStackTrace();
99 } catch (AuthenticatorException e) {
100 syncResult.stats.numAuthExceptions++;
101 e.printStackTrace();
102 } catch (IOException e) {
103 syncResult.stats.numIoExceptions++;
104 e.printStackTrace();
105 } catch (DavException e) {
106 syncResult.stats.numIoExceptions++;
107 e.printStackTrace();
108 }
109 }
110
111 }