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 android
.util
.Log
;
35 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
36 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
37 import eu
.alefzero
.webdav
.WebdavEntry
;
40 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
41 * platform ContactOperations provider.
43 * @author Bartek Przybylski
45 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
46 private static final String TAG
= "FileSyncAdapter";
48 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
49 super(context
, autoInitialize
);
53 public synchronized void onPerformSync(
57 ContentProviderClient provider
,
58 SyncResult syncResult
) {
60 this.setAccount(account
);
61 this.setContentProvider(provider
);
62 this.setStorageManager(new FileDataStorageManager(account
, getContentProvider()));
66 query
= new PropFindMethod(getUri().toString());
67 getClient().executeMethod(query
);
68 MultiStatus resp
= null
;
69 resp
= query
.getResponseBodyAsMultiStatus();
70 if (resp
.getResponses().length
> 0) {
71 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0]);
72 OCFile file
= fillOCFile(we
);
74 getStorageManager().saveFile(file
);
75 Log
.d(TAG
, file
.getPath() + " " + file
.getFileId());
76 fetchData(getUri().toString(), syncResult
, file
.getFileId());
78 } catch (OperationCanceledException e
) {
80 } catch (AuthenticatorException e
) {
81 syncResult
.stats
.numAuthExceptions
++;
83 } catch (IOException e
) {
84 syncResult
.stats
.numIoExceptions
++;
86 } catch (DavException e
) {
87 syncResult
.stats
.numIoExceptions
++;
93 private void fetchData(String uri
, SyncResult syncResult
, long parentId
) {
95 PropFindMethod query
= new PropFindMethod(uri
);
96 getClient().executeMethod(query
);
97 MultiStatus resp
= null
;
98 resp
= query
.getResponseBodyAsMultiStatus();
99 for (int i
= 1; i
< resp
.getResponses().length
; ++i
) {
100 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
]);
101 OCFile file
= fillOCFile(we
);
102 file
.setParentId(parentId
);
103 getStorageManager().saveFile(file
);
104 if (parentId
== 0) parentId
= file
.getFileId();
105 if (we
.contentType().equals("DIR"))
106 fetchData(getUri().toString() + we
.path(), syncResult
, file
.getFileId());
108 } catch (OperationCanceledException e
) {
110 } catch (AuthenticatorException e
) {
111 syncResult
.stats
.numAuthExceptions
++;
113 } catch (IOException e
) {
114 syncResult
.stats
.numIoExceptions
++;
116 } catch (DavException e
) {
117 syncResult
.stats
.numIoExceptions
++;
122 private OCFile
fillOCFile(WebdavEntry we
) {
123 OCFile file
= new OCFile(we
.path());
124 file
.setCreationTimestamp(we
.createTimestamp());
125 file
.setFileLength(we
.contentLength());
126 file
.setMimetype(we
.contentType());
127 file
.setModificationTimestamp(we
.modifiedTimesamp());