Disable eTag as a criteria for synchronization of file contents
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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.DavConstants;
23 import org.apache.jackrabbit.webdav.MultiStatus;
24 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
25
26 import android.accounts.Account;
27 import android.content.Context;
28 import android.content.Intent;
29
30 import com.owncloud.android.Log_OC;
31 import com.owncloud.android.datamodel.FileDataStorageManager;
32 import com.owncloud.android.datamodel.OCFile;
33 import com.owncloud.android.files.services.FileDownloader;
34 import com.owncloud.android.files.services.FileUploader;
35 import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
36
37 import eu.alefzero.webdav.WebdavClient;
38 import eu.alefzero.webdav.WebdavEntry;
39 import eu.alefzero.webdav.WebdavUtils;
40
41 public class SynchronizeFileOperation extends RemoteOperation {
42
43 private String TAG = SynchronizeFileOperation.class.getSimpleName();
44 private static final int SYNC_READ_TIMEOUT = 10000;
45 private static final int SYNC_CONNECTION_TIMEOUT = 5000;
46
47 private OCFile mLocalFile;
48 private OCFile mServerFile;
49 private FileDataStorageManager mStorageManager;
50 private Account mAccount;
51 private boolean mSyncFileContents;
52 private boolean mLocalChangeAlreadyKnown;
53 private Context mContext;
54
55 private boolean mTransferWasRequested = false;
56
57 public SynchronizeFileOperation(
58 OCFile localFile,
59 OCFile serverFile, // make this null to let the operation checks the server; added to reuse info from SynchronizeFolderOperation
60 FileDataStorageManager storageManager,
61 Account account,
62 boolean syncFileContents,
63 boolean localChangeAlreadyKnown,
64 Context context) {
65
66 mLocalFile = localFile;
67 mServerFile = serverFile;
68 mStorageManager = storageManager;
69 mAccount = account;
70 mSyncFileContents = syncFileContents;
71 mLocalChangeAlreadyKnown = localChangeAlreadyKnown;
72 mContext = context;
73 }
74
75
76 @Override
77 protected RemoteOperationResult run(WebdavClient client) {
78
79 PropFindMethod propfind = null;
80 RemoteOperationResult result = null;
81 mTransferWasRequested = false;
82 try {
83 if (!mLocalFile.isDown()) {
84 /// easy decision
85 requestForDownload(mLocalFile);
86 result = new RemoteOperationResult(ResultCode.OK);
87
88 } else {
89 /// local copy in the device -> need to think a bit more before do anything
90
91 if (mServerFile == null) {
92 /// take the duty of check the server for the current state of the file there
93 propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mLocalFile.getRemotePath()),
94 DavConstants.PROPFIND_ALL_PROP,
95 DavConstants.DEPTH_0);
96 int status = client.executeMethod(propfind, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
97 boolean isMultiStatus = status == HttpStatus.SC_MULTI_STATUS;
98 if (isMultiStatus) {
99 MultiStatus resp = propfind.getResponseBodyAsMultiStatus();
100 WebdavEntry we = new WebdavEntry(resp.getResponses()[0],
101 client.getBaseUri().getPath());
102 mServerFile = fillOCFile(we);
103 mServerFile.setLastSyncDateForProperties(System.currentTimeMillis());
104
105 } else {
106 client.exhaustResponse(propfind.getResponseBodyAsStream());
107 result = new RemoteOperationResult(false, status, propfind.getResponseHeaders());
108 }
109 }
110
111 if (result == null) { // true if the server was not checked. nothing was wrong with the remote request
112
113 /// check changes in server and local file
114 boolean serverChanged = false;
115 /* time for eTag is coming, but not yet
116 if (mServerFile.getEtag() != null) {
117 serverChanged = (!mServerFile.getEtag().equals(mLocalFile.getEtag())); // TODO could this be dangerous when the user upgrades the server from non-tagged to tagged?
118 } else { */
119 // server without etags
120 serverChanged = (mServerFile.getModificationTimestamp() > mLocalFile.getModificationTimestampAtLastSyncForData());
121 //}
122 boolean localChanged = (mLocalChangeAlreadyKnown || mLocalFile.getLocalModificationTimestamp() > mLocalFile.getLastSyncDateForData());
123 // TODO this will be always true after the app is upgraded to database version 2; will result in unnecessary uploads
124
125 /// decide action to perform depending upon changes
126 //if (!mLocalFile.getEtag().isEmpty() && localChanged && serverChanged) {
127 if (localChanged && serverChanged) {
128 result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
129
130 } else if (localChanged) {
131 if (mSyncFileContents) {
132 requestForUpload(mLocalFile);
133 // the local update of file properties will be done by the FileUploader service when the upload finishes
134 } else {
135 // NOTHING TO DO HERE: updating the properties of the file in the server without uploading the contents would be stupid;
136 // So, an instance of SynchronizeFileOperation created with syncFileContents == false is completely useless when we suspect
137 // that an upload is necessary (for instance, in FileObserverService).
138 }
139 result = new RemoteOperationResult(ResultCode.OK);
140
141 } else if (serverChanged) {
142 if (mSyncFileContents) {
143 requestForDownload(mLocalFile); // local, not server; we won't to keep the value of keepInSync!
144 // the update of local data will be done later by the FileUploader service when the upload finishes
145 } else {
146 // TODO CHECK: is this really useful in some point in the code?
147 mServerFile.setKeepInSync(mLocalFile.keepInSync());
148 mServerFile.setLastSyncDateForData(mLocalFile.getLastSyncDateForData());
149 mServerFile.setStoragePath(mLocalFile.getStoragePath());
150 mServerFile.setParentId(mLocalFile.getParentId());
151 mStorageManager.saveFile(mServerFile);
152
153 }
154 result = new RemoteOperationResult(ResultCode.OK);
155
156 } else {
157 // nothing changed, nothing to do
158 result = new RemoteOperationResult(ResultCode.OK);
159 }
160
161 }
162
163 }
164
165 Log_OC.i(TAG, "Synchronizing " + mAccount.name + ", file " + mLocalFile.getRemotePath() + ": " + result.getLogMessage());
166
167 } catch (Exception e) {
168 result = new RemoteOperationResult(e);
169 Log_OC.e(TAG, "Synchronizing " + mAccount.name + ", file " + (mLocalFile != null ? mLocalFile.getRemotePath() : "NULL") + ": " + result.getLogMessage(), result.getException());
170
171 } finally {
172 if (propfind != null)
173 propfind.releaseConnection();
174 }
175 return result;
176 }
177
178
179 /**
180 * Requests for an upload to the FileUploader service
181 *
182 * @param file OCFile object representing the file to upload
183 */
184 private void requestForUpload(OCFile file) {
185 Intent i = new Intent(mContext, FileUploader.class);
186 i.putExtra(FileUploader.KEY_ACCOUNT, mAccount);
187 i.putExtra(FileUploader.KEY_FILE, file);
188 /*i.putExtra(FileUploader.KEY_REMOTE_FILE, mRemotePath); // doing this we would lose the value of keepInSync in the road, and maybe it's not updated in the database when the FileUploader service gets it!
189 i.putExtra(FileUploader.KEY_LOCAL_FILE, localFile.getStoragePath());*/
190 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
191 i.putExtra(FileUploader.KEY_FORCE_OVERWRITE, true);
192 mContext.startService(i);
193 mTransferWasRequested = true;
194 }
195
196
197 /**
198 * Requests for a download to the FileDownloader service
199 *
200 * @param file OCFile object representing the file to download
201 */
202 private void requestForDownload(OCFile file) {
203 Intent i = new Intent(mContext, FileDownloader.class);
204 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
205 i.putExtra(FileDownloader.EXTRA_FILE, file);
206 mContext.startService(i);
207 mTransferWasRequested = true;
208 }
209
210
211 /**
212 * Creates and populates a new {@link OCFile} object with the data read from the server.
213 *
214 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
215 * @return New OCFile instance representing the remote resource described by we.
216 */
217 private OCFile fillOCFile(WebdavEntry we) {
218 OCFile file = new OCFile(we.decodedPath());
219 file.setCreationTimestamp(we.createTimestamp());
220 file.setFileLength(we.contentLength());
221 file.setMimetype(we.contentType());
222 file.setModificationTimestamp(we.modifiedTimestamp());
223 file.setEtag(we.etag());
224
225 return file;
226 }
227
228
229 public boolean transferWasRequested() {
230 return mTransferWasRequested;
231 }
232
233
234 public OCFile getLocalFile() {
235 return mLocalFile;
236 }
237
238 }