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