Merge branch 'master' of gitorious.org:owncloud/android-devel
[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 import java.util.Vector;
23
24 import org.apache.jackrabbit.webdav.DavException;
25 import org.apache.jackrabbit.webdav.MultiStatus;
26 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
27
28 import android.accounts.Account;
29 import android.accounts.AuthenticatorException;
30 import android.accounts.OperationCanceledException;
31 import android.content.ContentProviderClient;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.content.SyncResult;
35 import android.os.Bundle;
36 import android.util.Log;
37 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
38 import eu.alefzero.owncloud.datamodel.OCFile;
39 import eu.alefzero.webdav.WebdavEntry;
40
41 /**
42 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
43 * platform ContactOperations provider.
44 *
45 * @author Bartek Przybylski
46 */
47 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
48
49 private final static String TAG = "FileSyncAdapter";
50
51 private long mCurrentSyncTime;
52
53 public FileSyncAdapter(Context context, boolean autoInitialize) {
54 super(context, autoInitialize);
55 }
56
57 @Override
58 public synchronized void onPerformSync(Account account, Bundle extras,
59 String authority, ContentProviderClient provider,
60 SyncResult syncResult) {
61
62 this.setAccount(account);
63 this.setContentProvider(provider);
64 this.setStorageManager(new FileDataStorageManager(account,
65 getContentProvider()));
66
67 Log.d(TAG, "syncing owncloud account " + account.name);
68
69 Intent i = new Intent(FileSyncService.SYNC_MESSAGE);
70 i.putExtra(FileSyncService.IN_PROGRESS, true);
71 i.putExtra(FileSyncService.ACCOUNT_NAME, account.name);
72 getContext().sendStickyBroadcast(i);
73
74 PropFindMethod query;
75 try {
76 mCurrentSyncTime = System.currentTimeMillis();
77 query = new PropFindMethod(getUri().toString() + "/");
78 getClient().executeMethod(query);
79 MultiStatus resp = null;
80 resp = query.getResponseBodyAsMultiStatus();
81
82 if (resp.getResponses().length > 0) {
83 WebdavEntry we = new WebdavEntry(resp.getResponses()[0], getUri().getPath());
84 OCFile file = fillOCFile(we);
85 file.setParentId(0);
86 getStorageManager().saveFile(file);
87 fetchData(getUri().toString(), syncResult, file.getFileId());
88 }
89 } catch (OperationCanceledException e) {
90 e.printStackTrace();
91 } catch (AuthenticatorException e) {
92 syncResult.stats.numAuthExceptions++;
93 e.printStackTrace();
94 } catch (IOException e) {
95 syncResult.stats.numIoExceptions++;
96 e.printStackTrace();
97 } catch (DavException e) {
98 syncResult.stats.numIoExceptions++;
99 e.printStackTrace();
100 }
101 i.putExtra(FileSyncService.IN_PROGRESS, false);
102 getContext().sendStickyBroadcast(i);
103 }
104
105 private void fetchData(String uri, SyncResult syncResult, long parentId) {
106 try {
107 PropFindMethod query = new PropFindMethod(uri);
108 getClient().executeMethod(query);
109 MultiStatus resp = null;
110 resp = query.getResponseBodyAsMultiStatus();
111 for (int i = 1; i < resp.getResponses().length; ++i) {
112 WebdavEntry we = new WebdavEntry(resp.getResponses()[i], getUri().getPath());
113 OCFile file = fillOCFile(we);
114 file.setParentId(parentId);
115 getStorageManager().saveFile(file);
116 if (parentId == 0)
117 parentId = file.getFileId();
118 if (we.contentType().equals("DIR"))
119 fetchData(getUri().toString() + we.path(), syncResult, file.getFileId());
120 }
121 Vector<OCFile> files = getStorageManager().getDirectoryContent(
122 getStorageManager().getFileById(parentId));
123 for (OCFile file : files) {
124 if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0)
125 getStorageManager().removeFile(file);
126 }
127 } catch (OperationCanceledException e) {
128 e.printStackTrace();
129 } catch (AuthenticatorException e) {
130 syncResult.stats.numAuthExceptions++;
131 e.printStackTrace();
132 } catch (IOException e) {
133 syncResult.stats.numIoExceptions++;
134 e.printStackTrace();
135 } catch (DavException e) {
136 syncResult.stats.numIoExceptions++;
137 e.printStackTrace();
138 }
139 }
140
141 private OCFile fillOCFile(WebdavEntry we) {
142 OCFile file = new OCFile(we.path());
143 file.setCreationTimestamp(we.createTimestamp());
144 file.setFileLength(we.contentLength());
145 file.setMimetype(we.contentType());
146 file.setModificationTimestamp(we.modifiedTimesamp());
147 file.setLastSyncDate(mCurrentSyncTime);
148 return file;
149 }
150
151 }