Studying lack of upload when a file that was not previously down is set as favourite...
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 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 com.owncloud.android.operations;
20
21 import org.apache.http.HttpStatus;
22 import org.apache.jackrabbit.webdav.MultiStatus;
23 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
24
25 import android.accounts.Account;
26 import android.util.Log;
27
28 import com.owncloud.android.datamodel.DataStorageManager;
29 import com.owncloud.android.datamodel.OCFile;
30
31 import eu.alefzero.webdav.WebdavClient;
32 import eu.alefzero.webdav.WebdavEntry;
33 import eu.alefzero.webdav.WebdavUtils;
34
35 public class SynchronizeFileOperation extends RemoteOperation {
36
37 private String TAG = SynchronizeFileOperation.class.getSimpleName();
38
39 private String mRemotePath;
40
41 private DataStorageManager mStorageManager;
42
43 private Account mAccount;
44
45 public SynchronizeFileOperation(
46 String remotePath,
47 DataStorageManager dataStorageManager,
48 Account account) {
49 mRemotePath = remotePath;
50 mStorageManager = dataStorageManager;
51 mAccount = account;
52 }
53
54 @Override
55 protected RemoteOperationResult run(WebdavClient client) {
56 PropFindMethod propfind = null;
57 RemoteOperationResult result = null;
58 try {
59 propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
60 int status = client.executeMethod(propfind);
61 boolean isMultiStatus = status == HttpStatus.SC_MULTI_STATUS;
62 Boolean isConflict = Boolean.FALSE;
63 if (isMultiStatus) {
64 MultiStatus resp = propfind.getResponseBodyAsMultiStatus();
65 WebdavEntry we = new WebdavEntry(resp.getResponses()[0],
66 client.getBaseUri().getPath());
67 OCFile file = fillOCFile(we);
68 OCFile oldFile = mStorageManager.getFileByPath(file.getRemotePath());
69 if (oldFile.getFileLength() != file.getFileLength() ||
70 oldFile.getModificationTimestamp() != file.getModificationTimestamp()) {
71 isConflict = Boolean.TRUE;
72 }
73
74 } else {
75 client.exhaustResponse(propfind.getResponseBodyAsStream());
76 }
77
78 result = new RemoteOperationResult(isMultiStatus, status);
79 result.setExtraData(isConflict);
80 Log.i(TAG, "Synchronizing " + mAccount.name + ", file " + mRemotePath + ": " + result.getLogMessage());
81 } catch (Exception e) {
82 result = new RemoteOperationResult(e);
83 Log.e(TAG, "Synchronizing " + mAccount.name + ", file " + mRemotePath + ": " + result.getLogMessage(), result.getException());
84
85 } finally {
86 if (propfind != null)
87 propfind.releaseConnection();
88 }
89 return result;
90 }
91
92 /**
93 * Creates and populates a new {@link OCFile} object with the data read from the server.
94 *
95 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
96 * @return New OCFile instance representing the remote resource described by we.
97 */
98 private OCFile fillOCFile(WebdavEntry we) {
99 OCFile file = new OCFile(we.decodedPath());
100 file.setCreationTimestamp(we.createTimestamp());
101 file.setFileLength(we.contentLength());
102 file.setMimetype(we.contentType());
103 file.setModificationTimestamp(we.modifiedTimesamp());
104 file.setLastSyncDate(System.currentTimeMillis());
105 return file;
106 }
107
108 }