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