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