56892626bd862676c6cb94551d9e0a59bd4314bd
[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.ArrayList;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Queue;
27 import java.util.Vector;
28
29 import org.apache.jackrabbit.webdav.DavException;
30 import org.apache.jackrabbit.webdav.MultiStatus;
31 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
32
33 import android.accounts.Account;
34 import android.accounts.AuthenticatorException;
35 import android.accounts.OperationCanceledException;
36 import android.content.ContentProviderClient;
37 import android.content.Context;
38 import android.content.Intent;
39 import android.content.SyncResult;
40 import android.os.Bundle;
41 import android.util.Log;
42 import android.webkit.MimeTypeMap;
43 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
44 import eu.alefzero.owncloud.datamodel.OCFile;
45 import eu.alefzero.webdav.WebdavEntry;
46
47 /**
48 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
49 * platform ContactOperations provider.
50 *
51 * @author Bartek Przybylski
52 */
53 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
54
55 private final static String TAG = "FileSyncAdapter";
56
57 private long mCurrentSyncTime;
58
59 public FileSyncAdapter(Context context, boolean autoInitialize) {
60 super(context, autoInitialize);
61 }
62
63 @Override
64 public synchronized void onPerformSync(Account account, Bundle extras,
65 String authority, ContentProviderClient provider,
66 SyncResult syncResult) {
67
68 this.setAccount(account);
69 this.setContentProvider(provider);
70 this.setStorageManager(new FileDataStorageManager(account,
71 getContentProvider()));
72
73 Log.d(TAG, "syncing owncloud account " + account.name);
74
75 sendStickyBroadcast(true, -1); // message to signal the start to the UI
76
77 PropFindMethod query;
78 try {
79 mCurrentSyncTime = System.currentTimeMillis();
80 query = new PropFindMethod(getUri().toString() + "/");
81 getClient().executeMethod(query);
82 MultiStatus resp = null;
83 resp = query.getResponseBodyAsMultiStatus();
84
85 if (resp.getResponses().length > 0) {
86 WebdavEntry we = new WebdavEntry(resp.getResponses()[0], getUri().getPath());
87 OCFile file = fillOCFile(we);
88 file.setParentId(0);
89 getStorageManager().saveFile(file);
90 fetchData(getUri().toString(), syncResult, file.getFileId());
91 }
92 } catch (OperationCanceledException e) {
93 e.printStackTrace();
94 } catch (AuthenticatorException e) {
95 syncResult.stats.numAuthExceptions++;
96 e.printStackTrace();
97 } catch (IOException e) {
98 syncResult.stats.numIoExceptions++;
99 e.printStackTrace();
100 } catch (DavException e) {
101 syncResult.stats.numIoExceptions++;
102 e.printStackTrace();
103 } catch (Throwable t) {
104 //TODO count ; any type of exception should be treated, and the progress indicator finished;
105 // reporting the user about bad synchronizations should be discussed
106 t.printStackTrace();
107 }
108 sendStickyBroadcast(false, -1);
109 }
110
111 private void fetchData(String uri, SyncResult syncResult, long parentId) {
112 try {
113 PropFindMethod query = new PropFindMethod(uri);
114 getClient().executeMethod(query);
115 MultiStatus resp = null;
116 resp = query.getResponseBodyAsMultiStatus();
117 Queue<String> paths = new LinkedList<String>();
118 Queue<Long> fileIds = new LinkedList<Long>();
119 for (int i = 1; i < resp.getResponses().length; ++i) {
120 WebdavEntry we = new WebdavEntry(resp.getResponses()[i], getUri().getPath());
121 OCFile file = fillOCFile(we);
122 file.setParentId(parentId);
123 getStorageManager().saveFile(file);
124 if (parentId == 0)
125 parentId = file.getFileId();
126 if (we.contentType().equals("DIR")) {
127 // for recursive fetch later
128 paths.add(we.path());
129 fileIds.add(file.getFileId());
130 }
131 }
132
133 Vector<OCFile> files = getStorageManager().getDirectoryContent(
134 getStorageManager().getFileById(parentId));
135 for (OCFile file : files) {
136 if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0)
137 getStorageManager().removeFile(file);
138 }
139
140 // synched folder -> notice to IU
141 sendStickyBroadcast(true, parentId);
142
143 // recursive fetch
144 while(!paths.isEmpty()) {
145 fetchData(getUri().toString() + paths.remove(), syncResult, fileIds.remove());
146 }
147 paths = null;
148 fileIds = null;
149
150
151 } catch (OperationCanceledException e) {
152 e.printStackTrace();
153 } catch (AuthenticatorException e) {
154 syncResult.stats.numAuthExceptions++;
155 e.printStackTrace();
156 } catch (IOException e) {
157 syncResult.stats.numIoExceptions++;
158 e.printStackTrace();
159 } catch (DavException e) {
160 syncResult.stats.numIoExceptions++;
161 e.printStackTrace();
162 }
163 }
164
165 private OCFile fillOCFile(WebdavEntry we) {
166 OCFile file = new OCFile(we.path());
167 file.setCreationTimestamp(we.createTimestamp());
168 file.setFileLength(we.contentLength());
169 file.setMimetype(we.contentType());
170 file.setModificationTimestamp(we.modifiedTimesamp());
171 file.setLastSyncDate(mCurrentSyncTime);
172 return file;
173 }
174
175
176 private void sendStickyBroadcast(boolean inProgress, long OCDirId) {
177 Intent i = new Intent(FileSyncService.SYNC_MESSAGE);
178 i.putExtra(FileSyncService.IN_PROGRESS, inProgress);
179 i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name);
180 if (OCDirId > 0) {
181 i.putExtra(FileSyncService.SYNC_FOLDER, OCDirId);
182 }
183 getContext().sendStickyBroadcast(i);
184 }
185
186 }