use of com.owncloud.android.datamodel.OCFile in project android by owncloud.
the class SynchronizeFolderOperation method mergeRemoteFolder.
/**
* Synchronizes the data retrieved from the server about the contents of the target folder
* with the current data in the local database.
* <p>
* Grants that mFoldersToVisit is updated with fresh data after execution.
*
* @param remoteFolderAndFiles Remote folder and children files in folder
*/
private void mergeRemoteFolder(ArrayList<RemoteFile> remoteFolderAndFiles) throws OperationCancelledException {
Timber.d("Synchronizing " + mAccount.name + mRemotePath);
FileDataStorageManager storageManager = getStorageManager();
// parse data from remote folder
OCFile updatedFolder = FileStorageUtils.createOCFileFromRemoteFile(remoteFolderAndFiles.get(0));
// NOTE: updates ETag with remote value; that's INTENDED
updatedFolder.copyLocalPropertiesFrom(mLocalFolder);
Timber.d("Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
List<OCFile> updatedFiles = new Vector<>(remoteFolderAndFiles.size() - 1);
mFoldersToVisit = new Vector<>(remoteFolderAndFiles.size() - 1);
mFilesToSyncContents.clear();
if (mCancellationRequested.get()) {
throw new OperationCancelledException();
}
// get current data about local contents of the folder to synchronize
List<OCFile> localFiles = storageManager.getFolderContent(mLocalFolder);
Map<String, OCFile> localFilesMap = new HashMap<>(localFiles.size());
for (OCFile file : localFiles) {
String key = file.getRemoteId() != null ? file.getRemoteId() : file.getRemotePath();
if (key != null) {
localFilesMap.put(key, file);
}
}
// loop to synchronize every child
OCFile remoteFile, localFile, updatedLocalFile;
int foldersToExpand = 0;
for (int i = 1; i < remoteFolderAndFiles.size(); i++) {
// / new OCFile instance with the data from the server
remoteFile = FileStorageUtils.createOCFileFromRemoteFile(remoteFolderAndFiles.get(i));
// / retrieve local data for the read file
localFile = localFilesMap.remove(remoteFile.getRemoteId());
// Let's try with remote path if the file does not have remote id yet
if (localFile == null) {
localFile = localFilesMap.remove(remoteFile.getRemotePath());
}
// / new OCFile instance to merge fresh data from server with local state
updatedLocalFile = FileStorageUtils.createOCFileFromRemoteFile(remoteFolderAndFiles.get(i));
// / add to updatedFile data about LOCAL STATE (not existing in server)
updatedLocalFile.setLastSyncDateForProperties(mCurrentSyncTime);
if (localFile != null) {
updatedLocalFile.copyLocalPropertiesFrom(localFile);
updatedLocalFile.setFileName(remoteFile.getFileName());
// remote eTag will not be set unless file CONTENTS are synchronized
updatedLocalFile.setEtag(localFile.getEtag());
if (!updatedLocalFile.isFolder() && remoteFile.getModificationTimestamp() != localFile.getModificationTimestamp()) {
updatedLocalFile.setNeedsUpdateThumbnail(true);
}
} else {
updatedLocalFile.setParentId(mLocalFolder.getFileId());
// remote eTag will not be set unless file CONTENTS are synchronized
updatedLocalFile.setEtag("");
// new files need to check av-off status of parent folder!
if (updatedFolder.isAvailableOffline()) {
updatedLocalFile.setAvailableOfflineStatus(OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE_PARENT);
}
// new files need to update thumbnails
updatedLocalFile.setNeedsUpdateThumbnail(true);
}
// / check and fix, if needed, local storage path
searchForLocalFileInDefaultPath(updatedLocalFile);
// / prepare content synchronizations
boolean serverUnchanged = addToSyncContents(updatedLocalFile, remoteFile);
if (updatedLocalFile.isFolder() && !serverUnchanged) {
foldersToExpand++;
}
updatedFiles.add(updatedLocalFile);
}
// save updated contents in local database
if (foldersToExpand == 0) {
updatedFolder.setTreeEtag(updatedFolder.getEtag());
// TODO - propagate up
}
storageManager.saveFolder(updatedFolder, updatedFiles, localFilesMap.values());
}
use of com.owncloud.android.datamodel.OCFile in project android by owncloud.
the class SynchronizeFolderOperation method searchForLocalFileInDefaultPath.
/**
* /**
* Scans the default location for saving local copies of files searching for
* a 'lost' file with the same full name as the {@link OCFile} received as
* parameter.
* <p>
* This method helps to keep linked local copies of the files when the app is uninstalled, and then
* reinstalled in the device. OR after the cache of the app was deleted in system settings.
* <p>
* The method is assuming that all the local changes in the file where synchronized in the past. This is dangerous,
* but assuming the contrary could lead to massive unnecessary synchronizations of downloaded file after deleting
* the app cache.
* <p>
* This should be changed in the near future to avoid any chance of data loss, but we need to add some options
* to limit hard automatic synchronizations to wifi, unless the user wants otherwise.
*
* @param file File to associate a possible 'lost' local file.
*/
private void searchForLocalFileInDefaultPath(OCFile file) {
if (file.getStoragePath() == null && !file.isFolder()) {
File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
if (f.exists()) {
file.setStoragePath(f.getAbsolutePath());
file.setLastSyncDateForData(f.lastModified());
}
}
}
use of com.owncloud.android.datamodel.OCFile in project android by owncloud.
the class CreateFolderOperation method saveFolderInDB.
/**
* Save new directory in local database
*
* @return Instance of {@link OCFile} just created
*/
private OCFile saveFolderInDB() {
OCFile newDir = null;
if (mCreateFullPath && getStorageManager().getFileByPath(FileStorageUtils.getParentPath(mRemotePath)) == null) {
// When parent
// of remote path
// is not created
String[] subFolders = mRemotePath.split("/");
String composedRemotePath = "/";
// Create all the ancestors
for (String subFolder : subFolders) {
if (!subFolder.isEmpty()) {
composedRemotePath = composedRemotePath + subFolder + "/";
mRemotePath = composedRemotePath;
newDir = saveFolderInDB();
}
}
} else {
// Create directory on DB
newDir = new OCFile(mRemotePath);
newDir.setMimetype(MimeTypeConstantsKt.MIME_DIR);
long parentId = getStorageManager().getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
newDir.setParentId(parentId);
newDir.setModificationTimestamp(System.currentTimeMillis());
getStorageManager().saveFile(newDir);
Timber.d("Create directory " + mRemotePath + " in Database");
}
return newDir;
}
use of com.owncloud.android.datamodel.OCFile in project android by owncloud.
the class CreateFolderOperation method run.
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
CreateRemoteFolderOperation createRemoteFolderOperation = new CreateRemoteFolderOperation(mRemotePath, mCreateFullPath);
RemoteOperationResult result = createRemoteFolderOperation.execute(client);
if (result.isSuccess()) {
OCFile newDir = saveFolderInDB();
String localPath = FileStorageUtils.getDefaultSavePathFor(getStorageManager().getAccount().name, newDir);
File localFile = new File(localPath);
boolean created = localFile.mkdirs();
if (!created) {
Timber.w("Local folder " + localPath + " was not fully created");
}
} else {
Timber.e("%s hasn't been created", mRemotePath);
}
return result;
}
use of com.owncloud.android.datamodel.OCFile in project android by owncloud.
the class RefreshFolderOperation method resetShareFlagsInFolderChilds.
private void resetShareFlagsInFolderChilds() {
Vector<OCFile> files = getStorageManager().getFolderContent(mLocalFolder);
for (OCFile file : files) {
file.setSharedViaLink(false);
file.setSharedWithSharee(false);
getStorageManager().saveFile(file);
}
}
Aggregations