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