Process share with password
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFileOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * @author masensio
6 * Copyright (C) 2012 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 package com.owncloud.android.operations;
24
25 import com.owncloud.android.datamodel.OCFile;
26 import com.owncloud.android.files.services.FileDownloader;
27 import com.owncloud.android.files.services.FileUploader;
28 import com.owncloud.android.lib.common.OwnCloudClient;
29 import com.owncloud.android.lib.resources.files.RemoteFile;
30 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
31 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
32 import com.owncloud.android.lib.common.utils.Log_OC;
33 import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
34 import com.owncloud.android.operations.common.SyncOperation;
35 import com.owncloud.android.utils.FileStorageUtils;
36
37 import android.accounts.Account;
38 import android.content.Context;
39 import android.content.Intent;
40
41 /**
42 * Remote operation performing the read of remote file in the ownCloud server.
43 */
44
45 public class SynchronizeFileOperation extends SyncOperation {
46
47 private String TAG = SynchronizeFileOperation.class.getSimpleName();
48
49 private OCFile mLocalFile;
50 private String mRemotePath;
51 private OCFile mServerFile;
52 private Account mAccount;
53 private boolean mSyncFileContents;
54 private Context mContext;
55
56 private boolean mTransferWasRequested = false;
57
58 /**
59 * When 'false', uploads to the server are not done; only downloads or conflict detection.
60 * This is a temporal field.
61 * TODO Remove when 'folder synchronization' replaces 'folder download'.
62 */
63 private boolean mAllowUploads;
64
65
66 /**
67 * Constructor for "full synchronization mode".
68 *
69 * Uses remotePath to retrieve all the data both in local cache and in the remote OC server when the operation
70 * is executed, instead of reusing {@link OCFile} instances.
71 *
72 * Useful for direct synchronization of a single file.
73 *
74 * @param
75 * @param account ownCloud account holding the file.
76 * @param syncFileContents When 'true', transference of data will be started by the
77 * operation if needed and no conflict is detected.
78 * @param context Android context; needed to start transfers.
79 */
80 public SynchronizeFileOperation(
81 String remotePath,
82 Account account,
83 boolean syncFileContents,
84 Context context) {
85
86 mRemotePath = remotePath;
87 mLocalFile = null;
88 mServerFile = null;
89 mAccount = account;
90 mSyncFileContents = syncFileContents;
91 mContext = context;
92 mAllowUploads = true;
93 }
94
95
96 /**
97 * Constructor allowing to reuse {@link OCFile} instances just queried from local cache or from remote OC server.
98 *
99 * Useful to include this operation as part of the synchronization of a folder (or a full account), avoiding the
100 * repetition of fetch operations (both in local database or remote server).
101 *
102 * At least one of localFile or serverFile MUST NOT BE NULL. If you don't have none of them, use the other
103 * constructor.
104 *
105 * @param localFile Data of file (just) retrieved from local cache/database.
106 * @param serverFile Data of file (just) retrieved from a remote server. If null, will be
107 * retrieved from network by the operation when executed.
108 * @param account ownCloud account holding the file.
109 * @param syncFileContents When 'true', transference of data will be started by the
110 * operation if needed and no conflict is detected.
111 * @param context Android context; needed to start transfers.
112 */
113 public SynchronizeFileOperation(
114 OCFile localFile,
115 OCFile serverFile,
116 Account account,
117 boolean syncFileContents,
118 Context context) {
119
120 mLocalFile = localFile;
121 mServerFile = serverFile;
122 if (mLocalFile != null) {
123 mRemotePath = mLocalFile.getRemotePath();
124 if (mServerFile != null && !mServerFile.getRemotePath().equals(mRemotePath)) {
125 throw new IllegalArgumentException("serverFile and localFile do not correspond to the same OC file");
126 }
127 } else if (mServerFile != null) {
128 mRemotePath = mServerFile.getRemotePath();
129 } else {
130 throw new IllegalArgumentException("Both serverFile and localFile are NULL");
131 }
132 mAccount = account;
133 mSyncFileContents = syncFileContents;
134 mContext = context;
135 mAllowUploads = true;
136 }
137
138
139 /**
140 * Temporal constructor.
141 *
142 * Extends the previous one to allow constrained synchronizations where uploads are never performed - only
143 * downloads or conflict detection.
144 *
145 * Do not use unless you are involved in 'folder synchronization' or 'folder download' work in progress.
146 *
147 * TODO Remove when 'folder synchronization' replaces 'folder download'.
148 *
149 * @param localFile Data of file (just) retrieved from local cache/database. MUSTN't be null.
150 * @param serverFile Data of file (just) retrieved from a remote server. If null, will be
151 * retrieved from network by the operation when executed.
152 * @param account ownCloud account holding the file.
153 * @param syncFileContents When 'true', transference of data will be started by the
154 * operation if needed and no conflict is detected.
155 * @param allowUploads When 'false', uploads to the server are not done; only downloads or conflict
156 * detection.
157 * @param context Android context; needed to start transfers.
158 */
159 public SynchronizeFileOperation(
160 OCFile localFile,
161 OCFile serverFile,
162 Account account,
163 boolean syncFileContents,
164 boolean allowUploads,
165 Context context) {
166
167 this(localFile, serverFile, account, syncFileContents, context);
168 mAllowUploads = allowUploads;
169 }
170
171
172 @Override
173 protected RemoteOperationResult run(OwnCloudClient client) {
174
175 RemoteOperationResult result = null;
176 mTransferWasRequested = false;
177
178 if (mLocalFile == null) {
179 // Get local file from the DB
180 mLocalFile = getStorageManager().getFileByPath(mRemotePath);
181 }
182
183 if (!mLocalFile.isDown()) {
184 /// easy decision
185 requestForDownload(mLocalFile);
186 result = new RemoteOperationResult(ResultCode.OK);
187
188 } else {
189 /// local copy in the device -> need to think a bit more before do anything
190
191 if (mServerFile == null) {
192 ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mRemotePath);
193 result = operation.execute(client);
194 if (result.isSuccess()){
195 mServerFile = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
196 mServerFile.setLastSyncDateForProperties(System.currentTimeMillis());
197 }
198 }
199
200 if (mServerFile != null) {
201
202 /// check changes in server and local file
203 boolean serverChanged = false;
204 /* time for eTag is coming, but not yet
205 if (mServerFile.getEtag() != null) {
206 serverChanged = (!mServerFile.getEtag().equals(mLocalFile.getEtag()));
207 } else { */
208 serverChanged = (
209 mServerFile.getModificationTimestamp() != mLocalFile.getModificationTimestampAtLastSyncForData()
210 );
211 //}
212 boolean localChanged = (
213 mLocalFile.getLocalModificationTimestamp() > mLocalFile.getLastSyncDateForData()
214 );
215
216 /// decide action to perform depending upon changes
217 //if (!mLocalFile.getEtag().isEmpty() && localChanged && serverChanged) {
218 if (localChanged && serverChanged) {
219 result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
220
221 } else if (localChanged) {
222 if (mSyncFileContents && mAllowUploads) {
223 requestForUpload(mLocalFile);
224 // the local update of file properties will be done by the FileUploader service when the upload finishes
225 } else {
226 // NOTHING TO DO HERE: updating the properties of the file in the server without uploading the contents would be stupid;
227 // So, an instance of SynchronizeFileOperation created with syncFileContents == false is completely useless when we suspect
228 // that an upload is necessary (for instance, in FileObserverService).
229 }
230 result = new RemoteOperationResult(ResultCode.OK);
231
232 } else if (serverChanged) {
233 mLocalFile.setRemoteId(mServerFile.getRemoteId());
234
235 if (mSyncFileContents) {
236 requestForDownload(mLocalFile); // local, not server; we won't to keep the value of keepInSync!
237 // the update of local data will be done later by the FileUploader service when the upload finishes
238 } else {
239 // TODO CHECK: is this really useful in some point in the code?
240 mServerFile.setKeepInSync(mLocalFile.keepInSync());
241 mServerFile.setLastSyncDateForData(mLocalFile.getLastSyncDateForData());
242 mServerFile.setStoragePath(mLocalFile.getStoragePath());
243 mServerFile.setParentId(mLocalFile.getParentId());
244 getStorageManager().saveFile(mServerFile);
245
246 }
247 result = new RemoteOperationResult(ResultCode.OK);
248
249 } else {
250 // nothing changed, nothing to do
251 result = new RemoteOperationResult(ResultCode.OK);
252 }
253
254 }
255
256 }
257
258 Log_OC.i(TAG, "Synchronizing " + mAccount.name + ", file " + mLocalFile.getRemotePath() + ": "
259 + result.getLogMessage());
260
261 return result;
262 }
263
264
265 /**
266 * Requests for an upload to the FileUploader service
267 *
268 * @param file OCFile object representing the file to upload
269 */
270 private void requestForUpload(OCFile file) {
271 Intent i = new Intent(mContext, FileUploader.class);
272 i.putExtra(FileUploader.KEY_ACCOUNT, mAccount);
273 i.putExtra(FileUploader.KEY_FILE, file);
274 /*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!
275 i.putExtra(FileUploader.KEY_LOCAL_FILE, localFile.getStoragePath());*/
276 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
277 i.putExtra(FileUploader.KEY_FORCE_OVERWRITE, true);
278 mContext.startService(i);
279 mTransferWasRequested = true;
280 }
281
282
283 /**
284 * Requests for a download to the FileDownloader service
285 *
286 * @param file OCFile object representing the file to download
287 */
288 private void requestForDownload(OCFile file) {
289 Intent i = new Intent(mContext, FileDownloader.class);
290 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
291 i.putExtra(FileDownloader.EXTRA_FILE, file);
292 mContext.startService(i);
293 mTransferWasRequested = true;
294 }
295
296
297 public boolean transferWasRequested() {
298 return mTransferWasRequested;
299 }
300
301
302 public OCFile getLocalFile() {
303 return mLocalFile;
304 }
305
306 }