|
@@ -71,9 +71,6 @@ public class SynchronizeFolderOperation extends RemoteOperation {
|
|
|
|
|
|
private OCFile mLocalFolder;
|
|
|
|
|
|
-
|
|
|
- private boolean mUpdateFolderProperties;
|
|
|
-
|
|
|
|
|
|
private FileDataStorageManager mStorageManager;
|
|
|
|
|
@@ -97,6 +94,9 @@ public class SynchronizeFolderOperation extends RemoteOperation {
|
|
|
|
|
|
|
|
|
private boolean mSyncFullAccount;
|
|
|
+
|
|
|
+
|
|
|
+ private boolean mRemoteFolderChanged;
|
|
|
|
|
|
|
|
|
|
|
@@ -113,19 +113,18 @@ public class SynchronizeFolderOperation extends RemoteOperation {
|
|
|
*/
|
|
|
public SynchronizeFolderOperation( OCFile folder,
|
|
|
long currentSyncTime,
|
|
|
- boolean updateFolderProperties,
|
|
|
boolean syncFullAccount,
|
|
|
FileDataStorageManager dataStorageManager,
|
|
|
Account account,
|
|
|
Context context ) {
|
|
|
mLocalFolder = folder;
|
|
|
mCurrentSyncTime = currentSyncTime;
|
|
|
- mUpdateFolderProperties = updateFolderProperties;
|
|
|
mSyncFullAccount = syncFullAccount;
|
|
|
mStorageManager = dataStorageManager;
|
|
|
mAccount = account;
|
|
|
mContext = context;
|
|
|
mForgottenLocalFiles = new HashMap<String, String>();
|
|
|
+ mRemoteFolderChanged = false;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -161,6 +160,86 @@ public class SynchronizeFolderOperation extends RemoteOperation {
|
|
|
mFailsInFavouritesFound = 0;
|
|
|
mConflictsFound = 0;
|
|
|
mForgottenLocalFiles.clear();
|
|
|
+
|
|
|
+ result = checkForChanges(client);
|
|
|
+
|
|
|
+ if (result.isSuccess()) {
|
|
|
+ if (mRemoteFolderChanged) {
|
|
|
+ result = fetchAndSyncRemoteFolder(client);
|
|
|
+ } else {
|
|
|
+ mChildren = mStorageManager.getFolderContent(mLocalFolder);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!mSyncFullAccount) {
|
|
|
+ sendStickyBroadcast(false, mLocalFolder.getRemotePath(), result);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private RemoteOperationResult checkForChanges(WebdavClient client) {
|
|
|
+ mRemoteFolderChanged = false;
|
|
|
+ RemoteOperationResult result = null;
|
|
|
+ String remotePath = null;
|
|
|
+ PropFindMethod query = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ remotePath = mLocalFolder.getRemotePath();
|
|
|
+ Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath);
|
|
|
+
|
|
|
+
|
|
|
+ query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(remotePath),
|
|
|
+ DavConstants.PROPFIND_ALL_PROP,
|
|
|
+ DavConstants.DEPTH_0);
|
|
|
+ int status = client.executeMethod(query);
|
|
|
+
|
|
|
+
|
|
|
+ if (isMultiStatus(status)) {
|
|
|
+
|
|
|
+ WebdavEntry we = new WebdavEntry(query.getResponseBodyAsMultiStatus().getResponses()[0], client.getBaseUri().getPath());
|
|
|
+ OCFile remoteFolder = fillOCFile(we);
|
|
|
+
|
|
|
+
|
|
|
+ mRemoteFolderChanged = !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag()));
|
|
|
+
|
|
|
+ result = new RemoteOperationResult(ResultCode.OK);
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ client.exhaustResponse(query.getResponseBodyAsStream());
|
|
|
+ if (status == HttpStatus.SC_NOT_FOUND) {
|
|
|
+ removeLocalFolder();
|
|
|
+ }
|
|
|
+ result = new RemoteOperationResult(false, status, query.getResponseHeaders());
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ result = new RemoteOperationResult(e);
|
|
|
+
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ if (query != null)
|
|
|
+ query.releaseConnection();
|
|
|
+ if (result.isSuccess()) {
|
|
|
+ Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " + (mRemoteFolderChanged ? "changed" : "not changed"));
|
|
|
+ } else {
|
|
|
+ if (result.isException()) {
|
|
|
+ Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + result.getLogMessage(), result.getException());
|
|
|
+ } else {
|
|
|
+ Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + result.getLogMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private RemoteOperationResult fetchAndSyncRemoteFolder(WebdavClient client) {
|
|
|
+ RemoteOperationResult result = null;
|
|
|
String remotePath = null;
|
|
|
PropFindMethod query = null;
|
|
|
try {
|
|
@@ -175,25 +254,18 @@ public class SynchronizeFolderOperation extends RemoteOperation {
|
|
|
|
|
|
|
|
|
if (isMultiStatus(status)) {
|
|
|
- boolean folderChanged = synchronizeData(query.getResponseBodyAsMultiStatus(), client);
|
|
|
- if (folderChanged) {
|
|
|
- if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
|
|
|
- result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
|
|
|
- } else {
|
|
|
- result = new RemoteOperationResult(true, status, query.getResponseHeaders());
|
|
|
- }
|
|
|
+ synchronizeData(query.getResponseBodyAsMultiStatus(), client);
|
|
|
+ if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
|
|
|
+ result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
|
|
|
} else {
|
|
|
- result = new RemoteOperationResult(ResultCode.OK_NO_CHANGES_ON_DIR);
|
|
|
+ result = new RemoteOperationResult(true, status, query.getResponseHeaders());
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
client.exhaustResponse(query.getResponseBodyAsStream());
|
|
|
if (status == HttpStatus.SC_NOT_FOUND) {
|
|
|
- if (mStorageManager.fileExists(mLocalFolder.getFileId())) {
|
|
|
- String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);
|
|
|
- mStorageManager.removeFolder(mLocalFolder, true, (mLocalFolder.isDown() && mLocalFolder.getStoragePath().startsWith(currentSavePath)));
|
|
|
- }
|
|
|
+ removeLocalFolder();
|
|
|
}
|
|
|
result = new RemoteOperationResult(false, status, query.getResponseHeaders());
|
|
|
}
|
|
@@ -215,15 +287,19 @@ public class SynchronizeFolderOperation extends RemoteOperation {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (!mSyncFullAccount) {
|
|
|
- sendStickyBroadcast(false, remotePath, result);
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
+ private void removeLocalFolder() {
|
|
|
+ if (mStorageManager.fileExists(mLocalFolder.getFileId())) {
|
|
|
+ String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);
|
|
|
+ mStorageManager.removeFolder(mLocalFolder, true, (mLocalFolder.isDown() && mLocalFolder.getStoragePath().startsWith(currentSavePath)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
* Synchronizes the data retrieved from the server about the contents of the target folder
|
|
|
* with the current data in the local database.
|
|
@@ -236,7 +312,7 @@ public class SynchronizeFolderOperation extends RemoteOperation {
|
|
|
* retrieved.
|
|
|
* @return 'True' when any change was made in the local data, 'false' otherwise.
|
|
|
*/
|
|
|
- private boolean synchronizeData(MultiStatus dataInServer, WebdavClient client) {
|
|
|
+ private void synchronizeData(MultiStatus dataInServer, WebdavClient client) {
|
|
|
|
|
|
mLocalFolder = mStorageManager.getFileById(mLocalFolder.getFileId());
|
|
|
|
|
@@ -246,91 +322,75 @@ public class SynchronizeFolderOperation extends RemoteOperation {
|
|
|
remoteFolder.setParentId(mLocalFolder.getParentId());
|
|
|
remoteFolder.setFileId(mLocalFolder.getFileId());
|
|
|
|
|
|
-
|
|
|
- boolean folderChanged = !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag()));
|
|
|
+ Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
|
|
|
|
|
|
- if (!folderChanged) {
|
|
|
- if (mUpdateFolderProperties) {
|
|
|
- mStorageManager.saveFile(remoteFolder);
|
|
|
- }
|
|
|
-
|
|
|
- Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " didn't change");
|
|
|
- mChildren = mStorageManager.getFolderContent(mLocalFolder);
|
|
|
-
|
|
|
- } else {
|
|
|
- Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
|
|
|
+ List<OCFile> updatedFiles = new Vector<OCFile>(dataInServer.getResponses().length - 1);
|
|
|
+ List<SynchronizeFileOperation> filesToSyncContents = new Vector<SynchronizeFileOperation>();
|
|
|
+
|
|
|
+
|
|
|
+ List<OCFile> localFiles = mStorageManager.getFolderContent(mLocalFolder);
|
|
|
+ Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
|
|
|
+ for (OCFile file : localFiles) {
|
|
|
+ localFilesMap.put(file.getRemotePath(), file);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ OCFile remoteFile = null, localFile = null;
|
|
|
+ for (int i = 1; i < dataInServer.getResponses().length; ++i) {
|
|
|
+
|
|
|
+ we = new WebdavEntry(dataInServer.getResponses()[i], client.getBaseUri().getPath());
|
|
|
+ remoteFile = fillOCFile(we);
|
|
|
+ remoteFile.setParentId(mLocalFolder.getFileId());
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ localFile = localFilesMap.remove(remoteFile.getRemotePath());
|
|
|
|
|
|
- List<OCFile> updatedFiles = new Vector<OCFile>(dataInServer.getResponses().length - 1);
|
|
|
- List<SynchronizeFileOperation> filesToSyncContents = new Vector<SynchronizeFileOperation>();
|
|
|
-
|
|
|
-
|
|
|
- List<OCFile> localFiles = mStorageManager.getFolderContent(mLocalFolder);
|
|
|
- Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
|
|
|
- for (OCFile file : localFiles) {
|
|
|
- localFilesMap.put(file.getRemotePath(), file);
|
|
|
+
|
|
|
+ remoteFile.setLastSyncDateForProperties(mCurrentSyncTime);
|
|
|
+ if (localFile != null) {
|
|
|
+
|
|
|
+ remoteFile.setFileId(localFile.getFileId());
|
|
|
+ remoteFile.setKeepInSync(localFile.keepInSync());
|
|
|
+ remoteFile.setLastSyncDateForData(localFile.getLastSyncDateForData());
|
|
|
+ remoteFile.setModificationTimestampAtLastSyncForData(localFile.getModificationTimestampAtLastSyncForData());
|
|
|
+ remoteFile.setStoragePath(localFile.getStoragePath());
|
|
|
+ remoteFile.setEtag(localFile.getEtag());
|
|
|
+ } else {
|
|
|
+ remoteFile.setEtag("");
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- OCFile remoteFile = null, localFile = null;
|
|
|
- for (int i = 1; i < dataInServer.getResponses().length; ++i) {
|
|
|
-
|
|
|
- we = new WebdavEntry(dataInServer.getResponses()[i], client.getBaseUri().getPath());
|
|
|
- remoteFile = fillOCFile(we);
|
|
|
- remoteFile.setParentId(mLocalFolder.getFileId());
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- localFile = localFilesMap.remove(remoteFile.getRemotePath());
|
|
|
-
|
|
|
-
|
|
|
- remoteFile.setLastSyncDateForProperties(mCurrentSyncTime);
|
|
|
- if (localFile != null) {
|
|
|
-
|
|
|
- remoteFile.setFileId(localFile.getFileId());
|
|
|
- remoteFile.setKeepInSync(localFile.keepInSync());
|
|
|
- remoteFile.setLastSyncDateForData(localFile.getLastSyncDateForData());
|
|
|
- remoteFile.setModificationTimestampAtLastSyncForData(localFile.getModificationTimestampAtLastSyncForData());
|
|
|
- remoteFile.setStoragePath(localFile.getStoragePath());
|
|
|
- remoteFile.setEtag(localFile.getEtag());
|
|
|
- } else {
|
|
|
- remoteFile.setEtag("");
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
- checkAndFixForeignStoragePath(remoteFile);
|
|
|
- searchForLocalFileInDefaultPath(remoteFile);
|
|
|
-
|
|
|
-
|
|
|
- if (remoteFile.keepInSync()) {
|
|
|
- SynchronizeFileOperation operation = new SynchronizeFileOperation( localFile,
|
|
|
- remoteFile,
|
|
|
- mStorageManager,
|
|
|
- mAccount,
|
|
|
- true,
|
|
|
- mContext
|
|
|
- );
|
|
|
- filesToSyncContents.add(operation);
|
|
|
- }
|
|
|
-
|
|
|
- updatedFiles.add(remoteFile);
|
|
|
+
|
|
|
+ checkAndFixForeignStoragePath(remoteFile);
|
|
|
+ searchForLocalFileInDefaultPath(remoteFile);
|
|
|
+
|
|
|
+
|
|
|
+ if (remoteFile.keepInSync()) {
|
|
|
+ SynchronizeFileOperation operation = new SynchronizeFileOperation( localFile,
|
|
|
+ remoteFile,
|
|
|
+ mStorageManager,
|
|
|
+ mAccount,
|
|
|
+ true,
|
|
|
+ mContext
|
|
|
+ );
|
|
|
+ filesToSyncContents.add(operation);
|
|
|
}
|
|
|
+
|
|
|
+ updatedFiles.add(remoteFile);
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- mStorageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
|
|
|
+
|
|
|
+ mStorageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
|
|
|
|
|
|
-
|
|
|
- startContentSynchronizations(filesToSyncContents, client);
|
|
|
+
|
|
|
+ startContentSynchronizations(filesToSyncContents, client);
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- mChildren = updatedFiles;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return folderChanged;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ mChildren = updatedFiles;
|
|
|
|
|
|
}
|
|
|
|
|
@@ -486,4 +546,9 @@ public class SynchronizeFolderOperation extends RemoteOperation {
|
|
|
mContext.sendStickyBroadcast(i);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public boolean getRemoteFolderChanged() {
|
|
|
+ return mRemoteFolderChanged;
|
|
|
+ }
|
|
|
+
|
|
|
}
|