71c6d437635c081a35ab2d40f0eaeddf17ef1207
[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.content.Context;
27 import android.content.Intent;
28 import android.util.Log;
29
30 import com.owncloud.android.datamodel.DataStorageManager;
31 import com.owncloud.android.datamodel.OCFile;
32 import com.owncloud.android.files.services.FileDownloader;
33 import com.owncloud.android.files.services.FileUploader;
34 import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
35
36 import eu.alefzero.webdav.WebdavClient;
37 import eu.alefzero.webdav.WebdavEntry;
38 import eu.alefzero.webdav.WebdavUtils;
39
40 public class SynchronizeFileOperation extends RemoteOperation {
41
42 private String TAG = SynchronizeFileOperation.class.getSimpleName();
43 private String mRemotePath;
44 private DataStorageManager mStorageManager;
45 private Account mAccount;
46 private boolean mSyncFileContents;
47 private boolean mLocalChangeAlreadyKnown;
48 private Context mContext;
49
50 private boolean mTransferWasRequested = false;
51
52 public SynchronizeFileOperation(
53 String remotePath,
54 DataStorageManager dataStorageManager,
55 Account account,
56 boolean syncFileContents,
57 boolean localChangeAlreadyKnown,
58 Context context) {
59
60 mRemotePath = remotePath;
61 mStorageManager = dataStorageManager;
62 mAccount = account;
63 mSyncFileContents = syncFileContents;
64 mLocalChangeAlreadyKnown = localChangeAlreadyKnown;
65 mContext = context;
66 }
67
68
69 @Override
70 protected RemoteOperationResult run(WebdavClient client) {
71
72 PropFindMethod propfind = null;
73 RemoteOperationResult result = null;
74 mTransferWasRequested = false;
75 try {
76 OCFile localFile = mStorageManager.getFileByPath(mRemotePath);
77
78 if (!localFile.isDown()) {
79 /// easy decision
80 requestForDownload(localFile);
81 result = new RemoteOperationResult(ResultCode.OK);
82
83 } else {
84 /// local copy in the device -> need to think a bit more before do nothing
85
86 propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
87 int status = client.executeMethod(propfind);
88 boolean isMultiStatus = status == HttpStatus.SC_MULTI_STATUS;
89 if (isMultiStatus) {
90 MultiStatus resp = propfind.getResponseBodyAsMultiStatus();
91 WebdavEntry we = new WebdavEntry(resp.getResponses()[0],
92 client.getBaseUri().getPath());
93 OCFile serverFile = fillOCFile(we);
94
95 /// check changes in server and local file
96 boolean serverChanged = false;
97 if (serverFile.getEtag() != null) {
98 serverChanged = (!serverFile.getEtag().equals(localFile.getEtag()));
99 } else {
100 // server without etags
101 serverChanged = (serverFile.getModificationTimestamp() > localFile.getModificationTimestamp());
102 }
103 boolean localChanged = (mLocalChangeAlreadyKnown || localFile.getLocalModificationTimestamp() > localFile.getLastSyncDateForData());
104
105 /// decide action to perform depending upon changes
106 if (localChanged && serverChanged) {
107 // conflict
108 result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
109
110 } else if (localChanged) {
111 if (mSyncFileContents) {
112 requestForUpload(localFile);
113 // the local update of file properties will be done by the FileUploader service when the upload finishes
114 } else {
115 // NOTHING TO DO HERE: updating the properties of the file in the server without uploading the contents would be stupid;
116 // So, an instance of SynchronizeFileOperation created with syncFileContents == false is completely useless when we suspect
117 // that an upload is necessary (for instance, in FileObserverService).
118 }
119 result = new RemoteOperationResult(ResultCode.OK);
120
121 } else if (serverChanged) {
122 if (mSyncFileContents) {
123 requestForDownload(serverFile);
124 // the update of local data will be done later by the FileUploader service when the upload finishes
125 } else {
126 // TODO CHECK: is this really useful in some point in the code?
127 serverFile.setKeepInSync(localFile.keepInSync());
128 serverFile.setParentId(localFile.getParentId());
129 mStorageManager.saveFile(serverFile);
130
131 }
132 result = new RemoteOperationResult(ResultCode.OK);
133
134 } else {
135 // nothing changed, nothing to do
136 result = new RemoteOperationResult(ResultCode.OK);
137 }
138
139 } else {
140 client.exhaustResponse(propfind.getResponseBodyAsStream());
141 result = new RemoteOperationResult(false, status);
142 }
143
144 }
145
146 Log.i(TAG, "Synchronizing " + mAccount.name + ", file " + mRemotePath + ": " + result.getLogMessage());
147
148 } catch (Exception e) {
149 result = new RemoteOperationResult(e);
150 Log.e(TAG, "Synchronizing " + mAccount.name + ", file " + mRemotePath + ": " + result.getLogMessage(), result.getException());
151
152 } finally {
153 if (propfind != null)
154 propfind.releaseConnection();
155 }
156 return result;
157 }
158
159
160 /**
161 * Requests for an upload to the FileUploader service
162 *
163 * @param localFile OCFile object representing the file to upload
164 */
165 private void requestForUpload(OCFile localFile) {
166 Intent i = new Intent(mContext, FileUploader.class);
167 i.putExtra(FileUploader.KEY_ACCOUNT, mAccount);
168 i.putExtra(FileUploader.KEY_REMOTE_FILE, mRemotePath);
169 i.putExtra(FileUploader.KEY_LOCAL_FILE, localFile.getStoragePath());
170 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
171 i.putExtra(FileUploader.KEY_FORCE_OVERWRITE, true);
172 mContext.startService(i);
173 mTransferWasRequested = true;
174 }
175
176
177 /**
178 * Requests for a download to the FileDownloader service
179 *
180 * @param file OCFile object representing the file to download
181 */
182 private void requestForDownload(OCFile file) {
183 Intent i = new Intent(mContext, FileDownloader.class);
184 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
185 i.putExtra(FileDownloader.EXTRA_FILE, file);
186 mContext.startService(i);
187 mTransferWasRequested = true;
188 }
189
190
191 /**
192 * Creates and populates a new {@link OCFile} object with the data read from the server.
193 *
194 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
195 * @return New OCFile instance representing the remote resource described by we.
196 */
197 private OCFile fillOCFile(WebdavEntry we) {
198 OCFile file = new OCFile(we.decodedPath());
199 file.setCreationTimestamp(we.createTimestamp());
200 file.setFileLength(we.contentLength());
201 file.setMimetype(we.contentType());
202 file.setModificationTimestamp(we.modifiedTimesamp());
203 file.setLastSyncDateForProperties(System.currentTimeMillis());
204 file.setLastSyncDateForData(0);
205 return file;
206 }
207
208
209 public boolean transferWasRequested() {
210 return mTransferWasRequested;
211 }
212
213 }