133e5fc17e1e534081807fe2250e024dedf0e875
[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.jackrabbit.webdav.DavException;
24 import org.apache.jackrabbit.webdav.MultiStatus;
25 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
26
27 import android.accounts.Account;
28 import android.accounts.AuthenticatorException;
29 import android.accounts.OperationCanceledException;
30 import android.content.ContentProviderClient;
31 import android.content.Context;
32 import android.content.SyncResult;
33 import android.os.Bundle;
34 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
35 import eu.alefzero.owncloud.datamodel.OCFile;
36 import eu.alefzero.webdav.WebdavEntry;
37
38 /**
39 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
40 * platform ContactOperations provider.
41 *
42 * @author Bartek Przybylski
43 */
44 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
45
46 public FileSyncAdapter(Context context, boolean autoInitialize) {
47 super(context, autoInitialize);
48 }
49
50 @Override
51 public synchronized void onPerformSync(
52 Account account,
53 Bundle extras,
54 String authority,
55 ContentProviderClient provider,
56 SyncResult syncResult) {
57
58 this.setAccount(account);
59 this.setContentProvider(provider);
60 this.setStorageManager(new FileDataStorageManager(account, getContentProvider()));
61
62 PropFindMethod query;
63 try {
64 query = new PropFindMethod(getUri().toString());
65 getClient().executeMethod(query);
66 MultiStatus resp = null;
67 resp = query.getResponseBodyAsMultiStatus();
68 if (resp.getResponses().length > 0) {
69 WebdavEntry we = new WebdavEntry(resp.getResponses()[0]);
70 OCFile file = fillOCFile(we);
71 file.setParentId(0);
72 getStorageManager().saveFile(file);
73 fetchData(getUri().toString(), syncResult, file.getFileId());
74 }
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 (DavException e) {
84 syncResult.stats.numIoExceptions++;
85 e.printStackTrace();
86 }
87
88 }
89
90 private void fetchData(String uri, SyncResult syncResult, long parentId) {
91 try {
92 PropFindMethod query = new PropFindMethod(uri);
93 getClient().executeMethod(query);
94 MultiStatus resp = null;
95 resp = query.getResponseBodyAsMultiStatus();
96 for (int i = 1; i < resp.getResponses().length; ++i) {
97 WebdavEntry we = new WebdavEntry(resp.getResponses()[i]);
98 OCFile file = fillOCFile(we);
99 file.setParentId(parentId);
100 getStorageManager().saveFile(file);
101 if (parentId == 0) parentId = file.getFileId();
102 if (we.contentType().equals("DIR"))
103 fetchData(getUri().toString() + we.path(), syncResult, file.getFileId());
104 }
105 } catch (OperationCanceledException e) {
106 e.printStackTrace();
107 } catch (AuthenticatorException e) {
108 syncResult.stats.numAuthExceptions++;
109 e.printStackTrace();
110 } catch (IOException e) {
111 syncResult.stats.numIoExceptions++;
112 e.printStackTrace();
113 } catch (DavException e) {
114 syncResult.stats.numIoExceptions++;
115 e.printStackTrace();
116 }
117 }
118
119 private OCFile fillOCFile(WebdavEntry we) {
120 OCFile file = new OCFile(we.path());
121 file.setCreationTimestamp(we.createTimestamp());
122 file.setFileLength(we.contentLength());
123 file.setMimetype(we.contentType());
124 file.setModificationTimestamp(we.modifiedTimesamp());
125 return file;
126 }
127
128 }