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