added rectangle with first character
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFolderOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.operations;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Vector;
34
35 import org.apache.http.HttpStatus;
36
37 import android.accounts.Account;
38 import android.content.Context;
39 import android.content.Intent;
40 import android.util.Log;
41
42 import com.owncloud.android.MainApp;
43 import com.owncloud.android.datamodel.FileDataStorageManager;
44 import com.owncloud.android.datamodel.OCFile;
45 import com.owncloud.android.files.services.FileDownloader;
46 import com.owncloud.android.lib.common.OwnCloudClient;
47 import com.owncloud.android.lib.common.operations.OperationCancelledException;
48 import com.owncloud.android.lib.common.operations.RemoteOperation;
49 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
50 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
51 import com.owncloud.android.lib.common.utils.Log_OC;
52 import com.owncloud.android.lib.resources.files.FileUtils;
53 import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
54 import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
55 import com.owncloud.android.lib.resources.files.RemoteFile;
56 import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
57 import com.owncloud.android.lib.resources.shares.OCShare;
58 import com.owncloud.android.syncadapter.FileSyncAdapter;
59 import com.owncloud.android.operations.common.SyncOperation;
60 import com.owncloud.android.services.OperationsService;
61 import com.owncloud.android.utils.FileStorageUtils;
62
63 import java.io.File;
64 import java.util.ArrayList;
65 import java.util.HashMap;
66 import java.util.List;
67 import java.util.Map;
68 import java.util.Vector;
69 import java.util.concurrent.atomic.AtomicBoolean;
70
71 //import android.support.v4.content.LocalBroadcastManager;
72
73
74 /**
75 * Remote operation performing the synchronization of the list of files contained
76 * in a folder identified with its remote path.
77 *
78 * Fetches the list and properties of the files contained in the given folder, including their
79 * properties, and updates the local database with them.
80 *
81 * Does NOT enter in the child folders to synchronize their contents also.
82 */
83 public class SynchronizeFolderOperation extends SyncOperation {
84
85 private static final String TAG = SynchronizeFolderOperation.class.getSimpleName();
86
87 /** Time stamp for the synchronization process in progress */
88 private long mCurrentSyncTime;
89
90 /** Remote path of the folder to synchronize */
91 private String mRemotePath;
92
93 /** Account where the file to synchronize belongs */
94 private Account mAccount;
95
96 /** Android context; necessary to send requests to the download service */
97 private Context mContext;
98
99 /** Locally cached information about folder to synchronize */
100 private OCFile mLocalFolder;
101
102 /** Files and folders contained in the synchronized folder after a successful operation */
103 //private List<OCFile> mChildren;
104
105 /** Counter of conflicts found between local and remote files */
106 private int mConflictsFound;
107
108 /** Counter of failed operations in synchronization of kept-in-sync files */
109 private int mFailsInFileSyncsFound;
110
111 /** 'True' means that the remote folder changed and should be fetched */
112 private boolean mRemoteFolderChanged;
113
114 private List<OCFile> mFilesForDirectDownload;
115 // to avoid extra PROPFINDs when there was no change in the folder
116
117 private List<SyncOperation> mFilesToSyncContentsWithoutUpload;
118 // this will go out when 'folder synchronization' replaces 'folder download'; step by step
119
120 private List<SyncOperation> mFavouriteFilesToSyncContents;
121 // this will be used for every file when 'folder synchronization' replaces 'folder download'
122
123 private final AtomicBoolean mCancellationRequested;
124
125 /**
126 * Creates a new instance of {@link SynchronizeFolderOperation}.
127 *
128 * @param context Application context.
129 * @param remotePath Path to synchronize.
130 * @param account ownCloud account where the folder is located.
131 * @param currentSyncTime Time stamp for the synchronization process in progress.
132 */
133 public SynchronizeFolderOperation(Context context, String remotePath, Account account,
134 long currentSyncTime){
135 mRemotePath = remotePath;
136 mCurrentSyncTime = currentSyncTime;
137 mAccount = account;
138 mContext = context;
139 mRemoteFolderChanged = false;
140 mFilesForDirectDownload = new Vector<OCFile>();
141 mFilesToSyncContentsWithoutUpload = new Vector<SyncOperation>();
142 mFavouriteFilesToSyncContents = new Vector<SyncOperation>();
143 mCancellationRequested = new AtomicBoolean(false);
144 }
145
146
147 public int getConflictsFound() {
148 return mConflictsFound;
149 }
150
151 public int getFailsInFileSyncsFound() {
152 return mFailsInFileSyncsFound;
153 }
154
155 /**
156 * Performs the synchronization.
157 *
158 * {@inheritDoc}
159 */
160 @Override
161 protected RemoteOperationResult run(OwnCloudClient client) {
162 RemoteOperationResult result = null;
163 mFailsInFileSyncsFound = 0;
164 mConflictsFound = 0;
165
166 try {
167 // get locally cached information about folder
168 mLocalFolder = getStorageManager().getFileByPath(mRemotePath);
169
170 result = checkForChanges(client);
171
172 if (result.isSuccess()) {
173 if (mRemoteFolderChanged) {
174 result = fetchAndSyncRemoteFolder(client);
175
176 } else {
177 prepareOpsFromLocalKnowledge();
178 }
179
180 if (result.isSuccess()) {
181 syncContents(client);
182 }
183
184 }
185
186 if (mCancellationRequested.get()) {
187 throw new OperationCancelledException();
188 }
189
190 } catch (OperationCancelledException e) {
191 result = new RemoteOperationResult(e);
192 }
193
194 return result;
195
196 }
197
198 private RemoteOperationResult checkForChanges(OwnCloudClient client)
199 throws OperationCancelledException {
200 Log_OC.d(TAG, "Checking changes in " + mAccount.name + mRemotePath);
201
202 mRemoteFolderChanged = true;
203 RemoteOperationResult result = null;
204
205 if (mCancellationRequested.get()) {
206 throw new OperationCancelledException();
207 }
208
209 // remote request
210 ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mRemotePath);
211 result = operation.execute(client);
212 if (result.isSuccess()){
213 OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
214
215 // check if remote and local folder are different
216 mRemoteFolderChanged =
217 !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag()));
218
219 result = new RemoteOperationResult(ResultCode.OK);
220
221 Log_OC.i(TAG, "Checked " + mAccount.name + mRemotePath + " : " +
222 (mRemoteFolderChanged ? "changed" : "not changed"));
223
224 } else {
225 // check failed
226 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
227 removeLocalFolder();
228 }
229 if (result.isException()) {
230 Log_OC.e(TAG, "Checked " + mAccount.name + mRemotePath + " : " +
231 result.getLogMessage(), result.getException());
232 } else {
233 Log_OC.e(TAG, "Checked " + mAccount.name + mRemotePath + " : " +
234 result.getLogMessage());
235 }
236
237 }
238
239 return result;
240 }
241
242
243 private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client)
244 throws OperationCancelledException {
245 if (mCancellationRequested.get()) {
246 throw new OperationCancelledException();
247 }
248
249 ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(mRemotePath);
250 RemoteOperationResult result = operation.execute(client);
251 Log_OC.d(TAG, "Synchronizing " + mAccount.name + mRemotePath);
252
253 if (result.isSuccess()) {
254 synchronizeData(result.getData(), client);
255 if (mConflictsFound > 0 || mFailsInFileSyncsFound > 0) {
256 result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
257 // should be a different result code, but will do the job
258 }
259 } else {
260 if (result.getCode() == ResultCode.FILE_NOT_FOUND)
261 removeLocalFolder();
262 }
263
264
265 return result;
266 }
267
268
269 private void removeLocalFolder() {
270 FileDataStorageManager storageManager = getStorageManager();
271 if (storageManager.fileExists(mLocalFolder.getFileId())) {
272 String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);
273 storageManager.removeFolder(
274 mLocalFolder,
275 true,
276 ( mLocalFolder.isDown() && // TODO: debug, I think this is
277 // always false for folders
278 mLocalFolder.getStoragePath().startsWith(currentSavePath)
279 )
280 );
281 }
282 }
283
284
285 /**
286 * Synchronizes the data retrieved from the server about the contents of the target folder
287 * with the current data in the local database.
288 *
289 * Grants that mChildren is updated with fresh data after execution.
290 *
291 * @param folderAndFiles Remote folder and children files in Folder
292 *
293 * @param client Client instance to the remote server where the data were
294 * retrieved.
295 * @return 'True' when any change was made in the local data, 'false' otherwise
296 */
297 private void synchronizeData(ArrayList<Object> folderAndFiles, OwnCloudClient client)
298 throws OperationCancelledException {
299 FileDataStorageManager storageManager = getStorageManager();
300
301 // parse data from remote folder
302 OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0));
303 remoteFolder.setParentId(mLocalFolder.getParentId());
304 remoteFolder.setFileId(mLocalFolder.getFileId());
305
306 Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath()
307 + " changed - starting update of local data ");
308
309 List<OCFile> updatedFiles = new Vector<OCFile>(folderAndFiles.size() - 1);
310 mFilesForDirectDownload.clear();
311 mFilesToSyncContentsWithoutUpload.clear();
312 mFavouriteFilesToSyncContents.clear();
313
314 if (mCancellationRequested.get()) {
315 throw new OperationCancelledException();
316 }
317
318 // get current data about local contents of the folder to synchronize
319 List<OCFile> localFiles = storageManager.getFolderContent(mLocalFolder, false);
320 Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
321 for (OCFile file : localFiles) {
322 localFilesMap.put(file.getRemotePath(), file);
323 }
324
325 // loop to synchronize every child
326 OCFile remoteFile = null, localFile = null;
327 for (int i=1; i<folderAndFiles.size(); i++) {
328 /// new OCFile instance with the data from the server
329 remoteFile = fillOCFile((RemoteFile)folderAndFiles.get(i));
330 remoteFile.setParentId(mLocalFolder.getFileId());
331
332 /// retrieve local data for the read file
333 // localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
334 localFile = localFilesMap.remove(remoteFile.getRemotePath());
335
336 /// add to the remoteFile (the new one) data about LOCAL STATE (not existing in server)
337 remoteFile.setLastSyncDateForProperties(mCurrentSyncTime);
338 if (localFile != null) {
339 // some properties of local state are kept unmodified
340 remoteFile.setFileId(localFile.getFileId());
341 remoteFile.setKeepInSync(localFile.keepInSync());
342 remoteFile.setLastSyncDateForData(localFile.getLastSyncDateForData());
343 remoteFile.setModificationTimestampAtLastSyncForData(
344 localFile.getModificationTimestampAtLastSyncForData()
345 );
346 remoteFile.setStoragePath(localFile.getStoragePath());
347 // eTag will not be updated unless contents are synchronized
348 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
349 remoteFile.setEtag(localFile.getEtag());
350 if (remoteFile.isFolder()) {
351 remoteFile.setFileLength(localFile.getFileLength());
352 // TODO move operations about size of folders to FileContentProvider
353 } else if (mRemoteFolderChanged && remoteFile.isImage() &&
354 remoteFile.getModificationTimestamp() !=
355 localFile.getModificationTimestamp()) {
356 remoteFile.setNeedsUpdateThumbnail(true);
357 Log.d(TAG, "Image " + remoteFile.getFileName() + " updated on the server");
358 }
359 remoteFile.setPublicLink(localFile.getPublicLink());
360 remoteFile.setShareByLink(localFile.isShareByLink());
361 } else {
362 // remote eTag will not be updated unless contents are synchronized
363 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
364 remoteFile.setEtag("");
365 }
366
367 /// check and fix, if needed, local storage path
368 searchForLocalFileInDefaultPath(remoteFile);
369
370 /// classify file to sync/download contents later
371 if (remoteFile.isFolder()) {
372 /// to download children files recursively
373 synchronized(mCancellationRequested) {
374 if (mCancellationRequested.get()) {
375 throw new OperationCancelledException();
376 }
377 startSyncFolderOperation(remoteFile.getRemotePath());
378 }
379
380 } else if (remoteFile.keepInSync()) {
381 /// prepare content synchronization for kept-in-sync files
382 SynchronizeFileOperation operation = new SynchronizeFileOperation(
383 localFile,
384 remoteFile,
385 mAccount,
386 true,
387 mContext
388 );
389 mFavouriteFilesToSyncContents.add(operation);
390
391 } else {
392 /// prepare limited synchronization for regular files
393 SynchronizeFileOperation operation = new SynchronizeFileOperation(
394 localFile,
395 remoteFile,
396 mAccount,
397 true,
398 false,
399 mContext
400 );
401 mFilesToSyncContentsWithoutUpload.add(operation);
402 }
403
404 updatedFiles.add(remoteFile);
405 }
406
407 // save updated contents in local database
408 storageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
409
410 }
411
412
413 private void prepareOpsFromLocalKnowledge() throws OperationCancelledException {
414 // TODO TOBI ist das richtig?
415 List<OCFile> children = getStorageManager().getFolderContent(mLocalFolder, false);
416 for (OCFile child : children) {
417 /// classify file to sync/download contents later
418 if (child.isFolder()) {
419 /// to download children files recursively
420 synchronized(mCancellationRequested) {
421 if (mCancellationRequested.get()) {
422 throw new OperationCancelledException();
423 }
424 startSyncFolderOperation(child.getRemotePath());
425 }
426
427 } else {
428 /// prepare limited synchronization for regular files
429 if (!child.isDown()) {
430 mFilesForDirectDownload.add(child);
431 }
432 }
433 }
434 }
435
436
437 private void syncContents(OwnCloudClient client) throws OperationCancelledException {
438 startDirectDownloads();
439 startContentSynchronizations(mFilesToSyncContentsWithoutUpload, client);
440 startContentSynchronizations(mFavouriteFilesToSyncContents, client);
441 }
442
443
444 private void startDirectDownloads() throws OperationCancelledException {
445 for (OCFile file : mFilesForDirectDownload) {
446 synchronized(mCancellationRequested) {
447 if (mCancellationRequested.get()) {
448 throw new OperationCancelledException();
449 }
450 Intent i = new Intent(mContext, FileDownloader.class);
451 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
452 i.putExtra(FileDownloader.EXTRA_FILE, file);
453 mContext.startService(i);
454 }
455 }
456 }
457
458 /**
459 * Performs a list of synchronization operations, determining if a download or upload is needed
460 * or if exists conflict due to changes both in local and remote contents of the each file.
461 *
462 * If download or upload is needed, request the operation to the corresponding service and goes
463 * on.
464 *
465 * @param filesToSyncContents Synchronization operations to execute.
466 * @param client Interface to the remote ownCloud server.
467 */
468 private void startContentSynchronizations(List<SyncOperation> filesToSyncContents,
469 OwnCloudClient client)
470 throws OperationCancelledException {
471
472 Log_OC.v(TAG, "Starting content synchronization... ");
473 RemoteOperationResult contentsResult = null;
474 for (SyncOperation op: filesToSyncContents) {
475 if (mCancellationRequested.get()) {
476 throw new OperationCancelledException();
477 }
478 contentsResult = op.execute(getStorageManager(), mContext);
479 if (!contentsResult.isSuccess()) {
480 if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
481 mConflictsFound++;
482 } else {
483 mFailsInFileSyncsFound++;
484 if (contentsResult.getException() != null) {
485 Log_OC.e(TAG, "Error while synchronizing file : "
486 + contentsResult.getLogMessage(), contentsResult.getException());
487 } else {
488 Log_OC.e(TAG, "Error while synchronizing file : "
489 + contentsResult.getLogMessage());
490 }
491 }
492 // TODO - use the errors count in notifications
493 } // won't let these fails break the synchronization process
494 }
495 }
496
497
498 /**
499 * Creates and populates a new {@link com.owncloud.android.datamodel.OCFile}
500 * object with the data read from the server.
501 *
502 * @param remote remote file read from the server (remote file or folder).
503 * @return New OCFile instance representing the remote resource described by we.
504 */
505 private OCFile fillOCFile(RemoteFile remote) {
506 OCFile file = new OCFile(remote.getRemotePath());
507 file.setCreationTimestamp(remote.getCreationTimestamp());
508 file.setFileLength(remote.getLength());
509 file.setMimetype(remote.getMimeType());
510 file.setModificationTimestamp(remote.getModifiedTimestamp());
511 file.setEtag(remote.getEtag());
512 file.setPermissions(remote.getPermissions());
513 file.setRemoteId(remote.getRemoteId());
514 return file;
515 }
516
517
518 /**
519 * Scans the default location for saving local copies of files searching for
520 * a 'lost' file with the same full name as the {@link com.owncloud.android.datamodel.OCFile}
521 * received as parameter.
522 *
523 * @param file File to associate a possible 'lost' local file.
524 */
525 private void searchForLocalFileInDefaultPath(OCFile file) {
526 if (file.getStoragePath() == null && !file.isFolder()) {
527 File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
528 if (f.exists()) {
529 file.setStoragePath(f.getAbsolutePath());
530 file.setLastSyncDateForData(f.lastModified());
531 }
532 }
533 }
534
535
536 /**
537 * Cancel operation
538 */
539 public void cancel() {
540 mCancellationRequested.set(true);
541 }
542
543 public String getFolderPath() {
544 String path = mLocalFolder.getStoragePath();
545 if (path != null && path.length() > 0) {
546 return path;
547 }
548 return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mLocalFolder);
549 }
550
551 private void startSyncFolderOperation(String path){
552 Intent intent = new Intent(mContext, OperationsService.class);
553 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
554 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mAccount);
555 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, path);
556 mContext.startService(intent);
557 }
558
559 public String getRemotePath() {
560 return mRemotePath;
561 }
562 }