import java.util.Map;
import java.util.Vector;
-import org.apache.commons.httpclient.Header;
import org.apache.http.HttpStatus;
+ import org.apache.jackrabbit.webdav.DavConstants;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
mFailsInFavouritesFound = 0;
mConflictsFound = 0;
mForgottenLocalFiles.clear();
-
- // code before in FileSyncAdapter.fetchData
+ String remotePath = null;
PropFindMethod query = null;
try {
- Log_OC.d(TAG, "Synchronizing " + mAccount.name + ", fetching files in " + mRemotePath);
-
+ remotePath = mLocalFolder.getRemotePath();
+ Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
+
// remote request
- query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(remotePath));
- query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath),
++ query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(remotePath),
+ DavConstants.PROPFIND_ALL_PROP,
+ DavConstants.DEPTH_1);
int status = client.executeMethod(query);
-
- // check and process response - /// TODO take into account all the possible status per child-resource
- if (isMultiStatus(status)) {
- MultiStatus resp = query.getResponseBodyAsMultiStatus();
-
- // synchronize properties of the parent folder, if necessary
- if (mParentId == DataStorageManager.ROOT_PARENT_ID) {
- WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getBaseUri().getPath());
- OCFile parent = fillOCFile(we);
- mStorageManager.saveFile(parent);
- mParentId = parent.getFileId();
- }
-
- // read contents in folder
- List<OCFile> updatedFiles = new Vector<OCFile>(resp.getResponses().length - 1);
- List<SynchronizeFileOperation> filesToSyncContents = new Vector<SynchronizeFileOperation>();
- for (int i = 1; i < resp.getResponses().length; ++i) {
- /// new OCFile instance with the data from the server
- WebdavEntry we = new WebdavEntry(resp.getResponses()[i], client.getBaseUri().getPath());
- OCFile file = fillOCFile(we);
-
- /// set data about local state, keeping unchanged former data if existing
- file.setLastSyncDateForProperties(mCurrentSyncTime);
- OCFile oldFile = mStorageManager.getFileByPath(file.getRemotePath());
- if (oldFile != null) {
- file.setKeepInSync(oldFile.keepInSync());
- file.setLastSyncDateForData(oldFile.getLastSyncDateForData());
- file.setModificationTimestampAtLastSyncForData(oldFile.getModificationTimestampAtLastSyncForData()); // must be kept unchanged when the file contents are not updated
- checkAndFixForeignStoragePath(oldFile);
- file.setStoragePath(oldFile.getStoragePath());
- }
- /// scan default location if local copy of file is not linked in OCFile instance
- if (file.getStoragePath() == null && !file.isDirectory()) {
- File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
- if (f.exists()) {
- file.setStoragePath(f.getAbsolutePath());
- file.setLastSyncDateForData(f.lastModified());
- }
- }
-
- /// prepare content synchronization for kept-in-sync files
- if (file.keepInSync()) {
- SynchronizeFileOperation operation = new SynchronizeFileOperation( oldFile,
- file,
- mStorageManager,
- mAccount,
- true,
- false,
- mContext
- );
- filesToSyncContents.add(operation);
- }
-
- updatedFiles.add(file);
- }
-
- // save updated contents in local database; all at once, trying to get a best performance in database update (not a big deal, indeed)
- mStorageManager.saveFiles(updatedFiles);
-
- // request for the synchronization of files AFTER saving last properties
- SynchronizeFileOperation op = null;
- RemoteOperationResult contentsResult = null;
- for (int i=0; i < filesToSyncContents.size(); i++) {
- op = filesToSyncContents.get(i);
- contentsResult = op.execute(client); // returns without waiting for upload or download finishes
- if (!contentsResult.isSuccess()) {
- if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
- mConflictsFound++;
- } else {
- mFailsInFavouritesFound++;
- if (contentsResult.getException() != null) {
- Log_OC.e(TAG, "Error while synchronizing favourites : " + contentsResult.getLogMessage(), contentsResult.getException());
- } else {
- Log_OC.e(TAG, "Error while synchronizing favourites : " + contentsResult.getLogMessage());
- }
- }
- } // won't let these fails break the synchronization process
- }
-
-
- // removal of obsolete files
- mChildren = mStorageManager.getDirectoryContent(mStorageManager.getFileById(mParentId));
- OCFile file;
- String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);
- for (int i=0; i < mChildren.size(); ) {
- file = mChildren.get(i);
- if (file.getLastSyncDateForProperties() != mCurrentSyncTime) {
- Log_OC.d(TAG, "removing file: " + file);
- mStorageManager.removeFile(file, (file.isDown() && file.getStoragePath().startsWith(currentSavePath)));
- mChildren.remove(i);
+ // check and process response
+ if (isMultiStatus(status)) {
+ boolean folderChanged = synchronizeData(query.getResponseBodyAsMultiStatus(), client);
+ if (folderChanged) {
+ if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
+ result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT); // should be different result, but will do the job
} else {
- i++;
+ result = new RemoteOperationResult(true, status, query.getResponseHeaders());
}
+ } else {
+ result = new RemoteOperationResult(ResultCode.OK_NO_CHANGES_ON_DIR);
}
} else {