removing file on sync
[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 long mCurrentSyncTime;
51
52 public FileSyncAdapter(Context context, boolean autoInitialize) {
53 super(context, autoInitialize);
54 }
55
56 @Override
57 public synchronized void onPerformSync(Account account, Bundle extras,
58 String authority, ContentProviderClient provider,
59 SyncResult syncResult) {
60
61 this.setAccount(account);
62 this.setContentProvider(provider);
63 this.setStorageManager(new FileDataStorageManager(account,
64 getContentProvider()));
65
66 Log.d("ASD", "syncing owncloud account " + account.name);
67
68 Intent i = new Intent(FileSyncService.SYNC_MESSAGE);
69 i.putExtra(FileSyncService.IN_PROGRESS, true);
70 i.putExtra(FileSyncService.ACCOUNT_NAME, account.name);
71 getContext().sendStickyBroadcast(i);
72
73 PropFindMethod query;
74 try {
75 mCurrentSyncTime = System.currentTimeMillis();
76 query = new PropFindMethod(getUri().toString() + "/");
77 getClient().executeMethod(query);
78 MultiStatus resp = null;
79 resp = query.getResponseBodyAsMultiStatus();
80
81 if (resp.getResponses().length > 0) {
82 WebdavEntry we = new WebdavEntry(resp.getResponses()[0]);
83 OCFile file = fillOCFile(we);
84 file.setParentId(0);
85 getStorageManager().saveFile(file);
86 fetchData(getUri().toString(), syncResult, file.getFileId());
87 }
88 } catch (OperationCanceledException e) {
89 e.printStackTrace();
90 } catch (AuthenticatorException e) {
91 syncResult.stats.numAuthExceptions++;
92 e.printStackTrace();
93 } catch (IOException e) {
94 syncResult.stats.numIoExceptions++;
95 e.printStackTrace();
96 } catch (DavException e) {
97 syncResult.stats.numIoExceptions++;
98 e.printStackTrace();
99 }
100 i.putExtra(FileSyncService.IN_PROGRESS, false);
101 getContext().sendStickyBroadcast(i);
102 }
103
104 private void fetchData(String uri, SyncResult syncResult, long parentId) {
105 try {
106 PropFindMethod query = new PropFindMethod(uri);
107 getClient().executeMethod(query);
108 MultiStatus resp = null;
109 resp = query.getResponseBodyAsMultiStatus();
110 for (int i = 1; i < resp.getResponses().length; ++i) {
111 WebdavEntry we = new WebdavEntry(resp.getResponses()[i]);
112 OCFile file = fillOCFile(we);
113 file.setParentId(parentId);
114 getStorageManager().saveFile(file);
115 if (parentId == 0)
116 parentId = file.getFileId();
117 if (we.contentType().equals("DIR"))
118 fetchData(getUri().toString() + we.path(), syncResult, file.getFileId());
119 }
120 Vector<OCFile> files = getStorageManager().getDirectoryContent(
121 getStorageManager().getFileById(parentId));
122 for (OCFile file : files) {
123 if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0)
124 getStorageManager().removeFile(file);
125 }
126 } catch (OperationCanceledException e) {
127 e.printStackTrace();
128 } catch (AuthenticatorException e) {
129 syncResult.stats.numAuthExceptions++;
130 e.printStackTrace();
131 } catch (IOException e) {
132 syncResult.stats.numIoExceptions++;
133 e.printStackTrace();
134 } catch (DavException e) {
135 syncResult.stats.numIoExceptions++;
136 e.printStackTrace();
137 }
138 }
139
140 private OCFile fillOCFile(WebdavEntry we) {
141 OCFile file = new OCFile(URLDecoder.decode(we.path()));
142 file.setCreationTimestamp(we.createTimestamp());
143 file.setFileLength(we.contentLength());
144 file.setMimetype(we.contentType());
145 file.setModificationTimestamp(we.modifiedTimesamp());
146 file.setLastSyncDate(mCurrentSyncTime);
147 return file;
148 }
149
150 }