1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
.syncadapter
;
21 import java
.io
.IOException
;
23 import org
.apache
.jackrabbit
.webdav
.DavException
;
24 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
25 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
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
.SyncResult
;
33 import android
.os
.Bundle
;
34 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
35 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
36 import eu
.alefzero
.webdav
.WebdavEntry
;
39 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
40 * platform ContactOperations provider.
42 * @author Bartek Przybylski
44 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
46 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
47 super(context
, autoInitialize
);
51 public synchronized void onPerformSync(
55 ContentProviderClient provider
,
56 SyncResult syncResult
) {
58 this.setAccount(account
);
59 this.setContentProvider(provider
);
60 this.setStorageManager(new FileDataStorageManager(account
, getContentProvider()));
64 query
= new PropFindMethod(getUri().toString());
65 getClient().executeMethod(query
);
66 MultiStatus resp
= null
;
67 resp
= query
.getResponseBodyAsMultiStatus();
68 if (resp
.getResponses().length
> 0) {
69 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0]);
70 OCFile file
= fillOCFile(we
);
72 getStorageManager().saveFile(file
);
73 fetchData(getUri().toString(), syncResult
, file
.getFileId());
75 } catch (OperationCanceledException e
) {
77 } catch (AuthenticatorException e
) {
78 syncResult
.stats
.numAuthExceptions
++;
80 } catch (IOException e
) {
81 syncResult
.stats
.numIoExceptions
++;
83 } catch (DavException e
) {
84 syncResult
.stats
.numIoExceptions
++;
90 private void fetchData(String uri
, SyncResult syncResult
, long parentId
) {
92 PropFindMethod query
= new PropFindMethod(uri
);
93 getClient().executeMethod(query
);
94 MultiStatus resp
= null
;
95 resp
= query
.getResponseBodyAsMultiStatus();
96 for (int i
= 1; i
< resp
.getResponses().length
; ++i
) {
97 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
]);
98 OCFile file
= fillOCFile(we
);
99 file
.setParentId(parentId
);
100 getStorageManager().saveFile(file
);
101 if (parentId
== 0) parentId
= file
.getFileId();
102 if (we
.contentType().equals("DIR"))
103 fetchData(getUri().toString() + we
.path(), syncResult
, file
.getFileId());
105 } catch (OperationCanceledException e
) {
107 } catch (AuthenticatorException e
) {
108 syncResult
.stats
.numAuthExceptions
++;
110 } catch (IOException e
) {
111 syncResult
.stats
.numIoExceptions
++;
113 } catch (DavException e
) {
114 syncResult
.stats
.numIoExceptions
++;
119 private OCFile
fillOCFile(WebdavEntry we
) {
120 OCFile file
= new OCFile(we
.path());
121 file
.setCreationTimestamp(we
.createTimestamp());
122 file
.setFileLength(we
.contentLength());
123 file
.setMimetype(we
.contentType());
124 file
.setModificationTimestamp(we
.modifiedTimesamp());