Fixed. When an image is updated in the server and app folder is refreshed, the thumbn...
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFolderOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
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 version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Vector;
31
32 import org.apache.http.HttpStatus;
33 import android.accounts.Account;
34 import android.content.Context;
35 import android.content.Intent;
36 import android.util.Log;
37 //import android.support.v4.content.LocalBroadcastManager;
38
39 import com.owncloud.android.datamodel.FileDataStorageManager;
40 import com.owncloud.android.datamodel.OCFile;
41
42 import com.owncloud.android.lib.common.OwnCloudClient;
43 import com.owncloud.android.lib.resources.shares.OCShare;
44 import com.owncloud.android.lib.common.operations.RemoteOperation;
45 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
46 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
47 import com.owncloud.android.lib.common.utils.Log_OC;
48 import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
49 import com.owncloud.android.lib.resources.files.FileUtils;
50 import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
51 import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
52 import com.owncloud.android.lib.resources.files.RemoteFile;
53
54 import com.owncloud.android.syncadapter.FileSyncAdapter;
55 import com.owncloud.android.utils.FileStorageUtils;
56
57
58
59 /**
60 * Remote operation performing the synchronization of the list of files contained
61 * in a folder identified with its remote path.
62 *
63 * Fetches the list and properties of the files contained in the given folder, including their
64 * properties, and updates the local database with them.
65 *
66 * Does NOT enter in the child folders to synchronize their contents also.
67 *
68 * @author David A. Velasco
69 */
70 public class SynchronizeFolderOperation extends RemoteOperation {
71
72 private static final String TAG = SynchronizeFolderOperation.class.getSimpleName();
73
74 public static final String EVENT_SINGLE_FOLDER_CONTENTS_SYNCED =
75 SynchronizeFolderOperation.class.getName() + ".EVENT_SINGLE_FOLDER_CONTENTS_SYNCED";
76 public static final String EVENT_SINGLE_FOLDER_SHARES_SYNCED =
77 SynchronizeFolderOperation.class.getName() + ".EVENT_SINGLE_FOLDER_SHARES_SYNCED";
78
79 /** Time stamp for the synchronization process in progress */
80 private long mCurrentSyncTime;
81
82 /** Remote folder to synchronize */
83 private OCFile mLocalFolder;
84
85 /** Access to the local database */
86 private FileDataStorageManager mStorageManager;
87
88 /** Account where the file to synchronize belongs */
89 private Account mAccount;
90
91 /** Android context; necessary to send requests to the download service */
92 private Context mContext;
93
94 /** Files and folders contained in the synchronized folder after a successful operation */
95 private List<OCFile> mChildren;
96
97 /** Counter of conflicts found between local and remote files */
98 private int mConflictsFound;
99
100 /** Counter of failed operations in synchronization of kept-in-sync files */
101 private int mFailsInFavouritesFound;
102
103 /**
104 * Map of remote and local paths to files that where locally stored in a location
105 * out of the ownCloud folder and couldn't be copied automatically into it
106 **/
107 private Map<String, String> mForgottenLocalFiles;
108
109 /** 'True' means that this operation is part of a full account synchronization */
110 private boolean mSyncFullAccount;
111
112 /** 'True' means that Share resources bound to the files into should be refreshed also */
113 private boolean mIsShareSupported;
114
115 /** 'True' means that the remote folder changed and should be fetched */
116 private boolean mRemoteFolderChanged;
117
118 /** 'True' means that Etag will be ignored */
119 private boolean mIgnoreETag;
120
121
122 /**
123 * Creates a new instance of {@link SynchronizeFolderOperation}.
124 *
125 * @param folder Folder to synchronize.
126 * @param currentSyncTime Time stamp for the synchronization process in progress.
127 * @param syncFullAccount 'True' means that this operation is part of a full account
128 * synchronization.
129 * @param isShareSupported 'True' means that the server supports the sharing API.
130 * @param ignoreEtag 'True' means that the content of the remote folder should
131 * be fetched and updated even though the 'eTag' did not
132 * change.
133 * @param dataStorageManager Interface with the local database.
134 * @param account ownCloud account where the folder is located.
135 * @param context Application context.
136 */
137 public SynchronizeFolderOperation( OCFile folder,
138 long currentSyncTime,
139 boolean syncFullAccount,
140 boolean isShareSupported,
141 boolean ignoreETag,
142 FileDataStorageManager dataStorageManager,
143 Account account,
144 Context context ) {
145 mLocalFolder = folder;
146 mCurrentSyncTime = currentSyncTime;
147 mSyncFullAccount = syncFullAccount;
148 mIsShareSupported = isShareSupported;
149 mStorageManager = dataStorageManager;
150 mAccount = account;
151 mContext = context;
152 mForgottenLocalFiles = new HashMap<String, String>();
153 mRemoteFolderChanged = false;
154 mIgnoreETag = ignoreETag;
155 }
156
157
158 public int getConflictsFound() {
159 return mConflictsFound;
160 }
161
162 public int getFailsInFavouritesFound() {
163 return mFailsInFavouritesFound;
164 }
165
166 public Map<String, String> getForgottenLocalFiles() {
167 return mForgottenLocalFiles;
168 }
169
170 /**
171 * Returns the list of files and folders contained in the synchronized folder,
172 * if called after synchronization is complete.
173 *
174 * @return List of files and folders contained in the synchronized folder.
175 */
176 public List<OCFile> getChildren() {
177 return mChildren;
178 }
179
180 /**
181 * Performs the synchronization.
182 *
183 * {@inheritDoc}
184 */
185 @Override
186 protected RemoteOperationResult run(OwnCloudClient client) {
187 RemoteOperationResult result = null;
188 mFailsInFavouritesFound = 0;
189 mConflictsFound = 0;
190 mForgottenLocalFiles.clear();
191
192 if (FileUtils.PATH_SEPARATOR.equals(mLocalFolder.getRemotePath()) && !mSyncFullAccount) {
193 updateOCVersion(client);
194 }
195
196 result = checkForChanges(client);
197
198 if (result.isSuccess()) {
199 if (mRemoteFolderChanged) {
200 result = fetchAndSyncRemoteFolder(client);
201 } else {
202 mChildren = mStorageManager.getFolderContent(mLocalFolder);
203 }
204 }
205
206 if (!mSyncFullAccount) {
207 sendLocalBroadcast(
208 EVENT_SINGLE_FOLDER_CONTENTS_SYNCED, mLocalFolder.getRemotePath(), result
209 );
210 }
211
212 if (result.isSuccess() && mIsShareSupported && !mSyncFullAccount) {
213 refreshSharesForFolder(client); // share result is ignored
214 }
215
216 if (!mSyncFullAccount) {
217 sendLocalBroadcast(
218 EVENT_SINGLE_FOLDER_SHARES_SYNCED, mLocalFolder.getRemotePath(), result
219 );
220 }
221
222 return result;
223
224 }
225
226
227 private void updateOCVersion(OwnCloudClient client) {
228 UpdateOCVersionOperation update = new UpdateOCVersionOperation(mAccount, mContext);
229 RemoteOperationResult result = update.execute(client);
230 if (result.isSuccess()) {
231 mIsShareSupported = update.getOCVersion().isSharedSupported();
232 }
233 }
234
235
236 private RemoteOperationResult checkForChanges(OwnCloudClient client) {
237 mRemoteFolderChanged = true;
238 RemoteOperationResult result = null;
239 String remotePath = null;
240
241 remotePath = mLocalFolder.getRemotePath();
242 Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath);
243
244 // remote request
245 ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath);
246 result = operation.execute(client);
247 if (result.isSuccess()){
248 OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
249
250 if (!mIgnoreETag) {
251 // check if remote and local folder are different
252 mRemoteFolderChanged =
253 !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag()));
254 }
255
256 result = new RemoteOperationResult(ResultCode.OK);
257
258 Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " +
259 (mRemoteFolderChanged ? "changed" : "not changed"));
260
261 } else {
262 // check failed
263 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
264 removeLocalFolder();
265 }
266 if (result.isException()) {
267 Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " +
268 result.getLogMessage(), result.getException());
269 } else {
270 Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " +
271 result.getLogMessage());
272 }
273 }
274
275 return result;
276 }
277
278
279 private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) {
280 String remotePath = mLocalFolder.getRemotePath();
281 ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(remotePath);
282 RemoteOperationResult result = operation.execute(client);
283 Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
284
285 if (result.isSuccess()) {
286 synchronizeData(result.getData(), client);
287 if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
288 result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
289 // should be a different result code, but will do the job
290 }
291 } else {
292 if (result.getCode() == ResultCode.FILE_NOT_FOUND)
293 removeLocalFolder();
294 }
295
296 return result;
297 }
298
299
300 private void removeLocalFolder() {
301 if (mStorageManager.fileExists(mLocalFolder.getFileId())) {
302 String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);
303 mStorageManager.removeFolder(
304 mLocalFolder,
305 true,
306 ( mLocalFolder.isDown() &&
307 mLocalFolder.getStoragePath().startsWith(currentSavePath)
308 )
309 );
310 }
311 }
312
313
314 /**
315 * Synchronizes the data retrieved from the server about the contents of the target folder
316 * with the current data in the local database.
317 *
318 * Grants that mChildren is updated with fresh data after execution.
319 *
320 * @param folderAndFiles Remote folder and children files in Folder
321 *
322 * @param client Client instance to the remote server where the data were
323 * retrieved.
324 * @return 'True' when any change was made in the local data, 'false' otherwise
325 */
326 private void synchronizeData(ArrayList<Object> folderAndFiles, OwnCloudClient client) {
327 Vector<OCFile> mImageFiles = mStorageManager.getFolderImages(mLocalFolder);
328
329 // get 'fresh data' from the database
330 mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath());
331
332 // parse data from remote folder
333 OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0));
334 remoteFolder.setParentId(mLocalFolder.getParentId());
335 remoteFolder.setFileId(mLocalFolder.getFileId());
336
337 Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath()
338 + " changed - starting update of local data ");
339
340 List<OCFile> updatedFiles = new Vector<OCFile>(folderAndFiles.size() - 1);
341 List<SynchronizeFileOperation> filesToSyncContents = new Vector<SynchronizeFileOperation>();
342
343 // get current data about local contents of the folder to synchronize
344 List<OCFile> localFiles = mStorageManager.getFolderContent(mLocalFolder);
345 Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
346 for (OCFile file : localFiles) {
347 localFilesMap.put(file.getRemotePath(), file);
348 }
349
350 // loop to update every child
351 OCFile remoteFile = null, localFile = null;
352 for (int i=1; i<folderAndFiles.size(); i++) {
353 /// new OCFile instance with the data from the server
354 remoteFile = fillOCFile((RemoteFile)folderAndFiles.get(i));
355 remoteFile.setParentId(mLocalFolder.getFileId());
356
357 /// retrieve local data for the read file
358 // localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
359 localFile = localFilesMap.remove(remoteFile.getRemotePath());
360
361 /// add to the remoteFile (the new one) data about LOCAL STATE (not existing in server)
362 remoteFile.setLastSyncDateForProperties(mCurrentSyncTime);
363 if (localFile != null) {
364 // some properties of local state are kept unmodified
365 remoteFile.setFileId(localFile.getFileId());
366 remoteFile.setKeepInSync(localFile.keepInSync());
367 remoteFile.setLastSyncDateForData(localFile.getLastSyncDateForData());
368 remoteFile.setModificationTimestampAtLastSyncForData(
369 localFile.getModificationTimestampAtLastSyncForData()
370 );
371 remoteFile.setStoragePath(localFile.getStoragePath());
372 // eTag will not be updated unless contents are synchronized
373 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
374 remoteFile.setEtag(localFile.getEtag());
375 if (remoteFile.isFolder()) {
376 remoteFile.setFileLength(localFile.getFileLength());
377 // TODO move operations about size of folders to FileContentProvider
378 } else if (mRemoteFolderChanged && remoteFile.isImage()) {
379 // If image has been updated on the server, set for updating the thumbnail
380 for (OCFile fileImage: mImageFiles) {
381 if (remoteFile.getRemoteId().equals(fileImage.getRemoteId()) &&
382 remoteFile.getModificationTimestamp() > fileImage.getModificationTimestamp()) {
383 remoteFile.setNeedsUpdateThumbnail(true);
384 Log.d(TAG, "Image " + remoteFile.getFileName() + " updated on the server");
385 }
386 }
387 }
388 remoteFile.setPublicLink(localFile.getPublicLink());
389 remoteFile.setShareByLink(localFile.isShareByLink());
390 } else {
391 // remote eTag will not be updated unless contents are synchronized
392 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
393 remoteFile.setEtag("");
394 }
395
396 /// check and fix, if needed, local storage path
397 checkAndFixForeignStoragePath(remoteFile); // policy - local files are COPIED
398 // into the ownCloud local folder;
399 searchForLocalFileInDefaultPath(remoteFile); // legacy
400
401 /// prepare content synchronization for kept-in-sync files
402 if (remoteFile.keepInSync()) {
403 SynchronizeFileOperation operation = new SynchronizeFileOperation( localFile,
404 remoteFile,
405 mAccount,
406 true,
407 mContext
408 );
409
410 filesToSyncContents.add(operation);
411 }
412
413 updatedFiles.add(remoteFile);
414 }
415
416 // save updated contents in local database
417 mStorageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
418
419 // request for the synchronization of file contents AFTER saving current remote properties
420 startContentSynchronizations(filesToSyncContents, client);
421
422 mChildren = updatedFiles;
423 }
424
425 /**
426 * Performs a list of synchronization operations, determining if a download or upload is needed
427 * or if exists conflict due to changes both in local and remote contents of the each file.
428 *
429 * If download or upload is needed, request the operation to the corresponding service and goes
430 * on.
431 *
432 * @param filesToSyncContents Synchronization operations to execute.
433 * @param client Interface to the remote ownCloud server.
434 */
435 private void startContentSynchronizations(
436 List<SynchronizeFileOperation> filesToSyncContents, OwnCloudClient client
437 ) {
438 RemoteOperationResult contentsResult = null;
439 for (SynchronizeFileOperation op: filesToSyncContents) {
440 contentsResult = op.execute(mStorageManager, mContext); // async
441 if (!contentsResult.isSuccess()) {
442 if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
443 mConflictsFound++;
444 } else {
445 mFailsInFavouritesFound++;
446 if (contentsResult.getException() != null) {
447 Log_OC.e(TAG, "Error while synchronizing favourites : "
448 + contentsResult.getLogMessage(), contentsResult.getException());
449 } else {
450 Log_OC.e(TAG, "Error while synchronizing favourites : "
451 + contentsResult.getLogMessage());
452 }
453 }
454 } // won't let these fails break the synchronization process
455 }
456 }
457
458
459 public boolean isMultiStatus(int status) {
460 return (status == HttpStatus.SC_MULTI_STATUS);
461 }
462
463 /**
464 * Creates and populates a new {@link OCFile} object with the data read from the server.
465 *
466 * @param remote remote file read from the server (remote file or folder).
467 * @return New OCFile instance representing the remote resource described by we.
468 */
469 private OCFile fillOCFile(RemoteFile remote) {
470 OCFile file = new OCFile(remote.getRemotePath());
471 file.setCreationTimestamp(remote.getCreationTimestamp());
472 file.setFileLength(remote.getLength());
473 file.setMimetype(remote.getMimeType());
474 file.setModificationTimestamp(remote.getModifiedTimestamp());
475 file.setEtag(remote.getEtag());
476 file.setPermissions(remote.getPermissions());
477 file.setRemoteId(remote.getRemoteId());
478 return file;
479 }
480
481
482 /**
483 * Checks the storage path of the OCFile received as parameter.
484 * If it's out of the local ownCloud folder, tries to copy the file inside it.
485 *
486 * If the copy fails, the link to the local file is nullified. The account of forgotten
487 * files is kept in {@link #mForgottenLocalFiles}
488 *)
489 * @param file File to check and fix.
490 */
491 private void checkAndFixForeignStoragePath(OCFile file) {
492 String storagePath = file.getStoragePath();
493 String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
494 if (storagePath != null && !storagePath.equals(expectedPath)) {
495 /// fix storagePaths out of the local ownCloud folder
496 File originalFile = new File(storagePath);
497 if (FileStorageUtils.getUsableSpace(mAccount.name) < originalFile.length()) {
498 mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
499 file.setStoragePath(null);
500
501 } else {
502 InputStream in = null;
503 OutputStream out = null;
504 try {
505 File expectedFile = new File(expectedPath);
506 File expectedParent = expectedFile.getParentFile();
507 expectedParent.mkdirs();
508 if (!expectedParent.isDirectory()) {
509 throw new IOException(
510 "Unexpected error: parent directory could not be created"
511 );
512 }
513 expectedFile.createNewFile();
514 if (!expectedFile.isFile()) {
515 throw new IOException("Unexpected error: target file could not be created");
516 }
517 in = new FileInputStream(originalFile);
518 out = new FileOutputStream(expectedFile);
519 byte[] buf = new byte[1024];
520 int len;
521 while ((len = in.read(buf)) > 0){
522 out.write(buf, 0, len);
523 }
524 file.setStoragePath(expectedPath);
525
526 } catch (Exception e) {
527 Log_OC.e(TAG, "Exception while copying foreign file " + expectedPath, e);
528 mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
529 file.setStoragePath(null);
530
531 } finally {
532 try {
533 if (in != null) in.close();
534 } catch (Exception e) {
535 Log_OC.d(TAG, "Weird exception while closing input stream for "
536 + storagePath + " (ignoring)", e);
537 }
538 try {
539 if (out != null) out.close();
540 } catch (Exception e) {
541 Log_OC.d(TAG, "Weird exception while closing output stream for "
542 + expectedPath + " (ignoring)", e);
543 }
544 }
545 }
546 }
547 }
548
549
550 private RemoteOperationResult refreshSharesForFolder(OwnCloudClient client) {
551 RemoteOperationResult result = null;
552
553 // remote request
554 GetRemoteSharesForFileOperation operation =
555 new GetRemoteSharesForFileOperation(mLocalFolder.getRemotePath(), false, true);
556 result = operation.execute(client);
557
558 if (result.isSuccess()) {
559 // update local database
560 ArrayList<OCShare> shares = new ArrayList<OCShare>();
561 for(Object obj: result.getData()) {
562 shares.add((OCShare) obj);
563 }
564 mStorageManager.saveSharesInFolder(shares, mLocalFolder);
565 }
566
567 return result;
568 }
569
570
571 /**
572 * Scans the default location for saving local copies of files searching for
573 * a 'lost' file with the same full name as the {@link OCFile} received as
574 * parameter.
575 *
576 * @param file File to associate a possible 'lost' local file.
577 */
578 private void searchForLocalFileInDefaultPath(OCFile file) {
579 if (file.getStoragePath() == null && !file.isFolder()) {
580 File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
581 if (f.exists()) {
582 file.setStoragePath(f.getAbsolutePath());
583 file.setLastSyncDateForData(f.lastModified());
584 }
585 }
586 }
587
588
589 /**
590 * Sends a message to any application component interested in the progress
591 * of the synchronization.
592 *
593 * @param event
594 * @param dirRemotePath Remote path of a folder that was just synchronized
595 * (with or without success)
596 * @param result
597 */
598 private void sendLocalBroadcast(
599 String event, String dirRemotePath, RemoteOperationResult result
600 ) {
601 Log_OC.d(TAG, "Send broadcast " + event);
602 Intent intent = new Intent(event);
603 intent.putExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME, mAccount.name);
604 if (dirRemotePath != null) {
605 intent.putExtra(FileSyncAdapter.EXTRA_FOLDER_PATH, dirRemotePath);
606 }
607 intent.putExtra(FileSyncAdapter.EXTRA_RESULT, result);
608 mContext.sendStickyBroadcast(intent);
609 //LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
610 }
611
612
613 public boolean getRemoteFolderChanged() {
614 return mRemoteFolderChanged;
615 }
616
617 }