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